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