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