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