1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
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;
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 HEADER_MODELTYPES = "<modeltypes.h>";
32 public static final String ThreadIDType = "thread_id_t";
33 public static final String BOOLEAN = "bool";
34 public static final String UINT64 = "uint64_t";
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";
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";
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";
68 // Specification variables
69 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
70 public static final String DEFAULT_ID = "0";
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";
84 public static final String MACRO_ID = "__ID__";
85 public static final String MACRO_COND = "__COND_SAT__";
86 public static final String MACRO_RETURN = "__RET__";
87 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
88 public static final String MACRO_THREAD_ID = "__TID__";
90 public static void printCode(ArrayList<String> code) {
91 for (int i = 0; i < code.size(); i++) {
92 System.out.println(code.get(i));
96 private static String COMMENT(String comment) {
97 return "/* " + comment + " */";
100 private static String INCLUDE(String header) {
101 return "#include " + header;
104 private static String DEFINE(String left, String right) {
105 return "#define " + left + " " + right;
108 private static String UNDEFINE(String macro) {
109 return "#undef " + macro;
112 private static String GET_FIELD_BY_PTR(String ptr, String field) {
113 return ptr + "->" + field;
116 private static String GET_FIELD(String var, String field) {
117 return var + "->" + field;
120 private static String BRACE(String val) {
121 return "(" + val + ")";
124 private static String ASSIGN(String structName, String field, String val) {
125 return structName + "." + field + " = " + val + ";";
128 private static String ASSIGN(String varName, String val) {
129 return varName + " = " + val + ";";
132 private static String ASSIGN_PTR(String structName, String field, String val) {
133 return structName + "." + field + " = &" + val + ";";
136 private static String ASSIGN_TO_PTR(String structName, String field,
138 return structName + "->" + field + " = " + val + ";";
141 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
143 return structName + "->" + field + " = &" + val + ";";
146 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
147 return "struct " + type + " *" + name + " = (struct " + type
148 + "*) malloc(sizeof(struct " + type + "));";
151 private static String DECLARE(String type, String name) {
152 return type + " " + name + ";";
155 private static String DECLARE(VariableDeclaration varDecl) {
156 String type = varDecl.type, name = varDecl.name;
157 return type + " " + name + ";";
160 private static String DECLARE_DEFINE(String type, String var, String val) {
161 return type + " " + var + " = " + val + ";";
164 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
165 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
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));
175 for (int i = 0; i < header.args.size(); i++) {
176 code.add(DECLARE(header.args.get(i)));
178 code.add("} " + interfaceName + "_info;");
182 private static ArrayList<String> DEFINE_ID_FUNC(
183 InterfaceConstruct construct, FunctionHeader header) {
184 String interfaceName = construct.name;
185 ArrayList<String> code = new ArrayList<String>();
186 String idCode = construct.idCode;
187 code.add("inline static " + IDType + " " + interfaceName + "_id("
188 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
191 if (!header.returnType.equals("void") || header.args.size() != 0) {
192 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
193 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
194 BRACE(infoStructType + "*") + "info"));
195 if (!header.returnType.equals("void")) {
196 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
197 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
199 for (int i = 0; i < header.args.size(); i++) {
200 String type = header.args.get(i).type, var = header.args.get(i).name;
201 code.add((DECLARE_DEFINE(type, var,
202 GET_FIELD_BY_PTR(infoStructName, var))));
207 if (!idCode.equals("")) {
208 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
210 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
212 code.add("return " + MACRO_ID + ";");
217 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
218 InterfaceConstruct construct, FunctionHeader header) {
219 String interfaceName = construct.name;
220 ArrayList<String> code = new ArrayList<String>();
221 code.add("inline static bool " + interfaceName
222 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
223 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
224 code.add(DECLARE("bool", "check_passed"));
226 if (!header.returnType.equals("void") || header.args.size() != 0) {
227 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
228 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
229 BRACE(infoStructType + "*") + "info"));
230 if (!header.returnType.equals("void")) {
231 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
232 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
234 for (int i = 0; i < header.args.size(); i++) {
235 String type = header.args.get(i).type, var = header.args.get(i).name;
236 code.add((DECLARE_DEFINE(type, var,
237 GET_FIELD_BY_PTR(infoStructName, var))));
242 if (!construct.condition.equals("")) {
243 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
246 if (!construct.check.equals("")) {
247 code.add(ASSIGN("check_passed", construct.check));
248 code.add("if (!check_passed) return false;");
251 if (construct.action.size() > 0) {
252 code.addAll(construct.action);
255 if (!construct.postCheck.equals("")) {
256 code.add(ASSIGN("check_passed", construct.postCheck));
257 code.add("if (!check_passed) return false;");
260 if (construct.postAction.size() > 0) {
261 code.addAll(construct.postAction);
263 // Return true finally
264 code.add("return true;");
271 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
272 HashSet<String> headers = new HashSet<String>();
273 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
274 File f = semantics.interfaceName2Construct.get(interfaceName).file;
275 headers.addAll(semantics.srcFilesInfo.get(f).headers);
277 headers.add(HEADER_STDLIB);
278 headers.add(HEADER_STDINT);
279 headers.add(HEADER_MODELMEMORY);
280 headers.add(HEADER_MODELTYPES);
281 headers.add(HEADER_SPEC_LIB);
282 headers.add(HEADER_STDINT);
283 headers.add(HEADER_CDSANNOTATE);
284 // headers.add(HEADER_COMMON);
285 headers.add(HEADER_SPECANNOTATION);
289 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
290 String headLine = funcDefine.get(0);
291 headLine = "inline static " + headLine;
292 funcDefine.set(0, headLine);
295 private static String makeVariablesStatic(VariableDeclaration varDecl) {
296 String res = "static " + varDecl.type + " " + varDecl.name + ";";
300 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
301 Construct construct) {
302 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
303 String headerLine = content.get(construct.beginLineNum), templateLine = null;
304 if (headerLine.startsWith("template")) {
305 templateLine = headerLine;
306 headerLine = content.get(construct.beginLineNum + 1);
308 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
310 FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
311 if (templateLine != null) {
312 ArrayList<VariableDeclaration> templateArgs = SpecParser
313 .getTemplateArg(templateLine);
314 header.setTemplateList(templateArgs);
317 } catch (ParseException e) {
323 public static ArrayList<String> generateGlobalVarDeclaration(
324 SemanticsChecker semantics, GlobalConstruct construct) {
325 ArrayList<String> newCode = new ArrayList<String>();
326 HashSet<String> allHeaders = getAllHeaders(semantics);
328 SequentialDefineSubConstruct code = construct.code;
329 // User-defined structs first
330 newCode.add(COMMENT("All other user-defined structs"));
331 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
332 for (int i = 0; i < declareStructs.size(); i++) {
333 ArrayList<String> declareStruct = declareStructs.get(i);
334 newCode.addAll(declareStruct);
337 // User-defined variables
338 ArrayList<VariableDeclaration> varDecls = code.declareVar;
339 for (int i = 0; i < varDecls.size(); i++) {
340 VariableDeclaration varDecl = varDecls.get(i);
341 // Don't forget to make them static
342 newCode.add(makeVariablesStatic(varDecl));
344 // User-defined functions
345 newCode.add(COMMENT("All other user-defined functions"));
346 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
347 for (int i = 0; i < defineFuncs.size(); i++) {
348 ArrayList<String> defineFunc = defineFuncs.get(i);
349 makeFunctionStatic(defineFunc);
350 newCode.addAll(defineFunc);
354 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
355 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
357 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
358 // Define necessary info structure
359 if (!funcHeader.returnType.equals("void")
360 || funcHeader.args.size() > 0) {
361 newCode.add(COMMENT("Definition of interface info struct: "
363 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
364 newCode.add(COMMENT("End of info struct definition: "
369 // Define ID function
370 newCode.add(COMMENT("ID function of interface: " + interfaceName));
371 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
372 newCode.add(COMMENT("End of ID function: " + interfaceName));
375 // Define check_action function
376 newCode.add(COMMENT("Check action function of interface: "
378 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
379 newCode.add(COMMENT("End of check action function: "
383 // Interface function pointer table
384 String interfaceSize = Integer
385 .toString(semantics.interfaceName2Construct.size());
386 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
388 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
389 // Happens-before initialization rules
390 // Should make it static
391 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
394 newCode.add(COMMENT("Define function for sequential code initialization"));
395 newCode.add("inline static void __sequential_init() {");
396 // Init func_ptr_table
397 newCode.add(COMMENT("Init func_ptr_table"));
398 newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
399 + semantics.interface2Num.size() + " * 2)"));
400 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
401 String interfaceNum = Integer.toString(semantics.interface2Num
402 .get(interfaceName));
403 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
404 "(void*) &" + interfaceName + "_id"));
405 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
406 "(void*) &" + interfaceName + "_check_action"));
408 // Init Happens-before rules table
409 newCode.addAll(generateHBInitAnnotation(semantics));
411 // Pass init info, including function table info & HB rules
412 newCode.add(COMMENT("Pass init info, including function table info & HB rules"));
413 String structName = "anno_init", anno = "init";
414 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
415 newCode.add(ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
416 newCode.add(ASSIGN_TO_PTR(structName, "func_table_size",
418 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
419 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table_size",
420 "HB_INIT_TABLE_SIZE"));
421 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
422 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
423 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
424 newCode.add(ANNOTATE(semantics, anno));
427 // Init user-defined variables
428 newCode.addAll(construct.code.initVar);
431 newCode.add(COMMENT("End of Global construct generation in class"));
437 public static ArrayList<String> generateStaticVarDefine(
438 SemanticsChecker semantics, GlobalConstruct construct) {
439 ArrayList<String> newCode = new ArrayList<String>();
440 String className = semantics.getClassName();
441 if (className == null)
442 return newCode; // No need to define any static variables
443 String templateList = semantics.getTemplateStr();
445 if (templateList == null) {
446 varPrefix = className + "::";
448 varPrefix = className + templateList + "::";
450 String templateDecl = semantics.getTemplateFullStr();
451 if (templateList == null) {
452 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
453 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
454 for (int i = 0; i < construct.code.declareVar.size(); i++) {
455 VariableDeclaration varDecl = construct.code.declareVar.get(i);
456 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
459 newCode.add(templateDecl);
460 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
461 newCode.add(templateDecl);
462 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
463 for (int i = 0; i < construct.code.declareVar.size(); i++) {
464 VariableDeclaration varDecl = construct.code.declareVar.get(i);
465 newCode.add(templateDecl);
466 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
472 private static ArrayList<String> generateHBInitAnnotation(
473 SemanticsChecker semantics) {
474 ArrayList<String> newCode = new ArrayList<String>();
476 int hbConditionInitIdx = 0;
477 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
478 for (ConditionalInterface right : semantics.getHBConditions().get(
480 String structVarName = "hbConditionInit" + hbConditionInitIdx;
481 // String annotationVarName = "hb_init" + hbConditionInitIdx;
482 hbConditionInitIdx++;
483 String interfaceNumBefore = Integer
484 .toString(semantics.interface2Num
485 .get(left.interfaceName)), hbLabelNumBefore = Integer
486 .toString(semantics.hbLabel2Num
487 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
488 .toString(semantics.interface2Num
489 .get(right.interfaceName)), hbLabelNumAfter = Integer
490 .toString(semantics.hbLabel2Num
491 .get(right.hbConditionLabel));
492 newCode.add(COMMENT(left + " -> " + right));
494 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
496 newCode.add(ASSIGN_TO_PTR(structVarName,
497 "interface_num_before", interfaceNumBefore));
498 newCode.add(ASSIGN_TO_PTR(structVarName,
499 "hb_condition_num_before", hbLabelNumBefore));
500 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
502 newCode.add(ASSIGN_TO_PTR(structVarName,
503 "hb_condition_num_after", hbLabelNumAfter));
505 // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
506 // annotationVarName));
507 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
508 // SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
509 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
510 // SPEC_ANNOTATION_FIELD_ANNO, structVarName));
511 // newCode.add(ANNOTATE(annotationVarName));
514 // Init hb_init_table
515 newCode.add(COMMENT("Init hb_init_table"));
516 newCode.add(ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
517 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
518 + hbConditionInitIdx + ")"));
519 // Define HB_INIT_TABLE_SIZE
520 newCode.add(DEFINE("HB_INIT_TABLE_SIZE",
521 Integer.toString(hbConditionInitIdx)));
522 for (int i = 0; i < hbConditionInitIdx; i++) {
523 newCode.add(ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
529 public static ArrayList<String> generateEntryPointInitCall() {
530 ArrayList<String> newCode = new ArrayList<String>();
531 newCode.add("__sequential_init();");
535 // Only generate the declaration of the wrapper, don't do any renaming
536 // public static ArrayList<String> generateInterfaceWrapperDeclaration(
537 // SemanticsChecker semantics, InterfaceConstruct construct) {
538 // ArrayList<String> newCode = new ArrayList<String>();
539 // FunctionHeader header = getFunctionHeader(semantics, construct);
540 // newCode.add(COMMENT("Declaration of the wrapper"));
541 // String templateStr = header.getTemplateFullStr();
542 // newCode.add(templateStr);
543 // newCode.add(header.getFuncStr() + ";");
549 public static ArrayList<String> generateInterfaceWrapperDeclaration(
550 SemanticsChecker semantics, InterfaceConstruct construct) {
551 FunctionHeader header = getFunctionHeader(semantics, construct);
552 ArrayList<String> declaration = new ArrayList<String>();
553 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
554 .getDeclaration() + ";");
558 // Only generate the definition of the wrapper, don't do any renaming
559 public static ArrayList<String> generateInterfaceWrapperDefinition(
560 SemanticsChecker semantics, InterfaceConstruct construct) {
561 ArrayList<String> newCode = new ArrayList<String>();
562 String interfaceName = construct.name;
564 FunctionHeader header = getFunctionHeader(semantics, construct);
565 String interfaceNum = Integer.toString(semantics.interface2Num
566 .get(construct.name));
568 newCode.add(header.getTemplateFullStr());
569 newCode.add(header.getFuncStr() + " {");
570 // Wrapper function body
571 newCode.add(COMMENT("Interface begins"));
573 String structName = "interface_begin";
574 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
576 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
578 String anno = "annotation_interface_begin";
579 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
580 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
581 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
582 newCode.add(ANNOTATE(semantics, anno));
583 // Call original renamed function
584 if (header.returnType.equals("void")) {
585 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
587 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
588 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
591 for (String label : construct.hbConditions.keySet()) {
592 String condition = construct.hbConditions.get(label);
593 String hbCondNum = Integer.toString(semantics.hbLabel2Num
595 newCode.add("if " + BRACE(condition) + " {");
596 structName = "hb_condition";
597 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
598 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
600 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
601 anno = "annotation_hb_condition";
602 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
603 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
604 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
605 newCode.add(ANNOTATE(semantics, anno));
609 // Also add the true condition if any
610 if (semantics.containsConditionalInterface(new ConditionalInterface(
611 interfaceName, ""))) {
612 structName = "hb_condition";
613 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
614 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
615 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
616 anno = "annotation_hb_condition";
617 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
618 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
619 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
620 newCode.add(ANNOTATE(semantics, anno));
624 String infoStructType = null, infoName = null;
625 if (!header.returnType.equals("void") || header.args.size() > 0) {
626 infoStructType = interfaceName + "_info";
628 newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
629 BRACE(infoStructType + "*") + " malloc(sizeof("
630 + infoStructType + "))"));
631 if (!header.returnType.equals("void")) {
632 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
634 for (int i = 0; i < header.args.size(); i++) {
635 String argName = header.args.get(i).name;
636 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
641 structName = "interface_end";
642 anno = "annoation_interface_end";
643 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
644 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
645 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
646 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
647 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
648 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
649 newCode.add(ANNOTATE(semantics, anno));
650 // Return __RET__ if it's not void
651 if (!header.returnType.equals("void")) {
652 newCode.add("return " + MACRO_RETURN + ";");
654 // End of the wrapper function
657 // printCode(newCode);
661 // Rename the interface name for declaration or definition
662 public static void renameInterface(SemanticsChecker semantics,
663 Construct construct) {
664 FunctionHeader header = getFunctionHeader(semantics, construct);
665 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
666 int lineNum = construct.beginLineNum;
667 String headerLine = content.get(construct.beginLineNum);
668 if (headerLine.startsWith("template")) {
669 headerLine = content.get(construct.beginLineNum + 1);
672 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
675 if (construct instanceof InterfaceConstruct) {
676 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
677 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
678 .get(iConstruct.name);
679 if (defineConstruct != null) { // There is a defineConstruct
680 newLine = newLine + " ;";
681 renameInterface(semantics, defineConstruct);
682 } else { // This is a declare & define construct
683 newLine = newLine + " {";
686 newLine = newLine + " {";
688 content.set(lineNum, newLine);
691 public static void addAtomicReturn(SemanticsChecker semantics,
692 Construct construct) {
693 int lineNum = construct.beginLineNum - 1;
694 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
695 String oldLine = content.get(lineNum);
696 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
697 content.set(lineNum, newLine);
700 public static ArrayList<String> generatePotentialCPDefine(
701 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
702 ArrayList<String> newCode = new ArrayList<String>();
703 // Add atomic return variable if the predicate accesses to it
704 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
705 addAtomicReturn(semantics, construct);
707 // Generate redundant header files
708 newCode.add(COMMENT("Automatically generated code for potential commit point: "
712 newCode.add("if (" + construct.condition + ") {");
713 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
714 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
716 String labelNum = Integer.toString(semantics.commitPointLabel2Num
717 .get(construct.label));
718 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
719 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
720 newCode.add(ASSIGN_TO_PTR(anno, "type",
721 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
722 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
723 newCode.add(ANNOTATE(semantics, anno));
728 public static ArrayList<String> generateCPDefineCheck(
729 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
730 ArrayList<String> newCode = new ArrayList<String>();
731 // Add atomic return variable if the predicate accesses to it
732 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
733 addAtomicReturn(semantics, construct);
735 // Generate redundant header files
736 newCode.add(COMMENT("Automatically generated code for commit point define check: "
740 newCode.add("if (" + construct.condition + ") {");
741 String structName = "cp_define_check", anno = "annotation_cp_define_check";
742 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
743 String labelNum = Integer.toString(semantics.commitPointLabel2Num
744 .get(construct.label));
745 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
746 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
747 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
748 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
749 newCode.add(ANNOTATE(semantics, anno));
754 public static ArrayList<String> generateCPDefine(
755 SemanticsChecker semantics, CPDefineConstruct construct) {
756 ArrayList<String> newCode = new ArrayList<String>();
757 // Generate redundant header files
758 newCode.add(COMMENT("Automatically generated code for commit point define check: "
762 newCode.add("if (" + construct.condition + ") {");
763 String structName = "cp_define", anno = "annotation_cp_define";
764 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
765 String labelNum = Integer.toString(semantics.commitPointLabel2Num
766 .get(construct.label));
767 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
768 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
769 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
770 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
771 newCode.add(ANNOTATE(semantics, anno));