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