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