980e42064d74cb09dd1533fbeff413e5ab5591d1
[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_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";
33
34         // Model checker code
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";
41
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";
53
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";
61
62         // Specification variables
63         public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
64         public static final String DEFAULT_ID = "0";
65
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";
76
77         // Macro
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__";
82
83         public static void printCode(ArrayList<String> code) {
84                 for (int i = 0; i < code.size(); i++) {
85                         System.out.println(code.get(i));
86                 }
87         }
88
89         private static String COMMENT(String comment) {
90                 return "/* " + comment + " */";
91         }
92
93         private static String INCLUDE(String header) {
94                 return "#include " + header;
95         }
96
97         private static String DEFINE(String left, String right) {
98                 return "#define " + left + " " + right;
99         }
100
101         private static String UNDEFINE(String macro) {
102                 return "#undef " + macro;
103         }
104
105         private static String GET_FIELD_BY_PTR(String ptr, String field) {
106                 return ptr + "->" + field;
107         }
108
109         private static String GET_FIELD(String var, String field) {
110                 return var + "->" + field;
111         }
112
113         private static String BRACE(String val) {
114                 return "(" + val + ")";
115         }
116
117         private static String ASSIGN(String structName, String field, String val) {
118                 return structName + "." + field + " = " + val + ";";
119         }
120
121         private static String ASSIGN(String varName, String val) {
122                 return varName + " = " + val + ";";
123         }
124
125         private static String ASSIGN_PTR(String structName, String field, String val) {
126                 return structName + "." + field + " = &" + val + ";";
127         }
128
129         private static String ASSIGN_TO_PTR(String structName, String field,
130                         String val) {
131                 return structName + "->" + field + " = " + val + ";";
132         }
133
134         private static String DECLARE(String type, String name) {
135                 return type + " " + name + ";";
136         }
137
138         private static String DECLARE(VariableDeclaration varDecl) {
139                 String type = varDecl.type, name = varDecl.name;
140                 return type + " " + name + ";";
141         }
142
143         private static String DECLARE_DEFINE(String type, String var, String val) {
144                 return type + " " + var + " = " + val + ";";
145         }
146
147         private static String ANNOTATE(String structName) {
148                 return CDSAnnotate + "(" + CDSAnnotateType + ", &" + structName + ");";
149         }
150
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));
157                 }
158                 for (int i = 0; i < header.args.size(); i++) {
159                         code.add(DECLARE(header.args.get(i)));
160                 }
161                 code.add("} " + interfaceName + "_info {");
162                 return code;
163         }
164
165         private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
166                         String idCode) {
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));
171                 } else {
172                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
173                 }
174                 code.add("return " + MACRO_ID + ";");
175                 code.add("}");
176                 return code;
177         }
178
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"));
186                 // Read info struct
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))));
194                         }
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))));
199                         }
200                         code.add("");
201                 }
202                 // __COND_SAT
203                 if (!construct.condition.equals("")) {
204                         code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
205                 }
206                 // Check
207                 if (!construct.check.equals("")) {
208                         code.add(ASSIGN("check_passed", construct.check));
209                         code.add("if (!check_passed) return false;");
210                 }
211                 // Action
212                 if (construct.action.size() > 0) {
213                         code.addAll(construct.action);
214                 }
215                 // Post_check
216                 if (!construct.postCheck.equals("")) {
217                         code.add(ASSIGN("check_passed", construct.postCheck));
218                         code.add("if (!check_passed) return false;");
219                 }
220                 // Post_action
221                 if (construct.postAction.size() > 0) {
222                         code.addAll(construct.postAction);
223                 }
224
225                 code.add("}");
226
227                 return code;
228         }
229
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);
235                 }
236                 return headers;
237         }
238
239         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
240                 String headLine = funcDefine.get(0);
241                 headLine = "static " + headLine;
242                 funcDefine.set(0, headLine);
243         }
244
245         private static String makeVariablesStatic(VariableDeclaration varDecl) {
246                 String res = "static " + varDecl.type + " " + varDecl.name + ";";
247                 return res;
248         }
249
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);
256                 }
257                 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
258                 try {
259                         return SpecParser.parseFuncHeader(headerLine);
260                 } catch (ParseException e) {
261                         e.printStackTrace();
262                 }
263                 return null;
264         }
265
266         public static ArrayList<String> generateGlobalVarDeclaration(
267                         SemanticsChecker semantics, GlobalConstruct construct) {
268                 ArrayList<String> newCode = new ArrayList<String>();
269                 HashSet<String> allHeaders = getAllHeaders(semantics);
270
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));
275                 }
276                 newCode.add("");
277                 // Other necessary headers
278                 newCode.add(INCLUDE(HEADER_STDINT));
279                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
280                 newCode.add(INCLUDE(HEADER_SPEC_LIB));
281                 newCode.add("");
282
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);
290                         newCode.add("");
291                 }
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);
299                         newCode.add("");
300                 }
301
302                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
303                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
304                                         .get(interfaceName);
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: "
309                                                 + interfaceName));
310                                 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
311                                 newCode.add(COMMENT("End of info struct definition: "
312                                                 + interfaceName));
313                                 newCode.add("");
314                         }
315
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));
320                         newCode.add("");
321
322                         // Define check_action function
323                         newCode.add(COMMENT("Check action function of interface: "
324                                         + interfaceName));
325                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
326                         newCode.add(COMMENT("End of check action function: + "
327                                         + interfaceName));
328                         newCode.add("");
329                 }
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));
341                 }
342
343                 newCode.add("");
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"));
357                 }
358                 newCode.add("");
359                 // Init user-defined variables
360                 newCode.addAll(construct.code.initVar);
361                 // Pass Happens-before relationship
362                 generateHBInitAnnotation(semantics);
363                 newCode.add("}");
364                 newCode.add(COMMENT("End of Global construct generation in class"));
365
366                 printCode(newCode);
367                 return newCode;
368         }
369
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();
377                 String varPrefix;
378                 if (templateList == null) {
379                         varPrefix = className + "::";
380                 } else {
381                         varPrefix = className + templateList + "::";
382                 }
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));
389                         }
390                 } else {
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));
397                         }
398                 }
399                 return newCode;
400         }
401
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(
408                                         left)) {
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));
422
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",
427                                                 hbLabelNumBefore));
428                                 newCode.add(ASSIGN(structVarName, "interface_num_after",
429                                                 interfaceNumAfter));
430                                 newCode.add(ASSIGN(structVarName, "hb_condition_num_after",
431                                                 hbLabelNumAfter));
432
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));
439                         }
440                 }
441                 return newCode;
442         }
443
444         public static ArrayList<String> generateEntryPointInitCall() {
445                 ArrayList<String> newCode = new ArrayList<String>(1);
446                 newCode.add("__sequential_init();");
447                 return newCode;
448         }
449
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));
460
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
467                                 .get(interfaceName);
468                 if (defineConstruct != null) {
469                         renameInterface(semantics, defineConstruct);
470                 }
471
472                 // Generate wrapper header
473                 newCode.add(header.toString() + " {");
474                 // Wrapper function body
475                 newCode.add(COMMENT("Interface begins"));
476                 // Interface begin
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));
488                 } else {
489                         newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
490                                         header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
491                 }
492                 // HB conditions
493                 for (String label : construct.hbConditions.keySet()) {
494                         String condition = construct.hbConditions.get(label);
495                         String hbCondNum = Integer.toString(semantics.hbLabel2Num
496                                         .get(label));
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));
501
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));
508                         newCode.add("}");
509                         newCode.add("");
510                 }
511                 // Interface end
512                 String infoStructType = null, infoName = null;
513                 if (!header.returnType.equals("void") || header.args.size() > 0) {
514                         infoStructType = interfaceName + "_info";
515                         infoName = "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));
521                         }
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));
525                         }
526                 } else {
527                         infoName = "NULL";
528                 }
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));
537                 ANNOTATE(anno);
538                 // End of the wrapper function
539                 newCode.add("}");
540
541                 // printCode(newCode);
542                 return newCode;
543         }
544
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);
553                         lineNum++;
554                 }
555                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
556                                 .toString() + " {";
557                 content.set(lineNum, newLine);
558         }
559
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);
567         }
568
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);
575                 }
576                 // Generate redundant header files
577                 newCode.add(COMMENT("Automatically generated code for potential commit point: "
578                                 + construct.label));
579                 newCode.add(COMMENT("Include redundant headers"));
580                 newCode.add(INCLUDE(HEADER_STDINT));
581                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
582                 newCode.add("");
583                 // Add annotation
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));
594                 newCode.add("}");
595                 return newCode;
596         }
597
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);
604                 }
605                 // Generate redundant header files
606                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
607                                 + construct.label));
608                 newCode.add(COMMENT("Include redundant headers"));
609                 newCode.add(INCLUDE(HEADER_STDINT));
610                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
611                 newCode.add("");
612                 // Add annotation
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));
623                 newCode.add("}");
624                 return newCode;
625         }
626
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: "
632                                 + construct.label));
633                 newCode.add("");
634                 // Add annotation
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));
645                 newCode.add("}");
646                 return newCode;
647         }
648 }