1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashSet;
7 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
8 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
9 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
12 import edu.uci.eecs.specCompiler.specExtraction.Construct;
13 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
14 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
16 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
19 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
22 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
24 public class CodeVariables {
25 // C++ code or library
26 public static final String HEADER_STDLIB = "<stdlib.h>";
27 public static final String HEADER_THREADS = "<threads.h>";
28 public static final String HEADER_STDINT = "<stdint.h>";
29 public static final String ThreadIDType = "thrd_t";
30 public static final String GET_THREAD_ID = "thrd_current";
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_SPECANNOTATION = "<specannotation.h>";
37 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
38 public static final String CDSAnnotate = "cdsannotate";
39 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
40 public static final String IDType = "id_t";
42 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
43 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
44 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
45 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
46 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
47 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
48 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
49 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
50 public static final String SPEC_ANNOTATION = "spec_annotation";
51 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
52 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
54 public static final String ANNO_HB_INIT = "anno_hb_init";
55 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
56 public static final String ANNO_INTERFACE_END = "anno_interface_end";
57 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potentail_cp_define";
58 public static final String ANNO_CP_DEFINE = "anno_cp_define";
59 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
60 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
62 // Specification variables
63 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
64 public static final String DEFAULT_ID = "0";
66 // Specification library
67 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
68 public static final String SPEC_QUEUE = "spec_queue";
69 public static final String SPEC_STACK = "spec_stack";
70 public static final String SPEC_DEQUE = "spec_deque";
71 public static final String SPEC_HASHTABLE = "spec_hashtable";
72 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
73 public static final String SPEC_TAG = "spec_tag";
74 public static final String SPEC_TAG_CURRENT = "current";
75 public static final String SPEC_TAG_NEXT = "next";
78 public static final String MACRO_ID = "__ID__";
79 public static final String MACRO_COND = "__COND_SAT__";
80 public static final String MACRO_RETURN = "__RET__";
81 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
83 public static void printCode(ArrayList<String> code) {
84 for (int i = 0; i < code.size(); i++) {
85 System.out.println(code.get(i));
89 private static String COMMENT(String comment) {
90 return "/* " + comment + " */";
93 private static String INCLUDE(String header) {
94 return "#include " + header;
97 private static String DEFINE(String left, String right) {
98 return "#define " + left + " " + right;
101 private static String UNDEFINE(String macro) {
102 return "#undef " + macro;
105 private static String GET_FIELD_BY_PTR(String ptr, String field) {
106 return ptr + "->" + field;
109 private static String GET_FIELD(String var, String field) {
110 return var + "->" + field;
113 private static String BRACE(String val) {
114 return "(" + val + ")";
117 private static String ASSIGN(String structName, String field, String val) {
118 return structName + "." + field + " = " + val + ";";
121 private static String ASSIGN(String varName, String val) {
122 return varName + " = " + val + ";";
125 private static String ASSIGN_PTR(String structName, String field, String val) {
126 return structName + "." + field + " = &" + val + ";";
129 private static String ASSIGN_TO_PTR(String structName, String field,
131 return structName + "->" + field + " = " + val + ";";
134 private static String DECLARE(String type, String name) {
135 return type + " " + name + ";";
138 private static String DECLARE(VariableDeclaration varDecl) {
139 String type = varDecl.type, name = varDecl.name;
140 return type + " " + name + ";";
143 private static String DECLARE_DEFINE(String type, String var, String val) {
144 return type + " " + var + " = " + val + ";";
147 private static String ANNOTATE(String structName) {
148 return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
151 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
152 FunctionHeader header) {
153 ArrayList<String> code = new ArrayList<String>();
154 code.add("typedef struct " + interfaceName + "_info {");
155 if (!header.returnType.equals("void")) {
156 code.add(DECLARE(header.returnType, MACRO_RETURN));
158 for (int i = 0; i < header.args.size(); i++) {
159 code.add(DECLARE(header.args.get(i)));
161 code.add("} " + interfaceName + "_info {");
165 private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
167 ArrayList<String> code = new ArrayList<String>();
168 code.add("static " + IDType + " " + interfaceName + "_id() {");
169 if (!idCode.equals("")) {
170 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
172 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
174 code.add("return " + MACRO_ID + ";");
179 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
180 InterfaceConstruct construct, FunctionHeader header) {
181 String interfaceName = construct.name;
182 ArrayList<String> code = new ArrayList<String>();
183 code.add("static bool " + interfaceName + "_check_action(void *info, "
184 + IDType + " " + MACRO_ID + ") {");
185 code.add(DECLARE("bool", "check_passed"));
187 if (!header.returnType.equals("void") || header.args.size() != 0) {
188 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
189 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
190 BRACE(infoStructType) + "info"));
191 if (!header.returnType.equals("void")) {
192 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
193 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
195 for (int i = 0; i < header.args.size(); i++) {
196 String type = header.args.get(i).type, var = header.args.get(i).name;
197 code.add((DECLARE_DEFINE(type, var,
198 GET_FIELD_BY_PTR(infoStructName, var))));
203 if (!construct.condition.equals("")) {
204 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
207 if (!construct.check.equals("")) {
208 code.add(ASSIGN("check_passed", construct.check));
209 code.add("if (!check_passed) return false;");
212 if (construct.action.size() > 0) {
213 code.addAll(construct.action);
216 if (!construct.postCheck.equals("")) {
217 code.add(ASSIGN("check_passed", construct.postCheck));
218 code.add("if (!check_passed) return false;");
221 if (construct.postAction.size() > 0) {
222 code.addAll(construct.postAction);
230 private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
231 HashSet<String> headers = new HashSet<String>();
232 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
233 File f = semantics.interfaceName2Construct.get(interfaceName).file;
234 headers.addAll(semantics.srcFilesInfo.get(f).headers);
239 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
240 String headLine = funcDefine.get(0);
241 headLine = "static " + headLine;
242 funcDefine.set(0, headLine);
245 private static String makeVariablesStatic(VariableDeclaration varDecl) {
246 String res = "static " + varDecl.type + " " + varDecl.name + ";";
250 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
251 Construct construct) {
252 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
253 String headerLine = content.get(construct.beginLineNum);
254 if (headerLine.startsWith("template")) {
255 headerLine = content.get(construct.beginLineNum + 1);
257 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
259 return SpecParser.parseFuncHeader(headerLine);
260 } catch (ParseException e) {
266 public static ArrayList<String> generateGlobalVarDeclaration(
267 SemanticsChecker semantics, GlobalConstruct construct) {
268 ArrayList<String> newCode = new ArrayList<String>();
269 HashSet<String> allHeaders = getAllHeaders(semantics);
271 // All headers needed by the interface decalration
272 newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
273 for (String header : allHeaders) {
274 newCode.add(INCLUDE(header));
277 // Other necessary headers
278 newCode.add(INCLUDE(HEADER_STDINT));
279 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
280 newCode.add(INCLUDE(HEADER_SPEC_LIB));
283 SequentialDefineSubConstruct code = construct.code;
284 // User-defined structs first
285 newCode.add(COMMENT("All other user-defined structs"));
286 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
287 for (int i = 0; i < declareStructs.size(); i++) {
288 ArrayList<String> declareStruct = declareStructs.get(i);
289 newCode.addAll(declareStruct);
292 // User-defined functions
293 newCode.add(COMMENT("All other user-defined functions"));
294 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
295 for (int i = 0; i < defineFuncs.size(); i++) {
296 ArrayList<String> defineFunc = defineFuncs.get(i);
297 makeFunctionStatic(defineFunc);
298 newCode.addAll(defineFunc);
302 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
303 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
305 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
306 // Define necessary info structure
307 if (!funcHeader.returnType.equals("void") || funcHeader.args.size() > 0) {
308 newCode.add(COMMENT("Definition of interface info struct: "
310 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
311 newCode.add(COMMENT("End of info struct definition: "
316 // Define ID function
317 newCode.add(COMMENT("ID function of interface: " + interfaceName));
318 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
319 newCode.add(COMMENT("End of ID function: + " + interfaceName));
322 // Define check_action function
323 newCode.add(COMMENT("Check action function of interface: "
325 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
326 newCode.add(COMMENT("End of check action function: + "
330 // Interface function pointer table
331 String interfaceSize = Integer
332 .toString(semantics.interfaceName2Construct.size());
333 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
334 newCode.add(DECLARE("void**", "func_ptr_table"));
335 // User-defined variables
336 ArrayList<VariableDeclaration> varDecls = code.declareVar;
337 for (int i = 0; i < varDecls.size(); i++) {
338 VariableDeclaration varDecl = varDecls.get(i);
339 // Don't forget to make them static
340 newCode.add(makeVariablesStatic(varDecl));
344 newCode.add(COMMENT("Define function for sequential code initialization"));
345 newCode.add("static void __sequential_init() {");
346 // Init func_ptr_table
347 newCode.add(COMMENT("Init func_ptr_table"));
348 newCode.add(ASSIGN("func_ptr_table",
349 "(void**) malloc(sizeof(void*) * 2)"));
350 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
351 String interfaceNum = Integer.toString(semantics.interface2Num
352 .get(interfaceName));
353 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
354 "(void*) &" + interfaceName + "_id"));
355 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
356 "(void*) &" + interfaceName + "_check_action"));
359 // Init user-defined variables
360 newCode.addAll(construct.code.initVar);
361 // Pass Happens-before relationship
362 generateHBInitAnnotation(semantics);
364 newCode.add(COMMENT("End of Global construct generation in class"));
370 public static ArrayList<String> generateStaticVarDefine(
371 SemanticsChecker semantics, GlobalConstruct construct) {
372 ArrayList<String> newCode = new ArrayList<String>();
373 String className = semantics.getClassName();
374 if (className == null)
375 return newCode; // No need to define any static variables
376 String templateList = semantics.getTemplateStr();
378 if (templateList == null) {
379 varPrefix = className + "::";
381 varPrefix = className + templateList + "::";
383 String templateDecl = semantics.getTemplateFullStr();
384 if (templateList == null) {
385 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
386 for (int i = 0; i < construct.code.declareVar.size(); i++) {
387 VariableDeclaration varDecl = construct.code.declareVar.get(i);
388 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
391 newCode.add(templateDecl);
392 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
393 for (int i = 0; i < construct.code.declareVar.size(); i++) {
394 VariableDeclaration varDecl = construct.code.declareVar.get(i);
395 newCode.add(templateDecl);
396 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
402 private static ArrayList<String> generateHBInitAnnotation(
403 SemanticsChecker semantics) {
404 ArrayList<String> newCode = new ArrayList<String>();
405 int hbConditionInitIdx = 0;
406 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
407 for (ConditionalInterface right : semantics.getHBConditions().get(
409 String structVarName = "hbConditionInit" + hbConditionInitIdx;
410 String annotationVarName = "hb_init" + hbConditionInitIdx;
411 hbConditionInitIdx++;
412 String interfaceNumBefore = Integer
413 .toString(semantics.interface2Num
414 .get(left.interfaceName)), hbLabelNumBefore = Integer
415 .toString(semantics.hbLabel2Num
416 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
417 .toString(semantics.interface2Num
418 .get(right.interfaceName)), hbLabelNumAfter = Integer
419 .toString(semantics.hbLabel2Num
420 .get(right.hbConditionLabel));
421 newCode.add(COMMENT(left + " -> " + right));
423 newCode.add(ANNO_HB_INIT + " " + structVarName + ";");
424 newCode.add(ASSIGN(structVarName, "interface_num_before",
425 interfaceNumBefore));
426 newCode.add(ASSIGN(structVarName, "hb_condition_num_before",
428 newCode.add(ASSIGN(structVarName, "interface_num_after",
430 newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
433 newCode.add(DECLARE(SPEC_ANNOTATION, annotationVarName));
434 newCode.add(ASSIGN(annotationVarName,
435 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
436 newCode.add(ASSIGN_PTR(annotationVarName,
437 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
438 newCode.add(ANNOTATE(annotationVarName));
444 public static ArrayList<String> generateEntryPointInitCall() {
445 ArrayList<String> newCode = new ArrayList<String>(1);
446 newCode.add("__sequential_init();");
450 public static ArrayList<String> generateInterfaceWrapper(
451 SemanticsChecker semantics, InterfaceConstruct construct) {
452 ArrayList<String> newCode = new ArrayList<String>();
453 String interfaceName = construct.name;
454 // Generate necessary header file (might be redundant but never mind)
455 newCode.add(INCLUDE(HEADER_STDLIB));
456 newCode.add(INCLUDE(HEADER_THREADS));
457 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
458 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
459 newCode.add(INCLUDE(HEADER_SPEC_LIB));
461 FunctionHeader header = getFunctionHeader(semantics, construct);
462 String interfaceNum = Integer.toString(semantics.interface2Num
463 .get(construct.name));
464 // Rename the interface
465 renameInterface(semantics, construct);
466 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
468 if (defineConstruct != null) {
469 renameInterface(semantics, defineConstruct);
472 // Generate wrapper header
473 newCode.add(header.toString() + " {");
474 // Wrapper function body
475 newCode.add(COMMENT("Interface begins"));
477 String structName = "interface_begin";
478 newCode.add(DECLARE(ANNO_INTERFACE_BEGIN, "interface_begin"));
479 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
480 String anno = "annotation_interface_begin";
481 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
482 newCode.add(ASSIGN(structName, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
483 newCode.add(ASSIGN_PTR(structName, "annotation", structName));
484 newCode.add(ANNOTATE(anno));
485 // Call original renamed function
486 if (header.returnType.equals("void")) {
487 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER));
489 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
490 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
493 for (String label : construct.hbConditions.keySet()) {
494 String condition = construct.hbConditions.get(label);
495 String hbCondNum = Integer.toString(semantics.hbLabel2Num
497 newCode.add("if " + BRACE(condition) + " {");
498 structName = "hb_condition";
499 newCode.add(DECLARE(ANNO_HB_CONDITION, structName));
500 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
502 newCode.add(ASSIGN(structName, "hb_condition_num", hbCondNum));
503 anno = "annotation_hb_condition";
504 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
505 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
506 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
507 newCode.add(ANNOTATE(anno));
512 String infoStructType = null, infoName = null;
513 if (!header.returnType.equals("void") || header.args.size() > 0) {
514 infoStructType = interfaceName + "_info";
516 newCode.add(DECLARE_DEFINE(infoStructType, infoName,
517 BRACE(infoStructType + "*") + " malloc(sizeof("
518 + infoStructType + "))"));
519 if (!header.returnType.equals("void")) {
520 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
522 for (int i = 0; i < header.args.size(); i++) {
523 String argName = header.args.get(i).name;
524 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
529 structName = "interface_end";
530 anno = "annoation_interface_end";
531 newCode.add(DECLARE(ANNO_INTERFACE_END, structName));
532 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
533 newCode.add(ASSIGN(structName, "info", infoName));
534 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
535 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
536 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
538 // End of the wrapper function
541 // printCode(newCode);
545 public static void renameInterface(SemanticsChecker semantics,
546 Construct construct) {
547 FunctionHeader header = getFunctionHeader(semantics, construct);
548 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
549 int lineNum = construct.beginLineNum;
550 String headerLine = content.get(construct.beginLineNum);
551 if (headerLine.startsWith("template")) {
552 headerLine = content.get(construct.beginLineNum + 1);
555 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
557 content.set(lineNum, newLine);
560 public static void addAtomicReturn(SemanticsChecker semantics,
561 Construct construct) {
562 int lineNum = construct.beginLineNum - 1;
563 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
564 String oldLine = content.get(lineNum);
565 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
566 content.set(lineNum, newLine);
569 public static ArrayList<String> generatePotentialCPDefine(
570 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
571 ArrayList<String> newCode = new ArrayList<String>();
572 // Add atomic return variable if the predicate accesses to it
573 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
574 addAtomicReturn(semantics, construct);
576 // Generate redundant header files
577 newCode.add(COMMENT("Automatically generated code for potential commit point: "
579 newCode.add(COMMENT("Include redundant headers"));
580 newCode.add(INCLUDE(HEADER_STDINT));
581 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
584 newCode.add("if (" + construct.condition + ") {");
585 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
586 newCode.add(DECLARE(ANNO_POTENTIAL_CP_DEFINE, structName));
587 String labelNum = Integer.toString(semantics.commitPointLabel2Num
588 .get(construct.label));
589 newCode.add(ASSIGN(structName, "label_num", labelNum));
590 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
591 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
592 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
593 newCode.add(ANNOTATE(anno));
598 public static ArrayList<String> generateCPDefineCheck(
599 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
600 ArrayList<String> newCode = new ArrayList<String>();
601 // Add atomic return variable if the predicate accesses to it
602 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
603 addAtomicReturn(semantics, construct);
605 // Generate redundant header files
606 newCode.add(COMMENT("Automatically generated code for commit point define check: "
608 newCode.add(COMMENT("Include redundant headers"));
609 newCode.add(INCLUDE(HEADER_STDINT));
610 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
613 newCode.add("if (" + construct.condition + ") {");
614 String structName = "cp_define_check", anno = "annotation_cp_define_check";
615 newCode.add(DECLARE(ANNO_CP_DEFINE_CHECK, structName));
616 String labelNum = Integer.toString(semantics.commitPointLabel2Num
617 .get(construct.label));
618 newCode.add(ASSIGN(structName, "label_num", labelNum));
619 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
620 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
621 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
622 newCode.add(ANNOTATE(anno));
627 public static ArrayList<String> generateCPDefine(
628 SemanticsChecker semantics, CPDefineConstruct construct) {
629 ArrayList<String> newCode = new ArrayList<String>();
630 // Generate redundant header files
631 newCode.add(COMMENT("Automatically generated code for commit point define check: "
635 newCode.add("if (" + construct.condition + ") {");
636 String structName = "cp_define", anno = "annotation_cp_define";
637 newCode.add(DECLARE(ANNO_CP_DEFINE, structName));
638 String labelNum = Integer.toString(semantics.commitPointLabel2Num
639 .get(construct.label));
640 newCode.add(ASSIGN(structName, "label_num", labelNum));
641 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
642 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
643 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
644 newCode.add(ANNOTATE(anno));