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