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