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.CPDefineCheckConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
13 import edu.uci.eecs.specCompiler.specExtraction.Construct;
14 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
15 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
22 public class CodeVariables {
23 // C++ code or library
24 public static final String HEADER_STDLIB = "<stdlib.h>";
25 public static final String HEADER_THREADS = "<threads.h>";
26 public static final String HEADER_STDINT = "<stdint.h>";
27 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
28 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
29 public static final String ThreadIDType = "thread_id_t";
30 public static final String BOOLEAN = "bool";
31 public static final String UINT64 = "uint64_t";
34 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
35 public static final String HEADER_COMMON = "<common.h>";
36 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
37 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
38 public static final String CDSAnnotate = "cdsannotate";
39 // public static final String CDSAnnotate = "cdsannotate";
40 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
41 public static final String IDType = "call_id_t";
43 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
44 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
45 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
46 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
47 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
48 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
49 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
50 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
51 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
52 public static final String SPEC_ANNOTATION = "spec_annotation";
53 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
54 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
56 public static final String ANNO_INIT = "anno_init";
57 public static final String ANNO_HB_INIT = "anno_hb_init";
58 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
59 public static final String ANNO_INTERFACE_END = "anno_interface_end";
60 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
61 public static final String ANNO_CP_DEFINE = "anno_cp_define";
62 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
63 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
65 // Specification variables
66 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
67 public static final String DEFAULT_ID = "0";
69 // Specification library
70 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
71 public static final String SPEC_QUEUE = "spec_queue";
72 public static final String SPEC_STACK = "spec_stack";
73 public static final String SPEC_DEQUE = "spec_deque";
74 public static final String SPEC_HASHTABLE = "spec_hashtable";
75 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
76 public static final String SPEC_TAG = "spec_tag";
77 public static final String SPEC_TAG_CURRENT = "current";
78 public static final String SPEC_TAG_NEXT = "next";
81 public static final String MACRO_ID = "__ID__";
82 public static final String MACRO_COND = "__COND_SAT__";
83 public static final String MACRO_RETURN = "__RET__";
84 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
85 public static final String MACRO_THREAD_ID = "__TID__";
87 public static void printCode(ArrayList<String> code) {
88 for (int i = 0; i < code.size(); i++) {
89 System.out.println(code.get(i));
93 private static String COMMENT(String comment) {
94 return "/* " + comment + " */";
97 private static String SHORT_COMMENT(String comment) {
98 return " // " + comment;
101 private static String INCLUDE(String header) {
102 return "#include " + header;
105 private static String DEFINE(String left, String right) {
106 return "#define " + left + " " + right;
109 private static String UNDEFINE(String macro) {
110 return "#undef " + macro;
113 private static String GET_FIELD_BY_PTR(String ptr, String field) {
114 return ptr + "->" + field;
117 private static String GET_FIELD(String var, String field) {
118 return var + "->" + field;
121 private static String BRACE(String val) {
122 return "(" + val + ")";
125 private static String ASSIGN(String structName, String field, String val) {
126 return structName + "." + field + " = " + val + ";";
129 private static String ASSIGN(String varName, String val) {
130 return varName + " = " + val + ";";
133 private static String ASSIGN_PTR(String structName, String field, String val) {
134 return structName + "." + field + " = &" + val + ";";
137 private static String ASSIGN_TO_PTR(String structName, String field,
139 return structName + "->" + field + " = " + val + ";";
142 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
144 return structName + "->" + field + " = &" + val + ";";
147 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
148 return "struct " + type + " *" + name + " = (struct " + type
149 + "*) malloc(sizeof(struct " + type + "));";
152 private static String DECLARE(String type, String name) {
153 return type + " " + name + ";";
156 private static String DECLARE(VariableDeclaration varDecl) {
157 String type = varDecl.type, name = varDecl.name;
158 return type + " " + name + ";";
161 private static String DECLARE_DEFINE(String type, String var, String val) {
162 return type + " " + var + " = " + val + ";";
165 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
166 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
169 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
170 FunctionHeader header) {
171 ArrayList<String> code = new ArrayList<String>();
172 code.add("typedef struct " + interfaceName + "_info {");
173 if (!header.returnType.equals("void")) {
174 code.add(DECLARE(header.returnType, MACRO_RETURN));
176 for (int i = 0; i < header.args.size(); i++) {
177 code.add(DECLARE(header.args.get(i)));
179 code.add("} " + interfaceName + "_info;");
183 private static ArrayList<String> DEFINE_ID_FUNC(
184 InterfaceConstruct construct, FunctionHeader header) {
185 String interfaceName = construct.name;
186 ArrayList<String> code = new ArrayList<String>();
187 String idCode = construct.idCode;
188 code.add("inline static " + IDType + " " + interfaceName + "_id("
189 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
192 if (!header.returnType.equals("void") || header.args.size() != 0) {
193 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
194 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
195 infoStructName, BRACE(infoStructType + "*") + "info"));
196 if (!header.returnType.equals("void")) {
197 code.add((DECLARE_DEFINE("\t" + header.returnType,
199 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
201 for (int i = 0; i < header.args.size(); i++) {
202 String type = header.args.get(i).type, var = header.args.get(i).name;
204 + (DECLARE_DEFINE(type, var,
205 GET_FIELD_BY_PTR(infoStructName, var))));
210 if (!idCode.equals("")) {
211 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
213 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
215 code.add("\treturn " + MACRO_ID + ";");
220 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
221 InterfaceConstruct construct, FunctionHeader header) {
222 String interfaceName = construct.name;
223 ArrayList<String> code = new ArrayList<String>();
224 code.add("inline static bool " + interfaceName
225 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
226 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
227 code.add("\t" + DECLARE("bool", "check_passed"));
229 if (!header.returnType.equals("void") || header.args.size() != 0) {
230 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
232 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
233 BRACE(infoStructType + "*") + "info"));
234 if (!header.returnType.equals("void")) {
236 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
237 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
239 for (int i = 0; i < header.args.size(); i++) {
240 String type = header.args.get(i).type, var = header.args.get(i).name;
242 + (DECLARE_DEFINE(type, var,
243 GET_FIELD_BY_PTR(infoStructName, var))));
248 if (!construct.condition.equals("")) {
250 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
253 if (!construct.check.equals("")) {
254 code.add("\t" + ASSIGN("check_passed", construct.check));
255 code.add("\tif (!check_passed)");
256 code.add("\t\treturn false;");
260 if (construct.action.size() > 0) {
261 addAllCodeWithIndent(code, construct.action, "\t");
264 if (!construct.postCheck.equals("")) {
265 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
266 code.add("\tif (!check_passed)");
267 code.add("\t\treturn false;");
270 if (construct.postAction.size() > 0) {
271 addAllCodeWithIndent(code, construct.postAction, "\t");
273 // Return true finally
274 code.add("\treturn true;");
281 private static void addAllCodeWithIndent(ArrayList<String> allCode,
282 ArrayList<String> target, String indent) {
283 for (int i = 0; i < target.size(); i++) {
284 allCode.add(indent + target.get(i));
288 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
289 HashSet<String> headers = new HashSet<String>();
290 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
291 File f = semantics.interfaceName2Construct.get(interfaceName).file;
292 headers.addAll(semantics.srcFilesInfo.get(f).headers);
294 headers.add(HEADER_STDLIB);
295 headers.add(HEADER_STDINT);
296 headers.add(HEADER_MODELMEMORY);
297 headers.add(HEADER_MODELTYPES);
298 headers.add(HEADER_SPEC_LIB);
299 headers.add(HEADER_STDINT);
300 headers.add(HEADER_CDSANNOTATE);
301 // headers.add(HEADER_COMMON);
302 headers.add(HEADER_SPECANNOTATION);
306 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
307 String headLine = funcDefine.get(0);
308 headLine = "inline static " + headLine;
309 funcDefine.set(0, headLine);
312 private static String makeVariablesStatic(VariableDeclaration varDecl) {
313 String res = "static " + varDecl.type + " " + varDecl.name + ";";
317 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
318 Construct construct) {
319 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
320 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
321 if (headerLine.startsWith("template")) {
322 templateLine = headerLine;
323 headerLine = content.get(construct.beginLineNum + 2);
325 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
327 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
328 if (templateLine != null) {
329 ArrayList<VariableDeclaration> templateArgs = UtilParser
330 .getTemplateArg(templateLine);
331 header.setTemplateList(templateArgs);
334 } catch (ParseException e) {
340 public static ArrayList<String> generateGlobalVarDeclaration(
341 SemanticsChecker semantics, GlobalConstruct construct) {
342 ArrayList<String> newCode = new ArrayList<String>();
343 HashSet<String> allHeaders = getAllHeaders(semantics);
345 SequentialDefineSubConstruct code = construct.code;
346 // User-defined structs first
347 newCode.add(COMMENT("All other user-defined structs"));
348 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
349 for (int i = 0; i < declareStructs.size(); i++) {
350 ArrayList<String> declareStruct = declareStructs.get(i);
351 newCode.addAll(declareStruct);
354 // User-defined variables
355 ArrayList<VariableDeclaration> varDecls = code.declareVar;
356 for (int i = 0; i < varDecls.size(); i++) {
357 VariableDeclaration varDecl = varDecls.get(i);
358 // Don't forget to make them static
359 newCode.add(makeVariablesStatic(varDecl));
361 // User-defined functions
362 newCode.add(COMMENT("All other user-defined functions"));
363 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
364 for (int i = 0; i < defineFuncs.size(); i++) {
365 ArrayList<String> defineFunc = defineFuncs.get(i);
366 makeFunctionStatic(defineFunc);
367 newCode.addAll(defineFunc);
371 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
372 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
374 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
375 // Define necessary info structure
376 if (!funcHeader.returnType.equals("void")
377 || funcHeader.args.size() > 0) {
378 newCode.add(COMMENT("Definition of interface info struct: "
380 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
381 newCode.add(COMMENT("End of info struct definition: "
386 // Define ID function
387 newCode.add(COMMENT("ID function of interface: " + interfaceName));
388 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
389 newCode.add(COMMENT("End of ID function: " + interfaceName));
392 // Define check_action function
393 newCode.add(COMMENT("Check action function of interface: "
395 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
396 newCode.add(COMMENT("End of check action function: "
400 // Interface function pointer table
401 String interfaceSize = Integer
402 .toString(semantics.interfaceName2Construct.size());
403 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
405 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
406 // Happens-before initialization rules
407 // Should make it static
408 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
411 newCode.add(COMMENT("Define function for sequential code initialization"));
412 newCode.add("inline static void __sequential_init() {");
413 // Init func_ptr_table
414 newCode.add("\t" + COMMENT("Init func_ptr_table"));
416 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
417 + semantics.interface2Num.size() + " * 2)"));
418 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
419 String interfaceNum = Integer.toString(semantics.interface2Num
420 .get(interfaceName));
422 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
423 "(void*) &" + interfaceName + "_id"));
425 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
426 "(void*) &" + interfaceName + "_check_action"));
428 // Init Happens-before rules table
429 newCode.addAll(generateHBInitAnnotation(semantics));
431 // Pass init info, including function table info & HB rules
433 + COMMENT("Pass init info, including function table info & HB rules"));
434 String structName = "anno_init", anno = "init";
435 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
437 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
439 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
441 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
443 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
444 "HB_INIT_TABLE_SIZE"));
445 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
446 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
447 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
448 newCode.add("\t" + ANNOTATE(semantics, anno));
451 // Init user-defined variables
452 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
455 newCode.add(COMMENT("End of Global construct generation in class"));
457 // printCode(newCode);
461 public static ArrayList<String> generateStaticVarDefine(
462 SemanticsChecker semantics, GlobalConstruct construct) {
463 ArrayList<String> newCode = new ArrayList<String>();
464 String className = semantics.getClassName();
465 if (className == null)
466 return newCode; // No need to define any static variables
467 String templateList = semantics.getTemplateStr();
469 if (templateList == null) {
470 varPrefix = className + "::";
472 varPrefix = className + templateList + "::";
474 String templateDecl = semantics.getTemplateFullStr();
475 if (templateList == null) {
476 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
477 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
478 for (int i = 0; i < construct.code.declareVar.size(); i++) {
479 VariableDeclaration varDecl = construct.code.declareVar.get(i);
480 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
483 newCode.add(templateDecl);
484 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
485 newCode.add(templateDecl);
486 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
487 for (int i = 0; i < construct.code.declareVar.size(); i++) {
488 VariableDeclaration varDecl = construct.code.declareVar.get(i);
489 newCode.add(templateDecl);
490 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
496 private static ArrayList<String> generateHBInitAnnotation(
497 SemanticsChecker semantics) {
498 ArrayList<String> newCode = new ArrayList<String>();
500 int hbConditionInitIdx = 0;
501 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
502 for (ConditionalInterface right : semantics.getHBConditions().get(
504 String structVarName = "hbConditionInit" + hbConditionInitIdx;
505 // String annotationVarName = "hb_init" + hbConditionInitIdx;
506 hbConditionInitIdx++;
507 String interfaceNumBefore = Integer
508 .toString(semantics.interface2Num
509 .get(left.interfaceName)), hbLabelNumBefore = Integer
510 .toString(semantics.hbLabel2Num
511 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
512 .toString(semantics.interface2Num
513 .get(right.interfaceName)), hbLabelNumAfter = Integer
514 .toString(semantics.hbLabel2Num
515 .get(right.hbConditionLabel));
516 newCode.add("\t" + COMMENT(left + " -> " + right));
519 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
521 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
523 + SHORT_COMMENT(left.interfaceName));
525 + ASSIGN_TO_PTR(structVarName,
526 "hb_condition_num_before", hbLabelNumBefore)
527 + SHORT_COMMENT(left.hbConditionLabel));
529 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
531 + SHORT_COMMENT(right.interfaceName));
533 + ASSIGN_TO_PTR(structVarName,
534 "hb_condition_num_after", hbLabelNumAfter)
535 + SHORT_COMMENT(right.hbConditionLabel));
538 // Init hb_init_table
539 newCode.add("\t" + COMMENT("Init hb_init_table"));
541 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
542 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
543 + hbConditionInitIdx + ")"));
544 // Define HB_INIT_TABLE_SIZE
546 + DEFINE("HB_INIT_TABLE_SIZE",
547 Integer.toString(hbConditionInitIdx)));
548 for (int i = 0; i < hbConditionInitIdx; i++) {
550 + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
555 public static ArrayList<String> generateEntryPointInitCall() {
556 ArrayList<String> newCode = new ArrayList<String>();
557 newCode.add("\t" + "__sequential_init();");
561 public static ArrayList<String> generateInterfaceWrapperDeclaration(
562 SemanticsChecker semantics, InterfaceConstruct construct) {
563 FunctionHeader header = getFunctionHeader(semantics, construct);
564 ArrayList<String> declaration = new ArrayList<String>();
565 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
566 .getDeclaration() + ";");
570 // Only generate the definition of the wrapper, don't do any renaming
571 public static ArrayList<String> generateInterfaceWrapperDefinition(
572 SemanticsChecker semantics, InterfaceConstruct construct) {
573 ArrayList<String> newCode = new ArrayList<String>();
574 String interfaceName = construct.name;
576 FunctionHeader header = getFunctionHeader(semantics, construct);
577 String interfaceNum = Integer.toString(semantics.interface2Num
578 .get(construct.name));
580 newCode.add(header.getTemplateFullStr());
581 newCode.add(header.getFuncStr() + " {");
582 // Wrapper function body
583 newCode.add("\t" + COMMENT("Interface begins"));
585 String structName = "interface_begin";
587 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
590 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
591 + SHORT_COMMENT(construct.name));
593 String anno = "annotation_interface_begin";
594 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
596 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
597 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
598 newCode.add("\t" + ANNOTATE(semantics, anno));
599 // Call original renamed function
600 if (header.returnType.equals("void")) {
601 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
605 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
606 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
609 for (String label : construct.hbConditions.keySet()) {
610 String condition = construct.hbConditions.get(label);
611 String hbCondNum = Integer.toString(semantics.hbLabel2Num
613 newCode.add("\t" + "if " + BRACE(condition) + " {");
614 structName = "hb_condition";
616 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
618 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
619 + SHORT_COMMENT(construct.name));
622 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
623 anno = "annotation_hb_condition";
625 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
627 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
628 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
629 newCode.add("\t\t" + ANNOTATE(semantics, anno));
630 newCode.add("\t" + "}");
633 // Also add the true condition if any
634 if (semantics.containsConditionalInterface(new ConditionalInterface(
635 interfaceName, ""))) {
636 structName = "hb_condition";
638 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
640 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
641 + SHORT_COMMENT(construct.name));
643 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
644 anno = "annotation_hb_condition";
645 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
647 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
648 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
649 newCode.add("\t" + ANNOTATE(semantics, anno));
653 String infoStructType = null, infoName = null;
654 if (!header.returnType.equals("void") || header.args.size() > 0) {
655 infoStructType = interfaceName + "_info";
658 + DECLARE_DEFINE(infoStructType + "*", infoName,
659 BRACE(infoStructType + "*") + " malloc(sizeof("
660 + infoStructType + "))"));
661 if (!header.returnType.equals("void")) {
663 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
665 for (int i = 0; i < header.args.size(); i++) {
666 String argName = header.args.get(i).name;
667 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
672 structName = "interface_end";
673 anno = "annoation_interface_end";
675 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
677 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
678 + SHORT_COMMENT(construct.name));
679 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
680 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
682 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
683 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
684 newCode.add("\t" + ANNOTATE(semantics, anno));
685 // Return __RET__ if it's not void
686 if (!header.returnType.equals("void")) {
687 newCode.add("\t" + "return " + MACRO_RETURN + ";");
689 // End of the wrapper function
692 // printCode(newCode);
696 // Rename the interface name for declaration or definition
697 public static void renameInterface(SemanticsChecker semantics,
698 Construct construct) {
699 FunctionHeader header = getFunctionHeader(semantics, construct);
700 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
701 int lineNum = construct.beginLineNum;
702 String headerLine = content.get(construct.beginLineNum);
703 if (headerLine.startsWith("template")) {
704 headerLine = content.get(construct.beginLineNum + 1);
707 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
709 String oldLine = content.get(lineNum + 1);
711 if (construct instanceof InterfaceConstruct) {
712 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
713 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
714 .get(iConstruct.name);
715 if (defineConstruct != null) { // There is a defineConstruct
716 newLine = newLine + " ;";
717 renameInterface(semantics, defineConstruct);
718 } else { // This is a declare & define construct
719 if (oldLine.indexOf('{') != -1)
720 newLine = newLine + " {";
723 if (oldLine.indexOf('{') != -1)
724 newLine = newLine + " {";
727 content.set(lineNum + 1, newLine);
730 public static void addAtomicReturn(SemanticsChecker semantics,
731 Construct construct) {
732 int lineNum = construct.beginLineNum - 1;
733 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
734 String oldLine = content.get(lineNum);
735 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
736 content.set(lineNum, newLine);
739 public static ArrayList<String> generatePotentialCPDefine(
740 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
741 ArrayList<String> newCode = new ArrayList<String>();
742 // Add atomic return variable if the predicate accesses to it
743 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
744 addAtomicReturn(semantics, construct);
746 // Generate redundant header files
748 + COMMENT("Automatically generated code for potential commit point: "
752 newCode.add("\t" + "if (" + construct.condition + ") {");
753 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
755 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
757 String labelNum = Integer.toString(semantics.commitPointLabel2Num
758 .get(construct.label));
759 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
760 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
762 + ASSIGN_TO_PTR(anno, "type",
763 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
764 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
765 newCode.add("\t\t" + ANNOTATE(semantics, anno));
766 newCode.add("\t" + "}");
770 public static ArrayList<String> generateCPDefineCheck(
771 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
772 ArrayList<String> newCode = new ArrayList<String>();
773 // Add atomic return variable if the predicate accesses to it
774 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
775 addAtomicReturn(semantics, construct);
777 // Generate redundant header files
779 + COMMENT("Automatically generated code for commit point define check: "
783 newCode.add("\t" + "if (" + construct.condition + ") {");
784 String structName = "cp_define_check", anno = "annotation_cp_define_check";
786 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
787 String labelNum = Integer.toString(semantics.commitPointLabel2Num
788 .get(construct.label));
789 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
790 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
792 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
793 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
794 newCode.add("\t\t" + ANNOTATE(semantics, anno));
795 newCode.add("\t" + "}");
799 public static ArrayList<String> generateCPDefine(
800 SemanticsChecker semantics, CPDefineConstruct construct) {
801 ArrayList<String> newCode = new ArrayList<String>();
802 // Generate redundant header files
804 + COMMENT("Automatically generated code for commit point define: "
808 newCode.add("\t" + "if (" + construct.condition + ") {");
809 String structName = "cp_define", anno = "annotation_cp_define";
811 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
812 String labelNum = Integer.toString(semantics.commitPointLabel2Num
813 .get(construct.label));
814 String potentialLabelNum = Integer
815 .toString(semantics.commitPointLabel2Num
816 .get(construct.potentialCPLabel));
817 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
819 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
821 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
823 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
824 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
825 newCode.add("\t\t" + ANNOTATE(semantics, anno));
826 newCode.add("\t" + "}");