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