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