small change
[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 CDSAnnotate = "_Z11cdsannotatemPv";
40         public static final String CDSAnnotateType = "SPEC_ANALYSIS";
41         public static final String IDType = "call_id_t";
42
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";
55
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";
64
65         // Specification variables
66         public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
67         public static final String DEFAULT_ID = "0";
68
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";
79
80         // Macro
81         public static final String MACRO_ID = "__ID__";
82         public static final String MACRO_COND = "__COND_SAT__";
83         public static final String MACRO_RETURN = "__RET__";
84         public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
85
86         public static void printCode(ArrayList<String> code) {
87                 for (int i = 0; i < code.size(); i++) {
88                         System.out.println(code.get(i));
89                 }
90         }
91
92         private static String COMMENT(String comment) {
93                 return "/* " + comment + " */";
94         }
95
96         private static String INCLUDE(String header) {
97                 return "#include " + header;
98         }
99
100         private static String DEFINE(String left, String right) {
101                 return "#define " + left + " " + right;
102         }
103
104         private static String UNDEFINE(String macro) {
105                 return "#undef " + macro;
106         }
107
108         private static String GET_FIELD_BY_PTR(String ptr, String field) {
109                 return ptr + "->" + field;
110         }
111
112         private static String GET_FIELD(String var, String field) {
113                 return var + "->" + field;
114         }
115
116         private static String BRACE(String val) {
117                 return "(" + val + ")";
118         }
119
120         private static String ASSIGN(String structName, String field, String val) {
121                 return structName + "." + field + " = " + val + ";";
122         }
123
124         private static String ASSIGN(String varName, String val) {
125                 return varName + " = " + val + ";";
126         }
127
128         private static String ASSIGN_PTR(String structName, String field, String val) {
129                 return structName + "." + field + " = &" + val + ";";
130         }
131
132         private static String ASSIGN_TO_PTR(String structName, String field,
133                         String val) {
134                 return structName + "->" + field + " = " + val + ";";
135         }
136         
137         private static String ASSIGN_PTR_TO_PTR(String structName, String field, String val) {
138                 return structName + "->" + field + " = &" + val + ";";
139         }
140
141         private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
142                 return "struct " + type + " *" + name + " = (struct " + type + "*) malloc(sizeof(struct " + type + "));";
143         }
144         
145         private static String DECLARE(String type, String name) {
146                 return type + " " + name + ";";
147         }
148
149         private static String DECLARE(VariableDeclaration varDecl) {
150                 String type = varDecl.type, name = varDecl.name;
151                 return type + " " + name + ";";
152         }
153
154         private static String DECLARE_DEFINE(String type, String var, String val) {
155                 return type + " " + var + " = " + val + ";";
156         }
157
158         private static String ANNOTATE(String structName) {
159                 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
160         }
161
162         private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
163                         FunctionHeader header) {
164                 ArrayList<String> code = new ArrayList<String>();
165                 code.add("typedef struct " + interfaceName + "_info {");
166                 if (!header.returnType.equals("void")) {
167                         code.add(DECLARE(header.returnType, MACRO_RETURN));
168                 }
169                 for (int i = 0; i < header.args.size(); i++) {
170                         code.add(DECLARE(header.args.get(i)));
171                 }
172                 code.add("} " + interfaceName + "_info;");
173                 return code;
174         }
175
176         private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
177                         String idCode) {
178                 ArrayList<String> code = new ArrayList<String>();
179                 code.add("static " + IDType + " " + interfaceName + "_id() {");
180                 if (!idCode.equals("")) {
181                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
182                 } else {
183                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
184                 }
185                 code.add("return " + MACRO_ID + ";");
186                 code.add("}");
187                 return code;
188         }
189
190         private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
191                         InterfaceConstruct construct, FunctionHeader header) {
192                 String interfaceName = construct.name;
193                 ArrayList<String> code = new ArrayList<String>();
194                 code.add("static bool " + interfaceName + "_check_action(void *info, "
195                                 + IDType + " " + MACRO_ID + ") {");
196                 code.add(DECLARE("bool", "check_passed"));
197                 // Read info struct
198                 if (!header.returnType.equals("void") || header.args.size() != 0) {
199                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
200                         code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
201                                         BRACE(infoStructType + "*") + "info"));
202                         if (!header.returnType.equals("void")) {
203                                 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
204                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
205                         }
206                         for (int i = 0; i < header.args.size(); i++) {
207                                 String type = header.args.get(i).type, var = header.args.get(i).name;
208                                 code.add((DECLARE_DEFINE(type, var,
209                                                 GET_FIELD_BY_PTR(infoStructName, var))));
210                         }
211                         code.add("");
212                 }
213                 // __COND_SAT
214                 if (!construct.condition.equals("")) {
215                         code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
216                 }
217                 // Check
218                 if (!construct.check.equals("")) {
219                         code.add(ASSIGN("check_passed", construct.check));
220                         code.add("if (!check_passed) return false;");
221                 }
222                 // Action
223                 if (construct.action.size() > 0) {
224                         code.addAll(construct.action);
225                 }
226                 // Post_check
227                 if (!construct.postCheck.equals("")) {
228                         code.add(ASSIGN("check_passed", construct.postCheck));
229                         code.add("if (!check_passed) return false;");
230                 }
231                 // Post_action
232                 if (construct.postAction.size() > 0) {
233                         code.addAll(construct.postAction);
234                 }
235                 // Return true finally
236                 code.add("return true;");
237
238                 code.add("}");
239
240                 return code;
241         }
242
243         private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
244                 HashSet<String> headers = new HashSet<String>();
245                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
246                         File f = semantics.interfaceName2Construct.get(interfaceName).file;
247                         headers.addAll(semantics.srcFilesInfo.get(f).headers);
248                 }
249                 return headers;
250         }
251
252         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
253                 String headLine = funcDefine.get(0);
254                 headLine = "static " + headLine;
255                 funcDefine.set(0, headLine);
256         }
257
258         private static String makeVariablesStatic(VariableDeclaration varDecl) {
259                 String res = "static " + varDecl.type + " " + varDecl.name + ";";
260                 return res;
261         }
262
263         private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
264                         Construct construct) {
265                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
266                 String headerLine = content.get(construct.beginLineNum);
267                 if (headerLine.startsWith("template")) {
268                         headerLine = content.get(construct.beginLineNum + 1);
269                 }
270                 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
271                 try {
272                         return SpecParser.parseFuncHeader(headerLine);
273                 } catch (ParseException e) {
274                         e.printStackTrace();
275                 }
276                 return null;
277         }
278
279         public static ArrayList<String> generateGlobalVarDeclaration(
280                         SemanticsChecker semantics, GlobalConstruct construct) {
281                 ArrayList<String> newCode = new ArrayList<String>();
282                 HashSet<String> allHeaders = getAllHeaders(semantics);
283
284                 // All headers needed by the interface decalration
285                 newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
286                 for (String header : allHeaders) {
287                         newCode.add(INCLUDE(header));
288                 }
289                 newCode.add("");
290                 // Other necessary headers
291                 newCode.add(INCLUDE(HEADER_STDLIB));
292                 newCode.add(INCLUDE(HEADER_STDINT));
293                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
294                 newCode.add(INCLUDE(HEADER_SPEC_LIB));
295                 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
296                 newCode.add("");
297
298                 SequentialDefineSubConstruct code = construct.code;
299                 // User-defined structs first
300                 newCode.add(COMMENT("All other user-defined structs"));
301                 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
302                 for (int i = 0; i < declareStructs.size(); i++) {
303                         ArrayList<String> declareStruct = declareStructs.get(i);
304                         newCode.addAll(declareStruct);
305                         newCode.add("");
306                 }
307                 // User-defined variables
308                 ArrayList<VariableDeclaration> varDecls = code.declareVar;
309                 for (int i = 0; i < varDecls.size(); i++) {
310                         VariableDeclaration varDecl = varDecls.get(i);
311                         // Don't forget to make them static
312                         newCode.add(makeVariablesStatic(varDecl));
313                 }
314                 // User-defined functions
315                 newCode.add(COMMENT("All other user-defined functions"));
316                 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
317                 for (int i = 0; i < defineFuncs.size(); i++) {
318                         ArrayList<String> defineFunc = defineFuncs.get(i);
319                         makeFunctionStatic(defineFunc);
320                         newCode.addAll(defineFunc);
321                         newCode.add("");
322                 }
323
324                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
325                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
326                                         .get(interfaceName);
327                         FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
328                         // Define necessary info structure
329                         if (!funcHeader.returnType.equals("void")
330                                         || funcHeader.args.size() > 0) {
331                                 newCode.add(COMMENT("Definition of interface info struct: "
332                                                 + interfaceName));
333                                 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
334                                 newCode.add(COMMENT("End of info struct definition: "
335                                                 + interfaceName));
336                                 newCode.add("");
337                         }
338
339                         // Define ID function
340                         newCode.add(COMMENT("ID function of interface: " + interfaceName));
341                         newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
342                         newCode.add(COMMENT("End of ID function: " + interfaceName));
343                         newCode.add("");
344
345                         // Define check_action function
346                         newCode.add(COMMENT("Check action function of interface: "
347                                         + interfaceName));
348                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
349                         newCode.add(COMMENT("End of check action function: "
350                                         + interfaceName));
351                         newCode.add("");
352                 }
353                 // Interface function pointer table
354                 String interfaceSize = Integer
355                                 .toString(semantics.interfaceName2Construct.size());
356                 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
357                 newCode.add(DECLARE("void**", "func_ptr_table"));
358
359                 newCode.add("");
360                 newCode.add(COMMENT("Define function for sequential code initialization"));
361                 newCode.add("static void __sequential_init() {");
362                 // Init func_ptr_table
363                 newCode.add(COMMENT("Init func_ptr_table"));
364                 newCode.add(ASSIGN("func_ptr_table",
365                                 "(void**) malloc(sizeof(void*) * 2)"));
366                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
367                         String interfaceNum = Integer.toString(semantics.interface2Num
368                                         .get(interfaceName));
369                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
370                                         "(void*) &" + interfaceName + "_id"));
371                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
372                                         "(void*) &" + interfaceName + "_check_action"));
373                 }
374                 newCode.add("");
375                 // Init user-defined variables
376                 newCode.addAll(construct.code.initVar);
377                 // Pass function table info
378                 newCode.add(COMMENT("Pass function table info"));
379                 String structName = "anno_func_table_init", anno = "func_init";
380                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_FUNC_TABLE_INIT, structName));
381                 newCode.add(ASSIGN_TO_PTR(structName, "size", "INTERFACE_SIZE"));
382                 newCode.add(ASSIGN_TO_PTR(structName, "table", "func_ptr_table"));
383                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
384                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_FUNC_TABLE_INIT));
385                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
386                 newCode.add(ANNOTATE(anno));
387
388                 // Pass Happens-before relationship
389                 newCode.addAll(generateHBInitAnnotation(semantics));
390                 newCode.add("}");
391                 newCode.add(COMMENT("End of Global construct generation in class"));
392
393                 printCode(newCode);
394                 return newCode;
395         }
396
397         public static ArrayList<String> generateStaticVarDefine(
398                         SemanticsChecker semantics, GlobalConstruct construct) {
399                 ArrayList<String> newCode = new ArrayList<String>();
400                 String className = semantics.getClassName();
401                 if (className == null)
402                         return newCode; // No need to define any static variables
403                 String templateList = semantics.getTemplateStr();
404                 String varPrefix;
405                 if (templateList == null) {
406                         varPrefix = className + "::";
407                 } else {
408                         varPrefix = className + templateList + "::";
409                 }
410                 String templateDecl = semantics.getTemplateFullStr();
411                 if (templateList == null) {
412                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
413                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
414                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
415                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
416                         }
417                 } else {
418                         newCode.add(templateDecl);
419                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
420                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
421                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
422                                 newCode.add(templateDecl);
423                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
424                         }
425                 }
426                 return newCode;
427         }
428
429         private static ArrayList<String> generateHBInitAnnotation(
430                         SemanticsChecker semantics) {
431                 ArrayList<String> newCode = new ArrayList<String>();
432                 int hbConditionInitIdx = 0;
433                 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
434                         for (ConditionalInterface right : semantics.getHBConditions().get(
435                                         left)) {
436                                 String structVarName = "hbConditionInit" + hbConditionInitIdx;
437                                 String annotationVarName = "hb_init" + hbConditionInitIdx;
438                                 hbConditionInitIdx++;
439                                 String interfaceNumBefore = Integer
440                                                 .toString(semantics.interface2Num
441                                                                 .get(left.interfaceName)), hbLabelNumBefore = Integer
442                                                 .toString(semantics.hbLabel2Num
443                                                                 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
444                                                 .toString(semantics.interface2Num
445                                                                 .get(right.interfaceName)), hbLabelNumAfter = Integer
446                                                 .toString(semantics.hbLabel2Num
447                                                                 .get(right.hbConditionLabel));
448                                 newCode.add(COMMENT(left + " -> " + right));
449
450                                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
451                                 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_before",
452                                                 interfaceNumBefore));
453                                 newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_before",
454                                                 hbLabelNumBefore));
455                                 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
456                                                 interfaceNumAfter));
457                                 newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_after",
458                                                 hbLabelNumAfter));
459
460                                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, annotationVarName));
461                                 newCode.add(ASSIGN_TO_PTR(annotationVarName,
462                                                 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
463                                 newCode.add(ASSIGN_TO_PTR(annotationVarName,
464                                                 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
465                                 newCode.add(ANNOTATE(annotationVarName));
466                         }
467                 }
468                 return newCode;
469         }
470
471         public static ArrayList<String> generateEntryPointInitCall() {
472                 ArrayList<String> newCode = new ArrayList<String>(1);
473                 newCode.add("__sequential_init();");
474                 return newCode;
475         }
476
477         public static ArrayList<String> generateInterfaceWrapper(
478                         SemanticsChecker semantics, InterfaceConstruct construct) {
479                 ArrayList<String> newCode = new ArrayList<String>();
480                 String interfaceName = construct.name;
481                 // Generate necessary header file (might be redundant but never mind)
482                 newCode.add(INCLUDE(HEADER_STDLIB));
483                 newCode.add(INCLUDE(HEADER_THREADS));
484                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
485                 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
486                 newCode.add(INCLUDE(HEADER_SPEC_LIB));
487
488                 FunctionHeader header = getFunctionHeader(semantics, construct);
489                 String interfaceNum = Integer.toString(semantics.interface2Num
490                                 .get(construct.name));
491                 // Rename the interface
492                 renameInterface(semantics, construct);
493                 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
494                                 .get(interfaceName);
495                 if (defineConstruct != null) {
496                         renameInterface(semantics, defineConstruct);
497                 }
498
499                 // Generate wrapper header
500                 // If it's not in a class, we should declare the wrapper function
501                 if (semantics.getClassName() == null) {
502                         FunctionHeader renamedHeader = header
503                                         .getRenamedHeader(SPEC_INTERFACE_WRAPPER);
504                         newCode.add(COMMENT("Declaration of the wrapper"));
505                         newCode.add(renamedHeader + ";");
506                         newCode.add("");
507                 }
508                 newCode.add(header.toString() + " {");
509                 // Wrapper function body
510                 newCode.add(COMMENT("Interface begins"));
511                 // Interface begin
512                 String structName = "interface_begin";
513                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN, "interface_begin"));
514                 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
515                 String anno = "annotation_interface_begin";
516                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
517                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
518                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
519                 newCode.add(ANNOTATE(anno));
520                 // Call original renamed function
521                 if (header.returnType.equals("void")) {
522                         newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
523                 } else {
524                         newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
525                                         header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
526                 }
527                 // HB conditions
528                 for (String label : construct.hbConditions.keySet()) {
529                         String condition = construct.hbConditions.get(label);
530                         String hbCondNum = Integer.toString(semantics.hbLabel2Num
531                                         .get(label));
532                         newCode.add("if " + BRACE(condition) + " {");
533                         structName = "hb_condition";
534                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
535                         newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
536
537                         newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
538                         anno = "annotation_hb_condition";
539                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
540                         newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
541                         newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
542                         newCode.add(ANNOTATE(anno));
543                         newCode.add("}");
544                         newCode.add("");
545                 }
546                 // Interface end
547                 String infoStructType = null, infoName = null;
548                 if (!header.returnType.equals("void") || header.args.size() > 0) {
549                         infoStructType = interfaceName + "_info";
550                         infoName = "info";
551                         newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
552                                         BRACE(infoStructType + "*") + " malloc(sizeof("
553                                                         + infoStructType + "))"));
554                         if (!header.returnType.equals("void")) {
555                                 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
556                         }
557                         for (int i = 0; i < header.args.size(); i++) {
558                                 String argName = header.args.get(i).name;
559                                 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
560                         }
561                 } else {
562                         infoName = "NULL";
563                 }
564                 structName = "interface_end";
565                 anno = "annoation_interface_end";
566                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
567                 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
568                 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
569                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
570                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
571                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
572                 newCode.add(ANNOTATE(anno));
573                 // Return __RET__ if it's not void
574                 if (!header.returnType.equals("void")) {
575                         newCode.add("return " + MACRO_RETURN + ";");
576                 }
577                 // End of the wrapper function
578                 newCode.add("}");
579
580                 // printCode(newCode);
581                 return newCode;
582         }
583
584         public static void renameInterface(SemanticsChecker semantics,
585                         Construct construct) {
586                 FunctionHeader header = getFunctionHeader(semantics, construct);
587                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
588                 int lineNum = construct.beginLineNum;
589                 String headerLine = content.get(construct.beginLineNum);
590                 if (headerLine.startsWith("template")) {
591                         headerLine = content.get(construct.beginLineNum + 1);
592                         lineNum++;
593                 }
594                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
595                                 .toString() + " {";
596                 content.set(lineNum, newLine);
597         }
598
599         public static void addAtomicReturn(SemanticsChecker semantics,
600                         Construct construct) {
601                 int lineNum = construct.beginLineNum - 1;
602                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
603                 String oldLine = content.get(lineNum);
604                 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
605                 content.set(lineNum, newLine);
606         }
607
608         public static ArrayList<String> generatePotentialCPDefine(
609                         SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
610                 ArrayList<String> newCode = new ArrayList<String>();
611                 // Add atomic return variable if the predicate accesses to it
612                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
613                         addAtomicReturn(semantics, construct);
614                 }
615                 // Generate redundant header files
616                 newCode.add(COMMENT("Automatically generated code for potential commit point: "
617                                 + construct.label));
618                 newCode.add(COMMENT("Include redundant headers"));
619                 newCode.add(INCLUDE(HEADER_STDINT));
620                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
621                 newCode.add("");
622                 // Add annotation
623                 newCode.add("if (" + construct.condition + ") {");
624                 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
625                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE, structName));
626                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
627                                 .get(construct.label));
628                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
629                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
630                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
631                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
632                 newCode.add(ANNOTATE(anno));
633                 newCode.add("}");
634                 return newCode;
635         }
636
637         public static ArrayList<String> generateCPDefineCheck(
638                         SemanticsChecker semantics, CPDefineCheckConstruct construct) {
639                 ArrayList<String> newCode = new ArrayList<String>();
640                 // Add atomic return variable if the predicate accesses to it
641                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
642                         addAtomicReturn(semantics, construct);
643                 }
644                 // Generate redundant header files
645                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
646                                 + construct.label));
647                 newCode.add(COMMENT("Include redundant headers"));
648                 newCode.add(INCLUDE(HEADER_STDINT));
649                 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
650                 newCode.add("");
651                 // Add annotation
652                 newCode.add("if (" + construct.condition + ") {");
653                 String structName = "cp_define_check", anno = "annotation_cp_define_check";
654                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
655                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
656                                 .get(construct.label));
657                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
658                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
659                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
660                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
661                 newCode.add(ANNOTATE(anno));
662                 newCode.add("}");
663                 return newCode;
664         }
665
666         public static ArrayList<String> generateCPDefine(
667                         SemanticsChecker semantics, CPDefineConstruct construct) {
668                 ArrayList<String> newCode = new ArrayList<String>();
669                 // Generate redundant header files
670                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
671                                 + construct.label));
672                 newCode.add("");
673                 // Add annotation
674                 newCode.add("if (" + construct.condition + ") {");
675                 String structName = "cp_define", anno = "annotation_cp_define";
676                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
677                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
678                                 .get(construct.label));
679                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
680                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
681                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
682                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
683                 newCode.add(ANNOTATE(anno));
684                 newCode.add("}");
685                 return newCode;
686         }
687 }