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