1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
8 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
9 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
10 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
13 import edu.uci.eecs.specCompiler.specExtraction.Construct;
14 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
15 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
20 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
23 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
25 public class CodeVariables {
26 // C++ code or library
27 public static final String HEADER_STDLIB = "<stdlib.h>";
28 public static final String HEADER_THREADS = "<threads.h>";
29 public static final String HEADER_STDINT = "<stdint.h>";
30 public static final String HEADER_MODELMEMORY = "<model_memory.h>";
31 public static final String ThreadIDType = "thrd_t";
32 public static final String GET_THREAD_ID = "thrd_current";
33 public static final String BOOLEAN = "bool";
34 public static final String UINT64 = "uint64_t";
37 public static final String HEADER_CDSANNOTATE = "<cdsannotate.h>";
38 public static final String HEADER_COMMON = "<common.h>";
39 public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
40 public static final String HEADER_CDSTRACE = "<cdstrace.h>";
41 // public static final String CDSAnnotate = "cdsannotate";
42 public static final String CDSAnnotate = "_Z11cdsannotatemPv";
43 // public static final String CDSAnnotate = "cdsannotate";
44 public static final String CDSAnnotateType = "SPEC_ANALYSIS";
45 public static final String IDType = "call_id_t";
47 public static final String SPEC_ANNO_TYPE = "spec_anno_type";
48 public static final String SPEC_ANNO_TYPE_INIT = "INIT";
49 public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
50 public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
51 public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
52 public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
53 public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
54 public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
55 public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
56 public static final String SPEC_ANNOTATION = "spec_annotation";
57 public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
58 public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
60 public static final String ANNO_INIT = "anno_init";
61 public static final String ANNO_HB_INIT = "anno_hb_init";
62 public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
63 public static final String ANNO_INTERFACE_END = "anno_interface_end";
64 public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
65 public static final String ANNO_CP_DEFINE = "anno_cp_define";
66 public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
67 public static final String ANNO_HB_CONDITION = "anno_hb_condition";
69 // Specification variables
70 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
71 public static final String DEFAULT_ID = "0";
73 // Specification library
74 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
75 public static final String SPEC_QUEUE = "spec_queue";
76 public static final String SPEC_STACK = "spec_stack";
77 public static final String SPEC_DEQUE = "spec_deque";
78 public static final String SPEC_HASHTABLE = "spec_hashtable";
79 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
80 public static final String SPEC_TAG = "spec_tag";
81 public static final String SPEC_TAG_CURRENT = "current";
82 public static final String SPEC_TAG_NEXT = "next";
84 public static final String MODEL_MALLOC = "model_malloc";
87 public static final String MACRO_ID = "__ID__";
88 public static final String MACRO_COND = "__COND_SAT__";
89 public static final String MACRO_RETURN = "__RET__";
90 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
92 public static void printCode(ArrayList<String> code) {
93 for (int i = 0; i < code.size(); i++) {
94 System.out.println(code.get(i));
98 private static String COMMENT(String comment) {
99 return "/* " + comment + " */";
102 private static String INCLUDE(String header) {
103 return "#include " + header;
106 private static String DEFINE(String left, String right) {
107 return "#define " + left + " " + right;
110 private static String UNDEFINE(String macro) {
111 return "#undef " + macro;
114 private static String GET_FIELD_BY_PTR(String ptr, String field) {
115 return ptr + "->" + field;
118 private static String GET_FIELD(String var, String field) {
119 return var + "->" + field;
122 private static String BRACE(String val) {
123 return "(" + val + ")";
126 private static String ASSIGN(String structName, String field, String val) {
127 return structName + "." + field + " = " + val + ";";
130 private static String ASSIGN(String varName, String val) {
131 return varName + " = " + val + ";";
134 private static String ASSIGN_PTR(String structName, String field, String val) {
135 return structName + "." + field + " = &" + val + ";";
138 private static String ASSIGN_TO_PTR(String structName, String field,
140 return structName + "->" + field + " = " + val + ";";
143 private static String ASSIGN_PTR_TO_PTR(String structName, String field,
145 return structName + "->" + field + " = &" + val + ";";
148 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
149 return "struct " + type + " *" + name + " = (struct " + type
150 + "*) malloc(sizeof(struct " + type + "));";
153 private static String DECLARE(String type, String name) {
154 return type + " " + name + ";";
157 private static String DECLARE(VariableDeclaration varDecl) {
158 String type = varDecl.type, name = varDecl.name;
159 return type + " " + name + ";";
162 private static String DECLARE_DEFINE(String type, String var, String val) {
163 return type + " " + var + " = " + val + ";";
166 private static String ANNOTATE(String structName) {
167 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
170 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
171 FunctionHeader header) {
172 ArrayList<String> code = new ArrayList<String>();
173 code.add("typedef struct " + interfaceName + "_info {");
174 if (!header.returnType.equals("void")) {
175 code.add(DECLARE(header.returnType, MACRO_RETURN));
177 for (int i = 0; i < header.args.size(); i++) {
178 code.add(DECLARE(header.args.get(i)));
180 code.add("} " + interfaceName + "_info;");
184 private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
186 ArrayList<String> code = new ArrayList<String>();
187 code.add("inline static " + IDType + " " + interfaceName + "_id() {");
188 if (!idCode.equals("")) {
189 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
191 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
193 code.add("return " + MACRO_ID + ";");
198 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
199 InterfaceConstruct construct, FunctionHeader header) {
200 String interfaceName = construct.name;
201 ArrayList<String> code = new ArrayList<String>();
202 code.add("inline static bool " + interfaceName
203 + "_check_action(void *info, " + IDType + " " + MACRO_ID
205 code.add(DECLARE("bool", "check_passed"));
207 if (!header.returnType.equals("void") || header.args.size() != 0) {
208 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
209 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
210 BRACE(infoStructType + "*") + "info"));
211 if (!header.returnType.equals("void")) {
212 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
213 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
215 for (int i = 0; i < header.args.size(); i++) {
216 String type = header.args.get(i).type, var = header.args.get(i).name;
217 code.add((DECLARE_DEFINE(type, var,
218 GET_FIELD_BY_PTR(infoStructName, var))));
223 if (!construct.condition.equals("")) {
224 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
227 if (!construct.check.equals("")) {
228 code.add(ASSIGN("check_passed", construct.check));
229 code.add("if (!check_passed) return false;");
232 if (construct.action.size() > 0) {
233 code.addAll(construct.action);
236 if (!construct.postCheck.equals("")) {
237 code.add(ASSIGN("check_passed", construct.postCheck));
238 code.add("if (!check_passed) return false;");
241 if (construct.postAction.size() > 0) {
242 code.addAll(construct.postAction);
244 // Return true finally
245 code.add("return true;");
252 public static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
253 HashSet<String> headers = new HashSet<String>();
254 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
255 File f = semantics.interfaceName2Construct.get(interfaceName).file;
256 headers.addAll(semantics.srcFilesInfo.get(f).headers);
258 headers.add(HEADER_STDLIB);
259 headers.add(HEADER_STDINT);
260 headers.add(HEADER_MODELMEMORY);
261 headers.add(HEADER_STDINT);
262 headers.add(HEADER_CDSANNOTATE);
263 headers.add(HEADER_COMMON);
264 headers.add(HEADER_SPEC_LIB);
265 headers.add(HEADER_SPECANNOTATION);
269 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
270 String headLine = funcDefine.get(0);
271 headLine = "inline static " + headLine;
272 funcDefine.set(0, headLine);
275 private static String makeVariablesStatic(VariableDeclaration varDecl) {
276 String res = "static " + varDecl.type + " " + varDecl.name + ";";
280 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
281 Construct construct) {
282 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
283 String headerLine = content.get(construct.beginLineNum), templateLine = null;
284 if (headerLine.startsWith("template")) {
285 templateLine = headerLine;
286 headerLine = content.get(construct.beginLineNum + 1);
288 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
290 FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
291 if (templateLine != null) {
292 ArrayList<VariableDeclaration> templateArgs = SpecParser
293 .getTemplateArg(templateLine);
294 header.setTemplateList(templateArgs);
297 } catch (ParseException e) {
303 public static ArrayList<String> generateGlobalVarDeclaration(
304 SemanticsChecker semantics, GlobalConstruct construct) {
305 ArrayList<String> newCode = new ArrayList<String>();
306 HashSet<String> allHeaders = getAllHeaders(semantics);
309 SequentialDefineSubConstruct code = construct.code;
310 // User-defined structs first
311 newCode.add(COMMENT("All other user-defined structs"));
312 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
313 for (int i = 0; i < declareStructs.size(); i++) {
314 ArrayList<String> declareStruct = declareStructs.get(i);
315 newCode.addAll(declareStruct);
318 // User-defined variables
319 ArrayList<VariableDeclaration> varDecls = code.declareVar;
320 for (int i = 0; i < varDecls.size(); i++) {
321 VariableDeclaration varDecl = varDecls.get(i);
322 // Don't forget to make them static
323 newCode.add(makeVariablesStatic(varDecl));
325 // User-defined functions
326 newCode.add(COMMENT("All other user-defined functions"));
327 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
328 for (int i = 0; i < defineFuncs.size(); i++) {
329 ArrayList<String> defineFunc = defineFuncs.get(i);
330 makeFunctionStatic(defineFunc);
331 newCode.addAll(defineFunc);
335 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
336 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
338 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
339 // Define necessary info structure
340 if (!funcHeader.returnType.equals("void")
341 || funcHeader.args.size() > 0) {
342 newCode.add(COMMENT("Definition of interface info struct: "
344 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
345 newCode.add(COMMENT("End of info struct definition: "
350 // Define ID function
351 newCode.add(COMMENT("ID function of interface: " + interfaceName));
352 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
353 newCode.add(COMMENT("End of ID function: " + interfaceName));
356 // Define check_action function
357 newCode.add(COMMENT("Check action function of interface: "
359 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
360 newCode.add(COMMENT("End of check action function: "
364 // Interface function pointer table
365 String interfaceSize = Integer
366 .toString(semantics.interfaceName2Construct.size());
367 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
368 newCode.add(DECLARE("void**", "func_ptr_table"));
369 // Happens-before initialization rules
370 newCode.add(DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
373 newCode.add(COMMENT("Define function for sequential code initialization"));
374 newCode.add("inline static void __sequential_init() {");
375 // Init func_ptr_table
376 newCode.add(COMMENT("Init func_ptr_table"));
377 newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
378 + semantics.interface2Num.size() + " * 2)"));
379 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
380 String interfaceNum = Integer.toString(semantics.interface2Num
381 .get(interfaceName));
382 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
383 "(void*) &" + interfaceName + "_id"));
384 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
385 "(void*) &" + interfaceName + "_check_action"));
387 // Init Happens-before rules table
388 newCode.addAll(generateHBInitAnnotation(semantics));
390 // Pass init info, including function table info & HB rules
391 newCode.add(COMMENT("Pass init info, including function table info & HB rules"));
392 String structName = "anno_init", anno = "init";
393 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
394 newCode.add(ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
395 newCode.add(ASSIGN_TO_PTR(structName, "func_table_size",
397 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
398 newCode.add(ASSIGN_TO_PTR(structName, "hb_init_table_size",
399 "HB_INIT_TABLE_SIZE"));
400 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
401 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
402 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
403 newCode.add(ANNOTATE(anno));
406 // Init user-defined variables
407 newCode.addAll(construct.code.initVar);
410 newCode.add(COMMENT("End of Global construct generation in class"));
416 public static ArrayList<String> generateStaticVarDefine(
417 SemanticsChecker semantics, GlobalConstruct construct) {
418 ArrayList<String> newCode = new ArrayList<String>();
419 String className = semantics.getClassName();
420 if (className == null)
421 return newCode; // No need to define any static variables
422 String templateList = semantics.getTemplateStr();
424 if (templateList == null) {
425 varPrefix = className + "::";
427 varPrefix = className + templateList + "::";
429 String templateDecl = semantics.getTemplateFullStr();
430 if (templateList == null) {
431 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
432 newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
433 for (int i = 0; i < construct.code.declareVar.size(); i++) {
434 VariableDeclaration varDecl = construct.code.declareVar.get(i);
435 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
438 newCode.add(templateDecl);
439 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
440 newCode.add(templateDecl);
441 newCode.add(DECLARE("void**", varPrefix + "hb_init_table"));
442 for (int i = 0; i < construct.code.declareVar.size(); i++) {
443 VariableDeclaration varDecl = construct.code.declareVar.get(i);
444 newCode.add(templateDecl);
445 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
451 private static ArrayList<String> generateHBInitAnnotation(
452 SemanticsChecker semantics) {
453 ArrayList<String> newCode = new ArrayList<String>();
455 int hbConditionInitIdx = 0;
456 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
457 for (ConditionalInterface right : semantics.getHBConditions().get(
459 String structVarName = "hbConditionInit" + hbConditionInitIdx;
460 // String annotationVarName = "hb_init" + hbConditionInitIdx;
461 hbConditionInitIdx++;
462 String interfaceNumBefore = Integer
463 .toString(semantics.interface2Num
464 .get(left.interfaceName)), hbLabelNumBefore = Integer
465 .toString(semantics.hbLabel2Num
466 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
467 .toString(semantics.interface2Num
468 .get(right.interfaceName)), hbLabelNumAfter = Integer
469 .toString(semantics.hbLabel2Num
470 .get(right.hbConditionLabel));
471 newCode.add(COMMENT(left + " -> " + right));
473 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
475 newCode.add(ASSIGN_TO_PTR(structVarName,
476 "interface_num_before", interfaceNumBefore));
477 newCode.add(ASSIGN_TO_PTR(structVarName,
478 "hb_condition_num_before", hbLabelNumBefore));
479 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
481 newCode.add(ASSIGN_TO_PTR(structVarName,
482 "hb_condition_num_after", hbLabelNumAfter));
484 // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
485 // annotationVarName));
486 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
487 // SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
488 // newCode.add(ASSIGN_TO_PTR(annotationVarName,
489 // SPEC_ANNOTATION_FIELD_ANNO, structVarName));
490 // newCode.add(ANNOTATE(annotationVarName));
493 // Init hb_init_table
494 newCode.add(COMMENT("Init hb_init_table"));
495 newCode.add(ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
496 + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
497 + hbConditionInitIdx + ")"));
498 // Define HB_INIT_TABLE_SIZE
499 newCode.add(DEFINE("HB_INIT_TABLE_SIZE",
500 Integer.toString(hbConditionInitIdx)));
501 for (int i = 0; i < hbConditionInitIdx; i++) {
502 newCode.add(ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
508 public static ArrayList<String> generateEntryPointInitCall() {
509 ArrayList<String> newCode = new ArrayList<String>();
510 newCode.add("__sequential_init();");
514 // Only generate the declaration of the wrapper, don't do any renaming
515 public static ArrayList<String> generateInterfaceWrapperDeclaration(
516 SemanticsChecker semantics, InterfaceConstruct construct) {
517 ArrayList<String> newCode = new ArrayList<String>();
518 FunctionHeader header = getFunctionHeader(semantics, construct);
519 newCode.add(COMMENT("Declaration of the wrapper"));
520 String templateStr = header.getTemplateFullStr();
521 newCode.add(templateStr);
522 newCode.add(header.getFuncStr() + ";");
528 // Only generate the definition of the wrapper, don't do any renaming
529 public static ArrayList<String> generateInterfaceWrapperDefinition(
530 SemanticsChecker semantics, InterfaceConstruct construct) {
531 ArrayList<String> newCode = new ArrayList<String>();
532 String interfaceName = construct.name;
534 FunctionHeader header = getFunctionHeader(semantics, construct);
535 String interfaceNum = Integer.toString(semantics.interface2Num
536 .get(construct.name));
538 newCode.add(header.getTemplateFullStr());
539 newCode.add(header.getFuncStr() + " {");
540 // Wrapper function body
541 newCode.add(COMMENT("Interface begins"));
543 String structName = "interface_begin";
544 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
546 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
547 String anno = "annotation_interface_begin";
548 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
549 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
550 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
551 newCode.add(ANNOTATE(anno));
552 // Call original renamed function
553 if (header.returnType.equals("void")) {
554 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
556 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
557 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
560 for (String label : construct.hbConditions.keySet()) {
561 String condition = construct.hbConditions.get(label);
562 String hbCondNum = Integer.toString(semantics.hbLabel2Num
564 newCode.add("if " + BRACE(condition) + " {");
565 structName = "hb_condition";
566 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
567 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
569 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
570 anno = "annotation_hb_condition";
571 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
572 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
573 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
574 newCode.add(ANNOTATE(anno));
578 // Also add the true condition if any
579 if (semantics.containsConditionalInterface(new ConditionalInterface(
580 interfaceName, ""))) {
581 structName = "hb_condition";
582 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
583 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
584 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
585 anno = "annotation_hb_condition";
586 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
587 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
588 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
589 newCode.add(ANNOTATE(anno));
593 String infoStructType = null, infoName = null;
594 if (!header.returnType.equals("void") || header.args.size() > 0) {
595 infoStructType = interfaceName + "_info";
597 newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
598 BRACE(infoStructType + "*") + " malloc(sizeof("
599 + infoStructType + "))"));
600 if (!header.returnType.equals("void")) {
601 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
603 for (int i = 0; i < header.args.size(); i++) {
604 String argName = header.args.get(i).name;
605 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
610 structName = "interface_end";
611 anno = "annoation_interface_end";
612 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
613 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
614 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
615 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
616 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
617 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
618 newCode.add(ANNOTATE(anno));
619 // Return __RET__ if it's not void
620 if (!header.returnType.equals("void")) {
621 newCode.add("return " + MACRO_RETURN + ";");
623 // End of the wrapper function
626 // printCode(newCode);
630 // Rename the interface name for declaration or definition
631 public static void renameInterface(SemanticsChecker semantics,
632 Construct construct) {
633 FunctionHeader header = getFunctionHeader(semantics, construct);
634 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
635 int lineNum = construct.beginLineNum;
636 String headerLine = content.get(construct.beginLineNum);
637 if (headerLine.startsWith("template")) {
638 headerLine = content.get(construct.beginLineNum + 1);
641 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
644 if (construct instanceof InterfaceConstruct) {
645 InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
646 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
647 .get(iConstruct.name);
648 if (defineConstruct != null) { // There is a defineConstruct
649 newLine = newLine + " ;";
650 renameInterface(semantics, defineConstruct);
651 } else { // This is a declare & define construct
652 newLine = newLine + " {";
655 newLine = newLine + " {";
657 content.set(lineNum, newLine);
660 public static void addAtomicReturn(SemanticsChecker semantics,
661 Construct construct) {
662 int lineNum = construct.beginLineNum - 1;
663 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
664 String oldLine = content.get(lineNum);
665 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
666 content.set(lineNum, newLine);
669 public static ArrayList<String> generatePotentialCPDefine(
670 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
671 ArrayList<String> newCode = new ArrayList<String>();
672 // Add atomic return variable if the predicate accesses to it
673 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
674 addAtomicReturn(semantics, construct);
676 // Generate redundant header files
677 newCode.add(COMMENT("Automatically generated code for potential commit point: "
681 newCode.add("if (" + construct.condition + ") {");
682 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
683 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
685 String labelNum = Integer.toString(semantics.commitPointLabel2Num
686 .get(construct.label));
687 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
688 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
689 newCode.add(ASSIGN_TO_PTR(anno, "type",
690 SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
691 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
692 newCode.add(ANNOTATE(anno));
697 public static ArrayList<String> generateCPDefineCheck(
698 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
699 ArrayList<String> newCode = new ArrayList<String>();
700 // Add atomic return variable if the predicate accesses to it
701 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
702 addAtomicReturn(semantics, construct);
704 // Generate redundant header files
705 newCode.add(COMMENT("Automatically generated code for commit point define check: "
709 newCode.add("if (" + construct.condition + ") {");
710 String structName = "cp_define_check", anno = "annotation_cp_define_check";
711 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
712 String labelNum = Integer.toString(semantics.commitPointLabel2Num
713 .get(construct.label));
714 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
715 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
716 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
717 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
718 newCode.add(ANNOTATE(anno));
723 public static ArrayList<String> generateCPDefine(
724 SemanticsChecker semantics, CPDefineConstruct construct) {
725 ArrayList<String> newCode = new ArrayList<String>();
726 // Generate redundant header files
727 newCode.add(COMMENT("Automatically generated code for commit point define check: "
731 newCode.add("if (" + construct.condition + ") {");
732 String structName = "cp_define", anno = "annotation_cp_define";
733 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
734 String labelNum = Integer.toString(semantics.commitPointLabel2Num
735 .get(construct.label));
736 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
737 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
738 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
739 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
740 newCode.add(ANNOTATE(anno));