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.ConditionalInterface;
14 import edu.uci.eecs.specCompiler.specExtraction.Construct;
15 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
16 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
25 * Defines a list of commonly used constant strings.
31 public class CodeVariables {
32 // C++ code or library
33 public static final String HEADER_STDLIB = "<stdlib.h>";
34 public static final String HEADER_THREADS = "<threads.h>";
35 public static final String HEADER_STDINT = "<stdint.h>";
36 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
37 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
38 public static final String ThreadIDType = "thread_id_t";
39 public static final String BOOLEAN = "bool";
40 public static final String UINT64 = "uint64_t";
43 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
44 public static final String HEADER_COMMON = "<common.h>";
45 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
46 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
47 public static final String CDSAnnotate = "cdsannotate";
48 // public static final String CDSAnnotate = "cdsannotate";
49 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
50 public static final String IDType = "call_id_t";
52 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
53 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
54 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
55 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
56 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
57 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
58 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
59 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
60 public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
61 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
62 public static final String SPEC_ANNOTATION = "spec_annotation";
63 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
64 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
66 public static final String ANNO_INIT = "anno_init";
67 public static final String ANNO_HB_INIT = "anno_hb_init";
68 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
69 public static final String ANNO_INTERFACE_END = "anno_interface_end";
70 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
71 public static final String ANNO_CP_DEFINE = "anno_cp_define";
72 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
73 public static final String ANNO_CP_CLEAR = "anno_cp_clear";
74 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
76 // Specification variables
77 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
78 public static final String DEFAULT_ID = "0";
80 // Specification library
81 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
82 public static final String SPEC_QUEUE = "spec_queue";
83 public static final String SPEC_STACK = "spec_stack";
84 public static final String SPEC_DEQUE = "spec_deque";
85 public static final String SPEC_HASHTABLE = "spec_hashtable";
86 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
87 public static final String SPEC_TAG = "spec_tag";
88 public static final String SPEC_TAG_CURRENT = "current";
89 public static final String SPEC_TAG_NEXT = "next";
92 public static final String MACRO_ID = "__ID__";
93 public static final String MACRO_COND = "__COND_SAT__";
94 public static final String MACRO_RETURN = "__RET__";
95 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
96 public static final String MACRO_THREAD_ID = "__TID__";
98 public static void printCode(ArrayList<String> code) {
99 for (int i = 0; i < code.size(); i++) {
100 System.out.println(code.get(i));
104 private static String COMMENT(String comment) {
105 return "/* " + comment + " */";
108 private static String SHORT_COMMENT(String comment) {
109 return " // " + comment;
112 private static String INCLUDE(String header) {
113 return "#include " + header;
116 private static String DEFINE(String left, String right) {
117 return "#define " + left + " " + right;
120 private static String UNDEFINE(String macro) {
121 return "#undef " + macro;
124 private static String GET_FIELD_BY_PTR(String ptr, String field) {
125 return ptr + "->" + field;
128 private static String GET_FIELD(String var, String field) {
129 return var + "->" + field;
132 private static String BRACE(String val) {
133 return "(" + val + ")";
136 private static String ASSIGN(String structName, String field, String val) {
137 return structName + "." + field + " = " + val + ";";
140 private static String ASSIGN(String varName, String val) {
141 return varName + " = " + val + ";";
144 private static String ASSIGN_PTR(String structName, String field, String val) {
145 return structName + "." + field + " = &" + val + ";";
148 private static String ASSIGN_TO_PTR(String structName, String field,
150 return structName + "->" + field + " = " + val + ";";
153 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
155 return structName + "->" + field + " = &" + val + ";";
158 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
159 return "struct " + type + " *" + name + " = (struct " + type
160 + "*) malloc(sizeof(struct " + type + "));";
163 private static String DECLARE(String type, String name) {
164 return type + " " + name + ";";
167 private static String DECLARE(VariableDeclaration varDecl) {
168 String type = varDecl.type, name = varDecl.name;
169 return type + " " + name + ";";
172 private static String DECLARE_DEFINE(String type, String var, String val) {
173 return type + " " + var + " = " + val + ";";
176 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
177 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
180 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
181 FunctionHeader header) {
182 ArrayList<String> code = new ArrayList<String>();
183 code.add("typedef struct " + interfaceName + "_info {");
184 if (!header.returnType.equals("void")) {
185 code.add(DECLARE(header.returnType, MACRO_RETURN));
187 for (int i = 0; i < header.args.size(); i++) {
188 code.add(DECLARE(header.args.get(i)));
190 code.add("} " + interfaceName + "_info;");
194 private static ArrayList<String> DEFINE_ID_FUNC(
195 InterfaceConstruct construct, FunctionHeader header) {
196 String interfaceName = construct.name;
197 ArrayList<String> code = new ArrayList<String>();
198 String idCode = construct.idCode;
199 code.add("inline static " + IDType + " " + interfaceName + "_id("
200 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
203 if (!header.returnType.equals("void") || header.args.size() != 0) {
204 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
205 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
206 infoStructName, BRACE(infoStructType + "*") + "info"));
207 if (!header.returnType.equals("void")) {
208 code.add((DECLARE_DEFINE("\t" + header.returnType,
210 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
212 for (int i = 0; i < header.args.size(); i++) {
213 String type = header.args.get(i).type, var = header.args.get(i).name;
215 + (DECLARE_DEFINE(type, var,
216 GET_FIELD_BY_PTR(infoStructName, var))));
221 if (!idCode.equals("")) {
222 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
224 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
226 code.add("\treturn " + MACRO_ID + ";");
231 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
232 InterfaceConstruct construct, FunctionHeader header) {
233 String interfaceName = construct.name;
234 ArrayList<String> code = new ArrayList<String>();
235 code.add("inline static bool " + interfaceName
236 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
237 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
238 code.add("\t" + DECLARE("bool", "check_passed"));
240 if (!header.returnType.equals("void") || header.args.size() != 0) {
241 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
243 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
244 BRACE(infoStructType + "*") + "info"));
245 if (!header.returnType.equals("void")) {
247 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
248 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
250 for (int i = 0; i < header.args.size(); i++) {
251 String type = header.args.get(i).type, var = header.args.get(i).name;
253 + (DECLARE_DEFINE(type, var,
254 GET_FIELD_BY_PTR(infoStructName, var))));
259 if (!construct.condition.equals("")) {
261 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
264 if (!construct.check.equals("")) {
265 code.add("\t" + ASSIGN("check_passed", construct.check));
266 code.add("\tif (!check_passed)");
267 code.add("\t\treturn false;");
271 if (construct.action.size() > 0) {
272 addAllCodeWithIndent(code, construct.action, "\t");
275 if (!construct.postCheck.equals("")) {
276 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
277 code.add("\tif (!check_passed)");
278 code.add("\t\treturn false;");
281 if (construct.postAction.size() > 0) {
282 addAllCodeWithIndent(code, construct.postAction, "\t");
284 // Return true finally
285 code.add("\treturn true;");
292 private static void addAllCodeWithIndent(ArrayList<String> allCode,
293 ArrayList<String> target, String indent) {
294 for (int i = 0; i < target.size(); i++) {
295 allCode.add(indent + target.get(i));
299 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
300 HashSet<String> headers = new HashSet<String>();
301 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
302 File f = semantics.interfaceName2Construct.get(interfaceName).file;
303 headers.addAll(semantics.srcFilesInfo.get(f).headers);
305 headers.add(HEADER_STDLIB);
306 headers.add(HEADER_STDINT);
307 headers.add(HEADER_MODELMEMORY);
308 headers.add(HEADER_MODELTYPES);
309 headers.add(HEADER_SPEC_LIB);
310 headers.add(HEADER_STDINT);
311 headers.add(HEADER_CDSANNOTATE);
312 // headers.add(HEADER_COMMON);
313 headers.add(HEADER_SPECANNOTATION);
317 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
318 String headLine = funcDefine.get(0);
319 headLine = "inline static " + headLine;
320 funcDefine.set(0, headLine);
323 private static String makeVariablesStatic(VariableDeclaration varDecl) {
324 String res = "static " + varDecl.type + " " + varDecl.name + ";";
328 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
329 Construct construct) {
330 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
331 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
332 if (headerLine.startsWith("template")) {
333 templateLine = headerLine;
334 headerLine = content.get(construct.beginLineNum + 2);
336 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
338 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
339 if (templateLine != null) {
340 ArrayList<VariableDeclaration> templateArgs = UtilParser
341 .getTemplateArg(templateLine);
342 header.setTemplateList(templateArgs);
345 } catch (ParseException e) {
351 public static ArrayList<String> generateGlobalVarDeclaration(
352 SemanticsChecker semantics, GlobalConstruct construct) {
353 ArrayList<String> newCode = new ArrayList<String>();
354 HashSet<String> allHeaders = getAllHeaders(semantics);
356 SequentialDefineSubConstruct code = construct.code;
357 // User-defined structs first
358 newCode.add(COMMENT("All other user-defined structs"));
359 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
360 for (int i = 0; i < declareStructs.size(); i++) {
361 ArrayList<String> declareStruct = declareStructs.get(i);
362 newCode.addAll(declareStruct);
365 // User-defined variables
366 ArrayList<VariableDeclaration> varDecls = code.declareVar;
367 for (int i = 0; i < varDecls.size(); i++) {
368 VariableDeclaration varDecl = varDecls.get(i);
369 // Don't forget to make them static
370 newCode.add(makeVariablesStatic(varDecl));
372 // User-defined functions
373 newCode.add(COMMENT("All other user-defined functions"));
374 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
375 for (int i = 0; i < defineFuncs.size(); i++) {
376 ArrayList<String> defineFunc = defineFuncs.get(i);
377 makeFunctionStatic(defineFunc);
378 newCode.addAll(defineFunc);
382 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
383 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
385 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
386 // Define necessary info structure
387 if (!funcHeader.returnType.equals("void")
388 || funcHeader.args.size() > 0) {
389 newCode.add(COMMENT("Definition of interface info struct: "
391 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
392 newCode.add(COMMENT("End of info struct definition: "
397 // Define ID function
398 newCode.add(COMMENT("ID function of interface: " + interfaceName));
399 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
400 newCode.add(COMMENT("End of ID function: " + interfaceName));
403 // Define check_action function
404 newCode.add(COMMENT("Check action function of interface: "
406 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
407 newCode.add(COMMENT("End of check action function: "
411 // Interface function pointer table
412 String interfaceSize = Integer
413 .toString(semantics.interfaceName2Construct.size());
414 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
416 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
417 // Happens-before initialization rules
418 // Should make it static
419 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
423 // Define the __SPEC_INIT__ function to initialize user-defined
425 newCode.add(COMMENT("Initialization of sequential varialbes"));
426 newCode.add("static void __SPEC_INIT__() {");
427 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
431 // Define the __SPEC_CLEAN__ function for clean-up
432 newCode.add(COMMENT("Cleanup routine of sequential variables"));
433 newCode.add("static void __SPEC_CLEANUP__() {");
434 addAllCodeWithIndent(newCode, construct.code.cleanupCode, "\t");
438 newCode.add(COMMENT("Define function for sequential code initialization"));
439 newCode.add("inline static void __sequential_init() {");
440 // Init func_ptr_table
441 newCode.add("\t" + COMMENT("Init func_ptr_table"));
443 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
444 + semantics.interface2Num.size() + " * 2)"));
445 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
446 String interfaceNum = Integer.toString(semantics.interface2Num
447 .get(interfaceName));
449 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
450 "(void*) &" + interfaceName + "_id"));
452 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
453 "(void*) &" + interfaceName + "_check_action"));
455 // Init Happens-before rules table
456 newCode.addAll(generateHBInitAnnotation(semantics));
458 // Pass init info, including function table info & HB rules
460 + COMMENT("Pass init info, including function table info & HB rules"));
461 String structName = "anno_init", anno = "init";
462 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
464 + ASSIGN_TO_PTR(structName, "init_func", "(void*) __SPEC_INIT__"));
466 + ASSIGN_TO_PTR(structName, "cleanup_func", "(void*) __SPEC_CLEANUP__"));
468 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
470 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
472 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
474 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
475 "HB_INIT_TABLE_SIZE"));
476 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
477 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
478 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
479 newCode.add("\t" + ANNOTATE(semantics, anno));
485 newCode.add(COMMENT("End of Global construct generation in class"));
487 // printCode(newCode);
491 public static ArrayList<String> generateStaticVarDefine(
492 SemanticsChecker semantics, GlobalConstruct construct) {
493 ArrayList<String> newCode = new ArrayList<String>();
494 String className = semantics.getClassName();
495 if (className == null)
496 return newCode; // No need to define any static variables
497 String templateList = semantics.getTemplateStr();
499 if (templateList == null) {
500 varPrefix = className + "::";
502 varPrefix = className + templateList + "::";
504 String templateDecl = semantics.getTemplateFullStr();
505 if (templateList == null) {
506 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
507 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
508 for (int i = 0; i < construct.code.declareVar.size(); i++) {
509 VariableDeclaration varDecl = construct.code.declareVar.get(i);
510 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
513 newCode.add(templateDecl);
514 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
515 newCode.add(templateDecl);
516 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
517 for (int i = 0; i < construct.code.declareVar.size(); i++) {
518 VariableDeclaration varDecl = construct.code.declareVar.get(i);
519 newCode.add(templateDecl);
520 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
526 private static ArrayList<String> generateHBInitAnnotation(
527 SemanticsChecker semantics) {
528 ArrayList<String> newCode = new ArrayList<String>();
530 int hbConditionInitIdx = 0;
531 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
532 for (ConditionalInterface right : semantics.getHBConditions().get(
534 String structVarName = "hbConditionInit" + hbConditionInitIdx;
535 // String annotationVarName = "hb_init" + hbConditionInitIdx;
536 hbConditionInitIdx++;
537 String interfaceNumBefore = Integer
538 .toString(semantics.interface2Num
539 .get(left.interfaceName)), hbLabelNumBefore = Integer
540 .toString(semantics.hbLabel2Num
541 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
542 .toString(semantics.interface2Num
543 .get(right.interfaceName)), hbLabelNumAfter = Integer
544 .toString(semantics.hbLabel2Num
545 .get(right.hbConditionLabel));
546 newCode.add("\t" + COMMENT(left + " -> " + right));
549 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
551 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
553 + SHORT_COMMENT(left.interfaceName));
555 + ASSIGN_TO_PTR(structVarName,
556 "hb_condition_num_before", hbLabelNumBefore)
557 + SHORT_COMMENT(left.hbConditionLabel));
559 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
561 + SHORT_COMMENT(right.interfaceName));
563 + ASSIGN_TO_PTR(structVarName,
564 "hb_condition_num_after", hbLabelNumAfter)
565 + SHORT_COMMENT(right.hbConditionLabel));
568 // Init hb_init_table
569 newCode.add("\t" + COMMENT("Init hb_init_table"));
571 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
572 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
573 + hbConditionInitIdx + ")"));
574 // Define HB_INIT_TABLE_SIZE
576 + DEFINE("HB_INIT_TABLE_SIZE",
577 Integer.toString(hbConditionInitIdx)));
578 for (int i = 0; i < hbConditionInitIdx; i++) {
580 + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
585 public static ArrayList<String> generateEntryPointInitCall() {
586 ArrayList<String> newCode = new ArrayList<String>();
587 newCode.add("\t" + "__sequential_init();");
591 public static ArrayList<String> generateInterfaceWrapperDeclaration(
592 SemanticsChecker semantics, InterfaceConstruct construct) {
593 FunctionHeader header = getFunctionHeader(semantics, construct);
594 ArrayList<String> declaration = new ArrayList<String>();
595 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
596 .getDeclaration() + ";");
600 // Only generate the definition of the wrapper, don't do any renaming
601 public static ArrayList<String> generateInterfaceWrapperDefinition(
602 SemanticsChecker semantics, InterfaceConstruct construct) {
603 ArrayList<String> newCode = new ArrayList<String>();
604 String interfaceName = construct.name;
606 FunctionHeader header = getFunctionHeader(semantics, construct);
607 String interfaceNum = Integer.toString(semantics.interface2Num
608 .get(construct.name));
610 newCode.add(header.getTemplateFullStr());
611 newCode.add(header.getFuncStr() + " {");
612 // Wrapper function body
613 newCode.add("\t" + COMMENT("Interface begins"));
615 String structName = "interface_begin";
617 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
620 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
621 + SHORT_COMMENT(construct.name));
623 + ASSIGN_TO_PTR(structName, "interface_name", "\""
624 + construct.name + "\""));
626 String anno = "annotation_interface_begin";
627 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
629 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
630 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
631 newCode.add("\t" + ANNOTATE(semantics, anno));
632 // Call original renamed function
633 if (header.returnType.equals("void")) {
634 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
638 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
639 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
642 for (String label : construct.hbConditions.keySet()) {
643 String condition = construct.hbConditions.get(label);
644 String hbCondNum = Integer.toString(semantics.hbLabel2Num
646 newCode.add("\t" + "if " + BRACE(condition) + " {");
647 structName = "hb_condition";
649 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
651 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
652 + SHORT_COMMENT(construct.name));
655 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
656 anno = "annotation_hb_condition";
658 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
660 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
661 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
662 newCode.add("\t\t" + ANNOTATE(semantics, anno));
663 newCode.add("\t" + "}");
666 // Also add the true condition if any
667 if (semantics.containsConditionalInterface(new ConditionalInterface(
668 interfaceName, ""))) {
669 structName = "hb_condition";
671 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
673 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
674 + SHORT_COMMENT(construct.name));
676 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
677 anno = "annotation_hb_condition";
678 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
680 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
681 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
682 newCode.add("\t" + ANNOTATE(semantics, anno));
686 String infoStructType = null, infoName = null;
687 if (!header.returnType.equals("void") || header.args.size() > 0) {
688 infoStructType = interfaceName + "_info";
691 + DECLARE_DEFINE(infoStructType + "*", infoName,
692 BRACE(infoStructType + "*") + " malloc(sizeof("
693 + infoStructType + "))"));
694 if (!header.returnType.equals("void")) {
696 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
698 for (int i = 0; i < header.args.size(); i++) {
699 String argName = header.args.get(i).name;
700 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
705 structName = "interface_end";
706 anno = "annoation_interface_end";
708 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
710 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
711 + SHORT_COMMENT(construct.name));
712 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
713 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
715 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
716 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
717 newCode.add("\t" + ANNOTATE(semantics, anno));
718 // Return __RET__ if it's not void
719 if (!header.returnType.equals("void")) {
720 newCode.add("\t" + "return " + MACRO_RETURN + ";");
722 // End of the wrapper function
725 // printCode(newCode);
729 // Rename the interface name for declaration or definition
730 public static void renameInterface(SemanticsChecker semantics,
731 Construct construct) {
732 FunctionHeader header = getFunctionHeader(semantics, construct);
733 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
734 int lineNum = construct.beginLineNum;
735 String headerLine = content.get(construct.beginLineNum);
736 if (headerLine.startsWith("template")) {
737 headerLine = content.get(construct.beginLineNum + 1);
740 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
742 String oldLine = content.get(lineNum + 1);
744 if (construct instanceof InterfaceConstruct) {
745 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
746 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
747 .get(iConstruct.name);
748 if (defineConstruct != null) { // There is a defineConstruct
749 newLine = newLine + " ;";
750 renameInterface(semantics, defineConstruct);
751 } else { // This is a declare & define construct
752 if (oldLine.indexOf('{') != -1)
753 newLine = newLine + " {";
756 if (oldLine.indexOf('{') != -1)
757 newLine = newLine + " {";
760 content.set(lineNum + 1, newLine);
763 public static void addAtomicReturn(SemanticsChecker semantics,
764 Construct construct) {
765 int lineNum = construct.beginLineNum - 1;
766 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
767 String oldLine = content.get(lineNum);
768 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
769 content.set(lineNum, newLine);
772 public static ArrayList<String> generatePotentialCPDefine(
773 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
774 ArrayList<String> newCode = new ArrayList<String>();
775 // Add atomic return variable if the predicate accesses to it
776 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
777 addAtomicReturn(semantics, construct);
779 // Generate redundant header files
781 + COMMENT("Automatically generated code for potential commit point: "
785 newCode.add("\t" + "if (" + construct.condition + ") {");
786 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
788 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
790 String labelNum = Integer.toString(semantics.commitPointLabel2Num
791 .get(construct.label));
792 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
794 + ASSIGN_TO_PTR(structName, "label_name", "\""
795 + construct.label + "\""));
796 if (construct.isAdditionalOrderingPoint) {
798 + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
801 + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
803 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
805 + ASSIGN_TO_PTR(anno, "type",
806 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
807 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
808 newCode.add("\t\t" + ANNOTATE(semantics, anno));
809 newCode.add("\t" + "}");
813 public static String getCPInterfaceNum(SemanticsChecker semantics,
814 String commitPointLabel) {
815 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
816 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
817 String interfaceName = iConstruct.name;
818 String interfaceNum = Integer.toString(semantics.interface2Num
819 .get(interfaceName));
825 * Commit point define check should be unique to each interface, meaning
826 * that they are not shared between different interfaces
833 public static ArrayList<String> generateCPDefineCheck(
834 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
835 ArrayList<String> newCode = new ArrayList<String>();
836 // Add atomic return variable if the predicate accesses to it
837 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
838 addAtomicReturn(semantics, construct);
840 // Generate redundant header files
842 + COMMENT("Automatically generated code for commit point define check: "
847 newCode.add("\t" + "if (" + construct.condition + ") {");
848 String structName = "cp_define_check", anno = "annotation_cp_define_check";
850 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
851 String labelNum = Integer.toString(semantics.commitPointLabel2Num
852 .get(construct.label));
853 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
854 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
856 + ASSIGN_TO_PTR(structName, "label_name", "\""
857 + construct.label + "\""));
859 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
860 if (construct.isAdditionalOrderingPoint) {
862 + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
865 + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
867 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
869 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
870 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
871 newCode.add("\t\t" + ANNOTATE(semantics, anno));
872 newCode.add("\t" + "}");
878 * Commit point define check should be unique to each interface, meaning
879 * that they are not shared between different interfaces
886 public static ArrayList<String> generateCPClear(SemanticsChecker semantics,
887 CPClearConstruct construct) {
888 ArrayList<String> newCode = new ArrayList<String>();
889 // Add atomic return variable if the predicate accesses to it
890 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
891 addAtomicReturn(semantics, construct);
893 // Generate redundant header files
895 + COMMENT("Automatically generated code for commit point clear: "
900 newCode.add("\t" + "if (" + construct.condition + ") {");
901 String structName = "cp_clear", anno = "annotation_cp_clear";
903 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
904 // String labelNum = Integer.toString(semantics.commitPointLabel2Num
905 // .get(construct.label));
906 // String interfaceNum = getCPInterfaceNum(semantics, construct.label);
907 // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num",
909 // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num",
911 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
913 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
914 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
915 newCode.add("\t\t" + ANNOTATE(semantics, anno));
916 newCode.add("\t" + "}");
922 * Commit point define should be unique to each interface, meaning that they
923 * are not shared between different interfaces
930 public static ArrayList<String> generateCPDefine(
931 SemanticsChecker semantics, CPDefineConstruct construct) {
932 ArrayList<String> newCode = new ArrayList<String>();
933 // Generate redundant header files
935 + COMMENT("Automatically generated code for commit point define: "
939 newCode.add("\t" + "if (" + construct.condition + ") {");
940 String structName = "cp_define", anno = "annotation_cp_define";
942 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
943 String labelNum = Integer.toString(semantics.commitPointLabel2Num
944 .get(construct.label));
945 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
946 String potentialLabelNum = Integer
947 .toString(semantics.commitPointLabel2Num
948 .get(construct.potentialCPLabel));
949 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
951 + ASSIGN_TO_PTR(structName, "label_name", "\""
952 + construct.label + "\""));
954 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
957 + ASSIGN_TO_PTR(structName, "potential_label_name", "\""
958 + construct.potentialCPLabel + "\""));
960 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
961 if (construct.isAdditionalOrderingPoint) {
963 + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
966 + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
968 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
970 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
971 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
972 newCode.add("\t\t" + ANNOTATE(semantics, anno));
973 newCode.add("\t" + "}");