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