dd815db37bdb92ca9cec78dbe89dd9c628689cff
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.io.File;
6
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;
23
24 public class CodeVariables {
25         // C++ code or library
26         public static final String HEADER_THREADS = "<threads.h>";
27         public static final String HEADER_STDINT = "<stdint.h>";
28         public static final String ThreadIDType = "thrd_t";
29         public static final String GET_THREAD_ID = "thrd_current";
30         public static final String BOOLEAN = "bool";
31         public static final String UINT64 = "uint64_t";
32
33         // Model checker code
34         public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
35         public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
36         public static final String HEADER_CDSTRACE = "<cdstrace.h>";
37         public static final String CDSAnnotate = "cdsannotate";
38         public static final String CDSAnnotateType = "SPEC_ANALYSIS";
39         public static final String IDType = "id_t";
40
41         public static final String SPEC_ANNO_TYPE = "spec_anno_type";
42         public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
43         public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
44         public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
45         public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
46         public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
47         public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
48         public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
49         public static final String SPEC_ANNOTATION = "spec_annotation";
50         public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
51         public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
52
53         public static final String ANNO_HB_INIT = "anno_hb_init";
54         public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
55         public static final String ANNO_INTERFACE_END = "anno_interface_end";
56         public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potentail_cp_define";
57         public static final String ANNO_CP_DEFINE = "anno_cp_define";
58         public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
59         public static final String ANNO_HB_CONDITION = "anno_hb_condition";
60
61         // Specification variables
62         public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
63         public static final String DEFAULT_ID = "0";
64
65         // Specification library
66         public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
67         public static final String SPEC_QUEUE = "spec_queue";
68         public static final String SPEC_STACK = "spec_stack";
69         public static final String SPEC_DEQUE = "spec_deque";
70         public static final String SPEC_HASHTABLE = "spec_hashtable";
71         public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
72         public static final String SPEC_TAG = "spec_tag";
73         public static final String SPEC_TAG_CURRENT = "current";
74         public static final String SPEC_TAG_NEXT = "next";
75
76         // Macro
77         public static final String MACRO_ID = "__ID__";
78         public static final String MACRO_COND = "__COND_SAT__";
79         public static final String MACRO_RETURN = "__RET__";
80         public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
81
82         public static void printCode(ArrayList<String> code) {
83                 for (int i = 0; i < code.size(); i++) {
84                         System.out.println(code.get(i));
85                 }
86         }
87
88         private static String COMMENT(String comment) {
89                 return "/* " + comment + " */";
90         }
91
92         private static String INCLUDE(String header) {
93                 return "#include " + header;
94         }
95
96         private static String DEFINE(String left, String right) {
97                 return "#define " + left + " " + right;
98         }
99
100         private static String UNDEFINE(String macro) {
101                 return "#undef " + macro;
102         }
103
104         private static String GET_FIELD_BY_PTR(String ptr, String field) {
105                 return ptr + "->" + field;
106         }
107
108         private static String GET_FIELD(String var, String field) {
109                 return var + "->" + field;
110         }
111
112         private static String BRACE(String val) {
113                 return "(" + val + ")";
114         }
115
116         private static String ASSIGN(String structName, String field, String val) {
117                 return structName + "." + field + " = " + val + ";";
118         }
119
120         private static String ASSIGN(String varName, String val) {
121                 return varName + " = " + val + ";";
122         }
123
124         private static String ASSIGN_PTR(String structName, String field, String val) {
125                 return structName + "." + field + " = &" + val + ";";
126         }
127
128         private static String ASSIGN_TO_PTR(String structName, String field,
129                         String val) {
130                 return structName + "->" + field + " = " + val + ";";
131         }
132
133         private static String DECLARE(String type, String name) {
134                 return type + " " + name + ";";
135         }
136
137         private static String DECLARE(VariableDeclaration varDecl) {
138                 String type = varDecl.type, name = varDecl.name;
139                 return type + " " + name + ";";
140         }
141
142         private static String DECLARE_DEFINE(String type, String var, String val) {
143                 return type + " " + var + " = " + val + ";";
144         }
145
146         private static String ANNOTATE(String structName) {
147                 return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
148         }
149
150         private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
151                         FunctionHeader funcDecl) {
152                 ArrayList<String> code = new ArrayList<String>();
153                 code.add("typedef struct " + interfaceName + "_info {");
154                 code.add(DECLARE(funcDecl.returnType, MACRO_RETURN));
155                 for (int i = 0; i < funcDecl.args.size(); i++) {
156                         code.add(DECLARE(funcDecl.args.get(i)));
157                 }
158                 code.add("} " + interfaceName + "_info {");
159                 return code;
160         }
161
162         private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
163                         String idCode) {
164                 ArrayList<String> code = new ArrayList<String>();
165                 code.add("static " + IDType + " " + interfaceName + "_id() {");
166                 if (idCode != null) {
167                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
168                 } else {
169                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
170                 }
171                 code.add("return " + MACRO_ID + ";");
172                 code.add("}");
173                 return code;
174         }
175
176         private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
177                         InterfaceConstruct construct, FunctionHeader header) {
178                 String interfaceName = construct.name;
179                 ArrayList<String> code = new ArrayList<String>();
180                 code.add("bool " + interfaceName + "_check_action(void *info, "
181                                 + IDType + " " + MACRO_ID + ") {");
182                 code.add(DECLARE("bool", "check_passed"));
183                 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
184                 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
185                                 BRACE(infoStructType) + "info"));
186                 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
187                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
188                 for (int i = 0; i < header.args.size(); i++) {
189                         String type = header.args.get(i).type, var = header.args.get(i).name;
190                         code.add((DECLARE_DEFINE(type, var,
191                                         GET_FIELD_BY_PTR(infoStructName, var))));
192                 }
193                 code.add("");
194                 // __COND_SAT
195                 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
196                 // Check
197                 code.add(ASSIGN("check_passed", construct.check));
198                 code.add("if (!check_passed) return false;");
199                 // Action
200                 code.addAll(construct.action);
201                 // Post_check
202                 code.add(ASSIGN("check_passed", construct.postCheck));
203                 code.add("if (!check_passed) return false;");
204                 // Post_action
205                 code.addAll(construct.postAction);
206                 code.add("}");
207
208                 return code;
209         }
210
211         private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
212                 HashSet<String> headers = new HashSet<String>();
213                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
214                         File f = semantics.interfaceName2Construct.get(interfaceName).file;
215                         headers.addAll(semantics.srcFilesInfo.get(f).headers);
216                 }
217                 return headers;
218         }
219
220         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
221                 String headLine = funcDefine.get(0);
222                 headLine = "static " + headLine;
223                 funcDefine.set(0, headLine);
224         }
225
226         private static void makeVariablesStatic(ArrayList<String> varDecls) {
227                 for (int i = 0; i < varDecls.size(); i++) {
228                         String varDecl = varDecls.get(i);
229                         varDecl = "static " + varDecl;
230                         varDecls.set(i, varDecl);
231                 }
232         }
233
234         private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
235                         Construct construct) {
236                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
237                 String headerLine = content.get(construct.beginLineNum);
238                 if (headerLine.startsWith("template")) {
239                         headerLine = content.get(construct.beginLineNum + 1);
240                 }
241                 try {
242                         return SpecParser.parseFuncHeader(headerLine);
243                 } catch (ParseException e) {
244                         e.printStackTrace();
245                 }
246                 return null;
247         }
248
249         public static ArrayList<String> generateGlobalVarDeclaration(
250                         SemanticsChecker semantics, GlobalConstruct construct) {
251                 ArrayList<String> newCode = new ArrayList<String>();
252                 HashSet<String> allHeaders = getAllHeaders(semantics);
253
254                 // All headers needed by the interface decalration
255                 newCode.add(COMMENT("/* Include all the header files that contains the interface declaration */"));
256                 for (String header : allHeaders) {
257                         newCode.add(INCLUDE(header));
258                 }
259                 newCode.add("");
260                 // Other necessary headers
261                 newCode.add(INCLUDE(HEADER_STDINT));
262                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
263                 newCode.add(INCLUDE(HEADER_SPEC_LIB));
264                 newCode.add("");
265
266                 SequentialDefineSubConstruct code = construct.code;
267                 // User-defined functions
268                 newCode.add(COMMENT("All other user-defined functions"));
269                 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
270                 for (int i = 0; i < defineFuncs.size(); i++) {
271                         ArrayList<String> defineFunc = defineFuncs.get(i);
272                         makeFunctionStatic(defineFunc);
273                         newCode.addAll(defineFunc);
274                         newCode.add("");
275                 }
276
277                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
278                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
279                                         .get(interfaceName);
280                         FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
281                         // Define necessary info structure
282                         newCode.add(COMMENT("Definition of interface info struct: "
283                                         + interfaceName));
284                         newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
285                         newCode.add(COMMENT("End of info struct definition: "
286                                         + interfaceName));
287                         newCode.add("");
288
289                         // Define ID function
290                         newCode.add(COMMENT("ID function of interface: " + interfaceName));
291                         newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
292                         newCode.add(COMMENT("End of ID function: + " + interfaceName));
293                         newCode.add("");
294
295                         // Define check_action function
296                         newCode.add(COMMENT("Check action function of interface: "
297                                         + interfaceName));
298                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
299                         newCode.add(COMMENT("End of check action function: + "
300                                         + interfaceName));
301                         newCode.add("");
302                 }
303                 // Interface function pointer table
304                 String interfaceSize = Integer
305                                 .toString(semantics.interfaceName2Construct.size());
306                 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
307                 newCode.add(DECLARE("void**", "func_ptr_table"));
308                 // User-defined variables
309                 ArrayList<VariableDeclaration> varDecls = code.declareVar;
310                 for (int i = 0; i < varDecls.size(); i++) {
311                         VariableDeclaration varDecl = varDecls.get(i);
312                         newCode.add(DECLARE(varDecl.type, varDecl.name));
313                 }
314
315                 newCode.add("");
316                 newCode.add(COMMENT("Define function for sequential code initialization"));
317                 newCode.add("static void __sequential_init() {");
318                 // Init func_ptr_table
319                 newCode.add(COMMENT("Init func_ptr_table"));
320                 newCode.add(ASSIGN("func_ptr_table",
321                                 "(void**) malloc(sizeof(void*) * 2)"));
322                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
323                         String interfaceNum = Integer.toString(semantics.interface2Num
324                                         .get(interfaceName));
325                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
326                                         "(void*) &" + interfaceName + "_id"));
327                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
328                                         "(void*) &" + interfaceName + "_check_action"));
329                 }
330                 newCode.add("");
331                 // Init user-defined variables
332                 newCode.addAll(construct.code.initVar);
333                 // Pass Happens-before relationship
334                 generateHBInitAnnotation(semantics);
335                 newCode.add("}");
336                 newCode.add(COMMENT("End of Global construct generation in class"));
337
338                 // printCode(newCode);
339                 return newCode;
340         }
341
342         public static ArrayList<String> generateStaticVarDefine(
343                         SemanticsChecker semantics, GlobalConstruct construct) {
344                 ArrayList<String> newCode = new ArrayList<String>();
345                 String className = semantics.getClassName();
346                 if (className == null)
347                         return newCode; // No need to define any static variables
348                 String templateList = semantics.getTemplateStr();
349                 String varPrefix;
350                 if (templateList == null) {
351                         varPrefix = className + "::";
352                 } else {
353                         varPrefix = className + templateList + "::";
354                 }
355                 String templateDecl = semantics.getTemplateFullStr();
356                 if (templateList == null) {
357                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
358                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
359                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
360                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
361                         }
362                 } else {
363                         newCode.add(templateDecl);
364                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
365                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
366                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
367                                 newCode.add(templateDecl);
368                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
369                         }
370                 }
371                 return newCode;
372         }
373
374         private static ArrayList<String> generateHBInitAnnotation(
375                         SemanticsChecker semantics) {
376                 ArrayList<String> newCode = new ArrayList<String>();
377                 int hbConditionInitIdx = 0;
378                 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
379                         for (ConditionalInterface right : semantics.getHBConditions().get(
380                                         left)) {
381                                 String structVarName = "hbConditionInit" + hbConditionInitIdx;
382                                 String annotationVarName = "hb_init" + hbConditionInitIdx;
383                                 hbConditionInitIdx++;
384                                 String interfaceNumBefore = Integer
385                                                 .toString(semantics.interface2Num
386                                                                 .get(left.interfaceName)), hbLabelNumBefore = Integer
387                                                 .toString(semantics.hbLabel2Num
388                                                                 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
389                                                 .toString(semantics.interface2Num
390                                                                 .get(right.interfaceName)), hbLabelNumAfter = Integer
391                                                 .toString(semantics.hbLabel2Num
392                                                                 .get(right.hbConditionLabel));
393                                 newCode.add(COMMENT(left + " -> " + right));
394
395                                 newCode.add(ANNO_HB_INIT + " " + structVarName + ";");
396                                 newCode.add(ASSIGN(structVarName, "interface_num_before",
397                                                 interfaceNumBefore));
398                                 newCode.add(ASSIGN(structVarName, "hb_condition_num_before",
399                                                 hbLabelNumBefore));
400                                 newCode.add(ASSIGN(structVarName, "interface_num_after",
401                                                 interfaceNumAfter));
402                                 newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
403                                                 hbLabelNumAfter));
404
405                                 newCode.add(DECLARE(SPEC_ANNOTATION, annotationVarName));
406                                 newCode.add(ASSIGN(annotationVarName,
407                                                 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
408                                 newCode.add(ASSIGN_PTR(annotationVarName,
409                                                 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
410                                 newCode.add(ANNOTATE(annotationVarName));
411                         }
412                 }
413                 return newCode;
414         }
415
416         public static ArrayList<String> generateInterfaceWrapper(
417                         SemanticsChecker semantics, InterfaceConstruct construct) {
418                 ArrayList<String> newCode = new ArrayList<String>();
419                 String interfaceName = construct.name;
420                 // Generate necessary header file (might be redundant but never mind)
421                 newCode.add(INCLUDE(HEADER_THREADS));
422                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
423                 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
424                 newCode.add(INCLUDE(HEADER_SPEC_LIB));
425
426                 FunctionHeader header = getFunctionHeader(semantics, construct);
427                 String interfaceNum = Integer.toString(semantics.interface2Num
428                                 .get(construct.name));
429                 // Rename the interface
430                 renameInterface(semantics, construct);
431                 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct.get(interfaceName);
432                 if (defineConstruct != null) {
433                         renameInterface(semantics, defineConstruct);
434                 }
435                 
436                 // Generate wrapper header
437                 newCode.add(header.toString() + " {");
438                 // Wrapper function body
439                 newCode.add(COMMENT("Interface begins"));
440                 // Interface begin
441                 String structName = "interface_begin";
442                 newCode.add(DECLARE(ANNO_INTERFACE_BEGIN, "interface_begin"));
443                 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
444                 String anno = "annotation_interface_begin";
445                 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
446                 newCode.add(ASSIGN(structName, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
447                 newCode.add(ASSIGN_PTR(structName, "annotation", structName));
448                 newCode.add(ANNOTATE(anno));
449                 // Call original renamed function
450                 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
451                                 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
452                 // HB conditions
453                 for (String label : construct.hbConditions.keySet()) {
454                         String condition = construct.hbConditions.get(label);
455                         String hbCondNum = Integer.toString(semantics.interface2Num
456                                         .get(label));
457                         newCode.add("if " + BRACE(condition) + " {");
458                         structName = "hb_condition";
459                         newCode.add(DECLARE(ANNO_HB_CONDITION, structName));
460                         newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
461
462                         newCode.add(ASSIGN(structName, "hb_condition_num", hbCondNum));
463                         anno = "annotation_hb_condition";
464                         newCode.add(DECLARE(SPEC_ANNOTATION, anno));
465                         newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
466                         newCode.add(ASSIGN_PTR(anno, "annotation", structName));
467                         newCode.add(ANNOTATE(anno));
468                 }
469                 // Interface end
470                 String infoStructType = interfaceName + "_info", infoName = "info";
471                 newCode.add(DECLARE_DEFINE(infoStructType, infoName,
472                                 BRACE(infoStructType + "*") + " malloc(sizeof("
473                                                 + infoStructType + "))"));
474                 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
475                 for (int i = 0; i < header.args.size(); i++) {
476                         String argName = header.args.get(i).name;
477                         newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
478                 }
479                 structName = "interface_end";
480                 anno = "annoation_interface_end";
481                 newCode.add(DECLARE(ANNO_INTERFACE_END, structName));
482                 newCode.add(ASSIGN(structName, "interface_num", interfaceNum));
483                 newCode.add(ASSIGN(structName, "info", infoName));
484                 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
485                 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
486                 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
487                 ANNOTATE(anno);
488                 // End of the wrapper function
489                 newCode.add("}");
490
491                 // printCode(newCode);
492                 return newCode;
493         }
494
495         public static void renameInterface(SemanticsChecker semantics,
496                         Construct construct) {
497                 FunctionHeader header = getFunctionHeader(semantics, construct);
498                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
499                 int lineNum = construct.beginLineNum;
500                 String headerLine = content.get(construct.beginLineNum);
501                 if (headerLine.startsWith("template")) {
502                         headerLine = content.get(construct.beginLineNum + 1);
503                         lineNum++;
504                 }
505                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
506                                 .toString() + " {";
507                 content.set(lineNum, newLine);
508         }
509
510         public static void addAtomicReturn(SemanticsChecker semantics,
511                         Construct construct) {
512                 int lineNum = construct.beginLineNum - 1;
513                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
514                 String oldLine = content.get(lineNum);
515                 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
516                 content.set(lineNum, newLine);
517         }
518
519         public static ArrayList<String> generatePotentialCPDefine(
520                         SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
521                 ArrayList<String> newCode = new ArrayList<String>();
522                 // Add atomic return variable if the predicate accesses to it
523                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
524                         addAtomicReturn(semantics, construct);
525                 }
526                 // Generate redundant header files
527                 newCode.add(COMMENT("Automatically generated code for potential commit point: "
528                                 + construct.label));
529                 newCode.add(COMMENT("Include redundant headers"));
530                 newCode.add(INCLUDE(HEADER_STDINT));
531                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
532                 newCode.add("");
533                 // Add annotation
534                 newCode.add("if (" + construct.condition + ") {");
535                 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
536                 newCode.add(DECLARE(ANNO_POTENTIAL_CP_DEFINE, structName));
537                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
538                                 .get(construct.label));
539                 newCode.add(ASSIGN(structName, "label_num", labelNum));
540                 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
541                 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
542                 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
543                 newCode.add(ANNOTATE(anno));
544                 newCode.add("}");
545                 return newCode;
546         }
547         
548         public static ArrayList<String> generateCPDefineCheck(
549                         SemanticsChecker semantics, CPDefineCheckConstruct construct) {
550                 ArrayList<String> newCode = new ArrayList<String>();
551                 // Add atomic return variable if the predicate accesses to it
552                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
553                         addAtomicReturn(semantics, construct);
554                 }
555                 // Generate redundant header files
556                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
557                                 + construct.label));
558                 newCode.add(COMMENT("Include redundant headers"));
559                 newCode.add(INCLUDE(HEADER_STDINT));
560                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
561                 newCode.add("");
562                 // Add annotation
563                 newCode.add("if (" + construct.condition + ") {");
564                 String structName = "cp_define_check", anno = "annotation_cp_define_check";
565                 newCode.add(DECLARE(ANNO_CP_DEFINE_CHECK, structName));
566                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
567                                 .get(construct.label));
568                 newCode.add(ASSIGN(structName, "label_num", labelNum));
569                 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
570                 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
571                 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
572                 newCode.add(ANNOTATE(anno));
573                 newCode.add("}");
574                 return newCode;
575         }
576         
577         public static ArrayList<String> generateCPDefine(
578                         SemanticsChecker semantics, CPDefineConstruct construct) {
579                 ArrayList<String> newCode = new ArrayList<String>();
580                 // Generate redundant header files
581                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
582                                 + construct.label));
583                 newCode.add("");
584                 // Add annotation
585                 newCode.add("if (" + construct.condition + ") {");
586                 String structName = "cp_define", anno = "annotation_cp_define";
587                 newCode.add(DECLARE(ANNO_CP_DEFINE, structName));
588                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
589                                 .get(construct.label));
590                 newCode.add(ASSIGN(structName, "label_num", labelNum));
591                 newCode.add(DECLARE(SPEC_ANNOTATION, anno));
592                 newCode.add(ASSIGN(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
593                 newCode.add(ASSIGN_PTR(anno, "annotation", structName));
594                 newCode.add(ANNOTATE(anno));
595                 newCode.add("}");
596                 return newCode;
597         }
598 }