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.utilParser.UtilParser;
9 import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
10 import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
14 import edu.uci.eecs.specCompiler.specExtraction.Construct;
15 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
16 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
24 * <p> Defines a list of commonly used constant strings. </p>
28 public class CodeVariables {
29 // C++ code or library
30 public static final String HEADER_STDLIB = "<stdlib.h>";
31 public static final String HEADER_THREADS = "<threads.h>";
32 public static final String HEADER_STDINT = "<stdint.h>";
33 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
34 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
35 public static final String ThreadIDType = "thread_id_t";
36 public static final String BOOLEAN = "bool";
37 public static final String UINT64 = "uint64_t";
40 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
41 public static final String HEADER_COMMON = "<common.h>";
42 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
43 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
44 public static final String CDSAnnotate = "cdsannotate";
45 // public static final String CDSAnnotate = "cdsannotate";
46 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
47 public static final String IDType = "call_id_t";
49 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
50 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
51 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
52 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
53 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
54 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
55 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
56 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
57 public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
58 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
59 public static final String SPEC_ANNOTATION = "spec_annotation";
60 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
61 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
63 public static final String ANNO_INIT = "anno_init";
64 public static final String ANNO_HB_INIT = "anno_hb_init";
65 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
66 public static final String ANNO_INTERFACE_END = "anno_interface_end";
67 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
68 public static final String ANNO_CP_DEFINE = "anno_cp_define";
69 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
70 public static final String ANNO_CP_CLEAR = "anno_cp_clear";
71 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
73 // Specification variables
74 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
75 public static final String DEFAULT_ID = "0";
77 // Specification library
78 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
79 public static final String SPEC_QUEUE = "spec_queue";
80 public static final String SPEC_STACK = "spec_stack";
81 public static final String SPEC_DEQUE = "spec_deque";
82 public static final String SPEC_HASHTABLE = "spec_hashtable";
83 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
84 public static final String SPEC_TAG = "spec_tag";
85 public static final String SPEC_TAG_CURRENT = "current";
86 public static final String SPEC_TAG_NEXT = "next";
89 public static final String MACRO_ID = "__ID__";
90 public static final String MACRO_COND = "__COND_SAT__";
91 public static final String MACRO_RETURN = "__RET__";
92 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
93 public static final String MACRO_THREAD_ID = "__TID__";
95 public static void printCode(ArrayList<String> code) {
96 for (int i = 0; i < code.size(); i++) {
97 System.out.println(code.get(i));
101 private static String COMMENT(String comment) {
102 return "/* " + comment + " */";
105 private static String SHORT_COMMENT(String comment) {
106 return " // " + comment;
109 private static String INCLUDE(String header) {
110 return "#include " + header;
113 private static String DEFINE(String left, String right) {
114 return "#define " + left + " " + right;
117 private static String UNDEFINE(String macro) {
118 return "#undef " + macro;
121 private static String GET_FIELD_BY_PTR(String ptr, String field) {
122 return ptr + "->" + field;
125 private static String GET_FIELD(String var, String field) {
126 return var + "->" + field;
129 private static String BRACE(String val) {
130 return "(" + val + ")";
133 private static String ASSIGN(String structName, String field, String val) {
134 return structName + "." + field + " = " + val + ";";
137 private static String ASSIGN(String varName, String val) {
138 return varName + " = " + val + ";";
141 private static String ASSIGN_PTR(String structName, String field, String val) {
142 return structName + "." + field + " = &" + val + ";";
145 private static String ASSIGN_TO_PTR(String structName, String field,
147 return structName + "->" + field + " = " + val + ";";
150 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
152 return structName + "->" + field + " = &" + val + ";";
155 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
156 return "struct " + type + " *" + name + " = (struct " + type
157 + "*) malloc(sizeof(struct " + type + "));";
160 private static String DECLARE(String type, String name) {
161 return type + " " + name + ";";
164 private static String DECLARE(VariableDeclaration varDecl) {
165 String type = varDecl.type, name = varDecl.name;
166 return type + " " + name + ";";
169 private static String DECLARE_DEFINE(String type, String var, String val) {
170 return type + " " + var + " = " + val + ";";
173 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
174 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
177 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
178 FunctionHeader header) {
179 ArrayList<String> code = new ArrayList<String>();
180 code.add("typedef struct " + interfaceName + "_info {");
181 if (!header.returnType.equals("void")) {
182 code.add(DECLARE(header.returnType, MACRO_RETURN));
184 for (int i = 0; i < header.args.size(); i++) {
185 code.add(DECLARE(header.args.get(i)));
187 code.add("} " + interfaceName + "_info;");
191 private static ArrayList<String> DEFINE_ID_FUNC(
192 InterfaceConstruct construct, FunctionHeader header) {
193 String interfaceName = construct.name;
194 ArrayList<String> code = new ArrayList<String>();
195 String idCode = construct.idCode;
196 code.add("inline static " + IDType + " " + interfaceName + "_id("
197 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
200 if (!header.returnType.equals("void") || header.args.size() != 0) {
201 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
202 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
203 infoStructName, BRACE(infoStructType + "*") + "info"));
204 if (!header.returnType.equals("void")) {
205 code.add((DECLARE_DEFINE("\t" + header.returnType,
207 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
209 for (int i = 0; i < header.args.size(); i++) {
210 String type = header.args.get(i).type, var = header.args.get(i).name;
212 + (DECLARE_DEFINE(type, var,
213 GET_FIELD_BY_PTR(infoStructName, var))));
218 if (!idCode.equals("")) {
219 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
221 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
223 code.add("\treturn " + MACRO_ID + ";");
228 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
229 InterfaceConstruct construct, FunctionHeader header) {
230 String interfaceName = construct.name;
231 ArrayList<String> code = new ArrayList<String>();
232 code.add("inline static bool " + interfaceName
233 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
234 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
235 code.add("\t" + DECLARE("bool", "check_passed"));
237 if (!header.returnType.equals("void") || header.args.size() != 0) {
238 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
240 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
241 BRACE(infoStructType + "*") + "info"));
242 if (!header.returnType.equals("void")) {
244 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
245 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
247 for (int i = 0; i < header.args.size(); i++) {
248 String type = header.args.get(i).type, var = header.args.get(i).name;
250 + (DECLARE_DEFINE(type, var,
251 GET_FIELD_BY_PTR(infoStructName, var))));
256 if (!construct.condition.equals("")) {
258 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
261 if (!construct.check.equals("")) {
262 code.add("\t" + ASSIGN("check_passed", construct.check));
263 code.add("\tif (!check_passed)");
264 code.add("\t\treturn false;");
268 if (construct.action.size() > 0) {
269 addAllCodeWithIndent(code, construct.action, "\t");
272 if (!construct.postCheck.equals("")) {
273 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
274 code.add("\tif (!check_passed)");
275 code.add("\t\treturn false;");
278 if (construct.postAction.size() > 0) {
279 addAllCodeWithIndent(code, construct.postAction, "\t");
281 // Return true finally
282 code.add("\treturn true;");
289 private static void addAllCodeWithIndent(ArrayList<String> allCode,
290 ArrayList<String> target, String indent) {
291 for (int i = 0; i < target.size(); i++) {
292 allCode.add(indent + target.get(i));
296 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
297 HashSet<String> headers = new HashSet<String>();
298 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
299 File f = semantics.interfaceName2Construct.get(interfaceName).file;
300 headers.addAll(semantics.srcFilesInfo.get(f).headers);
302 headers.add(HEADER_STDLIB);
303 headers.add(HEADER_STDINT);
304 headers.add(HEADER_MODELMEMORY);
305 headers.add(HEADER_MODELTYPES);
306 headers.add(HEADER_SPEC_LIB);
307 headers.add(HEADER_STDINT);
308 headers.add(HEADER_CDSANNOTATE);
309 // headers.add(HEADER_COMMON);
310 headers.add(HEADER_SPECANNOTATION);
314 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
315 String headLine = funcDefine.get(0);
316 headLine = "inline static " + headLine;
317 funcDefine.set(0, headLine);
320 private static String makeVariablesStatic(VariableDeclaration varDecl) {
321 String res = "static " + varDecl.type + " " + varDecl.name + ";";
325 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
326 Construct construct) {
327 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
328 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
329 if (headerLine.startsWith("template")) {
330 templateLine = headerLine;
331 headerLine = content.get(construct.beginLineNum + 2);
333 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
335 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
336 if (templateLine != null) {
337 ArrayList<VariableDeclaration> templateArgs = UtilParser
338 .getTemplateArg(templateLine);
339 header.setTemplateList(templateArgs);
342 } catch (ParseException e) {
348 public static ArrayList<String> generateGlobalVarDeclaration(
349 SemanticsChecker semantics, GlobalConstruct construct) {
350 ArrayList<String> newCode = new ArrayList<String>();
351 HashSet<String> allHeaders = getAllHeaders(semantics);
353 SequentialDefineSubConstruct code = construct.code;
354 // User-defined structs first
355 newCode.add(COMMENT("All other user-defined structs"));
356 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
357 for (int i = 0; i < declareStructs.size(); i++) {
358 ArrayList<String> declareStruct = declareStructs.get(i);
359 newCode.addAll(declareStruct);
362 // User-defined variables
363 ArrayList<VariableDeclaration> varDecls = code.declareVar;
364 for (int i = 0; i < varDecls.size(); i++) {
365 VariableDeclaration varDecl = varDecls.get(i);
366 // Don't forget to make them static
367 newCode.add(makeVariablesStatic(varDecl));
369 // User-defined functions
370 newCode.add(COMMENT("All other user-defined functions"));
371 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
372 for (int i = 0; i < defineFuncs.size(); i++) {
373 ArrayList<String> defineFunc = defineFuncs.get(i);
374 makeFunctionStatic(defineFunc);
375 newCode.addAll(defineFunc);
379 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
380 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
382 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
383 // Define necessary info structure
384 if (!funcHeader.returnType.equals("void")
385 || funcHeader.args.size() > 0) {
386 newCode.add(COMMENT("Definition of interface info struct: "
388 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
389 newCode.add(COMMENT("End of info struct definition: "
394 // Define ID function
395 newCode.add(COMMENT("ID function of interface: " + interfaceName));
396 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
397 newCode.add(COMMENT("End of ID function: " + interfaceName));
400 // Define check_action function
401 newCode.add(COMMENT("Check action function of interface: "
403 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
404 newCode.add(COMMENT("End of check action function: "
408 // Interface function pointer table
409 String interfaceSize = Integer
410 .toString(semantics.interfaceName2Construct.size());
411 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
413 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
414 // Happens-before initialization rules
415 // Should make it static
416 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
419 newCode.add(COMMENT("Define function for sequential code initialization"));
420 newCode.add("inline static void __sequential_init() {");
421 // Init func_ptr_table
422 newCode.add("\t" + COMMENT("Init func_ptr_table"));
424 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
425 + semantics.interface2Num.size() + " * 2)"));
426 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
427 String interfaceNum = Integer.toString(semantics.interface2Num
428 .get(interfaceName));
430 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
431 "(void*) &" + interfaceName + "_id"));
433 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
434 "(void*) &" + interfaceName + "_check_action"));
436 // Init Happens-before rules table
437 newCode.addAll(generateHBInitAnnotation(semantics));
439 // Pass init info, including function table info & HB rules
441 + COMMENT("Pass init info, including function table info & HB rules"));
442 String structName = "anno_init", anno = "init";
443 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
445 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
447 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
449 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
451 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
452 "HB_INIT_TABLE_SIZE"));
453 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
454 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
455 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
456 newCode.add("\t" + ANNOTATE(semantics, anno));
459 // Init user-defined variables
460 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
463 newCode.add(COMMENT("End of Global construct generation in class"));
465 // printCode(newCode);
469 public static ArrayList<String> generateStaticVarDefine(
470 SemanticsChecker semantics, GlobalConstruct construct) {
471 ArrayList<String> newCode = new ArrayList<String>();
472 String className = semantics.getClassName();
473 if (className == null)
474 return newCode; // No need to define any static variables
475 String templateList = semantics.getTemplateStr();
477 if (templateList == null) {
478 varPrefix = className + "::";
480 varPrefix = className + templateList + "::";
482 String templateDecl = semantics.getTemplateFullStr();
483 if (templateList == null) {
484 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
485 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
486 for (int i = 0; i < construct.code.declareVar.size(); i++) {
487 VariableDeclaration varDecl = construct.code.declareVar.get(i);
488 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
491 newCode.add(templateDecl);
492 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
493 newCode.add(templateDecl);
494 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
495 for (int i = 0; i < construct.code.declareVar.size(); i++) {
496 VariableDeclaration varDecl = construct.code.declareVar.get(i);
497 newCode.add(templateDecl);
498 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
504 private static ArrayList<String> generateHBInitAnnotation(
505 SemanticsChecker semantics) {
506 ArrayList<String> newCode = new ArrayList<String>();
508 int hbConditionInitIdx = 0;
509 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
510 for (ConditionalInterface right : semantics.getHBConditions().get(
512 String structVarName = "hbConditionInit" + hbConditionInitIdx;
513 // String annotationVarName = "hb_init" + hbConditionInitIdx;
514 hbConditionInitIdx++;
515 String interfaceNumBefore = Integer
516 .toString(semantics.interface2Num
517 .get(left.interfaceName)), hbLabelNumBefore = Integer
518 .toString(semantics.hbLabel2Num
519 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
520 .toString(semantics.interface2Num
521 .get(right.interfaceName)), hbLabelNumAfter = Integer
522 .toString(semantics.hbLabel2Num
523 .get(right.hbConditionLabel));
524 newCode.add("\t" + COMMENT(left + " -> " + right));
527 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
529 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
531 + SHORT_COMMENT(left.interfaceName));
533 + ASSIGN_TO_PTR(structVarName,
534 "hb_condition_num_before", hbLabelNumBefore)
535 + SHORT_COMMENT(left.hbConditionLabel));
537 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
539 + SHORT_COMMENT(right.interfaceName));
541 + ASSIGN_TO_PTR(structVarName,
542 "hb_condition_num_after", hbLabelNumAfter)
543 + SHORT_COMMENT(right.hbConditionLabel));
546 // Init hb_init_table
547 newCode.add("\t" + COMMENT("Init hb_init_table"));
549 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
550 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
551 + hbConditionInitIdx + ")"));
552 // Define HB_INIT_TABLE_SIZE
554 + DEFINE("HB_INIT_TABLE_SIZE",
555 Integer.toString(hbConditionInitIdx)));
556 for (int i = 0; i < hbConditionInitIdx; i++) {
558 + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
563 public static ArrayList<String> generateEntryPointInitCall() {
564 ArrayList<String> newCode = new ArrayList<String>();
565 newCode.add("\t" + "__sequential_init();");
569 public static ArrayList<String> generateInterfaceWrapperDeclaration(
570 SemanticsChecker semantics, InterfaceConstruct construct) {
571 FunctionHeader header = getFunctionHeader(semantics, construct);
572 ArrayList<String> declaration = new ArrayList<String>();
573 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
574 .getDeclaration() + ";");
578 // Only generate the definition of the wrapper, don't do any renaming
579 public static ArrayList<String> generateInterfaceWrapperDefinition(
580 SemanticsChecker semantics, InterfaceConstruct construct) {
581 ArrayList<String> newCode = new ArrayList<String>();
582 String interfaceName = construct.name;
584 FunctionHeader header = getFunctionHeader(semantics, construct);
585 String interfaceNum = Integer.toString(semantics.interface2Num
586 .get(construct.name));
588 newCode.add(header.getTemplateFullStr());
589 newCode.add(header.getFuncStr() + " {");
590 // Wrapper function body
591 newCode.add("\t" + COMMENT("Interface begins"));
593 String structName = "interface_begin";
595 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
598 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
599 + SHORT_COMMENT(construct.name));
601 String anno = "annotation_interface_begin";
602 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
604 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
605 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
606 newCode.add("\t" + ANNOTATE(semantics, anno));
607 // Call original renamed function
608 if (header.returnType.equals("void")) {
609 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
613 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
614 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
617 for (String label : construct.hbConditions.keySet()) {
618 String condition = construct.hbConditions.get(label);
619 String hbCondNum = Integer.toString(semantics.hbLabel2Num
621 newCode.add("\t" + "if " + BRACE(condition) + " {");
622 structName = "hb_condition";
624 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
626 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
627 + SHORT_COMMENT(construct.name));
630 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
631 anno = "annotation_hb_condition";
633 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
635 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
636 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
637 newCode.add("\t\t" + ANNOTATE(semantics, anno));
638 newCode.add("\t" + "}");
641 // Also add the true condition if any
642 if (semantics.containsConditionalInterface(new ConditionalInterface(
643 interfaceName, ""))) {
644 structName = "hb_condition";
646 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
648 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
649 + SHORT_COMMENT(construct.name));
651 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
652 anno = "annotation_hb_condition";
653 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
655 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
656 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
657 newCode.add("\t" + ANNOTATE(semantics, anno));
661 String infoStructType = null, infoName = null;
662 if (!header.returnType.equals("void") || header.args.size() > 0) {
663 infoStructType = interfaceName + "_info";
666 + DECLARE_DEFINE(infoStructType + "*", infoName,
667 BRACE(infoStructType + "*") + " malloc(sizeof("
668 + infoStructType + "))"));
669 if (!header.returnType.equals("void")) {
671 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
673 for (int i = 0; i < header.args.size(); i++) {
674 String argName = header.args.get(i).name;
675 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
680 structName = "interface_end";
681 anno = "annoation_interface_end";
683 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
685 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
686 + SHORT_COMMENT(construct.name));
687 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
688 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
690 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
691 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
692 newCode.add("\t" + ANNOTATE(semantics, anno));
693 // Return __RET__ if it's not void
694 if (!header.returnType.equals("void")) {
695 newCode.add("\t" + "return " + MACRO_RETURN + ";");
697 // End of the wrapper function
700 // printCode(newCode);
704 // Rename the interface name for declaration or definition
705 public static void renameInterface(SemanticsChecker semantics,
706 Construct construct) {
707 FunctionHeader header = getFunctionHeader(semantics, construct);
708 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
709 int lineNum = construct.beginLineNum;
710 String headerLine = content.get(construct.beginLineNum);
711 if (headerLine.startsWith("template")) {
712 headerLine = content.get(construct.beginLineNum + 1);
715 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
717 String oldLine = content.get(lineNum + 1);
719 if (construct instanceof InterfaceConstruct) {
720 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
721 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
722 .get(iConstruct.name);
723 if (defineConstruct != null) { // There is a defineConstruct
724 newLine = newLine + " ;";
725 renameInterface(semantics, defineConstruct);
726 } else { // This is a declare & define construct
727 if (oldLine.indexOf('{') != -1)
728 newLine = newLine + " {";
731 if (oldLine.indexOf('{') != -1)
732 newLine = newLine + " {";
735 content.set(lineNum + 1, newLine);
738 public static void addAtomicReturn(SemanticsChecker semantics,
739 Construct construct) {
740 int lineNum = construct.beginLineNum - 1;
741 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
742 String oldLine = content.get(lineNum);
743 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
744 content.set(lineNum, newLine);
747 public static ArrayList<String> generatePotentialCPDefine(
748 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
749 ArrayList<String> newCode = new ArrayList<String>();
750 // Add atomic return variable if the predicate accesses to it
751 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
752 addAtomicReturn(semantics, construct);
754 // Generate redundant header files
756 + COMMENT("Automatically generated code for potential commit point: "
760 newCode.add("\t" + "if (" + construct.condition + ") {");
761 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
763 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
765 String labelNum = Integer.toString(semantics.commitPointLabel2Num
766 .get(construct.label));
767 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
768 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
770 + ASSIGN_TO_PTR(anno, "type",
771 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
772 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
773 newCode.add("\t\t" + ANNOTATE(semantics, anno));
774 newCode.add("\t" + "}");
778 public static String getCPInterfaceNum(SemanticsChecker semantics, String commitPointLabel) {
779 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
780 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
781 String interfaceName = iConstruct.name;
782 String interfaceNum = Integer.toString(semantics.interface2Num
783 .get(interfaceName));
789 * Commit point define check should be unique to each interface, meaning that they
790 * are not shared between different interfaces
797 public static ArrayList<String> generateCPDefineCheck(
798 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
799 ArrayList<String> newCode = new ArrayList<String>();
800 // Add atomic return variable if the predicate accesses to it
801 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
802 addAtomicReturn(semantics, construct);
804 // Generate redundant header files
806 + COMMENT("Automatically generated code for commit point define check: "
811 newCode.add("\t" + "if (" + construct.condition + ") {");
812 String structName = "cp_define_check", anno = "annotation_cp_define_check";
814 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
815 String labelNum = Integer.toString(semantics.commitPointLabel2Num
816 .get(construct.label));
817 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
818 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
819 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
820 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
822 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
823 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
824 newCode.add("\t\t" + ANNOTATE(semantics, anno));
825 newCode.add("\t" + "}");
831 * Commit point define check should be unique to each interface, meaning that they
832 * are not shared between different interfaces
839 public static ArrayList<String> generateCPClear(
840 SemanticsChecker semantics, CPClearConstruct construct) {
841 ArrayList<String> newCode = new ArrayList<String>();
842 // Add atomic return variable if the predicate accesses to it
843 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
844 addAtomicReturn(semantics, construct);
846 // Generate redundant header files
848 + COMMENT("Automatically generated code for commit point clear: "
853 newCode.add("\t" + "if (" + construct.condition + ") {");
854 String structName = "cp_clear", anno = "annotation_cp_clear";
856 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
857 // String labelNum = Integer.toString(semantics.commitPointLabel2Num
858 // .get(construct.label));
859 // String interfaceNum = getCPInterfaceNum(semantics, construct.label);
860 // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
861 // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
862 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
864 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
865 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
866 newCode.add("\t\t" + ANNOTATE(semantics, anno));
867 newCode.add("\t" + "}");
873 * Commit point define should be unique to each interface, meaning that they
874 * are not shared between different interfaces
881 public static ArrayList<String> generateCPDefine(
882 SemanticsChecker semantics, CPDefineConstruct construct) {
883 ArrayList<String> newCode = new ArrayList<String>();
884 // Generate redundant header files
886 + COMMENT("Automatically generated code for commit point define: "
890 newCode.add("\t" + "if (" + construct.condition + ") {");
891 String structName = "cp_define", anno = "annotation_cp_define";
893 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
894 String labelNum = Integer.toString(semantics.commitPointLabel2Num
895 .get(construct.label));
896 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
897 String potentialLabelNum = Integer
898 .toString(semantics.commitPointLabel2Num
899 .get(construct.potentialCPLabel));
900 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
902 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
904 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
905 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
907 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
908 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
909 newCode.add("\t\t" + ANNOTATE(semantics, anno));
910 newCode.add("\t" + "}");