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 CDSAnnotate = "_Z11cdsannotatemPv";
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_FUNC_TABLE_INIT = "FUNC_TABLE_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_FUNC_TABLE_INIT = "anno_func_table_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_potentail_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__";
86 public static void printCode(ArrayList<String> code) {
87 for (int i = 0; i < code.size(); i++) {
88 System.out.println(code.get(i));
92 private static String COMMENT(String comment) {
93 return "/* " + comment + " */";
96 private static String INCLUDE(String header) {
97 return "#include " + header;
100 private static String DEFINE(String left, String right) {
101 return "#define " + left + " " + right;
104 private static String UNDEFINE(String macro) {
105 return "#undef " + macro;
108 private static String GET_FIELD_BY_PTR(String ptr, String field) {
109 return ptr + "->" + field;
112 private static String GET_FIELD(String var, String field) {
113 return var + "->" + field;
116 private static String BRACE(String val) {
117 return "(" + val + ")";
120 private static String ASSIGN(String structName, String field, String val) {
121 return structName + "." + field + " = " + val + ";";
124 private static String ASSIGN(String varName, String val) {
125 return varName + " = " + val + ";";
128 private static String ASSIGN_PTR(String structName, String field, String val) {
129 return structName + "." + field + " = &" + val + ";";
132 private static String ASSIGN_TO_PTR(String structName, String field,
134 return structName + "->" + field + " = " + val + ";";
137 private static String DECLARE(String type, String name) {
138 return type + " " + name + ";";
141 private static String DECLARE(VariableDeclaration varDecl) {
142 String type = varDecl.type, name = varDecl.name;
143 return type + " " + name + ";";
146 private static String DECLARE_DEFINE(String type, String var, String val) {
147 return type + " " + var + " = " + val + ";";
150 private static String ANNOTATE(String structName) {
151 return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
154 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
155 FunctionHeader header) {
156 ArrayList<String> code = new ArrayList<String>();
157 code.add("typedef struct " + interfaceName + "_info {");
158 if (!header.returnType.equals("void")) {
159 code.add(DECLARE(header.returnType, MACRO_RETURN));
161 for (int i = 0; i < header.args.size(); i++) {
162 code.add(DECLARE(header.args.get(i)));
164 code.add("} " + interfaceName + "_info;");
168 private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
170 ArrayList<String> code = new ArrayList<String>();
171 code.add("static " + IDType + " " + interfaceName + "_id() {");
172 if (!idCode.equals("")) {
173 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
175 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
177 code.add("return " + MACRO_ID + ";");
182 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
183 InterfaceConstruct construct, FunctionHeader header) {
184 String interfaceName = construct.name;
185 ArrayList<String> code = new ArrayList<String>();
186 code.add("static bool " + interfaceName + "_check_action(void *info, "
187 + IDType + " " + MACRO_ID + ") {");
188 code.add(DECLARE("bool", "check_passed"));
190 if (!header.returnType.equals("void") || header.args.size() != 0) {
191 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
192 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
193 BRACE(infoStructType + "*") + "info"));
194 if (!header.returnType.equals("void")) {
195 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
196 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
198 for (int i = 0; i < header.args.size(); i++) {
199 String type = header.args.get(i).type, var = header.args.get(i).name;
200 code.add((DECLARE_DEFINE(type, var,
201 GET_FIELD_BY_PTR(infoStructName, var))));
206 if (!construct.condition.equals("")) {
207 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
210 if (!construct.check.equals("")) {
211 code.add(ASSIGN("check_passed", construct.check));
212 code.add("if (!check_passed) return false;");
215 if (construct.action.size() > 0) {
216 code.addAll(construct.action);
219 if (!construct.postCheck.equals("")) {
220 code.add(ASSIGN("check_passed", construct.postCheck));
221 code.add("if (!check_passed) return false;");
224 if (construct.postAction.size() > 0) {
225 code.addAll(construct.postAction);
227 // Return true finally
228 code.add("return true;");
235 private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
236 HashSet<String> headers = new HashSet<String>();
237 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
238 File f = semantics.interfaceName2Construct.get(interfaceName).file;
239 headers.addAll(semantics.srcFilesInfo.get(f).headers);
244 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
245 String headLine = funcDefine.get(0);
246 headLine = "static " + headLine;
247 funcDefine.set(0, headLine);
250 private static String makeVariablesStatic(VariableDeclaration varDecl) {
251 String res = "static " + varDecl.type + " " + varDecl.name + ";";
255 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
256 Construct construct) {
257 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
258 String headerLine = content.get(construct.beginLineNum);
259 if (headerLine.startsWith("template")) {
260 headerLine = content.get(construct.beginLineNum + 1);
262 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
264 return SpecParser.parseFuncHeader(headerLine);
265 } catch (ParseException e) {
271 public static ArrayList<String> generateGlobalVarDeclaration(
272 SemanticsChecker semantics, GlobalConstruct construct) {
273 ArrayList<String> newCode = new ArrayList<String>();
274 HashSet<String> allHeaders = getAllHeaders(semantics);
276 // All headers needed by the interface decalration
277 newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
278 for (String header : allHeaders) {
279 newCode.add(INCLUDE(header));
282 // Other necessary headers
283 newCode.add(INCLUDE(HEADER_STDLIB));
284 newCode.add(INCLUDE(HEADER_STDINT));
285 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
286 newCode.add(INCLUDE(HEADER_SPEC_LIB));
287 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
290 SequentialDefineSubConstruct code = construct.code;
291 // User-defined structs first
292 newCode.add(COMMENT("All other user-defined structs"));
293 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
294 for (int i = 0; i < declareStructs.size(); i++) {
295 ArrayList<String> declareStruct = declareStructs.get(i);
296 newCode.addAll(declareStruct);
299 // User-defined variables
300 ArrayList<VariableDeclaration> varDecls = code.declareVar;
301 for (int i = 0; i < varDecls.size(); i++) {
302 VariableDeclaration varDecl = varDecls.get(i);
303 // Don't forget to make them static
304 newCode.add(makeVariablesStatic(varDecl));
306 // User-defined functions
307 newCode.add(COMMENT("All other user-defined functions"));
308 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
309 for (int i = 0; i < defineFuncs.size(); i++) {
310 ArrayList<String> defineFunc = defineFuncs.get(i);
311 makeFunctionStatic(defineFunc);
312 newCode.addAll(defineFunc);
316 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
317 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
319 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
320 // Define necessary info structure
321 if (!funcHeader.returnType.equals("void")
322 || funcHeader.args.size() > 0) {
323 newCode.add(COMMENT("Definition of interface info struct: "
325 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
326 newCode.add(COMMENT("End of info struct definition: "
331 // Define ID function
332 newCode.add(COMMENT("ID function of interface: " + interfaceName));
333 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
334 newCode.add(COMMENT("End of ID function: " + interfaceName));
337 // Define check_action function
338 newCode.add(COMMENT("Check action function of interface: "
340 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
341 newCode.add(COMMENT("End of check action function: "
345 // Interface function pointer table
346 String interfaceSize = Integer
347 .toString(semantics.interfaceName2Construct.size());
348 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
349 newCode.add(DECLARE("void**", "func_ptr_table"));
352 newCode.add(COMMENT("Define function for sequential code initialization"));
353 newCode.add("static void __sequential_init() {");
354 // Init func_ptr_table
355 newCode.add(COMMENT("Init func_ptr_table"));
356 newCode.add(ASSIGN("func_ptr_table",
357 "(void**) malloc(sizeof(void*) * 2)"));
358 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
359 String interfaceNum = Integer.toString(semantics.interface2Num
360 .get(interfaceName));
361 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
362 "(void*) &" + interfaceName + "_id"));
363 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
364 "(void*) &" + interfaceName + "_check_action"));
367 // Init user-defined variables
368 newCode.addAll(construct.code.initVar);
369 // Pass function table info
370 newCode.add(COMMENT("Pass function table info"));
371 String structName = "anno_func_table_init", anno = "func_init";
372 newCode.add(DECLARE(ANNO_FUNC_TABLE_INIT, structName));
373 newCode.add(ASSIGN(structName, "size", "INTERFACE_SIZE"));
374 newCode.add(ASSIGN(structName, "table", "func_ptr_table"));
375 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
376 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_FUNC_TABLE_INIT));
377 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
378 newCode.add(ANNOTATE(anno));
380 // Pass Happens-before relationship
381 newCode.addAll(generateHBInitAnnotation(semantics));
383 newCode.add(COMMENT("End of Global construct generation in class"));
389 public static ArrayList<String> generateStaticVarDefine(
390 SemanticsChecker semantics, GlobalConstruct construct) {
391 ArrayList<String> newCode = new ArrayList<String>();
392 String className = semantics.getClassName();
393 if (className == null)
394 return newCode; // No need to define any static variables
395 String templateList = semantics.getTemplateStr();
397 if (templateList == null) {
398 varPrefix = className + "::";
400 varPrefix = className + templateList + "::";
402 String templateDecl = semantics.getTemplateFullStr();
403 if (templateList == null) {
404 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
405 for (int i = 0; i < construct.code.declareVar.size(); i++) {
406 VariableDeclaration varDecl = construct.code.declareVar.get(i);
407 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
410 newCode.add(templateDecl);
411 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
412 for (int i = 0; i < construct.code.declareVar.size(); i++) {
413 VariableDeclaration varDecl = construct.code.declareVar.get(i);
414 newCode.add(templateDecl);
415 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
421 private static ArrayList<String> generateHBInitAnnotation(
422 SemanticsChecker semantics) {
423 ArrayList<String> newCode = new ArrayList<String>();
424 int hbConditionInitIdx = 0;
425 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
426 for (ConditionalInterface right : semantics.getHBConditions().get(
428 String structVarName = "hbConditionInit" + hbConditionInitIdx;
429 String annotationVarName = "hb_init" + hbConditionInitIdx;
430 hbConditionInitIdx++;
431 String interfaceNumBefore = Integer
432 .toString(semantics.interface2Num
433 .get(left.interfaceName)), hbLabelNumBefore = Integer
434 .toString(semantics.hbLabel2Num
435 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
436 .toString(semantics.interface2Num
437 .get(right.interfaceName)), hbLabelNumAfter = Integer
438 .toString(semantics.hbLabel2Num
439 .get(right.hbConditionLabel));
440 newCode.add(COMMENT(left + " -> " + right));
442 newCode.add(ANNO_HB_INIT + " " + structVarName + ";");
443 newCode.add(ASSIGN(structVarName, "interface_num_before",
444 interfaceNumBefore));
445 newCode.add(ASSIGN(structVarName, "hb_condition_num_before",
447 newCode.add(ASSIGN(structVarName, "interface_num_after",
449 newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
452 newCode.add(DECLARE(SPEC_ANNOTATION, annotationVarName));
453 newCode.add(ASSIGN(annotationVarName,
454 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
455 newCode.add(ASSIGN_PTR(annotationVarName,
456 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
457 newCode.add(ANNOTATE(annotationVarName));
463 public static ArrayList<String> generateEntryPointInitCall() {
464 ArrayList<String> newCode = new ArrayList<String>(1);
465 newCode.add("__sequential_init();");
469 public static ArrayList<String> generateInterfaceWrapper(
470 SemanticsChecker semantics, InterfaceConstruct construct) {
471 ArrayList<String> newCode = new ArrayList<String>();
472 String interfaceName = construct.name;
473 // Generate necessary header file (might be redundant but never mind)
474 newCode.add(INCLUDE(HEADER_STDLIB));
475 newCode.add(INCLUDE(HEADER_THREADS));
476 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
477 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
478 newCode.add(INCLUDE(HEADER_SPEC_LIB));
480 FunctionHeader header = getFunctionHeader(semantics, construct);
481 String interfaceNum = Integer.toString(semantics.interface2Num
482 .get(construct.name));
483 // Rename the interface
484 renameInterface(semantics, construct);
485 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
487 if (defineConstruct != null) {
488 renameInterface(semantics, defineConstruct);
491 // Generate wrapper header
492 // If it's not in a class, we should declare the wrapper function
493 if (semantics.getClassName() == null) {
494 FunctionHeader renamedHeader = header
495 .getRenamedHeader(SPEC_INTERFACE_WRAPPER);
496 newCode.add(COMMENT("Declaration of the wrapper"));
497 newCode.add(renamedHeader + ";");
500 newCode.add(header.toString() + " {");
501 // Wrapper function body
502 newCode.add(COMMENT("Interface begins"));
504 String structName = "interface_begin";
505 newCode.add(DECLARE(ANNO_INTERFACE_BEGIN, "interface_begin"));
506 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
507 String anno = "annotation_interface_begin";
508 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
509 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
510 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
511 newCode.add(ANNOTATE(anno));
512 // Call original renamed function
513 if (header.returnType.equals("void")) {
514 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
516 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
517 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
520 for (String label : construct.hbConditions.keySet()) {
521 String condition = construct.hbConditions.get(label);
522 String hbCondNum = Integer.toString(semantics.hbLabel2Num
524 newCode.add("if " + BRACE(condition) + " {");
525 structName = "hb_condition";
526 newCode.add(DECLARE(ANNO_HB_CONDITION, structName));
527 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
529 newCode.add(ASSIGN(structName, "hb_condition_num", hbCondNum));
530 anno = "annotation_hb_condition";
531 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
532 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
533 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
534 newCode.add(ANNOTATE(anno));
539 String infoStructType = null, infoName = null;
540 if (!header.returnType.equals("void") || header.args.size() > 0) {
541 infoStructType = interfaceName + "_info";
543 newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
544 BRACE(infoStructType + "*") + " malloc(sizeof("
545 + infoStructType + "))"));
546 if (!header.returnType.equals("void")) {
547 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
549 for (int i = 0; i < header.args.size(); i++) {
550 String argName = header.args.get(i).name;
551 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
556 structName = "interface_end";
557 anno = "annoation_interface_end";
558 newCode.add(DECLARE(ANNO_INTERFACE_END, structName));
559 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
560 newCode.add(ASSIGN(structName, "info", infoName));
561 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
562 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
563 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
564 newCode.add(ANNOTATE(anno));
565 // Return __RET__ if it's not void
566 if (!header.returnType.equals("void")) {
567 newCode.add("return " + MACRO_RETURN + ";");
569 // End of the wrapper function
572 // printCode(newCode);
576 public static void renameInterface(SemanticsChecker semantics,
577 Construct construct) {
578 FunctionHeader header = getFunctionHeader(semantics, construct);
579 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
580 int lineNum = construct.beginLineNum;
581 String headerLine = content.get(construct.beginLineNum);
582 if (headerLine.startsWith("template")) {
583 headerLine = content.get(construct.beginLineNum + 1);
586 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
588 content.set(lineNum, newLine);
591 public static void addAtomicReturn(SemanticsChecker semantics,
592 Construct construct) {
593 int lineNum = construct.beginLineNum - 1;
594 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
595 String oldLine = content.get(lineNum);
596 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
597 content.set(lineNum, newLine);
600 public static ArrayList<String> generatePotentialCPDefine(
601 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
602 ArrayList<String> newCode = new ArrayList<String>();
603 // Add atomic return variable if the predicate accesses to it
604 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
605 addAtomicReturn(semantics, construct);
607 // Generate redundant header files
608 newCode.add(COMMENT("Automatically generated code for potential commit point: "
610 newCode.add(COMMENT("Include redundant headers"));
611 newCode.add(INCLUDE(HEADER_STDINT));
612 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
615 newCode.add("if (" + construct.condition + ") {");
616 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
617 newCode.add(DECLARE(ANNO_POTENTIAL_CP_DEFINE, structName));
618 String labelNum = Integer.toString(semantics.commitPointLabel2Num
619 .get(construct.label));
620 newCode.add(ASSIGN(structName, "label_num", labelNum));
621 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
622 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
623 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
624 newCode.add(ANNOTATE(anno));
629 public static ArrayList<String> generateCPDefineCheck(
630 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
631 ArrayList<String> newCode = new ArrayList<String>();
632 // Add atomic return variable if the predicate accesses to it
633 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
634 addAtomicReturn(semantics, construct);
636 // Generate redundant header files
637 newCode.add(COMMENT("Automatically generated code for commit point define check: "
639 newCode.add(COMMENT("Include redundant headers"));
640 newCode.add(INCLUDE(HEADER_STDINT));
641 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
644 newCode.add("if (" + construct.condition + ") {");
645 String structName = "cp_define_check", anno = "annotation_cp_define_check";
646 newCode.add(DECLARE(ANNO_CP_DEFINE_CHECK, structName));
647 String labelNum = Integer.toString(semantics.commitPointLabel2Num
648 .get(construct.label));
649 newCode.add(ASSIGN(structName, "label_num", labelNum));
650 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
651 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
652 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
653 newCode.add(ANNOTATE(anno));
658 public static ArrayList<String> generateCPDefine(
659 SemanticsChecker semantics, CPDefineConstruct construct) {
660 ArrayList<String> newCode = new ArrayList<String>();
661 // Generate redundant header files
662 newCode.add(COMMENT("Automatically generated code for commit point define check: "
666 newCode.add("if (" + construct.condition + ") {");
667 String structName = "cp_define", anno = "annotation_cp_define";
668 newCode.add(DECLARE(ANNO_CP_DEFINE, structName));
669 String labelNum = Integer.toString(semantics.commitPointLabel2Num
670 .get(construct.label));
671 newCode.add(ASSIGN(structName, "label_num", labelNum));
672 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
673 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
674 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
675 newCode.add(ANNOTATE(anno));