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