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;
23 public class CodeVariables {
24 // C++ code or library
25 public static final String HEADER_STDLIB = "<stdlib.h>";
26 public static final String HEADER_THREADS = "<threads.h>";
27 public static final String HEADER_STDINT = "<stdint.h>";
28 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
29 public static final String HEADER_MODELTYPES = "<modeltypes.h>";
30 public static final String ThreadIDType = "thread_id_t";
31 public static final String BOOLEAN = "bool";
32 public static final String UINT64 = "uint64_t";
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 = "cdsannotate";
41 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
42 public static final String IDType = "call_id_t";
44 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
45 public static final String SPEC_ANNO_TYPE_INIT = "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";
57 public static final String ANNO_INIT = "anno_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_potential_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_CP_CLEAR = "anno_cp_clear";
65 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
67 // Specification variables
68 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
69 public static final String DEFAULT_ID = "0";
71 // Specification library
72 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
73 public static final String SPEC_QUEUE = "spec_queue";
74 public static final String SPEC_STACK = "spec_stack";
75 public static final String SPEC_DEQUE = "spec_deque";
76 public static final String SPEC_HASHTABLE = "spec_hashtable";
77 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
78 public static final String SPEC_TAG = "spec_tag";
79 public static final String SPEC_TAG_CURRENT = "current";
80 public static final String SPEC_TAG_NEXT = "next";
83 public static final String MACRO_ID = "__ID__";
84 public static final String MACRO_COND = "__COND_SAT__";
85 public static final String MACRO_RETURN = "__RET__";
86 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
87 public static final String MACRO_THREAD_ID = "__TID__";
89 public static void printCode(ArrayList<String> code) {
90 for (int i = 0; i < code.size(); i++) {
91 System.out.println(code.get(i));
95 private static String COMMENT(String comment) {
96 return "/* " + comment + " */";
99 private static String SHORT_COMMENT(String comment) {
100 return " // " + comment;
103 private static String INCLUDE(String header) {
104 return "#include " + header;
107 private static String DEFINE(String left, String right) {
108 return "#define " + left + " " + right;
111 private static String UNDEFINE(String macro) {
112 return "#undef " + macro;
115 private static String GET_FIELD_BY_PTR(String ptr, String field) {
116 return ptr + "->" + field;
119 private static String GET_FIELD(String var, String field) {
120 return var + "->" + field;
123 private static String BRACE(String val) {
124 return "(" + val + ")";
127 private static String ASSIGN(String structName, String field, String val) {
128 return structName + "." + field + " = " + val + ";";
131 private static String ASSIGN(String varName, String val) {
132 return varName + " = " + val + ";";
135 private static String ASSIGN_PTR(String structName, String field, String val) {
136 return structName + "." + field + " = &" + val + ";";
139 private static String ASSIGN_TO_PTR(String structName, String field,
141 return structName + "->" + field + " = " + val + ";";
144 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
146 return structName + "->" + field + " = &" + val + ";";
149 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
150 return "struct " + type + " *" + name + " = (struct " + type
151 + "*) malloc(sizeof(struct " + type + "));";
154 private static String DECLARE(String type, String name) {
155 return type + " " + name + ";";
158 private static String DECLARE(VariableDeclaration varDecl) {
159 String type = varDecl.type, name = varDecl.name;
160 return type + " " + name + ";";
163 private static String DECLARE_DEFINE(String type, String var, String val) {
164 return type + " " + var + " = " + val + ";";
167 private static String ANNOTATE(SemanticsChecker semantics, String structName) {
168 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
171 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
172 FunctionHeader header) {
173 ArrayList<String> code = new ArrayList<String>();
174 code.add("typedef struct " + interfaceName + "_info {");
175 if (!header.returnType.equals("void")) {
176 code.add(DECLARE(header.returnType, MACRO_RETURN));
178 for (int i = 0; i < header.args.size(); i++) {
179 code.add(DECLARE(header.args.get(i)));
181 code.add("} " + interfaceName + "_info;");
185 private static ArrayList<String> DEFINE_ID_FUNC(
186 InterfaceConstruct construct, FunctionHeader header) {
187 String interfaceName = construct.name;
188 ArrayList<String> code = new ArrayList<String>();
189 String idCode = construct.idCode;
190 code.add("inline static " + IDType + " " + interfaceName + "_id("
191 + "void *info, " + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
194 if (!header.returnType.equals("void") || header.args.size() != 0) {
195 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
196 code.add(DECLARE_DEFINE("\t" + infoStructType + "*",
197 infoStructName, BRACE(infoStructType + "*") + "info"));
198 if (!header.returnType.equals("void")) {
199 code.add((DECLARE_DEFINE("\t" + header.returnType,
201 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
203 for (int i = 0; i < header.args.size(); i++) {
204 String type = header.args.get(i).type, var = header.args.get(i).name;
206 + (DECLARE_DEFINE(type, var,
207 GET_FIELD_BY_PTR(infoStructName, var))));
212 if (!idCode.equals("")) {
213 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, idCode));
215 code.add("\t" + DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
217 code.add("\treturn " + MACRO_ID + ";");
222 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
223 InterfaceConstruct construct, FunctionHeader header) {
224 String interfaceName = construct.name;
225 ArrayList<String> code = new ArrayList<String>();
226 code.add("inline static bool " + interfaceName
227 + "_check_action(void *info, " + IDType + " " + MACRO_ID + ", "
228 + ThreadIDType + " " + MACRO_THREAD_ID + ") {");
229 code.add("\t" + DECLARE("bool", "check_passed"));
231 if (!header.returnType.equals("void") || header.args.size() != 0) {
232 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
234 + DECLARE_DEFINE(infoStructType + "*", infoStructName,
235 BRACE(infoStructType + "*") + "info"));
236 if (!header.returnType.equals("void")) {
238 + (DECLARE_DEFINE(header.returnType, MACRO_RETURN,
239 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
241 for (int i = 0; i < header.args.size(); i++) {
242 String type = header.args.get(i).type, var = header.args.get(i).name;
244 + (DECLARE_DEFINE(type, var,
245 GET_FIELD_BY_PTR(infoStructName, var))));
250 if (!construct.condition.equals("")) {
252 + DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
255 if (!construct.check.equals("")) {
256 code.add("\t" + ASSIGN("check_passed", construct.check));
257 code.add("\tif (!check_passed)");
258 code.add("\t\treturn false;");
262 if (construct.action.size() > 0) {
263 addAllCodeWithIndent(code, construct.action, "\t");
266 if (!construct.postCheck.equals("")) {
267 code.add("\t" + ASSIGN("check_passed", construct.postCheck));
268 code.add("\tif (!check_passed)");
269 code.add("\t\treturn false;");
272 if (construct.postAction.size() > 0) {
273 addAllCodeWithIndent(code, construct.postAction, "\t");
275 // Return true finally
276 code.add("\treturn true;");
283 private static void addAllCodeWithIndent(ArrayList<String> allCode,
284 ArrayList<String> target, String indent) {
285 for (int i = 0; i < target.size(); i++) {
286 allCode.add(indent + target.get(i));
290 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
291 HashSet<String> headers = new HashSet<String>();
292 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
293 File f = semantics.interfaceName2Construct.get(interfaceName).file;
294 headers.addAll(semantics.srcFilesInfo.get(f).headers);
296 headers.add(HEADER_STDLIB);
297 headers.add(HEADER_STDINT);
298 headers.add(HEADER_MODELMEMORY);
299 headers.add(HEADER_MODELTYPES);
300 headers.add(HEADER_SPEC_LIB);
301 headers.add(HEADER_STDINT);
302 headers.add(HEADER_CDSANNOTATE);
303 // headers.add(HEADER_COMMON);
304 headers.add(HEADER_SPECANNOTATION);
308 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
309 String headLine = funcDefine.get(0);
310 headLine = "inline static " + headLine;
311 funcDefine.set(0, headLine);
314 private static String makeVariablesStatic(VariableDeclaration varDecl) {
315 String res = "static " + varDecl.type + " " + varDecl.name + ";";
319 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
320 Construct construct) {
321 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
322 String headerLine = content.get(construct.beginLineNum + 1), templateLine = null;
323 if (headerLine.startsWith("template")) {
324 templateLine = headerLine;
325 headerLine = content.get(construct.beginLineNum + 2);
327 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
329 FunctionHeader header = UtilParser.parseFuncHeader(headerLine);
330 if (templateLine != null) {
331 ArrayList<VariableDeclaration> templateArgs = UtilParser
332 .getTemplateArg(templateLine);
333 header.setTemplateList(templateArgs);
336 } catch (ParseException e) {
342 public static ArrayList<String> generateGlobalVarDeclaration(
343 SemanticsChecker semantics, GlobalConstruct construct) {
344 ArrayList<String> newCode = new ArrayList<String>();
345 HashSet<String> allHeaders = getAllHeaders(semantics);
347 SequentialDefineSubConstruct code = construct.code;
348 // User-defined structs first
349 newCode.add(COMMENT("All other user-defined structs"));
350 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
351 for (int i = 0; i < declareStructs.size(); i++) {
352 ArrayList<String> declareStruct = declareStructs.get(i);
353 newCode.addAll(declareStruct);
356 // User-defined variables
357 ArrayList<VariableDeclaration> varDecls = code.declareVar;
358 for (int i = 0; i < varDecls.size(); i++) {
359 VariableDeclaration varDecl = varDecls.get(i);
360 // Don't forget to make them static
361 newCode.add(makeVariablesStatic(varDecl));
363 // User-defined functions
364 newCode.add(COMMENT("All other user-defined functions"));
365 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
366 for (int i = 0; i < defineFuncs.size(); i++) {
367 ArrayList<String> defineFunc = defineFuncs.get(i);
368 makeFunctionStatic(defineFunc);
369 newCode.addAll(defineFunc);
373 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
374 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
376 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
377 // Define necessary info structure
378 if (!funcHeader.returnType.equals("void")
379 || funcHeader.args.size() > 0) {
380 newCode.add(COMMENT("Definition of interface info struct: "
382 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
383 newCode.add(COMMENT("End of info struct definition: "
388 // Define ID function
389 newCode.add(COMMENT("ID function of interface: " + interfaceName));
390 newCode.addAll(DEFINE_ID_FUNC(iConstruct, funcHeader));
391 newCode.add(COMMENT("End of ID function: " + interfaceName));
394 // Define check_action function
395 newCode.add(COMMENT("Check action function of interface: "
397 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
398 newCode.add(COMMENT("End of check action function: "
402 // Interface function pointer table
403 String interfaceSize = Integer
404 .toString(semantics.interfaceName2Construct.size());
405 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
407 newCode.add("static " + DECLARE("void**", "func_ptr_table"));
408 // Happens-before initialization rules
409 // Should make it static
410 newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
413 newCode.add(COMMENT("Define function for sequential code initialization"));
414 newCode.add("inline static void __sequential_init() {");
415 // Init func_ptr_table
416 newCode.add("\t" + COMMENT("Init func_ptr_table"));
418 + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
419 + semantics.interface2Num.size() + " * 2)"));
420 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
421 String interfaceNum = Integer.toString(semantics.interface2Num
422 .get(interfaceName));
424 + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
425 "(void*) &" + interfaceName + "_id"));
427 + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
428 "(void*) &" + interfaceName + "_check_action"));
430 // Init Happens-before rules table
431 newCode.addAll(generateHBInitAnnotation(semantics));
433 // Pass init info, including function table info & HB rules
435 + COMMENT("Pass init info, including function table info & HB rules"));
436 String structName = "anno_init", anno = "init";
437 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
439 + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
441 + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
443 + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
445 + ASSIGN_TO_PTR(structName, "hb_init_table_size",
446 "HB_INIT_TABLE_SIZE"));
447 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
448 newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
449 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
450 newCode.add("\t" + ANNOTATE(semantics, anno));
453 // Init user-defined variables
454 addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
457 newCode.add(COMMENT("End of Global construct generation in class"));
459 // printCode(newCode);
463 public static ArrayList<String> generateStaticVarDefine(
464 SemanticsChecker semantics, GlobalConstruct construct) {
465 ArrayList<String> newCode = new ArrayList<String>();
466 String className = semantics.getClassName();
467 if (className == null)
468 return newCode; // No need to define any static variables
469 String templateList = semantics.getTemplateStr();
471 if (templateList == null) {
472 varPrefix = className + "::";
474 varPrefix = className + templateList + "::";
476 String templateDecl = semantics.getTemplateFullStr();
477 if (templateList == null) {
478 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
479 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
480 for (int i = 0; i < construct.code.declareVar.size(); i++) {
481 VariableDeclaration varDecl = construct.code.declareVar.get(i);
482 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
485 newCode.add(templateDecl);
486 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
487 newCode.add(templateDecl);
488 newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
489 for (int i = 0; i < construct.code.declareVar.size(); i++) {
490 VariableDeclaration varDecl = construct.code.declareVar.get(i);
491 newCode.add(templateDecl);
492 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
498 private static ArrayList<String> generateHBInitAnnotation(
499 SemanticsChecker semantics) {
500 ArrayList<String> newCode = new ArrayList<String>();
502 int hbConditionInitIdx = 0;
503 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
504 for (ConditionalInterface right : semantics.getHBConditions().get(
506 String structVarName = "hbConditionInit" + hbConditionInitIdx;
507 // String annotationVarName = "hb_init" + hbConditionInitIdx;
508 hbConditionInitIdx++;
509 String interfaceNumBefore = Integer
510 .toString(semantics.interface2Num
511 .get(left.interfaceName)), hbLabelNumBefore = Integer
512 .toString(semantics.hbLabel2Num
513 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
514 .toString(semantics.interface2Num
515 .get(right.interfaceName)), hbLabelNumAfter = Integer
516 .toString(semantics.hbLabel2Num
517 .get(right.hbConditionLabel));
518 newCode.add("\t" + COMMENT(left + " -> " + right));
521 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
523 + ASSIGN_TO_PTR(structVarName, "interface_num_before",
525 + SHORT_COMMENT(left.interfaceName));
527 + ASSIGN_TO_PTR(structVarName,
528 "hb_condition_num_before", hbLabelNumBefore)
529 + SHORT_COMMENT(left.hbConditionLabel));
531 + ASSIGN_TO_PTR(structVarName, "interface_num_after",
533 + SHORT_COMMENT(right.interfaceName));
535 + ASSIGN_TO_PTR(structVarName,
536 "hb_condition_num_after", hbLabelNumAfter)
537 + SHORT_COMMENT(right.hbConditionLabel));
540 // Init hb_init_table
541 newCode.add("\t" + COMMENT("Init hb_init_table"));
543 + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
544 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
545 + hbConditionInitIdx + ")"));
546 // Define HB_INIT_TABLE_SIZE
548 + DEFINE("HB_INIT_TABLE_SIZE",
549 Integer.toString(hbConditionInitIdx)));
550 for (int i = 0; i < hbConditionInitIdx; i++) {
552 + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
557 public static ArrayList<String> generateEntryPointInitCall() {
558 ArrayList<String> newCode = new ArrayList<String>();
559 newCode.add("\t" + "__sequential_init();");
563 public static ArrayList<String> generateInterfaceWrapperDeclaration(
564 SemanticsChecker semantics, InterfaceConstruct construct) {
565 FunctionHeader header = getFunctionHeader(semantics, construct);
566 ArrayList<String> declaration = new ArrayList<String>();
567 declaration.add(header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
568 .getDeclaration() + ";");
572 // Only generate the definition of the wrapper, don't do any renaming
573 public static ArrayList<String> generateInterfaceWrapperDefinition(
574 SemanticsChecker semantics, InterfaceConstruct construct) {
575 ArrayList<String> newCode = new ArrayList<String>();
576 String interfaceName = construct.name;
578 FunctionHeader header = getFunctionHeader(semantics, construct);
579 String interfaceNum = Integer.toString(semantics.interface2Num
580 .get(construct.name));
582 newCode.add(header.getTemplateFullStr());
583 newCode.add(header.getFuncStr() + " {");
584 // Wrapper function body
585 newCode.add("\t" + COMMENT("Interface begins"));
587 String structName = "interface_begin";
589 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
592 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
593 + SHORT_COMMENT(construct.name));
595 String anno = "annotation_interface_begin";
596 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
598 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
599 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
600 newCode.add("\t" + ANNOTATE(semantics, anno));
601 // Call original renamed function
602 if (header.returnType.equals("void")) {
603 newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
607 + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
608 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
611 for (String label : construct.hbConditions.keySet()) {
612 String condition = construct.hbConditions.get(label);
613 String hbCondNum = Integer.toString(semantics.hbLabel2Num
615 newCode.add("\t" + "if " + BRACE(condition) + " {");
616 structName = "hb_condition";
618 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
620 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
621 + SHORT_COMMENT(construct.name));
624 + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
625 anno = "annotation_hb_condition";
627 + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
629 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
630 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
631 newCode.add("\t\t" + ANNOTATE(semantics, anno));
632 newCode.add("\t" + "}");
635 // Also add the true condition if any
636 if (semantics.containsConditionalInterface(new ConditionalInterface(
637 interfaceName, ""))) {
638 structName = "hb_condition";
640 + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
642 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
643 + SHORT_COMMENT(construct.name));
645 + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
646 anno = "annotation_hb_condition";
647 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
649 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
650 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
651 newCode.add("\t" + ANNOTATE(semantics, anno));
655 String infoStructType = null, infoName = null;
656 if (!header.returnType.equals("void") || header.args.size() > 0) {
657 infoStructType = interfaceName + "_info";
660 + DECLARE_DEFINE(infoStructType + "*", infoName,
661 BRACE(infoStructType + "*") + " malloc(sizeof("
662 + infoStructType + "))"));
663 if (!header.returnType.equals("void")) {
665 + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
667 for (int i = 0; i < header.args.size(); i++) {
668 String argName = header.args.get(i).name;
669 newCode.add("\t" + ASSIGN_TO_PTR(infoName, argName, argName));
674 structName = "interface_end";
675 anno = "annoation_interface_end";
677 + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
679 + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
680 + SHORT_COMMENT(construct.name));
681 newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
682 newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
684 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
685 newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
686 newCode.add("\t" + ANNOTATE(semantics, anno));
687 // Return __RET__ if it's not void
688 if (!header.returnType.equals("void")) {
689 newCode.add("\t" + "return " + MACRO_RETURN + ";");
691 // End of the wrapper function
694 // printCode(newCode);
698 // Rename the interface name for declaration or definition
699 public static void renameInterface(SemanticsChecker semantics,
700 Construct construct) {
701 FunctionHeader header = getFunctionHeader(semantics, construct);
702 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
703 int lineNum = construct.beginLineNum;
704 String headerLine = content.get(construct.beginLineNum);
705 if (headerLine.startsWith("template")) {
706 headerLine = content.get(construct.beginLineNum + 1);
709 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
711 String oldLine = content.get(lineNum + 1);
713 if (construct instanceof InterfaceConstruct) {
714 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
715 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
716 .get(iConstruct.name);
717 if (defineConstruct != null) { // There is a defineConstruct
718 newLine = newLine + " ;";
719 renameInterface(semantics, defineConstruct);
720 } else { // This is a declare & define construct
721 if (oldLine.indexOf('{') != -1)
722 newLine = newLine + " {";
725 if (oldLine.indexOf('{') != -1)
726 newLine = newLine + " {";
729 content.set(lineNum + 1, newLine);
732 public static void addAtomicReturn(SemanticsChecker semantics,
733 Construct construct) {
734 int lineNum = construct.beginLineNum - 1;
735 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
736 String oldLine = content.get(lineNum);
737 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
738 content.set(lineNum, newLine);
741 public static ArrayList<String> generatePotentialCPDefine(
742 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
743 ArrayList<String> newCode = new ArrayList<String>();
744 // Add atomic return variable if the predicate accesses to it
745 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
746 addAtomicReturn(semantics, construct);
748 // Generate redundant header files
750 + COMMENT("Automatically generated code for potential commit point: "
754 newCode.add("\t" + "if (" + construct.condition + ") {");
755 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
757 + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
759 String labelNum = Integer.toString(semantics.commitPointLabel2Num
760 .get(construct.label));
761 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
762 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
764 + ASSIGN_TO_PTR(anno, "type",
765 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
766 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
767 newCode.add("\t\t" + ANNOTATE(semantics, anno));
768 newCode.add("\t" + "}");
772 public static String getCPInterfaceNum(SemanticsChecker semantics, String commitPointLabel) {
773 HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
774 InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
775 String interfaceName = iConstruct.name;
776 String interfaceNum = Integer.toString(semantics.interface2Num
777 .get(interfaceName));
783 * Commit point define check should be unique to each interface, meaning that they
784 * are not shared between different interfaces
791 public static ArrayList<String> generateCPDefineCheck(
792 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
793 ArrayList<String> newCode = new ArrayList<String>();
794 // Add atomic return variable if the predicate accesses to it
795 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
796 addAtomicReturn(semantics, construct);
798 // Generate redundant header files
800 + COMMENT("Automatically generated code for commit point define check: "
805 newCode.add("\t" + "if (" + construct.condition + ") {");
806 String structName = "cp_define_check", anno = "annotation_cp_define_check";
808 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
809 String labelNum = Integer.toString(semantics.commitPointLabel2Num
810 .get(construct.label));
811 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
812 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
813 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
814 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
816 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
817 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
818 newCode.add("\t\t" + ANNOTATE(semantics, anno));
819 newCode.add("\t" + "}");
825 * Commit point define check should be unique to each interface, meaning that they
826 * are not shared between different interfaces
833 public static ArrayList<String> generateCPClear(
834 SemanticsChecker semantics, CPClearConstruct construct) {
835 ArrayList<String> newCode = new ArrayList<String>();
836 // Add atomic return variable if the predicate accesses to it
837 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
838 addAtomicReturn(semantics, construct);
840 // Generate redundant header files
842 + COMMENT("Automatically generated code for commit point clear: "
847 newCode.add("\t" + "if (" + construct.condition + ") {");
848 String structName = "cp_clear", anno = "annotation_cp_clear";
850 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
851 String labelNum = Integer.toString(semantics.commitPointLabel2Num
852 .get(construct.label));
853 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
854 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
855 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
856 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
858 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
859 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
860 newCode.add("\t\t" + ANNOTATE(semantics, anno));
861 newCode.add("\t" + "}");
867 * Commit point define should be unique to each interface, meaning that they
868 * are not shared between different interfaces
875 public static ArrayList<String> generateCPDefine(
876 SemanticsChecker semantics, CPDefineConstruct construct) {
877 ArrayList<String> newCode = new ArrayList<String>();
878 // Generate redundant header files
880 + COMMENT("Automatically generated code for commit point define: "
884 newCode.add("\t" + "if (" + construct.condition + ") {");
885 String structName = "cp_define", anno = "annotation_cp_define";
887 + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
888 String labelNum = Integer.toString(semantics.commitPointLabel2Num
889 .get(construct.label));
890 String interfaceNum = getCPInterfaceNum(semantics, construct.label);
891 String potentialLabelNum = Integer
892 .toString(semantics.commitPointLabel2Num
893 .get(construct.potentialCPLabel));
894 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
896 + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
898 newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
899 newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
901 + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
902 newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
903 newCode.add("\t\t" + ANNOTATE(semantics, anno));
904 newCode.add("\t" + "}");