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;
23 public class CodeVariables {
24 // C++ code or library
25 public static final String HEADER_STDLIB = "<stdlib.h>";
26 public static final String HEADER_THREADS = "<threads.h>";
27 public static final String HEADER_STDINT = "<stdint.h>";
28 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
29 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
30 public static final String ThreadIDType = "thread_id_t";
31 public static final String BOOLEAN = "bool";
32 public static final String UINT64 = "uint64_t";
35 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
36 public static final String HEADER_COMMON = "<common.h>";
37 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
38 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
39 public static final String CDSAnnotate = "cdsannotate";
40 // public static final String CDSAnnotate = "cdsannotate";
41 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
42 public static final String IDType = "call_id_t";
44 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
45 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
46 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
47 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
48 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
49 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
50 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
51 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
52 public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
53 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
54 public static final String SPEC_ANNOTATION = "spec_annotation";
55 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
56 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
58 public static final String ANNO_INIT = "anno_init";
59 public static final String ANNO_HB_INIT = "anno_hb_init";
60 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
61 public static final String ANNO_INTERFACE_END = "anno_interface_end";
62 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
63 public static final String ANNO_CP_DEFINE = "anno_cp_define";
64 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
65 public static final String ANNO_CP_CLEAR = "anno_cp_clear";
66 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
68 // Specification variables
69 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
70 public static final String DEFAULT_ID = "0";
72 // Specification library
73 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
74 public static final String SPEC_QUEUE = "spec_queue";
75 public static final String SPEC_STACK = "spec_stack";
76 public static final String SPEC_DEQUE = "spec_deque";
77 public static final String SPEC_HASHTABLE = "spec_hashtable";
78 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
79 public static final String SPEC_TAG = "spec_tag";
80 public static final String SPEC_TAG_CURRENT = "current";
81 public static final String SPEC_TAG_NEXT = "next";
84 public static final String MACRO_ID = "__ID__";
85 public static final String MACRO_COND = "__COND_SAT__";
86 public static final String MACRO_RETURN = "__RET__";
87 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
88 public static final String MACRO_THREAD_ID = "__TID__";
90 public static void printCode(ArrayList<String> code) {
91 for (int i = 0; i < code.size(); i++) {
92 System.out.println(code.get(i));
96 private static String COMMENT(String comment) {
97 return "/* " + comment + " */";
100 private static String SHORT_COMMENT(String comment) {
101 return " // " + comment;
104 private static String INCLUDE(String header) {
105 return "#include " + header;
108 private static String DEFINE(String left, String right) {
109 return "#define " + left + " " + right;
112 private static String UNDEFINE(String macro) {
113 return "#undef " + macro;
116 private static String GET_FIELD_BY_PTR(String ptr, String field) {
117 return ptr + "->" + field;
120 private static String GET_FIELD(String var, String field) {
121 return var + "->" + field;
124 private static String BRACE(String val) {
125 return "(" + val + ")";
128 private static String ASSIGN(String structName, String field, String val) {
129 return structName + "." + field + " = " + val + ";";
132 private static String ASSIGN(String varName, String val) {
133 return varName + " = " + val + ";";
136 private static String ASSIGN_PTR(String structName, String field, String val) {
137 return structName + "." + field + " = &" + val + ";";
140 private static String ASSIGN_TO_PTR(String structName, String field,
142 return structName + "->" + field + " = " + val + ";";
145 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
147 return structName + "->" + field + " = &" + val + ";";
150 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
151 return "struct " + type + " *" + name + " = (struct " + type
152 + "*) malloc(sizeof(struct " + type + "));";
155 private static String DECLARE(String type, String name) {
156 return type + " " + name + ";";
159 private static String DECLARE(VariableDeclaration varDecl) {
160 String type = varDecl.type, name = varDecl.name;
161 return type + " " + name + ";";
164 private static String DECLARE_DEFINE(String type, String var, String val) {
165 return type + " " + var + " = " + val + ";";
168 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
169 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
172 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
173 FunctionHeader header) {
174 ArrayList<String> code = new ArrayList<String>();
175 code.add("typedef struct " + interfaceName + "_info {");
176 if (!header.returnType.equals("void")) {
177 code.add(DECLARE(header.returnType, MACRO_RETURN));
179 for (int i = 0; i < header.args.size(); i++) {
180 code.add(DECLARE(header.args.get(i)));
182 code.add("} " + interfaceName + "_info;");
186 private static ArrayList<String> DEFINE_ID_FUNC(
187 InterfaceConstruct construct, FunctionHeader header) {
188 String interfaceName = construct.name;
189 ArrayList<String> code = new ArrayList<String>();
190 String idCode = construct.idCode;
191 code.add("inline static " + IDType + " " + interfaceName + "_id("
192 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
195 if (!header.returnType.equals("void") || header.args.size() != 0) {
196 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
197 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
198 infoStructName, BRACE(infoStructType + "*") + "info"));
199 if (!header.returnType.equals("void")) {
200 code.add((DECLARE_DEFINE("\t" + header.returnType,
202 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
204 for (int i = 0; i < header.args.size(); i++) {
205 String type = header.args.get(i).type, var = header.args.get(i).name;
207 + (DECLARE_DEFINE(type, var,
208 GET_FIELD_BY_PTR(infoStructName, var))));
213 if (!idCode.equals("")) {
214 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
216 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
218 code.add("\treturn " + MACRO_ID + ";");
223 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
224 InterfaceConstruct construct, FunctionHeader header) {
225 String interfaceName = construct.name;
226 ArrayList<String> code = new ArrayList<String>();
227 code.add("inline static bool " + interfaceName
228 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
229 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
230 code.add("\t" + DECLARE("bool", "check_passed"));
232 if (!header.returnType.equals("void") || header.args.size() != 0) {
233 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
235 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
236 BRACE(infoStructType + "*") + "info"));
237 if (!header.returnType.equals("void")) {
239 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
240 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
242 for (int i = 0; i < header.args.size(); i++) {
243 String type = header.args.get(i).type, var = header.args.get(i).name;
245 + (DECLARE_DEFINE(type, var,
246 GET_FIELD_BY_PTR(infoStructName, var))));
251 if (!construct.condition.equals("")) {
253 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
256 if (!construct.check.equals("")) {
257 code.add("\t" + ASSIGN("check_passed", construct.check));
258 code.add("\tif (!check_passed)");
259 code.add("\t\treturn false;");
263 if (construct.action.size() > 0) {
264 addAllCodeWithIndent(code, construct.action, "\t");
267 if (!construct.postCheck.equals("")) {
268 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
269 code.add("\tif (!check_passed)");
270 code.add("\t\treturn false;");
273 if (construct.postAction.size() > 0) {
274 addAllCodeWithIndent(code, construct.postAction, "\t");
276 // Return true finally
277 code.add("\treturn true;");
284 private static void addAllCodeWithIndent(ArrayList<String> allCode,
285 ArrayList<String> target, String indent) {
286 for (int i = 0; i < target.size(); i++) {
287 allCode.add(indent + target.get(i));
291 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
292 HashSet<String> headers = new HashSet<String>();
293 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
294 File f = semantics.interfaceName2Construct.get(interfaceName).file;
295 headers.addAll(semantics.srcFilesInfo.get(f).headers);
297 headers.add(HEADER_STDLIB);
298 headers.add(HEADER_STDINT);
299 headers.add(HEADER_MODELMEMORY);
300 headers.add(HEADER_MODELTYPES);
301 headers.add(HEADER_SPEC_LIB);
302 headers.add(HEADER_STDINT);
303 headers.add(HEADER_CDSANNOTATE);
304 // headers.add(HEADER_COMMON);
305 headers.add(HEADER_SPECANNOTATION);
309 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
310 String headLine = funcDefine.get(0);
311 headLine = "inline static " + headLine;
312 funcDefine.set(0, headLine);
315 private static String makeVariablesStatic(VariableDeclaration varDecl) {
316 String res = "static " + varDecl.type + " " + varDecl.name + ";";
320 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
321 Construct construct) {
322 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
323 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
324 if (headerLine.startsWith("template")) {
325 templateLine = headerLine;
326 headerLine = content.get(construct.beginLineNum + 2);
328 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
330 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
331 if (templateLine != null) {
332 ArrayList<VariableDeclaration> templateArgs = UtilParser
333 .getTemplateArg(templateLine);
334 header.setTemplateList(templateArgs);
337 } catch (ParseException e) {
343 public static ArrayList<String> generateGlobalVarDeclaration(
344 SemanticsChecker semantics, GlobalConstruct construct) {
345 ArrayList<String> newCode = new ArrayList<String>();
346 HashSet<String> allHeaders = getAllHeaders(semantics);
348 SequentialDefineSubConstruct code = construct.code;
349 // User-defined structs first
350 newCode.add(COMMENT("All other user-defined structs"));
351 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
352 for (int i = 0; i < declareStructs.size(); i++) {
353 ArrayList<String> declareStruct = declareStructs.get(i);
354 newCode.addAll(declareStruct);
357 // User-defined variables
358 ArrayList<VariableDeclaration> varDecls = code.declareVar;
359 for (int i = 0; i < varDecls.size(); i++) {
360 VariableDeclaration varDecl = varDecls.get(i);
361 // Don't forget to make them static
362 newCode.add(makeVariablesStatic(varDecl));
364 // User-defined functions
365 newCode.add(COMMENT("All other user-defined functions"));
366 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
367 for (int i = 0; i < defineFuncs.size(); i++) {
368 ArrayList<String> defineFunc = defineFuncs.get(i);
369 makeFunctionStatic(defineFunc);
370 newCode.addAll(defineFunc);
374 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
375 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
377 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
378 // Define necessary info structure
379 if (!funcHeader.returnType.equals("void")
380 || funcHeader.args.size() > 0) {
381 newCode.add(COMMENT("Definition of interface info struct: "
383 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
384 newCode.add(COMMENT("End of info struct definition: "
389 // Define ID function
390 newCode.add(COMMENT("ID function of interface: " + interfaceName));
391 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
392 newCode.add(COMMENT("End of ID function: " + interfaceName));
395 // Define check_action function
396 newCode.add(COMMENT("Check action function of interface: "
398 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
399 newCode.add(COMMENT("End of check action function: "
403 // Interface function pointer table
404 String interfaceSize = Integer
405 .toString(semantics.interfaceName2Construct.size());
406 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
408 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
409 // Happens-before initialization rules
410 // Should make it static
411 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
414 newCode.add(COMMENT("Define function for sequential code initialization"));
415 newCode.add("inline static void __sequential_init() {");
416 // Init func_ptr_table
417 newCode.add("\t" + COMMENT("Init func_ptr_table"));
419 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
420 + semantics.interface2Num.size() + " * 2)"));
421 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
422 String interfaceNum = Integer.toString(semantics.interface2Num
423 .get(interfaceName));
425 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
426 "(void*) &" + interfaceName + "_id"));
428 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
429 "(void*) &" + interfaceName + "_check_action"));
431 // Init Happens-before rules table
432 newCode.addAll(generateHBInitAnnotation(semantics));
434 // Pass init info, including function table info & HB rules
436 + COMMENT("Pass init info, including function table info & HB rules"));
437 String structName = "anno_init", anno = "init";
438 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
440 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
442 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
444 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
446 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
447 "HB_INIT_TABLE_SIZE"));
448 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
449 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
450 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
451 newCode.add("\t" + ANNOTATE(semantics, anno));
454 // Init user-defined variables
455 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
458 newCode.add(COMMENT("End of Global construct generation in class"));
460 // printCode(newCode);
464 public static ArrayList<String> generateStaticVarDefine(
465 SemanticsChecker semantics, GlobalConstruct construct) {
466 ArrayList<String> newCode = new ArrayList<String>();
467 String className = semantics.getClassName();
468 if (className == null)
469 return newCode; // No need to define any static variables
470 String templateList = semantics.getTemplateStr();
472 if (templateList == null) {
473 varPrefix = className + "::";
475 varPrefix = className + templateList + "::";
477 String templateDecl = semantics.getTemplateFullStr();
478 if (templateList == null) {
479 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
480 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
481 for (int i = 0; i < construct.code.declareVar.size(); i++) {
482 VariableDeclaration varDecl = construct.code.declareVar.get(i);
483 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
486 newCode.add(templateDecl);
487 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
488 newCode.add(templateDecl);
489 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
490 for (int i = 0; i < construct.code.declareVar.size(); i++) {
491 VariableDeclaration varDecl = construct.code.declareVar.get(i);
492 newCode.add(templateDecl);
493 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
499 private static ArrayList<String> generateHBInitAnnotation(
500 SemanticsChecker semantics) {
501 ArrayList<String> newCode = new ArrayList<String>();
503 int hbConditionInitIdx = 0;
504 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
505 for (ConditionalInterface right : semantics.getHBConditions().get(
507 String structVarName = "hbConditionInit" + hbConditionInitIdx;
508 // String annotationVarName = "hb_init" + hbConditionInitIdx;
509 hbConditionInitIdx++;
510 String interfaceNumBefore = Integer
511 .toString(semantics.interface2Num
512 .get(left.interfaceName)), hbLabelNumBefore = Integer
513 .toString(semantics.hbLabel2Num
514 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
515 .toString(semantics.interface2Num
516 .get(right.interfaceName)), hbLabelNumAfter = Integer
517 .toString(semantics.hbLabel2Num
518 .get(right.hbConditionLabel));
519 newCode.add("\t" + COMMENT(left + " -> " + right));
522 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
524 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
526 + SHORT_COMMENT(left.interfaceName));
528 + ASSIGN_TO_PTR(structVarName,
529 "hb_condition_num_before", hbLabelNumBefore)
530 + SHORT_COMMENT(left.hbConditionLabel));
532 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
534 + SHORT_COMMENT(right.interfaceName));
536 + ASSIGN_TO_PTR(structVarName,
537 "hb_condition_num_after", hbLabelNumAfter)
538 + SHORT_COMMENT(right.hbConditionLabel));
541 // Init hb_init_table
542 newCode.add("\t" + COMMENT("Init hb_init_table"));
544 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
545 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
546 + hbConditionInitIdx + ")"));
547 // Define HB_INIT_TABLE_SIZE
549 + DEFINE("HB_INIT_TABLE_SIZE",
550 Integer.toString(hbConditionInitIdx)));
551 for (int i = 0; i < hbConditionInitIdx; i++) {
553 + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
558 public static ArrayList<String> generateEntryPointInitCall() {
559 ArrayList<String> newCode = new ArrayList<String>();
560 newCode.add("\t" + "__sequential_init();");
564 public static ArrayList<String> generateInterfaceWrapperDeclaration(
565 SemanticsChecker semantics, InterfaceConstruct construct) {
566 FunctionHeader header = getFunctionHeader(semantics, construct);
567 ArrayList<String> declaration = new ArrayList<String>();
568 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
569 .getDeclaration() + ";");
573 // Only generate the definition of the wrapper, don't do any renaming
574 public static ArrayList<String> generateInterfaceWrapperDefinition(
575 SemanticsChecker semantics, InterfaceConstruct construct) {
576 ArrayList<String> newCode = new ArrayList<String>();
577 String interfaceName = construct.name;
579 FunctionHeader header = getFunctionHeader(semantics, construct);
580 String interfaceNum = Integer.toString(semantics.interface2Num
581 .get(construct.name));
583 newCode.add(header.getTemplateFullStr());
584 newCode.add(header.getFuncStr() + " {");
585 // Wrapper function body
586 newCode.add("\t" + COMMENT("Interface begins"));
588 String structName = "interface_begin";
590 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
593 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
594 + SHORT_COMMENT(construct.name));
596 String anno = "annotation_interface_begin";
597 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
599 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
600 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
601 newCode.add("\t" + ANNOTATE(semantics, anno));
602 // Call original renamed function
603 if (header.returnType.equals("void")) {
604 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
608 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
609 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
612 for (String label : construct.hbConditions.keySet()) {
613 String condition = construct.hbConditions.get(label);
614 String hbCondNum = Integer.toString(semantics.hbLabel2Num
616 newCode.add("\t" + "if " + BRACE(condition) + " {");
617 structName = "hb_condition";
619 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
621 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
622 + SHORT_COMMENT(construct.name));
625 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
626 anno = "annotation_hb_condition";
628 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
630 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
631 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
632 newCode.add("\t\t" + ANNOTATE(semantics, anno));
633 newCode.add("\t" + "}");
636 // Also add the true condition if any
637 if (semantics.containsConditionalInterface(new ConditionalInterface(
638 interfaceName, ""))) {
639 structName = "hb_condition";
641 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
643 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
644 + SHORT_COMMENT(construct.name));
646 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
647 anno = "annotation_hb_condition";
648 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
650 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
651 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
652 newCode.add("\t" + ANNOTATE(semantics, anno));
656 String infoStructType = null, infoName = null;
657 if (!header.returnType.equals("void") || header.args.size() > 0) {
658 infoStructType = interfaceName + "_info";
661 + DECLARE_DEFINE(infoStructType + "*", infoName,
662 BRACE(infoStructType + "*") + " malloc(sizeof("
663 + infoStructType + "))"));
664 if (!header.returnType.equals("void")) {
666 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
668 for (int i = 0; i < header.args.size(); i++) {
669 String argName = header.args.get(i).name;
670 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
675 structName = "interface_end";
676 anno = "annoation_interface_end";
678 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
680 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
681 + SHORT_COMMENT(construct.name));
682 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
683 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
685 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
686 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
687 newCode.add("\t" + ANNOTATE(semantics, anno));
688 // Return __RET__ if it's not void
689 if (!header.returnType.equals("void")) {
690 newCode.add("\t" + "return " + MACRO_RETURN + ";");
692 // End of the wrapper function
695 // printCode(newCode);
699 // Rename the interface name for declaration or definition
700 public static void renameInterface(SemanticsChecker semantics,
701 Construct construct) {
702 FunctionHeader header = getFunctionHeader(semantics, construct);
703 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
704 int lineNum = construct.beginLineNum;
705 String headerLine = content.get(construct.beginLineNum);
706 if (headerLine.startsWith("template")) {
707 headerLine = content.get(construct.beginLineNum + 1);
710 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
712 String oldLine = content.get(lineNum + 1);
714 if (construct instanceof InterfaceConstruct) {
715 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
716 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
717 .get(iConstruct.name);
718 if (defineConstruct != null) { // There is a defineConstruct
719 newLine = newLine + " ;";
720 renameInterface(semantics, defineConstruct);
721 } else { // This is a declare & define construct
722 if (oldLine.indexOf('{') != -1)
723 newLine = newLine + " {";
726 if (oldLine.indexOf('{') != -1)
727 newLine = newLine + " {";
730 content.set(lineNum + 1, newLine);
733 public static void addAtomicReturn(SemanticsChecker semantics,
734 Construct construct) {
735 int lineNum = construct.beginLineNum - 1;
736 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
737 String oldLine = content.get(lineNum);
738 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
739 content.set(lineNum, newLine);
742 public static ArrayList<String> generatePotentialCPDefine(
743 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
744 ArrayList<String> newCode = new ArrayList<String>();
745 // Add atomic return variable if the predicate accesses to it
746 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
747 addAtomicReturn(semantics, construct);
749 // Generate redundant header files
751 + COMMENT("Automatically generated code for potential commit point: "
755 newCode.add("\t" + "if (" + construct.condition + ") {");
756 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
758 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
760 String labelNum = Integer.toString(semantics.commitPointLabel2Num
761 .get(construct.label));
762 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
763 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
765 + ASSIGN_TO_PTR(anno, "type",
766 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
767 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
768 newCode.add("\t\t" + ANNOTATE(semantics, anno));
769 newCode.add("\t" + "}");
773 public static String getCPInterfaceNum(SemanticsChecker semantics, String commitPointLabel) {
774 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
775 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
776 String interfaceName = iConstruct.name;
777 String interfaceNum = Integer.toString(semantics.interface2Num
778 .get(interfaceName));
784 * Commit point define check should be unique to each interface, meaning that they
785 * are not shared between different interfaces
792 public static ArrayList<String> generateCPDefineCheck(
793 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
794 ArrayList<String> newCode = new ArrayList<String>();
795 // Add atomic return variable if the predicate accesses to it
796 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
797 addAtomicReturn(semantics, construct);
799 // Generate redundant header files
801 + COMMENT("Automatically generated code for commit point define check: "
806 newCode.add("\t" + "if (" + construct.condition + ") {");
807 String structName = "cp_define_check", anno = "annotation_cp_define_check";
809 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
810 String labelNum = Integer.toString(semantics.commitPointLabel2Num
811 .get(construct.label));
812 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
813 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
814 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
815 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
817 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
818 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
819 newCode.add("\t\t" + ANNOTATE(semantics, anno));
820 newCode.add("\t" + "}");
826 * Commit point define check should be unique to each interface, meaning that they
827 * are not shared between different interfaces
834 public static ArrayList<String> generateCPClear(
835 SemanticsChecker semantics, CPClearConstruct construct) {
836 ArrayList<String> newCode = new ArrayList<String>();
837 // Add atomic return variable if the predicate accesses to it
838 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
839 addAtomicReturn(semantics, construct);
841 // Generate redundant header files
843 + COMMENT("Automatically generated code for commit point clear: "
848 newCode.add("\t" + "if (" + construct.condition + ") {");
849 String structName = "cp_clear", anno = "annotation_cp_clear";
851 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
852 String labelNum = Integer.toString(semantics.commitPointLabel2Num
853 .get(construct.label));
854 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
855 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
856 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
857 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
859 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
860 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
861 newCode.add("\t\t" + ANNOTATE(semantics, anno));
862 newCode.add("\t" + "}");
868 * Commit point define should be unique to each interface, meaning that they
869 * are not shared between different interfaces
876 public static ArrayList<String> generateCPDefine(
877 SemanticsChecker semantics, CPDefineConstruct construct) {
878 ArrayList<String> newCode = new ArrayList<String>();
879 // Generate redundant header files
881 + COMMENT("Automatically generated code for commit point define: "
885 newCode.add("\t" + "if (" + construct.condition + ") {");
886 String structName = "cp_define", anno = "annotation_cp_define";
888 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
889 String labelNum = Integer.toString(semantics.commitPointLabel2Num
890 .get(construct.label));
891 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
892 String potentialLabelNum = Integer
893 .toString(semantics.commitPointLabel2Num
894 .get(construct.potentialCPLabel));
895 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
897 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
899 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
900 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
902 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
903 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
904 newCode.add("\t\t" + ANNOTATE(semantics, anno));
905 newCode.add("\t" + "}");