1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashSet;
7 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
8 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
9 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
12 import edu.uci.eecs.specCompiler.specExtraction.Construct;
13 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
14 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
16 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
19 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
22 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
24 public class CodeVariables {
25 // C++ code or library
26 public static final String HEADER_STDLIB = "<stdlib.h>";
27 public static final String HEADER_THREADS = "<threads.h>";
28 public static final String HEADER_STDINT = "<stdint.h>";
29 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
30 public static final String ThreadIDType = "thrd_t";
31 public static final String GET_THREAD_ID = "thrd_current";
32 public static final String BOOLEAN = "bool";
33 public static final String UINT64 = "uint64_t";
36 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
37 public static final String HEADER_COMMON = "<common.h>";
38 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
39 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
40 // public static final String CDSAnnotate = "cdsannotate";
41 public static final String CDSAnnotate = "_Z11cdsannotatemPv";
42 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
43 public static final String IDType = "call_id_t";
45 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
46 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
47 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
48 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
49 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
50 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
51 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
52 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
53 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
54 public static final String SPEC_ANNOTATION = "spec_annotation";
55 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
56 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
58 public static final String ANNO_INIT = "anno_init";
59 public static final String ANNO_HB_INIT = "anno_hb_init";
60 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
61 public static final String ANNO_INTERFACE_END = "anno_interface_end";
62 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potentail_cp_define";
63 public static final String ANNO_CP_DEFINE = "anno_cp_define";
64 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
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";
82 public static final String MODEL_MALLOC = "model_malloc";
85 public static final String MACRO_ID = "__ID__";
86 public static final String MACRO_COND = "__COND_SAT__";
87 public static final String MACRO_RETURN = "__RET__";
88 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
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(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(String interfaceName,
184 ArrayList<String> code = new ArrayList<String>();
185 code.add("inline static " + IDType + " " + interfaceName + "_id() {");
186 if (!idCode.equals("")) {
187 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
189 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
191 code.add("return " + MACRO_ID + ";");
196 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
197 InterfaceConstruct construct, FunctionHeader header) {
198 String interfaceName = construct.name;
199 ArrayList<String> code = new ArrayList<String>();
200 code.add("inline static bool " + interfaceName
201 + "_check_action(void *info, " + IDType + " " + MACRO_ID
203 code.add(DECLARE("bool", "check_passed"));
205 if (!header.returnType.equals("void") || header.args.size() != 0) {
206 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
207 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
208 BRACE(infoStructType + "*") + "info"));
209 if (!header.returnType.equals("void")) {
210 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
211 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
213 for (int i = 0; i < header.args.size(); i++) {
214 String type = header.args.get(i).type, var = header.args.get(i).name;
215 code.add((DECLARE_DEFINE(type, var,
216 GET_FIELD_BY_PTR(infoStructName, var))));
221 if (!construct.condition.equals("")) {
222 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
225 if (!construct.check.equals("")) {
226 code.add(ASSIGN("check_passed", construct.check));
227 code.add("if (!check_passed) return false;");
230 if (construct.action.size() > 0) {
231 code.addAll(construct.action);
234 if (!construct.postCheck.equals("")) {
235 code.add(ASSIGN("check_passed", construct.postCheck));
236 code.add("if (!check_passed) return false;");
239 if (construct.postAction.size() > 0) {
240 code.addAll(construct.postAction);
242 // Return true finally
243 code.add("return true;");
250 private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
251 HashSet<String> headers = new HashSet<String>();
252 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
253 File f = semantics.interfaceName2Construct.get(interfaceName).file;
254 headers.addAll(semantics.srcFilesInfo.get(f).headers);
259 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
260 String headLine = funcDefine.get(0);
261 headLine = "inline static " + headLine;
262 funcDefine.set(0, headLine);
265 private static String makeVariablesStatic(VariableDeclaration varDecl) {
266 String res = "static " + varDecl.type + " " + varDecl.name + ";";
270 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
271 Construct construct) {
272 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
273 String headerLine = content.get(construct.beginLineNum), templateLine = null;
274 if (headerLine.startsWith("template")) {
275 templateLine = headerLine;
276 headerLine = content.get(construct.beginLineNum + 1);
278 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
280 FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
281 if (templateLine != null) {
282 ArrayList<VariableDeclaration> templateArgs = SpecParser
283 .getTemplateArg(templateLine);
284 header.setTemplateList(templateArgs);
287 } catch (ParseException e) {
293 public static ArrayList<String> generateGlobalVarDeclaration(
294 SemanticsChecker semantics, GlobalConstruct construct) {
295 ArrayList<String> newCode = new ArrayList<String>();
296 HashSet<String> allHeaders = getAllHeaders(semantics);
298 // All headers needed by the interface decalration
299 newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
300 for (String header : allHeaders) {
301 newCode.add(INCLUDE(header));
304 // Other necessary headers
305 newCode.add(INCLUDE(HEADER_STDLIB));
306 newCode.add(INCLUDE(HEADER_MODELMEMORY));
307 newCode.add(INCLUDE(HEADER_STDINT));
308 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
309 newCode.add(INCLUDE(HEADER_COMMON));
310 newCode.add(INCLUDE(HEADER_SPEC_LIB));
311 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
314 SequentialDefineSubConstruct code = construct.code;
315 // User-defined structs first
316 newCode.add(COMMENT("All other user-defined structs"));
317 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
318 for (int i = 0; i < declareStructs.size(); i++) {
319 ArrayList<String> declareStruct = declareStructs.get(i);
320 newCode.addAll(declareStruct);
323 // User-defined variables
324 ArrayList<VariableDeclaration> varDecls = code.declareVar;
325 for (int i = 0; i < varDecls.size(); i++) {
326 VariableDeclaration varDecl = varDecls.get(i);
327 // Don't forget to make them static
328 newCode.add(makeVariablesStatic(varDecl));
330 // User-defined functions
331 newCode.add(COMMENT("All other user-defined functions"));
332 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
333 for (int i = 0; i < defineFuncs.size(); i++) {
334 ArrayList<String> defineFunc = defineFuncs.get(i);
335 makeFunctionStatic(defineFunc);
336 newCode.addAll(defineFunc);
340 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
341 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
343 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
344 // Define necessary info structure
345 if (!funcHeader.returnType.equals("void")
346 || funcHeader.args.size() > 0) {
347 newCode.add(COMMENT("Definition of interface info struct: "
349 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
350 newCode.add(COMMENT("End of info struct definition: "
355 // Define ID function
356 newCode.add(COMMENT("ID function of interface: " + interfaceName));
357 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
358 newCode.add(COMMENT("End of ID function: " + interfaceName));
361 // Define check_action function
362 newCode.add(COMMENT("Check action function of interface: "
364 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
365 newCode.add(COMMENT("End of check action function: "
369 // Interface function pointer table
370 String interfaceSize = Integer
371 .toString(semantics.interfaceName2Construct.size());
372 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
373 newCode.add(DECLARE("void**", "func_ptr_table"));
374 // Happens-before initialization rules
375 newCode.add(DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
378 newCode.add(COMMENT("Define function for sequential code initialization"));
379 newCode.add("inline static void __sequential_init() {");
380 // Init func_ptr_table
381 newCode.add(COMMENT("Init func_ptr_table"));
382 newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
383 + semantics.interface2Num.size() + " * 2)"));
384 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
385 String interfaceNum = Integer.toString(semantics.interface2Num
386 .get(interfaceName));
387 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
388 "(void*) &" + interfaceName + "_id"));
389 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
390 "(void*) &" + interfaceName + "_check_action"));
392 // Init Happens-before rules table
393 newCode.addAll(generateHBInitAnnotation(semantics));
395 // Pass init info, including function table info & HB rules
396 newCode.add(COMMENT("Pass init info, including function table info & HB rules"));
397 String structName = "anno_init", anno = "init";
398 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
399 newCode.add(ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
400 newCode.add(ASSIGN_TO_PTR(structName, "func_table_size",
402 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
403 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table_size",
404 "HB_INIT_TABLE_SIZE"));
405 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
406 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
407 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
408 newCode.add(ANNOTATE(anno));
411 // Init user-defined variables
412 newCode.addAll(construct.code.initVar);
415 newCode.add(COMMENT("End of Global construct generation in class"));
421 public static ArrayList<String> generateStaticVarDefine(
422 SemanticsChecker semantics, GlobalConstruct construct) {
423 ArrayList<String> newCode = new ArrayList<String>();
424 String className = semantics.getClassName();
425 if (className == null)
426 return newCode; // No need to define any static variables
427 String templateList = semantics.getTemplateStr();
429 if (templateList == null) {
430 varPrefix = className + "::";
432 varPrefix = className + templateList + "::";
434 String templateDecl = semantics.getTemplateFullStr();
435 if (templateList == null) {
436 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
437 newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
438 for (int i = 0; i < construct.code.declareVar.size(); i++) {
439 VariableDeclaration varDecl = construct.code.declareVar.get(i);
440 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
443 newCode.add(templateDecl);
444 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
445 newCode.add(templateDecl);
446 newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
447 for (int i = 0; i < construct.code.declareVar.size(); i++) {
448 VariableDeclaration varDecl = construct.code.declareVar.get(i);
449 newCode.add(templateDecl);
450 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
456 private static ArrayList<String> generateHBInitAnnotation(
457 SemanticsChecker semantics) {
458 ArrayList<String> newCode = new ArrayList<String>();
460 int hbConditionInitIdx = 0;
461 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
462 for (ConditionalInterface right : semantics.getHBConditions().get(
464 String structVarName = "hbConditionInit" + hbConditionInitIdx;
465 // String annotationVarName = "hb_init" + hbConditionInitIdx;
466 hbConditionInitIdx++;
467 String interfaceNumBefore = Integer
468 .toString(semantics.interface2Num
469 .get(left.interfaceName)), hbLabelNumBefore = Integer
470 .toString(semantics.hbLabel2Num
471 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
472 .toString(semantics.interface2Num
473 .get(right.interfaceName)), hbLabelNumAfter = Integer
474 .toString(semantics.hbLabel2Num
475 .get(right.hbConditionLabel));
476 newCode.add(COMMENT(left + " -> " + right));
478 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
480 newCode.add(ASSIGN_TO_PTR(structVarName,
481 "interface_num_before", interfaceNumBefore));
482 newCode.add(ASSIGN_TO_PTR(structVarName,
483 "hb_condition_num_before", hbLabelNumBefore));
484 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
486 newCode.add(ASSIGN_TO_PTR(structVarName,
487 "hb_condition_num_after", hbLabelNumAfter));
489 // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
490 // annotationVarName));
491 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
492 // SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
493 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
494 // SPEC_ANNOTATION_FIELD_ANNO, structVarName));
495 // newCode.add(ANNOTATE(annotationVarName));
498 // Init hb_init_table
499 newCode.add(COMMENT("Init hb_init_table"));
500 newCode.add(ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
501 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
502 + hbConditionInitIdx + ")"));
503 // Define HB_INIT_TABLE_SIZE
504 newCode.add(DEFINE("HB_INIT_TABLE_SIZE",
505 Integer.toString(hbConditionInitIdx)));
506 for (int i = 0; i < hbConditionInitIdx; i++) {
507 newCode.add(ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
513 public static ArrayList<String> generateEntryPointInitCall() {
514 ArrayList<String> newCode = new ArrayList<String>();
515 newCode.add("__sequential_init();");
519 // Only generate the declaration of the wrapper, don't do any renaming
520 public static ArrayList<String> generateInterfaceWrapperDeclaration(
521 SemanticsChecker semantics, InterfaceConstruct construct) {
522 ArrayList<String> newCode = new ArrayList<String>();
523 FunctionHeader header = getFunctionHeader(semantics, construct);
524 newCode.add(COMMENT("Declaration of the wrapper"));
525 String templateStr = header.getTemplateFullStr();
526 newCode.add(templateStr);
527 newCode.add(header.getFuncStr() + ";");
533 // Only generate the definition of the wrapper, don't do any renaming
534 public static ArrayList<String> generateInterfaceWrapperDefinition(
535 SemanticsChecker semantics, InterfaceConstruct construct) {
536 ArrayList<String> newCode = new ArrayList<String>();
537 String interfaceName = construct.name;
538 // Generate necessary header file (might be redundant but never mind)
539 newCode.add(INCLUDE(HEADER_STDLIB));
540 newCode.add(INCLUDE(HEADER_MODELMEMORY));
541 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
542 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
543 newCode.add(INCLUDE(HEADER_SPEC_LIB));
545 FunctionHeader header = getFunctionHeader(semantics, construct);
546 String interfaceNum = Integer.toString(semantics.interface2Num
547 .get(construct.name));
549 newCode.add(header.getTemplateFullStr());
550 newCode.add(header.getFuncStr() + " {");
551 // Wrapper function body
552 newCode.add(COMMENT("Interface begins"));
554 String structName = "interface_begin";
555 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
557 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
558 String anno = "annotation_interface_begin";
559 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
560 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
561 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
562 newCode.add(ANNOTATE(anno));
563 // Call original renamed function
564 if (header.returnType.equals("void")) {
565 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
567 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
568 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
571 for (String label : construct.hbConditions.keySet()) {
572 String condition = construct.hbConditions.get(label);
573 String hbCondNum = Integer.toString(semantics.hbLabel2Num
575 newCode.add("if " + BRACE(condition) + " {");
576 structName = "hb_condition";
577 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
578 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
580 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
581 anno = "annotation_hb_condition";
582 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
583 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
584 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
585 newCode.add(ANNOTATE(anno));
589 // Also add the true condition if any
590 if (semantics.containsConditionalInterface(new ConditionalInterface(
591 interfaceName, ""))) {
592 structName = "hb_condition";
593 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
594 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
595 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
596 anno = "annotation_hb_condition";
597 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
598 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
599 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
600 newCode.add(ANNOTATE(anno));
604 String infoStructType = null, infoName = null;
605 if (!header.returnType.equals("void") || header.args.size() > 0) {
606 infoStructType = interfaceName + "_info";
608 newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
609 BRACE(infoStructType + "*") + " malloc(sizeof("
610 + infoStructType + "))"));
611 if (!header.returnType.equals("void")) {
612 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
614 for (int i = 0; i < header.args.size(); i++) {
615 String argName = header.args.get(i).name;
616 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
621 structName = "interface_end";
622 anno = "annoation_interface_end";
623 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
624 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
625 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
626 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
627 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
628 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
629 newCode.add(ANNOTATE(anno));
630 // Return __RET__ if it's not void
631 if (!header.returnType.equals("void")) {
632 newCode.add("return " + MACRO_RETURN + ";");
634 // End of the wrapper function
637 // printCode(newCode);
641 // Rename the interface name for declaration or definition
642 public static void renameInterface(SemanticsChecker semantics,
643 Construct construct) {
644 FunctionHeader header = getFunctionHeader(semantics, construct);
645 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
646 int lineNum = construct.beginLineNum;
647 String headerLine = content.get(construct.beginLineNum);
648 if (headerLine.startsWith("template")) {
649 headerLine = content.get(construct.beginLineNum + 1);
652 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
655 if (construct instanceof InterfaceConstruct) {
656 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
657 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
658 .get(iConstruct.name);
659 if (defineConstruct != null) { // There is a defineConstruct
660 newLine = newLine + " ;";
661 renameInterface(semantics, defineConstruct);
662 } else { // This is a declare & define construct
663 newLine = newLine + " {";
666 newLine = newLine + " {";
668 content.set(lineNum, newLine);
671 public static void addAtomicReturn(SemanticsChecker semantics,
672 Construct construct) {
673 int lineNum = construct.beginLineNum - 1;
674 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
675 String oldLine = content.get(lineNum);
676 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
677 content.set(lineNum, newLine);
680 public static ArrayList<String> generatePotentialCPDefine(
681 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
682 ArrayList<String> newCode = new ArrayList<String>();
683 // Add atomic return variable if the predicate accesses to it
684 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
685 addAtomicReturn(semantics, construct);
687 // Generate redundant header files
688 newCode.add(COMMENT("Automatically generated code for potential commit point: "
690 newCode.add(COMMENT("Include redundant headers"));
691 newCode.add(INCLUDE(HEADER_STDINT));
692 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
693 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
696 newCode.add("if (" + construct.condition + ") {");
697 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
698 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
700 String labelNum = Integer.toString(semantics.commitPointLabel2Num
701 .get(construct.label));
702 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
703 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
704 newCode.add(ASSIGN_TO_PTR(anno, "type",
705 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
706 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
707 newCode.add(ANNOTATE(anno));
712 public static ArrayList<String> generateCPDefineCheck(
713 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
714 ArrayList<String> newCode = new ArrayList<String>();
715 // Add atomic return variable if the predicate accesses to it
716 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
717 addAtomicReturn(semantics, construct);
719 // Generate redundant header files
720 newCode.add(COMMENT("Automatically generated code for commit point define check: "
722 newCode.add(COMMENT("Include redundant headers"));
723 newCode.add(INCLUDE(HEADER_STDINT));
724 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
727 newCode.add("if (" + construct.condition + ") {");
728 String structName = "cp_define_check", anno = "annotation_cp_define_check";
729 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
730 String labelNum = Integer.toString(semantics.commitPointLabel2Num
731 .get(construct.label));
732 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
733 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
734 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
735 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
736 newCode.add(ANNOTATE(anno));
741 public static ArrayList<String> generateCPDefine(
742 SemanticsChecker semantics, CPDefineConstruct construct) {
743 ArrayList<String> newCode = new ArrayList<String>();
744 // Generate redundant header files
745 newCode.add(COMMENT("Automatically generated code for commit point define check: "
749 newCode.add("if (" + construct.condition + ") {");
750 String structName = "cp_define", anno = "annotation_cp_define";
751 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
752 String labelNum = Integer.toString(semantics.commitPointLabel2Num
753 .get(construct.label));
754 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
755 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
756 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
757 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
758 newCode.add(ANNOTATE(anno));