more
[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 SHORT_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(SemanticsChecker semantics, 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(
184                         InterfaceConstruct construct, FunctionHeader header) {
185                 String interfaceName = construct.name;
186                 ArrayList<String> code = new ArrayList<String>();
187                 String idCode = construct.idCode;
188                 code.add("inline static " + IDType + " " + interfaceName + "_id("
189                                 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
190
191                 // Read info struct
192                 if (!header.returnType.equals("void") || header.args.size() != 0) {
193                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
194                         code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
195                                         infoStructName, BRACE(infoStructType + "*") + "info"));
196                         if (!header.returnType.equals("void")) {
197                                 code.add((DECLARE_DEFINE("\t" + header.returnType,
198                                                 MACRO_RETURN,
199                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
200                         }
201                         for (int i = 0; i < header.args.size(); i++) {
202                                 String type = header.args.get(i).type, var = header.args.get(i).name;
203                                 code.add("\t"
204                                                 + (DECLARE_DEFINE(type, var,
205                                                                 GET_FIELD_BY_PTR(infoStructName, var))));
206                         }
207                         code.add("");
208                 }
209
210                 if (!idCode.equals("")) {
211                         code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
212                 } else {
213                         code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
214                 }
215                 code.add("\treturn " + MACRO_ID + ";");
216                 code.add("}");
217                 return code;
218         }
219
220         private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
221                         InterfaceConstruct construct, FunctionHeader header) {
222                 String interfaceName = construct.name;
223                 ArrayList<String> code = new ArrayList<String>();
224                 code.add("inline static bool " + interfaceName
225                                 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
226                                 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
227                 code.add("\t" + DECLARE("bool", "check_passed"));
228                 // Read info struct
229                 if (!header.returnType.equals("void") || header.args.size() != 0) {
230                         String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
231                         code.add("\t"
232                                         + DECLARE_DEFINE(infoStructType + "*", infoStructName,
233                                                         BRACE(infoStructType + "*") + "info"));
234                         if (!header.returnType.equals("void")) {
235                                 code.add("\t"
236                                                 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
237                                                                 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
238                         }
239                         for (int i = 0; i < header.args.size(); i++) {
240                                 String type = header.args.get(i).type, var = header.args.get(i).name;
241                                 code.add("\t"
242                                                 + (DECLARE_DEFINE(type, var,
243                                                                 GET_FIELD_BY_PTR(infoStructName, var))));
244                         }
245                         code.add("");
246                 }
247                 // __COND_SAT
248                 if (!construct.condition.equals("")) {
249                         code.add("\t"
250                                         + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
251                 }
252                 // Check
253                 if (!construct.check.equals("")) {
254                         code.add("\t" + ASSIGN("check_passed", construct.check));
255                         code.add("\tif (!check_passed)");
256                         code.add("\t\treturn false;");
257
258                 }
259                 // Action
260                 if (construct.action.size() > 0) {
261                         addAllCodeWithIndent(code, construct.action, "\t");
262                 }
263                 // Post_check
264                 if (!construct.postCheck.equals("")) {
265                         code.add("\t" + ASSIGN("check_passed", construct.postCheck));
266                         code.add("\tif (!check_passed)");
267                         code.add("\t\treturn false;");
268                 }
269                 // Post_action
270                 if (construct.postAction.size() > 0) {
271                         addAllCodeWithIndent(code, construct.postAction, "\t");
272                 }
273                 // Return true finally
274                 code.add("\treturn true;");
275
276                 code.add("}");
277
278                 return code;
279         }
280
281         private static void addAllCodeWithIndent(ArrayList<String> allCode,
282                         ArrayList<String> target, String indent) {
283                 for (int i = 0; i < target.size(); i++) {
284                         allCode.add(indent + target.get(i));
285                 }
286         }
287
288         public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
289                 HashSet<String> headers = new HashSet<String>();
290                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
291                         File f = semantics.interfaceName2Construct.get(interfaceName).file;
292                         headers.addAll(semantics.srcFilesInfo.get(f).headers);
293                 }
294                 headers.add(HEADER_STDLIB);
295                 headers.add(HEADER_STDINT);
296                 headers.add(HEADER_MODELMEMORY);
297                 headers.add(HEADER_MODELTYPES);
298                 headers.add(HEADER_SPEC_LIB);
299                 headers.add(HEADER_STDINT);
300                 headers.add(HEADER_CDSANNOTATE);
301                 // headers.add(HEADER_COMMON);
302                 headers.add(HEADER_SPECANNOTATION);
303                 return headers;
304         }
305
306         private static void makeFunctionStatic(ArrayList<String> funcDefine) {
307                 String headLine = funcDefine.get(0);
308                 headLine = "inline static " + headLine;
309                 funcDefine.set(0, headLine);
310         }
311
312         private static String makeVariablesStatic(VariableDeclaration varDecl) {
313                 String res = "static " + varDecl.type + " " + varDecl.name + ";";
314                 return res;
315         }
316
317         private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
318                         Construct construct) {
319                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
320                 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
321                 if (headerLine.startsWith("template")) {
322                         templateLine = headerLine;
323                         headerLine = content.get(construct.beginLineNum + 2);
324                 }
325                 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
326                 try {
327                         FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
328                         if (templateLine != null) {
329                                 ArrayList<VariableDeclaration> templateArgs = UtilParser
330                                                 .getTemplateArg(templateLine);
331                                 header.setTemplateList(templateArgs);
332                         }
333                         return header;
334                 } catch (ParseException e) {
335                         e.printStackTrace();
336                 }
337                 return null;
338         }
339
340         public static ArrayList<String> generateGlobalVarDeclaration(
341                         SemanticsChecker semantics, GlobalConstruct construct) {
342                 ArrayList<String> newCode = new ArrayList<String>();
343                 HashSet<String> allHeaders = getAllHeaders(semantics);
344
345                 SequentialDefineSubConstruct code = construct.code;
346                 // User-defined structs first
347                 newCode.add(COMMENT("All other user-defined structs"));
348                 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
349                 for (int i = 0; i < declareStructs.size(); i++) {
350                         ArrayList<String> declareStruct = declareStructs.get(i);
351                         newCode.addAll(declareStruct);
352                         newCode.add("");
353                 }
354                 // User-defined variables
355                 ArrayList<VariableDeclaration> varDecls = code.declareVar;
356                 for (int i = 0; i < varDecls.size(); i++) {
357                         VariableDeclaration varDecl = varDecls.get(i);
358                         // Don't forget to make them static
359                         newCode.add(makeVariablesStatic(varDecl));
360                 }
361                 // User-defined functions
362                 newCode.add(COMMENT("All other user-defined functions"));
363                 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
364                 for (int i = 0; i < defineFuncs.size(); i++) {
365                         ArrayList<String> defineFunc = defineFuncs.get(i);
366                         makeFunctionStatic(defineFunc);
367                         newCode.addAll(defineFunc);
368                         newCode.add("");
369                 }
370
371                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
372                         InterfaceConstruct iConstruct = semantics.interfaceName2Construct
373                                         .get(interfaceName);
374                         FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
375                         // Define necessary info structure
376                         if (!funcHeader.returnType.equals("void")
377                                         || funcHeader.args.size() > 0) {
378                                 newCode.add(COMMENT("Definition of interface info struct: "
379                                                 + interfaceName));
380                                 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
381                                 newCode.add(COMMENT("End of info struct definition: "
382                                                 + interfaceName));
383                                 newCode.add("");
384                         }
385
386                         // Define ID function
387                         newCode.add(COMMENT("ID function of interface: " + interfaceName));
388                         newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
389                         newCode.add(COMMENT("End of ID function: " + interfaceName));
390                         newCode.add("");
391
392                         // Define check_action function
393                         newCode.add(COMMENT("Check action function of interface: "
394                                         + interfaceName));
395                         newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
396                         newCode.add(COMMENT("End of check action function: "
397                                         + interfaceName));
398                         newCode.add("");
399                 }
400                 // Interface function pointer table
401                 String interfaceSize = Integer
402                                 .toString(semantics.interfaceName2Construct.size());
403                 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
404                 // Make it static
405                 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
406                 // Happens-before initialization rules
407                 // Should make it static
408                 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
409
410                 newCode.add("");
411                 newCode.add(COMMENT("Define function for sequential code initialization"));
412                 newCode.add("inline static void __sequential_init() {");
413                 // Init func_ptr_table
414                 newCode.add("\t" + COMMENT("Init func_ptr_table"));
415                 newCode.add("\t"
416                                 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
417                                                 + semantics.interface2Num.size() + " * 2)"));
418                 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
419                         String interfaceNum = Integer.toString(semantics.interface2Num
420                                         .get(interfaceName));
421                         newCode.add("\t"
422                                         + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
423                                                         "(void*) &" + interfaceName + "_id"));
424                         newCode.add("\t"
425                                         + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
426                                                         "(void*) &" + interfaceName + "_check_action"));
427                 }
428                 // Init Happens-before rules table
429                 newCode.addAll(generateHBInitAnnotation(semantics));
430
431                 // Pass init info, including function table info & HB rules
432                 newCode.add("\t"
433                                 + COMMENT("Pass init info, including function table info & HB rules"));
434                 String structName = "anno_init", anno = "init";
435                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
436                 newCode.add("\t"
437                                 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
438                 newCode.add("\t"
439                                 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
440                 newCode.add("\t"
441                                 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
442                 newCode.add("\t"
443                                 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
444                                                 "HB_INIT_TABLE_SIZE"));
445                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
446                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
447                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
448                 newCode.add("\t" + ANNOTATE(semantics, anno));
449
450                 newCode.add("");
451                 // Init user-defined variables
452                 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
453
454                 newCode.add("}");
455                 newCode.add(COMMENT("End of Global construct generation in class"));
456
457                 // printCode(newCode);
458                 return newCode;
459         }
460
461         public static ArrayList<String> generateStaticVarDefine(
462                         SemanticsChecker semantics, GlobalConstruct construct) {
463                 ArrayList<String> newCode = new ArrayList<String>();
464                 String className = semantics.getClassName();
465                 if (className == null)
466                         return newCode; // No need to define any static variables
467                 String templateList = semantics.getTemplateStr();
468                 String varPrefix;
469                 if (templateList == null) {
470                         varPrefix = className + "::";
471                 } else {
472                         varPrefix = className + templateList + "::";
473                 }
474                 String templateDecl = semantics.getTemplateFullStr();
475                 if (templateList == null) {
476                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
477                         newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
478                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
479                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
480                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
481                         }
482                 } else {
483                         newCode.add(templateDecl);
484                         newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
485                         newCode.add(templateDecl);
486                         newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
487                         for (int i = 0; i < construct.code.declareVar.size(); i++) {
488                                 VariableDeclaration varDecl = construct.code.declareVar.get(i);
489                                 newCode.add(templateDecl);
490                                 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
491                         }
492                 }
493                 return newCode;
494         }
495
496         private static ArrayList<String> generateHBInitAnnotation(
497                         SemanticsChecker semantics) {
498                 ArrayList<String> newCode = new ArrayList<String>();
499
500                 int hbConditionInitIdx = 0;
501                 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
502                         for (ConditionalInterface right : semantics.getHBConditions().get(
503                                         left)) {
504                                 String structVarName = "hbConditionInit" + hbConditionInitIdx;
505                                 // String annotationVarName = "hb_init" + hbConditionInitIdx;
506                                 hbConditionInitIdx++;
507                                 String interfaceNumBefore = Integer
508                                                 .toString(semantics.interface2Num
509                                                                 .get(left.interfaceName)), hbLabelNumBefore = Integer
510                                                 .toString(semantics.hbLabel2Num
511                                                                 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
512                                                 .toString(semantics.interface2Num
513                                                                 .get(right.interfaceName)), hbLabelNumAfter = Integer
514                                                 .toString(semantics.hbLabel2Num
515                                                                 .get(right.hbConditionLabel));
516                                 newCode.add("\t" + COMMENT(left + " -> " + right));
517
518                                 newCode.add("\t"
519                                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
520                                 newCode.add("\t"
521                                                 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
522                                                                 interfaceNumBefore)
523                                                 + SHORT_COMMENT(left.interfaceName));
524                                 newCode.add("\t"
525                                                 + ASSIGN_TO_PTR(structVarName,
526                                                                 "hb_condition_num_before", hbLabelNumBefore)
527                                                 + SHORT_COMMENT(left.hbConditionLabel));
528                                 newCode.add("\t"
529                                                 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
530                                                                 interfaceNumAfter)
531                                                 + SHORT_COMMENT(right.interfaceName));
532                                 newCode.add("\t"
533                                                 + ASSIGN_TO_PTR(structVarName,
534                                                                 "hb_condition_num_after", hbLabelNumAfter)
535                                                 + SHORT_COMMENT(right.hbConditionLabel));
536                         }
537                 }
538                 // Init hb_init_table
539                 newCode.add("\t" + COMMENT("Init hb_init_table"));
540                 newCode.add("\t"
541                                 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
542                                                 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
543                                                 + hbConditionInitIdx + ")"));
544                 // Define HB_INIT_TABLE_SIZE
545                 newCode.add("\t"
546                                 + DEFINE("HB_INIT_TABLE_SIZE",
547                                                 Integer.toString(hbConditionInitIdx)));
548                 for (int i = 0; i < hbConditionInitIdx; i++) {
549                         newCode.add("\t"
550                                         + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
551                 }
552                 return newCode;
553         }
554
555         public static ArrayList<String> generateEntryPointInitCall() {
556                 ArrayList<String> newCode = new ArrayList<String>();
557                 newCode.add("\t" + "__sequential_init();");
558                 return newCode;
559         }
560
561         public static ArrayList<String> generateInterfaceWrapperDeclaration(
562                         SemanticsChecker semantics, InterfaceConstruct construct) {
563                 FunctionHeader header = getFunctionHeader(semantics, construct);
564                 ArrayList<String> declaration = new ArrayList<String>();
565                 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
566                                 .getDeclaration() + ";");
567                 return declaration;
568         }
569
570         // Only generate the definition of the wrapper, don't do any renaming
571         public static ArrayList<String> generateInterfaceWrapperDefinition(
572                         SemanticsChecker semantics, InterfaceConstruct construct) {
573                 ArrayList<String> newCode = new ArrayList<String>();
574                 String interfaceName = construct.name;
575
576                 FunctionHeader header = getFunctionHeader(semantics, construct);
577                 String interfaceNum = Integer.toString(semantics.interface2Num
578                                 .get(construct.name));
579
580                 newCode.add(header.getTemplateFullStr());
581                 newCode.add(header.getFuncStr() + " {");
582                 // Wrapper function body
583                 newCode.add("\t" + COMMENT("Interface begins"));
584                 // Interface begin
585                 String structName = "interface_begin";
586                 newCode.add("\t"
587                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
588                                                 "interface_begin"));
589                 newCode.add("\t"
590                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
591                                 + SHORT_COMMENT(construct.name));
592
593                 String anno = "annotation_interface_begin";
594                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
595                 newCode.add("\t"
596                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
597                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
598                 newCode.add("\t" + ANNOTATE(semantics, anno));
599                 // Call original renamed function
600                 if (header.returnType.equals("void")) {
601                         newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
602                                         + ";");
603                 } else {
604                         newCode.add("\t"
605                                         + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
606                                                         header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
607                 }
608                 // HB conditions
609                 for (String label : construct.hbConditions.keySet()) {
610                         String condition = construct.hbConditions.get(label);
611                         String hbCondNum = Integer.toString(semantics.hbLabel2Num
612                                         .get(label));
613                         newCode.add("\t" + "if " + BRACE(condition) + " {");
614                         structName = "hb_condition";
615                         newCode.add("\t\t"
616                                         + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
617                         newCode.add("\t\t"
618                                         + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
619                                         + SHORT_COMMENT(construct.name));
620
621                         newCode.add("\t\t"
622                                         + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
623                         anno = "annotation_hb_condition";
624                         newCode.add("\t\t"
625                                         + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
626                         newCode.add("\t\t"
627                                         + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
628                         newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
629                         newCode.add("\t\t" + ANNOTATE(semantics, anno));
630                         newCode.add("\t" + "}");
631                         newCode.add("");
632                 }
633                 // Also add the true condition if any
634                 if (semantics.containsConditionalInterface(new ConditionalInterface(
635                                 interfaceName, ""))) {
636                         structName = "hb_condition";
637                         newCode.add("\t"
638                                         + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
639                         newCode.add("\t"
640                                         + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
641                                         + SHORT_COMMENT(construct.name));
642                         newCode.add("\t"
643                                         + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
644                         anno = "annotation_hb_condition";
645                         newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
646                         newCode.add("\t"
647                                         + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
648                         newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
649                         newCode.add("\t" + ANNOTATE(semantics, anno));
650                         newCode.add("");
651                 }
652                 // Interface end
653                 String infoStructType = null, infoName = null;
654                 if (!header.returnType.equals("void") || header.args.size() > 0) {
655                         infoStructType = interfaceName + "_info";
656                         infoName = "info";
657                         newCode.add("\t"
658                                         + DECLARE_DEFINE(infoStructType + "*", infoName,
659                                                         BRACE(infoStructType + "*") + " malloc(sizeof("
660                                                                         + infoStructType + "))"));
661                         if (!header.returnType.equals("void")) {
662                                 newCode.add("\t"
663                                                 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
664                         }
665                         for (int i = 0; i < header.args.size(); i++) {
666                                 String argName = header.args.get(i).name;
667                                 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
668                         }
669                 } else {
670                         infoName = "NULL";
671                 }
672                 structName = "interface_end";
673                 anno = "annoation_interface_end";
674                 newCode.add("\t"
675                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
676                 newCode.add("\t"
677                                 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
678                                 + SHORT_COMMENT(construct.name));
679                 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
680                 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
681                 newCode.add("\t"
682                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
683                 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
684                 newCode.add("\t" + ANNOTATE(semantics, anno));
685                 // Return __RET__ if it's not void
686                 if (!header.returnType.equals("void")) {
687                         newCode.add("\t" + "return " + MACRO_RETURN + ";");
688                 }
689                 // End of the wrapper function
690                 newCode.add("}");
691
692                 // printCode(newCode);
693                 return newCode;
694         }
695
696         // Rename the interface name for declaration or definition
697         public static void renameInterface(SemanticsChecker semantics,
698                         Construct construct) {
699                 FunctionHeader header = getFunctionHeader(semantics, construct);
700                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
701                 int lineNum = construct.beginLineNum;
702                 String headerLine = content.get(construct.beginLineNum);
703                 if (headerLine.startsWith("template")) {
704                         headerLine = content.get(construct.beginLineNum + 1);
705                         lineNum++;
706                 }
707                 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
708                                 .toString();
709                 String oldLine = content.get(lineNum + 1);
710
711                 if (construct instanceof InterfaceConstruct) {
712                         InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
713                         InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
714                                         .get(iConstruct.name);
715                         if (defineConstruct != null) { // There is a defineConstruct
716                                 newLine = newLine + " ;";
717                                 renameInterface(semantics, defineConstruct);
718                         } else { // This is a declare & define construct
719                                 if (oldLine.indexOf('{') != -1)
720                                         newLine = newLine + " {";
721                         }
722                 } else {
723                         if (oldLine.indexOf('{') != -1)
724                                 newLine = newLine + " {";
725                 }
726
727                 content.set(lineNum + 1, newLine);
728         }
729
730         public static void addAtomicReturn(SemanticsChecker semantics,
731                         Construct construct) {
732                 int lineNum = construct.beginLineNum - 1;
733                 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
734                 String oldLine = content.get(lineNum);
735                 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
736                 content.set(lineNum, newLine);
737         }
738
739         public static ArrayList<String> generatePotentialCPDefine(
740                         SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
741                 ArrayList<String> newCode = new ArrayList<String>();
742                 // Add atomic return variable if the predicate accesses to it
743                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
744                         addAtomicReturn(semantics, construct);
745                 }
746                 // Generate redundant header files
747                 newCode.add("\t"
748                                 + COMMENT("Automatically generated code for potential commit point: "
749                                                 + construct.label));
750                 newCode.add("");
751                 // Add annotation
752                 newCode.add("\t" + "if (" + construct.condition + ") {");
753                 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
754                 newCode.add("\t\t"
755                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
756                                                 structName));
757                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
758                                 .get(construct.label));
759                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
760                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
761                 newCode.add("\t\t"
762                                 + ASSIGN_TO_PTR(anno, "type",
763                                                 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
764                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
765                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
766                 newCode.add("\t" + "}");
767                 return newCode;
768         }
769
770         public static ArrayList<String> generateCPDefineCheck(
771                         SemanticsChecker semantics, CPDefineCheckConstruct construct) {
772                 ArrayList<String> newCode = new ArrayList<String>();
773                 // Add atomic return variable if the predicate accesses to it
774                 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
775                         addAtomicReturn(semantics, construct);
776                 }
777                 // Generate redundant header files
778                 newCode.add("\t"
779                                 + COMMENT("Automatically generated code for commit point define check: "
780                                                 + construct.label));
781                 newCode.add("");
782                 // Add annotation
783                 newCode.add("\t" + "if (" + construct.condition + ") {");
784                 String structName = "cp_define_check", anno = "annotation_cp_define_check";
785                 newCode.add("\t\t"
786                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
787                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
788                                 .get(construct.label));
789                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
790                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
791                 newCode.add("\t\t"
792                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
793                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
794                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
795                 newCode.add("\t" + "}");
796                 return newCode;
797         }
798
799         public static ArrayList<String> generateCPDefine(
800                         SemanticsChecker semantics, CPDefineConstruct construct) {
801                 ArrayList<String> newCode = new ArrayList<String>();
802                 // Generate redundant header files
803                 newCode.add("\t"
804                                 + COMMENT("Automatically generated code for commit point define: "
805                                                 + construct.label));
806                 newCode.add("");
807                 // Add annotation
808                 newCode.add("\t" + "if (" + construct.condition + ") {");
809                 String structName = "cp_define", anno = "annotation_cp_define";
810                 newCode.add("\t\t"
811                                 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
812                 String labelNum = Integer.toString(semantics.commitPointLabel2Num
813                                 .get(construct.label));
814                 String potentialLabelNum = Integer
815                                 .toString(semantics.commitPointLabel2Num
816                                                 .get(construct.potentialCPLabel));
817                 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
818                 newCode.add("\t\t"
819                                 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
820                                                 potentialLabelNum));
821                 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
822                 newCode.add("\t\t"
823                                 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
824                 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
825                 newCode.add("\t\t" + ANNOTATE(semantics, anno));
826                 newCode.add("\t" + "}");
827                 return newCode;
828         }
829 }