1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
8 import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
9 import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
10 import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.CommutativityRule;
14 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
15 import edu.uci.eecs.specCompiler.specExtraction.Construct;
16 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
17 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
26 * Defines a list of commonly used constant strings.
32 public class CodeVariables {
33 // C++ code or library
34 public static final String HEADER_STDLIB = "<stdlib.h>";
35 public static final String HEADER_THREADS = "<threads.h>";
36 public static final String HEADER_STDINT = "<stdint.h>";
37 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
38 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
39 public static final String ThreadIDType = "thread_id_t";
40 public static final String BOOLEAN = "bool";
41 public static final String UINT64 = "uint64_t";
44 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
45 public static final String HEADER_COMMON = "<common.h>";
46 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
47 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
48 public static final String CDSAnnotate = "cdsannotate";
49 // public static final String CDSAnnotate = "cdsannotate";
50 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
51 public static final String IDType = "call_id_t";
53 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
54 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
55 public static final String SPEC_ANNO_TYPE_HB_RULE = "HB_RULE";
56 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
57 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
58 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
59 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
60 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
61 public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
62 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
63 public static final String SPEC_ANNOTATION = "spec_annotation";
64 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
65 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
67 public static final String ANNO_INIT = "anno_init";
68 public static final String HB_RULE = "hb_rule";
69 public static final String COMMUTATIVITY_RULE = "commutativity_rule";
70 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
71 public static final String ANNO_INTERFACE_END = "anno_interface_end";
72 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
73 public static final String ANNO_CP_DEFINE = "anno_cp_define";
74 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
75 public static final String ANNO_CP_CLEAR = "anno_cp_clear";
76 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
78 // Specification variables
79 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
80 public static final String DEFAULT_ID = "0";
82 // Specification library
83 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
84 public static final String SPEC_QUEUE = "spec_queue";
85 public static final String SPEC_STACK = "spec_stack";
86 public static final String SPEC_DEQUE = "spec_deque";
87 public static final String SPEC_HASHTABLE = "spec_hashtable";
88 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
89 public static final String SPEC_TAG = "spec_tag";
90 public static final String SPEC_TAG_CURRENT = "current";
91 public static final String SPEC_TAG_NEXT = "next";
94 public static final String MACRO_ID = "__ID__";
95 public static final String MACRO_COND = "__COND_SAT__";
96 public static final String MACRO_RETURN = "__RET__";
97 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
98 public static final String MACRO_THREAD_ID = "__TID__";
100 public static void printCode(ArrayList<String> code) {
101 for (int i = 0; i < code.size(); i++) {
102 System.out.println(code.get(i));
106 private static String COMMENT(String comment) {
107 return "/* " + comment + " */";
110 private static String SHORT_COMMENT(String comment) {
111 return " // " + comment;
114 private static String INCLUDE(String header) {
115 return "#include " + header;
118 private static String DEFINE(String left, String right) {
119 return "#define " + left + " " + right;
122 private static String UNDEFINE(String macro) {
123 return "#undef " + macro;
126 private static String GET_FIELD_BY_PTR(String ptr, String field) {
127 return ptr + "->" + field;
130 private static String GET_FIELD(String var, String field) {
131 return var + "->" + field;
134 private static String BRACE(String val) {
135 return "(" + val + ")";
138 private static String ASSIGN(String structName, String field, String val) {
139 return structName + "." + field + " = " + val + ";";
142 private static String ASSIGN(String varName, String val) {
143 return varName + " = " + val + ";";
146 private static String ASSIGN_PTR(String structName, String field, String val) {
147 return structName + "." + field + " = &" + val + ";";
150 private static String ASSIGN_TO_PTR(String structName, String field,
152 return structName + "->" + field + " = " + val + ";";
155 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
157 return structName + "->" + field + " = &" + val + ";";
160 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
161 return "struct " + type + " *" + name + " = (struct " + type
162 + "*) malloc(sizeof(struct " + type + "));";
165 private static String DECLARE(String type, String name) {
166 return type + " " + name + ";";
169 private static String DECLARE(VariableDeclaration varDecl) {
170 String type = varDecl.type, name = varDecl.name;
171 return type + " " + name + ";";
174 private static String DECLARE_DEFINE(String type, String var, String val) {
175 return type + " " + var + " = " + val + ";";
178 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
179 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
182 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
183 FunctionHeader header) {
184 ArrayList<String> code = new ArrayList<String>();
185 code.add("typedef struct " + interfaceName + "_info {");
186 if (!header.returnType.equals("void")) {
187 code.add(DECLARE(header.returnType, MACRO_RETURN));
189 for (int i = 0; i < header.args.size(); i++) {
190 code.add(DECLARE(header.args.get(i)));
192 code.add("} " + interfaceName + "_info;");
196 private static ArrayList<String> DEFINE_ID_FUNC(
197 InterfaceConstruct construct, FunctionHeader header) {
198 String interfaceName = construct.name;
199 ArrayList<String> code = new ArrayList<String>();
200 String idCode = construct.idCode;
201 code.add("inline static " + IDType + " " + interfaceName + "_id("
202 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
205 if (!header.returnType.equals("void") || header.args.size() != 0) {
206 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
207 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
208 infoStructName, BRACE(infoStructType + "*") + "info"));
209 if (!header.returnType.equals("void")) {
210 code.add((DECLARE_DEFINE("\t" + header.returnType,
212 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
214 for (int i = 0; i < header.args.size(); i++) {
215 String type = header.args.get(i).type, var = header.args.get(i).name;
217 + (DECLARE_DEFINE(type, var,
218 GET_FIELD_BY_PTR(infoStructName, var))));
223 if (!idCode.equals("")) {
224 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
226 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
228 code.add("\treturn " + MACRO_ID + ";");
233 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
234 InterfaceConstruct construct, FunctionHeader header) {
235 String interfaceName = construct.name;
236 ArrayList<String> code = new ArrayList<String>();
237 code.add("inline static bool " + interfaceName
238 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
239 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
240 code.add("\t" + DECLARE("bool", "check_passed"));
242 if (!header.returnType.equals("void") || header.args.size() != 0) {
243 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
245 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
246 BRACE(infoStructType + "*") + "info"));
247 if (!header.returnType.equals("void")) {
249 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
250 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
252 for (int i = 0; i < header.args.size(); i++) {
253 String type = header.args.get(i).type, var = header.args.get(i).name;
255 + (DECLARE_DEFINE(type, var,
256 GET_FIELD_BY_PTR(infoStructName, var))));
261 if (!construct.condition.equals("")) {
263 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
266 if (!construct.check.equals("")) {
267 code.add("\t" + ASSIGN("check_passed", construct.check));
268 code.add("\tif (!check_passed)");
269 code.add("\t\treturn false;");
273 if (construct.action.size() > 0) {
274 addAllCodeWithIndent(code, construct.action, "\t");
277 if (!construct.postCheck.equals("")) {
278 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
279 code.add("\tif (!check_passed)");
280 code.add("\t\treturn false;");
283 if (construct.postAction.size() > 0) {
284 addAllCodeWithIndent(code, construct.postAction, "\t");
286 // Return true finally
287 code.add("\treturn true;");
294 private static void addAllCodeWithIndent(ArrayList<String> allCode,
295 ArrayList<String> target, String indent) {
296 for (int i = 0; i < target.size(); i++) {
297 allCode.add(indent + target.get(i));
301 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
302 HashSet<String> headers = new HashSet<String>();
303 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
304 File f = semantics.interfaceName2Construct.get(interfaceName).file;
305 headers.addAll(semantics.srcFilesInfo.get(f).headers);
307 headers.add(HEADER_STDLIB);
308 headers.add(HEADER_STDINT);
309 headers.add(HEADER_MODELMEMORY);
310 headers.add(HEADER_MODELTYPES);
311 headers.add(HEADER_SPEC_LIB);
312 headers.add(HEADER_STDINT);
313 headers.add(HEADER_CDSANNOTATE);
314 // headers.add(HEADER_COMMON);
315 headers.add(HEADER_SPECANNOTATION);
319 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
320 String headLine = funcDefine.get(0);
321 headLine = "inline static " + headLine;
322 funcDefine.set(0, headLine);
325 private static String makeVariablesStatic(VariableDeclaration varDecl) {
326 String res = "static " + varDecl.type + " " + varDecl.name + ";";
330 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
331 Construct construct) {
332 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
333 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
334 if (headerLine.startsWith("template")) {
335 templateLine = headerLine;
336 headerLine = content.get(construct.beginLineNum + 2);
338 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
340 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
341 if (templateLine != null) {
342 ArrayList<VariableDeclaration> templateArgs = UtilParser
343 .getTemplateArg(templateLine);
344 header.setTemplateList(templateArgs);
347 } catch (ParseException e) {
353 public static ArrayList<String> generateGlobalVarDeclaration(
354 SemanticsChecker semantics, GlobalConstruct construct) {
355 ArrayList<String> newCode = new ArrayList<String>();
356 HashSet<String> allHeaders = getAllHeaders(semantics);
358 SequentialDefineSubConstruct code = construct.code;
359 // User-defined structs first
360 newCode.add(COMMENT("All other user-defined structs"));
361 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
362 for (int i = 0; i < declareStructs.size(); i++) {
363 ArrayList<String> declareStruct = declareStructs.get(i);
364 newCode.addAll(declareStruct);
367 // User-defined variables
368 ArrayList<VariableDeclaration> varDecls = code.declareVar;
369 for (int i = 0; i < varDecls.size(); i++) {
370 VariableDeclaration varDecl = varDecls.get(i);
371 // Don't forget to make them static
372 newCode.add(makeVariablesStatic(varDecl));
374 // User-defined functions
375 newCode.add(COMMENT("All other user-defined functions"));
376 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
377 for (int i = 0; i < defineFuncs.size(); i++) {
378 ArrayList<String> defineFunc = defineFuncs.get(i);
379 makeFunctionStatic(defineFunc);
380 newCode.addAll(defineFunc);
384 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
385 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
387 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
388 // Define necessary info structure
389 if (!funcHeader.returnType.equals("void")
390 || funcHeader.args.size() > 0) {
391 newCode.add(COMMENT("Definition of interface info struct: "
393 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
394 newCode.add(COMMENT("End of info struct definition: "
399 // Define ID function
400 newCode.add(COMMENT("ID function of interface: " + interfaceName));
401 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
402 newCode.add(COMMENT("End of ID function: " + interfaceName));
405 // Define check_action function
406 newCode.add(COMMENT("Check action function of interface: "
408 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
409 newCode.add(COMMENT("End of check action function: "
413 // Interface function pointer table
414 String interfaceSize = Integer
415 .toString(semantics.interfaceName2Construct.size());
416 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
418 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
419 // Happens-before initialization rules
420 // Should make it static
421 newCode.add("static " + DECLARE(HB_RULE + "**", "hb_rule_table"));
423 // Declare the Commutativity Rule table
424 newCode.add("static "
425 + DECLARE(COMMUTATIVITY_RULE + "**", "commutativity_rule_table"));
426 // Define the Commutativity Rule condition functions
427 ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
428 for (int i = 0; i < rules.size(); i++) {
429 CommutativityRule rule = rules.get(i);
430 String infoStructType1 = rule.method1 + "_info";
431 String infoStructType2 = rule.method2 + "_info";
432 String condition = rule.condition;
433 String conditionFuncName = "CommutativityCondition"
434 + Integer.toString(i);
436 // Replace the "_M1." and "_M2." with the actual info struct
437 condition = condition.replaceAll("_Method1 \\.", "_info1->");
438 condition = condition.replaceAll("_Method2 \\.", "_info2->");
440 // Declare the signature of the condition function
441 newCode.add("inline static bool " + conditionFuncName
442 + "(void *info1, void *info2) {");
444 // Cast the "void*" type to the actual info type
446 + DECLARE_DEFINE(infoStructType1, "*_info1", "("
447 + infoStructType1 + "*) info1"));
449 + DECLARE_DEFINE(infoStructType2, "*_info2", "("
450 + infoStructType2 + "*) info2"));
451 newCode.add("\treturn " + condition + ";");
453 // End of the condition function
459 // Beginning initialization
460 // Define the __SPEC_INIT__ function to initialize user-defined
462 newCode.add(COMMENT("Initialization of sequential varialbes"));
463 newCode.add("static void __SPEC_INIT__() {");
464 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
468 // Define the __SPEC_CLEAN__ function for clean-up
469 newCode.add(COMMENT("Cleanup routine of sequential variables"));
470 newCode.add("static bool __SPEC_CLEANUP__() {");
471 if (construct.code.cleanupCode.size() > 0) {
472 addAllCodeWithIndent(newCode, construct.code.cleanupCode, "\t");
474 newCode.add("\treturn true;"); // If not specified return true
480 newCode.add(COMMENT("Define function for sequential code initialization"));
481 newCode.add("inline static void __sequential_init() {");
483 // Init func_ptr_table
484 newCode.add("\t" + COMMENT("Init func_ptr_table"));
486 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
487 + semantics.interface2Num.size() + " * 2)"));
488 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
489 String interfaceNum = Integer.toString(semantics.interface2Num
490 .get(interfaceName));
492 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
493 "(void*) &" + interfaceName + "_id"));
495 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
496 "(void*) &" + interfaceName + "_check_action"));
499 // Init Happens-before rules table
500 newCode.addAll(generateHBInitAnnotation(semantics));
502 // Init Commutativity rules table
503 newCode.addAll(generateCommutativityAnnotation(semantics));
505 // Pass init info, including function table info & HB rules
507 + COMMENT("Pass init info, including function table info & HB rules & Commutativity Rules"));
508 String structName = "anno_init", anno = "init";
509 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
511 + ASSIGN_TO_PTR(structName, "init_func",
512 "(void*) __SPEC_INIT__"));
514 + ASSIGN_TO_PTR(structName, "cleanup_func",
515 "(void*) __SPEC_CLEANUP__"));
517 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
519 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
521 + ASSIGN_TO_PTR(structName, "hb_rule_table", "hb_rule_table"));
523 + ASSIGN_TO_PTR(structName, "hb_rule_table_size",
524 "HB_RULE_TABLE_SIZE"));
526 + ASSIGN_TO_PTR(structName, "commutativity_rule_table",
527 "commutativity_rule_table"));
529 + ASSIGN_TO_PTR(structName, "commutativity_rule_table_size",
530 Integer.toString(rules.size())));
532 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
533 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
534 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
535 newCode.add("\t" + ANNOTATE(semantics, anno));
541 newCode.add(COMMENT("End of Global construct generation in class"));
543 // printCode(newCode);
547 public static ArrayList<String> generateStaticVarDefine(
548 SemanticsChecker semantics, GlobalConstruct construct) {
549 ArrayList<String> newCode = new ArrayList<String>();
550 String className = semantics.getClassName();
551 if (className == null)
552 return newCode; // No need to define any static variables
553 String templateList = semantics.getTemplateStr();
555 if (templateList == null) {
556 varPrefix = className + "::";
558 varPrefix = className + templateList + "::";
560 String templateDecl = semantics.getTemplateFullStr();
561 if (templateList == null) {
562 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
563 newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
564 for (int i = 0; i < construct.code.declareVar.size(); i++) {
565 VariableDeclaration varDecl = construct.code.declareVar.get(i);
566 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
569 newCode.add(templateDecl);
570 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
571 newCode.add(templateDecl);
572 newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
573 for (int i = 0; i < construct.code.declareVar.size(); i++) {
574 VariableDeclaration varDecl = construct.code.declareVar.get(i);
575 newCode.add(templateDecl);
576 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
582 private static ArrayList<String> generateHBInitAnnotation(
583 SemanticsChecker semantics) {
584 ArrayList<String> newCode = new ArrayList<String>();
586 int hbConditionInitIdx = 0;
587 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
588 for (ConditionalInterface right : semantics.getHBConditions().get(
590 String structVarName = "hbConditionInit" + hbConditionInitIdx;
591 // String annotationVarName = "hb_rule" + hbConditionInitIdx;
592 hbConditionInitIdx++;
593 String interfaceNumBefore = Integer
594 .toString(semantics.interface2Num
595 .get(left.interfaceName)), hbLabelNumBefore = Integer
596 .toString(semantics.hbLabel2Num
597 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
598 .toString(semantics.interface2Num
599 .get(right.interfaceName)), hbLabelNumAfter = Integer
600 .toString(semantics.hbLabel2Num
601 .get(right.hbConditionLabel));
602 newCode.add("\t" + COMMENT(left + " -> " + right));
605 + STRUCT_NEW_DECLARE_DEFINE(HB_RULE, structVarName));
607 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
609 + SHORT_COMMENT(left.interfaceName));
611 + ASSIGN_TO_PTR(structVarName,
612 "hb_condition_num_before", hbLabelNumBefore)
613 + SHORT_COMMENT(left.hbConditionLabel));
615 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
617 + SHORT_COMMENT(right.interfaceName));
619 + ASSIGN_TO_PTR(structVarName,
620 "hb_condition_num_after", hbLabelNumAfter)
621 + SHORT_COMMENT(right.hbConditionLabel));
624 // Init hb_rule_table
625 newCode.add("\t" + COMMENT("Init hb_rule_table"));
627 + ASSIGN("hb_rule_table", "(" + HB_RULE
628 + "**) malloc(sizeof(" + HB_RULE + "*) * "
629 + hbConditionInitIdx + ")"));
630 // Define HB_RULE_TABLE_SIZE
632 + DEFINE("HB_RULE_TABLE_SIZE",
633 Integer.toString(hbConditionInitIdx)));
634 for (int i = 0; i < hbConditionInitIdx; i++) {
636 + ASSIGN("hb_rule_table[" + i + "]", "hbConditionInit" + i));
641 private static ArrayList<String> generateCommutativityAnnotation(
642 SemanticsChecker semantics) {
643 ArrayList<String> newCode = new ArrayList<String>();
644 ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
646 // Init commutativity_rule_table
647 newCode.add("\t" + COMMENT("Init commutativity_rule_table"));
649 // Users have not defined any commutativity rules
650 if (rules.size() == 0)
654 + ASSIGN("commutativity_rule_table", "(" + COMMUTATIVITY_RULE
655 + "**) malloc(sizeof(" + COMMUTATIVITY_RULE + "*) * "
656 + rules.size() + ")"));
658 // Declare a rule pointer
659 newCode.add("\t" + DECLARE("commutativity_rule*", "rule"));
661 for (int i = 0; i < rules.size(); i++) {
662 CommutativityRule rule = rules.get(i);
663 String interfaceNumBefore = Integer
664 .toString(semantics.interface2Num.get(rule.method1));
665 String interfaceNumAfter = Integer.toString(semantics.interface2Num
667 String conditionFuncName = "CommutativityCondition" + i;
669 // Construct a new rule
672 "(commutativity_rule*) malloc (sizeof(commutativity_rule))"));
674 + ASSIGN_TO_PTR("rule", "interface_num_before",
675 interfaceNumBefore));
677 + ASSIGN_TO_PTR("rule", "interface_num_after",
680 + ASSIGN_TO_PTR("rule", "rule",
681 "\"" + rule.condition + "\""));
684 + ASSIGN_TO_PTR("rule", "condition", conditionFuncName));
686 // Assign the rule to the corresponding commutativity table slot
688 + ASSIGN("commutativity_rule_table[" + i + "]", "rule"));
694 public static ArrayList<String> generateEntryPointInitCall() {
695 ArrayList<String> newCode = new ArrayList<String>();
696 newCode.add("\t" + "__sequential_init();");
700 public static ArrayList<String> generateInterfaceWrapperDeclaration(
701 SemanticsChecker semantics, InterfaceConstruct construct) {
702 FunctionHeader header = getFunctionHeader(semantics, construct);
703 ArrayList<String> declaration = new ArrayList<String>();
704 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
705 .getDeclaration() + ";");
709 // Only generate the definition of the wrapper, don't do any renaming
710 public static ArrayList<String> generateInterfaceWrapperDefinition(
711 SemanticsChecker semantics, InterfaceConstruct construct) {
712 ArrayList<String> newCode = new ArrayList<String>();
713 String interfaceName = construct.name;
715 FunctionHeader header = getFunctionHeader(semantics, construct);
716 String interfaceNum = Integer.toString(semantics.interface2Num
717 .get(construct.name));
719 newCode.add(header.getTemplateFullStr());
720 newCode.add(header.getFuncStr() + " {");
721 // Wrapper function body
722 newCode.add("\t" + COMMENT("Interface begins"));
724 String structName = "interface_begin";
726 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
729 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
730 + SHORT_COMMENT(construct.name));
732 + ASSIGN_TO_PTR(structName, "interface_name", "\""
733 + construct.name + "\""));
735 String anno = "annotation_interface_begin";
736 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
738 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
739 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
740 newCode.add("\t" + ANNOTATE(semantics, anno));
741 // Call original renamed function
742 if (header.returnType.equals("void")) {
743 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
747 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
748 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
751 for (String label : construct.hbConditions.keySet()) {
752 String condition = construct.hbConditions.get(label);
753 String hbCondNum = Integer.toString(semantics.hbLabel2Num
755 newCode.add("\t" + "if " + BRACE(condition) + " {");
756 structName = "hb_condition";
758 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
760 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
761 + SHORT_COMMENT(construct.name));
764 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
765 anno = "annotation_hb_condition";
767 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
769 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
770 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
771 newCode.add("\t\t" + ANNOTATE(semantics, anno));
772 newCode.add("\t" + "}");
775 // Also add the true condition if any
776 if (semantics.containsConditionalInterface(new ConditionalInterface(
777 interfaceName, ""))) {
778 structName = "hb_condition";
780 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
782 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
783 + SHORT_COMMENT(construct.name));
785 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
786 anno = "annotation_hb_condition";
787 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
789 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
790 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
791 newCode.add("\t" + ANNOTATE(semantics, anno));
795 String infoStructType = null, infoName = null;
796 if (!header.returnType.equals("void") || header.args.size() > 0) {
797 infoStructType = interfaceName + "_info";
800 + DECLARE_DEFINE(infoStructType + "*", infoName,
801 BRACE(infoStructType + "*") + " malloc(sizeof("
802 + infoStructType + "))"));
803 if (!header.returnType.equals("void")) {
805 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
807 for (int i = 0; i < header.args.size(); i++) {
808 String argName = header.args.get(i).name;
809 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
814 structName = "interface_end";
815 anno = "annoation_interface_end";
817 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
819 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
820 + SHORT_COMMENT(construct.name));
821 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
822 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
824 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
825 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
826 newCode.add("\t" + ANNOTATE(semantics, anno));
827 // Return __RET__ if it's not void
828 if (!header.returnType.equals("void")) {
829 newCode.add("\t" + "return " + MACRO_RETURN + ";");
831 // End of the wrapper function
834 // printCode(newCode);
838 // Rename the interface name for declaration or definition
839 public static void renameInterface(SemanticsChecker semantics,
840 Construct construct) {
841 FunctionHeader header = getFunctionHeader(semantics, construct);
842 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
843 int lineNum = construct.beginLineNum;
844 String headerLine = content.get(construct.beginLineNum);
845 if (headerLine.startsWith("template")) {
846 headerLine = content.get(construct.beginLineNum + 1);
849 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
851 String oldLine = content.get(lineNum + 1);
853 if (construct instanceof InterfaceConstruct) {
854 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
855 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
856 .get(iConstruct.name);
857 if (defineConstruct != null) { // There is a defineConstruct
858 newLine = newLine + " ;";
859 renameInterface(semantics, defineConstruct);
860 } else { // This is a declare & define construct
861 if (oldLine.indexOf('{') != -1)
862 newLine = newLine + " {";
865 if (oldLine.indexOf('{') != -1)
866 newLine = newLine + " {";
869 content.set(lineNum + 1, newLine);
872 public static void addAtomicReturn(SemanticsChecker semantics,
873 Construct construct) {
874 int lineNum = construct.beginLineNum - 1;
875 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
876 String oldLine = content.get(lineNum);
877 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
878 content.set(lineNum, newLine);
881 public static ArrayList<String> generatePotentialCPDefine(
882 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
883 ArrayList<String> newCode = new ArrayList<String>();
884 // Add atomic return variable if the predicate accesses to it
885 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
886 addAtomicReturn(semantics, construct);
888 // Generate redundant header files
890 + COMMENT("Automatically generated code for potential commit point: "
894 newCode.add("\t" + "if (" + construct.condition + ") {");
895 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
897 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
899 String labelNum = Integer.toString(semantics.commitPointLabel2Num
900 .get(construct.label));
901 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
903 + ASSIGN_TO_PTR(structName, "label_name", "\""
904 + construct.label + "\""));
906 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
908 + ASSIGN_TO_PTR(anno, "type",
909 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
910 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
911 newCode.add("\t\t" + ANNOTATE(semantics, anno));
912 newCode.add("\t" + "}");
916 public static String getCPInterfaceNum(SemanticsChecker semantics,
917 String commitPointLabel) {
918 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
919 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
920 String interfaceName = iConstruct.name;
921 String interfaceNum = Integer.toString(semantics.interface2Num
922 .get(interfaceName));
928 * Commit point define check should be unique to each interface, meaning
929 * that they are not shared between different interfaces
936 public static ArrayList<String> generateCPDefineCheck(
937 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
938 ArrayList<String> newCode = new ArrayList<String>();
939 // Add atomic return variable if the predicate accesses to it
940 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
941 addAtomicReturn(semantics, construct);
943 // Generate redundant header files
945 + COMMENT("Automatically generated code for commit point define check: "
950 newCode.add("\t" + "if (" + construct.condition + ") {");
951 String structName = "cp_define_check", anno = "annotation_cp_define_check";
953 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
954 String labelNum = Integer.toString(semantics.commitPointLabel2Num
955 .get(construct.label));
956 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
957 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
959 + ASSIGN_TO_PTR(structName, "label_name", "\""
960 + construct.label + "\""));
962 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
964 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
966 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
967 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
968 newCode.add("\t\t" + ANNOTATE(semantics, anno));
969 newCode.add("\t" + "}");
975 * Commit point define check should be unique to each interface, meaning
976 * that they are not shared between different interfaces
983 public static ArrayList<String> generateCPClear(SemanticsChecker semantics,
984 CPClearConstruct construct) {
985 ArrayList<String> newCode = new ArrayList<String>();
986 // Add atomic return variable if the predicate accesses to it
987 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
988 addAtomicReturn(semantics, construct);
990 // Generate redundant header files
992 + COMMENT("Automatically generated code for commit point clear: "
997 newCode.add("\t" + "if (" + construct.condition + ") {");
998 String structName = "cp_clear", anno = "annotation_cp_clear";
1000 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
1002 String labelNum = Integer.toString(semantics.commitPointLabel2Num
1003 .get(construct.label));
1004 String labelName = construct.label;
1005 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_name",
1006 "\"" + labelName + "\""));
1007 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num",
1010 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
1012 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
1013 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
1014 newCode.add("\t\t" + ANNOTATE(semantics, anno));
1015 newCode.add("\t" + "}");
1021 * Commit point define should be unique to each interface, meaning that they
1022 * are not shared between different interfaces
1029 public static ArrayList<String> generateCPDefine(
1030 SemanticsChecker semantics, CPDefineConstruct construct) {
1031 ArrayList<String> newCode = new ArrayList<String>();
1032 // Generate redundant header files
1034 + COMMENT("Automatically generated code for commit point define: "
1035 + construct.label));
1038 newCode.add("\t" + "if (" + construct.condition + ") {");
1039 String structName = "cp_define", anno = "annotation_cp_define";
1041 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
1042 String labelNum = Integer.toString(semantics.commitPointLabel2Num
1043 .get(construct.label));
1044 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
1045 String potentialLabelNum = Integer
1046 .toString(semantics.commitPointLabel2Num
1047 .get(construct.potentialCPLabel));
1048 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
1050 + ASSIGN_TO_PTR(structName, "label_name", "\""
1051 + construct.label + "\""));
1053 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
1054 potentialLabelNum));
1056 + ASSIGN_TO_PTR(structName, "potential_label_name", "\""
1057 + construct.potentialCPLabel + "\""));
1059 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
1060 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
1062 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
1063 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
1064 newCode.add("\t\t" + ANNOTATE(semantics, anno));
1065 newCode.add("\t" + "}");