save
[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.HashMap;
5 import java.util.HashSet;
6 import java.io.File;
7
8 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
9 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
13 import edu.uci.eecs.specCompiler.specExtraction.Construct;
14 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
15 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
20 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
23 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
24
25 public class CodeVariables {
26         // C++ code or library
27         public static final String HEADER_STDLIB = "<stdlib.h>";
28         public static final String HEADER_THREADS = "<threads.h>";
29         public static final String HEADER_STDINT = "<stdint.h>";
30         public static final String HEADER_MODELMEMORY = "<model_memory.h>";
31         public static final String HEADER_MODELTYPES = "<modeltypes.h>";
32         public static final String ThreadIDType = "thread_id_t";
33         public static final String BOOLEAN = "bool";
34         public static final String UINT64 = "uint64_t";
35
36         // Model checker code
37         public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
38         public static final String HEADER_COMMON = "<common.h>";
39         public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
40         public static final String HEADER_CDSTRACE = "<cdstrace.h>";
41         public static final String CDSAnnotate = "cdsannotate";
42         // public static final String CDSAnnotate = "cdsannotate";
43         public static final String CDSAnnotateType = "SPEC_ANALYSIS";
44         public static final String IDType = "call_id_t";
45
46         public static final String SPEC_ANNO_TYPE = "spec_anno_type";
47         public static final String SPEC_ANNO_TYPE_INIT = "INIT";
48         public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
49         public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
50         public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
51         public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
52         public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
53         public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
54         public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
55         public static final String SPEC_ANNOTATION = "spec_annotation";
56         public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
57         public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
58
59         public static final String ANNO_INIT = "anno_init";
60         public static final String ANNO_HB_INIT = "anno_hb_init";
61         public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
62         public static final String ANNO_INTERFACE_END = "anno_interface_end";
63         public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
64         public static final String ANNO_CP_DEFINE = "anno_cp_define";
65         public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
66         public static final String ANNO_HB_CONDITION = "anno_hb_condition";
67
68         // Specification variables
69         public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
70         public static final String DEFAULT_ID = "0";
71
72         // Specification library
73         public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
74         public static final String SPEC_QUEUE = "spec_queue";
75         public static final String SPEC_STACK = "spec_stack";
76         public static final String SPEC_DEQUE = "spec_deque";
77         public static final String SPEC_HASHTABLE = "spec_hashtable";
78         public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
79         public static final String SPEC_TAG = "spec_tag";
80         public static final String SPEC_TAG_CURRENT = "current";
81         public static final String SPEC_TAG_NEXT = "next";
82
83         // Macro
84         public static final String MACRO_ID = "__ID__";
85         public static final String MACRO_COND = "__COND_SAT__";
86         public static final String MACRO_RETURN = "__RET__";
87         public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
88         public static final String MACRO_THREAD_ID = "__TID__";
89
90         public static void printCode(ArrayList<String> code) {
91                 for (int i = 0; i < code.size(); i++) {
92                         System.out.println(code.get(i));
93                 }
94         }
95
96         private static String COMMENT(String comment) {
97                 return "/* " + comment + " */";
98         }
99
100         private static String INCLUDE(String header) {
101                 return "#include " + header;
102         }
103
104         private static String DEFINE(String left, String right) {
105                 return "#define " + left + " " + right;
106         }
107
108         private static String UNDEFINE(String macro) {
109                 return "#undef " + macro;
110         }
111
112         private static String GET_FIELD_BY_PTR(String ptr, String field) {
113                 return ptr + "->" + field;
114         }
115
116         private static String GET_FIELD(String var, String field) {
117                 return var + "->" + field;
118         }
119
120         private static String BRACE(String val) {
121                 return "(" + val + ")";
122         }
123
124         private static String ASSIGN(String structName, String field, String val) {
125                 return structName + "." + field + " = " + val + ";";
126         }
127
128         private static String ASSIGN(String varName, String val) {
129                 return varName + " = " + val + ";";
130         }
131
132         private static String ASSIGN_PTR(String structName, String field, String val) {
133                 return structName + "." + field + " = &" + val + ";";
134         }
135
136         private static String ASSIGN_TO_PTR(String structName, String field,
137                         String val) {
138                 return structName + "->" + field + " = " + val + ";";
139         }
140
141         private static String ASSIGN_PTR_TO_PTR(String structName, String field,
142                         String val) {
143                 return structName + "->" + field + " = &" + val + ";";
144         }
145
146         private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
147                 return "struct " + type + " *" + name + " = (struct " + type
148                                 + "*) malloc(sizeof(struct " + type + "));";
149         }
150
151         private static String DECLARE(String type, String name) {
152                 return type + " " + name + ";";
153         }
154
155         private static String DECLARE(VariableDeclaration varDecl) {
156                 String type = varDecl.type, name = varDecl.name;
157                 return type + " " + name + ";";
158         }
159
160         private static String DECLARE_DEFINE(String type, String var, String val) {
161                 return type + " " + var + " = " + val + ";";
162         }
163
164         private static String ANNOTATE(SemanticsChecker semantics, String structName) {
165                 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
166         }
167
168         private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
169                         FunctionHeader header) {
170                 ArrayList<String> code = new ArrayList<String>();
171                 code.add("typedef struct " + interfaceName + "_info {");
172                 if (!header.returnType.equals("void")) {
173                         code.add(DECLARE(header.returnType, MACRO_RETURN));
174                 }
175                 for (int i = 0; i < header.args.size(); i++) {
176                         code.add(DECLARE(header.args.get(i)));
177                 }
178                 code.add("} " + interfaceName + "_info;");
179                 return code;
180         }
181
182         private static ArrayList<String> DEFINE_ID_FUNC(
183                         InterfaceConstruct construct, FunctionHeader header) {
184                 String interfaceName = construct.name;
185                 ArrayList<String> code = new ArrayList<String>();
186                 String idCode = construct.idCode;
187                 code.add("inline static " + IDType + " " + interfaceName + "_id("
188                                 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
189
190                 // Read info struct
191                 if (!header.returnType.equals("void") || header.args.size() != 0) {
192                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
193                         code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
194                                         BRACE(infoStructType + "*") + "info"));
195                         if (!header.returnType.equals("void")) {
196                                 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
197                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
198                         }
199                         for (int i = 0; i < header.args.size(); i++) {
200                                 String type = header.args.get(i).type, var = header.args.get(i).name;
201                                 code.add((DECLARE_DEFINE(type, var,
202                                                 GET_FIELD_BY_PTR(infoStructName, var))));
203                         }
204                         code.add("");
205                 }
206
207                 if (!idCode.equals("")) {
208                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
209                 } else {
210                         code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
211                 }
212                 code.add("return " + MACRO_ID + ";");
213                 code.add("}");
214                 return code;
215         }
216
217         private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
218                         InterfaceConstruct construct, FunctionHeader header) {
219                 String interfaceName = construct.name;
220                 ArrayList<String> code = new ArrayList<String>();
221                 code.add("inline static bool " + interfaceName
222                                 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
223                                 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
224                 code.add(DECLARE("bool", "check_passed"));
225                 // Read info struct
226                 if (!header.returnType.equals("void") || header.args.size() != 0) {
227                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
228                         code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
229                                         BRACE(infoStructType + "*") + "info"));
230                         if (!header.returnType.equals("void")) {
231                                 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
232                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
233                         }
234                         for (int i = 0; i < header.args.size(); i++) {
235                                 String type = header.args.get(i).type, var = header.args.get(i).name;
236                                 code.add((DECLARE_DEFINE(type, var,
237                                                 GET_FIELD_BY_PTR(infoStructName, var))));
238                         }
239                         code.add("");
240                 }
241                 // __COND_SAT
242                 if (!construct.condition.equals("")) {
243                         code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
244                 }
245                 // Check
246                 if (!construct.check.equals("")) {
247                         code.add(ASSIGN("check_passed", construct.check));
248                         code.add("if (!check_passed) return false;");
249                 }
250                 // Action
251                 if (construct.action.size() > 0) {
252                         code.addAll(construct.action);
253                 }
254                 // Post_check
255                 if (!construct.postCheck.equals("")) {
256                         code.add(ASSIGN("check_passed", construct.postCheck));
257                         code.add("if (!check_passed) return false;");
258                 }
259                 // Post_action
260                 if (construct.postAction.size() > 0) {
261                         code.addAll(construct.postAction);
262                 }
263                 // Return true finally
264                 code.add("return true;");
265
266                 code.add("}");
267
268                 return code;
269         }
270
271         public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
272                 HashSet<String> headers = new HashSet<String>();
273                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
274                         File f = semantics.interfaceName2Construct.get(interfaceName).file;
275                         headers.addAll(semantics.srcFilesInfo.get(f).headers);
276                 }
277                 headers.add(HEADER_STDLIB);
278                 headers.add(HEADER_STDINT);
279                 headers.add(HEADER_MODELMEMORY);
280                 headers.add(HEADER_MODELTYPES);
281                 headers.add(HEADER_SPEC_LIB);
282                 headers.add(HEADER_STDINT);
283                 headers.add(HEADER_CDSANNOTATE);
284                 // headers.add(HEADER_COMMON);
285                 headers.add(HEADER_SPECANNOTATION);
286                 return headers;
287         }
288
289         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
290                 String headLine = funcDefine.get(0);
291                 headLine = "inline static " + headLine;
292                 funcDefine.set(0, headLine);
293         }
294
295         private static String makeVariablesStatic(VariableDeclaration varDecl) {
296                 String res = "static " + varDecl.type + " " + varDecl.name + ";";
297                 return res;
298         }
299
300         private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
301                         Construct construct) {
302                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
303                 String headerLine = content.get(construct.beginLineNum), templateLine = null;
304                 if (headerLine.startsWith("template")) {
305                         templateLine = headerLine;
306                         headerLine = content.get(construct.beginLineNum + 1);
307                 }
308                 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
309                 try {
310                         FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
311                         if (templateLine != null) {
312                                 ArrayList<VariableDeclaration> templateArgs = SpecParser
313                                                 .getTemplateArg(templateLine);
314                                 header.setTemplateList(templateArgs);
315                         }
316                         return header;
317                 } catch (ParseException e) {
318                         e.printStackTrace();
319                 }
320                 return null;
321         }
322
323         public static ArrayList<String> generateGlobalVarDeclaration(
324                         SemanticsChecker semantics, GlobalConstruct construct) {
325                 ArrayList<String> newCode = new ArrayList<String>();
326                 HashSet<String> allHeaders = getAllHeaders(semantics);
327
328                 SequentialDefineSubConstruct code = construct.code;
329                 // User-defined structs first
330                 newCode.add(COMMENT("All other user-defined structs"));
331                 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
332                 for (int i = 0; i < declareStructs.size(); i++) {
333                         ArrayList<String> declareStruct = declareStructs.get(i);
334                         newCode.addAll(declareStruct);
335                         newCode.add("");
336                 }
337                 // User-defined variables
338                 ArrayList<VariableDeclaration> varDecls = code.declareVar;
339                 for (int i = 0; i < varDecls.size(); i++) {
340                         VariableDeclaration varDecl = varDecls.get(i);
341                         // Don't forget to make them static
342                         newCode.add(makeVariablesStatic(varDecl));
343                 }
344                 // User-defined functions
345                 newCode.add(COMMENT("All other user-defined functions"));
346                 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
347                 for (int i = 0; i < defineFuncs.size(); i++) {
348                         ArrayList<String> defineFunc = defineFuncs.get(i);
349                         makeFunctionStatic(defineFunc);
350                         newCode.addAll(defineFunc);
351                         newCode.add("");
352                 }
353
354                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
355                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
356                                         .get(interfaceName);
357                         FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
358                         // Define necessary info structure
359                         if (!funcHeader.returnType.equals("void")
360                                         || funcHeader.args.size() > 0) {
361                                 newCode.add(COMMENT("Definition of interface info struct: "
362                                                 + interfaceName));
363                                 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
364                                 newCode.add(COMMENT("End of info struct definition: "
365                                                 + interfaceName));
366                                 newCode.add("");
367                         }
368
369                         // Define ID function
370                         newCode.add(COMMENT("ID function of interface: " + interfaceName));
371                         newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
372                         newCode.add(COMMENT("End of ID function: " + interfaceName));
373                         newCode.add("");
374
375                         // Define check_action function
376                         newCode.add(COMMENT("Check action function of interface: "
377                                         + interfaceName));
378                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
379                         newCode.add(COMMENT("End of check action function: "
380                                         + interfaceName));
381                         newCode.add("");
382                 }
383                 // Interface function pointer table
384                 String interfaceSize = Integer
385                                 .toString(semantics.interfaceName2Construct.size());
386                 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
387                 // Make it static
388                 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
389                 // Happens-before initialization rules
390                 // Should make it static
391                 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
392
393                 newCode.add("");
394                 newCode.add(COMMENT("Define function for sequential code initialization"));
395                 newCode.add("inline static void __sequential_init() {");
396                 // Init func_ptr_table
397                 newCode.add(COMMENT("Init func_ptr_table"));
398                 newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
399                                 + semantics.interface2Num.size() + " * 2)"));
400                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
401                         String interfaceNum = Integer.toString(semantics.interface2Num
402                                         .get(interfaceName));
403                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
404                                         "(void*) &" + interfaceName + "_id"));
405                         newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
406                                         "(void*) &" + interfaceName + "_check_action"));
407                 }
408                 // Init Happens-before rules table
409                 newCode.addAll(generateHBInitAnnotation(semantics));
410
411                 // Pass init info, including function table info & HB rules
412                 newCode.add(COMMENT("Pass init info, including function table info & HB rules"));
413                 String structName = "anno_init", anno = "init";
414                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
415                 newCode.add(ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
416                 newCode.add(ASSIGN_TO_PTR(structName, "func_table_size",
417                                 "INTERFACE_SIZE"));
418                 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
419                 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table_size",
420                                 "HB_INIT_TABLE_SIZE"));
421                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
422                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
423                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
424                 newCode.add(ANNOTATE(semantics, anno));
425
426                 newCode.add("");
427                 // Init user-defined variables
428                 newCode.addAll(construct.code.initVar);
429
430                 newCode.add("}");
431                 newCode.add(COMMENT("End of Global construct generation in class"));
432
433                 printCode(newCode);
434                 return newCode;
435         }
436
437         public static ArrayList<String> generateStaticVarDefine(
438                         SemanticsChecker semantics, GlobalConstruct construct) {
439                 ArrayList<String> newCode = new ArrayList<String>();
440                 String className = semantics.getClassName();
441                 if (className == null)
442                         return newCode; // No need to define any static variables
443                 String templateList = semantics.getTemplateStr();
444                 String varPrefix;
445                 if (templateList == null) {
446                         varPrefix = className + "::";
447                 } else {
448                         varPrefix = className + templateList + "::";
449                 }
450                 String templateDecl = semantics.getTemplateFullStr();
451                 if (templateList == null) {
452                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
453                         newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
454                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
455                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
456                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
457                         }
458                 } else {
459                         newCode.add(templateDecl);
460                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
461                         newCode.add(templateDecl);
462                         newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
463                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
464                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
465                                 newCode.add(templateDecl);
466                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
467                         }
468                 }
469                 return newCode;
470         }
471
472         private static ArrayList<String> generateHBInitAnnotation(
473                         SemanticsChecker semantics) {
474                 ArrayList<String> newCode = new ArrayList<String>();
475
476                 int hbConditionInitIdx = 0;
477                 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
478                         for (ConditionalInterface right : semantics.getHBConditions().get(
479                                         left)) {
480                                 String structVarName = "hbConditionInit" + hbConditionInitIdx;
481                                 // String annotationVarName = "hb_init" + hbConditionInitIdx;
482                                 hbConditionInitIdx++;
483                                 String interfaceNumBefore = Integer
484                                                 .toString(semantics.interface2Num
485                                                                 .get(left.interfaceName)), hbLabelNumBefore = Integer
486                                                 .toString(semantics.hbLabel2Num
487                                                                 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
488                                                 .toString(semantics.interface2Num
489                                                                 .get(right.interfaceName)), hbLabelNumAfter = Integer
490                                                 .toString(semantics.hbLabel2Num
491                                                                 .get(right.hbConditionLabel));
492                                 newCode.add(COMMENT(left + " -> " + right));
493
494                                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
495                                                 structVarName));
496                                 newCode.add(ASSIGN_TO_PTR(structVarName,
497                                                 "interface_num_before", interfaceNumBefore));
498                                 newCode.add(ASSIGN_TO_PTR(structVarName,
499                                                 "hb_condition_num_before", hbLabelNumBefore));
500                                 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
501                                                 interfaceNumAfter));
502                                 newCode.add(ASSIGN_TO_PTR(structVarName,
503                                                 "hb_condition_num_after", hbLabelNumAfter));
504
505                                 // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
506                                 // annotationVarName));
507                                 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
508                                 // SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
509                                 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
510                                 // SPEC_ANNOTATION_FIELD_ANNO, structVarName));
511                                 // newCode.add(ANNOTATE(annotationVarName));
512                         }
513                 }
514                 // Init hb_init_table
515                 newCode.add(COMMENT("Init hb_init_table"));
516                 newCode.add(ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
517                                 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
518                                 + hbConditionInitIdx + ")"));
519                 // Define HB_INIT_TABLE_SIZE
520                 newCode.add(DEFINE("HB_INIT_TABLE_SIZE",
521                                 Integer.toString(hbConditionInitIdx)));
522                 for (int i = 0; i < hbConditionInitIdx; i++) {
523                         newCode.add(ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
524                                         + i));
525                 }
526                 return newCode;
527         }
528
529         public static ArrayList<String> generateEntryPointInitCall() {
530                 ArrayList<String> newCode = new ArrayList<String>();
531                 newCode.add("__sequential_init();");
532                 return newCode;
533         }
534
535         // Only generate the declaration of the wrapper, don't do any renaming
536         // public static ArrayList<String> generateInterfaceWrapperDeclaration(
537         // SemanticsChecker semantics, InterfaceConstruct construct) {
538         // ArrayList<String> newCode = new ArrayList<String>();
539         // FunctionHeader header = getFunctionHeader(semantics, construct);
540         // newCode.add(COMMENT("Declaration of the wrapper"));
541         // String templateStr = header.getTemplateFullStr();
542         // newCode.add(templateStr);
543         // newCode.add(header.getFuncStr() + ";");
544         // newCode.add("");
545         //
546         // return newCode;
547         // }
548
549         public static ArrayList<String> generateInterfaceWrapperDeclaration(
550                         SemanticsChecker semantics, InterfaceConstruct construct) {
551                 FunctionHeader header = getFunctionHeader(semantics, construct);
552                 ArrayList<String> declaration = new ArrayList<String>();
553                 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
554                                 .getDeclaration() + ";");
555                 return declaration;
556         }
557
558         // Only generate the definition of the wrapper, don't do any renaming
559         public static ArrayList<String> generateInterfaceWrapperDefinition(
560                         SemanticsChecker semantics, InterfaceConstruct construct) {
561                 ArrayList<String> newCode = new ArrayList<String>();
562                 String interfaceName = construct.name;
563
564                 FunctionHeader header = getFunctionHeader(semantics, construct);
565                 String interfaceNum = Integer.toString(semantics.interface2Num
566                                 .get(construct.name));
567
568                 newCode.add(header.getTemplateFullStr());
569                 newCode.add(header.getFuncStr() + " {");
570                 // Wrapper function body
571                 newCode.add(COMMENT("Interface begins"));
572                 // Interface begin
573                 String structName = "interface_begin";
574                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
575                                 "interface_begin"));
576                 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
577
578                 String anno = "annotation_interface_begin";
579                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
580                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
581                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
582                 newCode.add(ANNOTATE(semantics, anno));
583                 // Call original renamed function
584                 if (header.returnType.equals("void")) {
585                         newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
586                 } else {
587                         newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
588                                         header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
589                 }
590                 // HB conditions
591                 for (String label : construct.hbConditions.keySet()) {
592                         String condition = construct.hbConditions.get(label);
593                         String hbCondNum = Integer.toString(semantics.hbLabel2Num
594                                         .get(label));
595                         newCode.add("if " + BRACE(condition) + " {");
596                         structName = "hb_condition";
597                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
598                         newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
599
600                         newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
601                         anno = "annotation_hb_condition";
602                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
603                         newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
604                         newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
605                         newCode.add(ANNOTATE(semantics, anno));
606                         newCode.add("}");
607                         newCode.add("");
608                 }
609                 // Also add the true condition if any
610                 if (semantics.containsConditionalInterface(new ConditionalInterface(
611                                 interfaceName, ""))) {
612                         structName = "hb_condition";
613                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
614                         newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
615                         newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
616                         anno = "annotation_hb_condition";
617                         newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
618                         newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
619                         newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
620                         newCode.add(ANNOTATE(semantics, anno));
621                         newCode.add("");
622                 }
623                 // Interface end
624                 String infoStructType = null, infoName = null;
625                 if (!header.returnType.equals("void") || header.args.size() > 0) {
626                         infoStructType = interfaceName + "_info";
627                         infoName = "info";
628                         newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
629                                         BRACE(infoStructType + "*") + " malloc(sizeof("
630                                                         + infoStructType + "))"));
631                         if (!header.returnType.equals("void")) {
632                                 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
633                         }
634                         for (int i = 0; i < header.args.size(); i++) {
635                                 String argName = header.args.get(i).name;
636                                 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
637                         }
638                 } else {
639                         infoName = "NULL";
640                 }
641                 structName = "interface_end";
642                 anno = "annoation_interface_end";
643                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
644                 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
645                 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
646                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
647                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
648                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
649                 newCode.add(ANNOTATE(semantics, anno));
650                 // Return __RET__ if it's not void
651                 if (!header.returnType.equals("void")) {
652                         newCode.add("return " + MACRO_RETURN + ";");
653                 }
654                 // End of the wrapper function
655                 newCode.add("}");
656
657                 // printCode(newCode);
658                 return newCode;
659         }
660
661         // Rename the interface name for declaration or definition
662         public static void renameInterface(SemanticsChecker semantics,
663                         Construct construct) {
664                 FunctionHeader header = getFunctionHeader(semantics, construct);
665                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
666                 int lineNum = construct.beginLineNum;
667                 String headerLine = content.get(construct.beginLineNum);
668                 if (headerLine.startsWith("template")) {
669                         headerLine = content.get(construct.beginLineNum + 1);
670                         lineNum++;
671                 }
672                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
673                                 .toString();
674
675                 if (construct instanceof InterfaceConstruct) {
676                         InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
677                         InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
678                                         .get(iConstruct.name);
679                         if (defineConstruct != null) { // There is a defineConstruct
680                                 newLine = newLine + " ;";
681                                 renameInterface(semantics, defineConstruct);
682                         } else { // This is a declare & define construct
683                                 newLine = newLine + " {";
684                         }
685                 } else {
686                         newLine = newLine + " {";
687                 }
688                 content.set(lineNum, newLine);
689         }
690
691         public static void addAtomicReturn(SemanticsChecker semantics,
692                         Construct construct) {
693                 int lineNum = construct.beginLineNum - 1;
694                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
695                 String oldLine = content.get(lineNum);
696                 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
697                 content.set(lineNum, newLine);
698         }
699
700         public static ArrayList<String> generatePotentialCPDefine(
701                         SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
702                 ArrayList<String> newCode = new ArrayList<String>();
703                 // Add atomic return variable if the predicate accesses to it
704                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
705                         addAtomicReturn(semantics, construct);
706                 }
707                 // Generate redundant header files
708                 newCode.add(COMMENT("Automatically generated code for potential commit point: "
709                                 + construct.label));
710                 newCode.add("");
711                 // Add annotation
712                 newCode.add("if (" + construct.condition + ") {");
713                 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
714                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
715                                 structName));
716                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
717                                 .get(construct.label));
718                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
719                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
720                 newCode.add(ASSIGN_TO_PTR(anno, "type",
721                                 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
722                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
723                 newCode.add(ANNOTATE(semantics, anno));
724                 newCode.add("}");
725                 return newCode;
726         }
727
728         public static ArrayList<String> generateCPDefineCheck(
729                         SemanticsChecker semantics, CPDefineCheckConstruct construct) {
730                 ArrayList<String> newCode = new ArrayList<String>();
731                 // Add atomic return variable if the predicate accesses to it
732                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
733                         addAtomicReturn(semantics, construct);
734                 }
735                 // Generate redundant header files
736                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
737                                 + construct.label));
738                 newCode.add("");
739                 // Add annotation
740                 newCode.add("if (" + construct.condition + ") {");
741                 String structName = "cp_define_check", anno = "annotation_cp_define_check";
742                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
743                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
744                                 .get(construct.label));
745                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
746                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
747                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
748                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
749                 newCode.add(ANNOTATE(semantics, anno));
750                 newCode.add("}");
751                 return newCode;
752         }
753
754         public static ArrayList<String> generateCPDefine(
755                         SemanticsChecker semantics, CPDefineConstruct construct) {
756                 ArrayList<String> newCode = new ArrayList<String>();
757                 // Generate redundant header files
758                 newCode.add(COMMENT("Automatically generated code for commit point define check: "
759                                 + construct.label));
760                 newCode.add("");
761                 // Add annotation
762                 newCode.add("if (" + construct.condition + ") {");
763                 String structName = "cp_define", anno = "annotation_cp_define";
764                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
765                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
766                                 .get(construct.label));
767                 String potentialLabelNum = Integer.toString(semantics.commitPointLabel2Num
768                                 .get(construct.potentialCPLabel));
769                 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
770                 newCode.add(ASSIGN_TO_PTR(structName, "potential_cp_label_num", potentialLabelNum));
771                 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
772                 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
773                 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
774                 newCode.add(ANNOTATE(semantics, anno));
775                 newCode.add("}");
776                 return newCode;
777         }
778 }