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 ThreadIDType = "thrd_t";
30 public static final String GET_THREAD_ID = "thrd_current";
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 = "_Z11cdsannotatemPv";
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_FUNC_TABLE_INIT = "FUNC_TABLE_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_FUNC_TABLE_INIT = "anno_func_table_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_potentail_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_HB_CONDITION = "anno_hb_condition";
66 // Specification variables
67 public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_";
68 public static final String DEFAULT_ID = "0";
70 // Specification library
71 public static final String HEADER_SPEC_LIB = "<spec_lib.h>";
72 public static final String SPEC_QUEUE = "spec_queue";
73 public static final String SPEC_STACK = "spec_stack";
74 public static final String SPEC_DEQUE = "spec_deque";
75 public static final String SPEC_HASHTABLE = "spec_hashtable";
76 public static final String SPEC_PRIVATE_HASHTABLE = "spec_private_hashtable";
77 public static final String SPEC_TAG = "spec_tag";
78 public static final String SPEC_TAG_CURRENT = "current";
79 public static final String SPEC_TAG_NEXT = "next";
82 public static final String MACRO_ID = "__ID__";
83 public static final String MACRO_COND = "__COND_SAT__";
84 public static final String MACRO_RETURN = "__RET__";
85 public static final String MACRO_ATOMIC_RETURN = "__ATOMIC_RET__";
87 public static void printCode(ArrayList<String> code) {
88 for (int i = 0; i < code.size(); i++) {
89 System.out.println(code.get(i));
93 private static String COMMENT(String comment) {
94 return "/* " + comment + " */";
97 private static String INCLUDE(String header) {
98 return "#include " + header;
101 private static String DEFINE(String left, String right) {
102 return "#define " + left + " " + right;
105 private static String UNDEFINE(String macro) {
106 return "#undef " + macro;
109 private static String GET_FIELD_BY_PTR(String ptr, String field) {
110 return ptr + "->" + field;
113 private static String GET_FIELD(String var, String field) {
114 return var + "->" + field;
117 private static String BRACE(String val) {
118 return "(" + val + ")";
121 private static String ASSIGN(String structName, String field, String val) {
122 return structName + "." + field + " = " + val + ";";
125 private static String ASSIGN(String varName, String val) {
126 return varName + " = " + val + ";";
129 private static String ASSIGN_PTR(String structName, String field, String val) {
130 return structName + "." + field + " = &" + val + ";";
133 private static String ASSIGN_TO_PTR(String structName, String field,
135 return structName + "->" + field + " = " + val + ";";
138 private static String ASSIGN_PTR_TO_PTR(String structName, String field, String val) {
139 return structName + "->" + field + " = &" + val + ";";
142 private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
143 return "struct " + type + " *" + name + " = (struct " + type + "*) malloc(sizeof(struct " + type + "));";
146 private static String DECLARE(String type, String name) {
147 return type + " " + name + ";";
150 private static String DECLARE(VariableDeclaration varDecl) {
151 String type = varDecl.type, name = varDecl.name;
152 return type + " " + name + ";";
155 private static String DECLARE_DEFINE(String type, String var, String val) {
156 return type + " " + var + " = " + val + ";";
159 private static String ANNOTATE(String structName) {
160 return CDSAnnotate + "(" + CDSAnnotateType + ", " + structName + ");";
163 private static ArrayList<String> DEFINE_INFO_STRUCT(String interfaceName,
164 FunctionHeader header) {
165 ArrayList<String> code = new ArrayList<String>();
166 code.add("typedef struct " + interfaceName + "_info {");
167 if (!header.returnType.equals("void")) {
168 code.add(DECLARE(header.returnType, MACRO_RETURN));
170 for (int i = 0; i < header.args.size(); i++) {
171 code.add(DECLARE(header.args.get(i)));
173 code.add("} " + interfaceName + "_info;");
177 private static ArrayList<String> DEFINE_ID_FUNC(String interfaceName,
179 ArrayList<String> code = new ArrayList<String>();
180 code.add("static " + IDType + " " + interfaceName + "_id() {");
181 if (!idCode.equals("")) {
182 code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
184 code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
186 code.add("return " + MACRO_ID + ";");
191 private static ArrayList<String> DEFINE_CHECK_ACTION_FUNC(
192 InterfaceConstruct construct, FunctionHeader header) {
193 String interfaceName = construct.name;
194 ArrayList<String> code = new ArrayList<String>();
195 code.add("static bool " + interfaceName + "_check_action(void *info, "
196 + IDType + " " + MACRO_ID + ") {");
197 code.add(DECLARE("bool", "check_passed"));
199 if (!header.returnType.equals("void") || header.args.size() != 0) {
200 String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
201 code.add(DECLARE_DEFINE(infoStructType + "*", infoStructName,
202 BRACE(infoStructType + "*") + "info"));
203 if (!header.returnType.equals("void")) {
204 code.add((DECLARE_DEFINE(header.returnType, MACRO_RETURN,
205 GET_FIELD_BY_PTR(infoStructName, MACRO_RETURN))));
207 for (int i = 0; i < header.args.size(); i++) {
208 String type = header.args.get(i).type, var = header.args.get(i).name;
209 code.add((DECLARE_DEFINE(type, var,
210 GET_FIELD_BY_PTR(infoStructName, var))));
215 if (!construct.condition.equals("")) {
216 code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
219 if (!construct.check.equals("")) {
220 code.add(ASSIGN("check_passed", construct.check));
221 code.add("if (!check_passed) return false;");
224 if (construct.action.size() > 0) {
225 code.addAll(construct.action);
228 if (!construct.postCheck.equals("")) {
229 code.add(ASSIGN("check_passed", construct.postCheck));
230 code.add("if (!check_passed) return false;");
233 if (construct.postAction.size() > 0) {
234 code.addAll(construct.postAction);
236 // Return true finally
237 code.add("return true;");
244 private static HashSet<String> getAllHeaders(SemanticsChecker semantics) {
245 HashSet<String> headers = new HashSet<String>();
246 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
247 File f = semantics.interfaceName2Construct.get(interfaceName).file;
248 headers.addAll(semantics.srcFilesInfo.get(f).headers);
253 private static void makeFunctionStatic(ArrayList<String> funcDefine) {
254 String headLine = funcDefine.get(0);
255 headLine = "static " + headLine;
256 funcDefine.set(0, headLine);
259 private static String makeVariablesStatic(VariableDeclaration varDecl) {
260 String res = "static " + varDecl.type + " " + varDecl.name + ";";
264 private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
265 Construct construct) {
266 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
267 String headerLine = content.get(construct.beginLineNum);
268 if (headerLine.startsWith("template")) {
269 headerLine = content.get(construct.beginLineNum + 1);
271 headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
273 return SpecParser.parseFuncHeader(headerLine);
274 } catch (ParseException e) {
280 public static ArrayList<String> generateGlobalVarDeclaration(
281 SemanticsChecker semantics, GlobalConstruct construct) {
282 ArrayList<String> newCode = new ArrayList<String>();
283 HashSet<String> allHeaders = getAllHeaders(semantics);
285 // All headers needed by the interface decalration
286 newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
287 for (String header : allHeaders) {
288 newCode.add(INCLUDE(header));
291 // Other necessary headers
292 newCode.add(INCLUDE(HEADER_STDLIB));
293 newCode.add(INCLUDE(HEADER_STDINT));
294 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
295 newCode.add(INCLUDE(HEADER_COMMON));
296 newCode.add(INCLUDE(HEADER_SPEC_LIB));
297 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
300 SequentialDefineSubConstruct code = construct.code;
301 // User-defined structs first
302 newCode.add(COMMENT("All other user-defined structs"));
303 ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
304 for (int i = 0; i < declareStructs.size(); i++) {
305 ArrayList<String> declareStruct = declareStructs.get(i);
306 newCode.addAll(declareStruct);
309 // User-defined variables
310 ArrayList<VariableDeclaration> varDecls = code.declareVar;
311 for (int i = 0; i < varDecls.size(); i++) {
312 VariableDeclaration varDecl = varDecls.get(i);
313 // Don't forget to make them static
314 newCode.add(makeVariablesStatic(varDecl));
316 // User-defined functions
317 newCode.add(COMMENT("All other user-defined functions"));
318 ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
319 for (int i = 0; i < defineFuncs.size(); i++) {
320 ArrayList<String> defineFunc = defineFuncs.get(i);
321 makeFunctionStatic(defineFunc);
322 newCode.addAll(defineFunc);
326 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
327 InterfaceConstruct iConstruct = semantics.interfaceName2Construct
329 FunctionHeader funcHeader = getFunctionHeader(semantics, iConstruct);
330 // Define necessary info structure
331 if (!funcHeader.returnType.equals("void")
332 || funcHeader.args.size() > 0) {
333 newCode.add(COMMENT("Definition of interface info struct: "
335 newCode.addAll(DEFINE_INFO_STRUCT(interfaceName, funcHeader));
336 newCode.add(COMMENT("End of info struct definition: "
341 // Define ID function
342 newCode.add(COMMENT("ID function of interface: " + interfaceName));
343 newCode.addAll(DEFINE_ID_FUNC(interfaceName, iConstruct.idCode));
344 newCode.add(COMMENT("End of ID function: " + interfaceName));
347 // Define check_action function
348 newCode.add(COMMENT("Check action function of interface: "
350 newCode.addAll(DEFINE_CHECK_ACTION_FUNC(iConstruct, funcHeader));
351 newCode.add(COMMENT("End of check action function: "
355 // Interface function pointer table
356 String interfaceSize = Integer
357 .toString(semantics.interfaceName2Construct.size());
358 newCode.add(DEFINE("INTERFACE_SIZE", interfaceSize));
359 newCode.add(DECLARE("void**", "func_ptr_table"));
362 newCode.add(COMMENT("Define function for sequential code initialization"));
363 newCode.add("static void __sequential_init() {");
364 // Init func_ptr_table
365 newCode.add(COMMENT("Init func_ptr_table"));
366 newCode.add(ASSIGN("func_ptr_table",
367 "(void**) malloc(sizeof(void*) * " + semantics.interface2Num.size() + " * 2)"));
368 for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
369 String interfaceNum = Integer.toString(semantics.interface2Num
370 .get(interfaceName));
371 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
372 "(void*) &" + interfaceName + "_id"));
373 newCode.add(ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
374 "(void*) &" + interfaceName + "_check_action"));
377 // Init user-defined variables
378 newCode.addAll(construct.code.initVar);
379 // Pass function table info
380 newCode.add(COMMENT("Pass function table info"));
381 String structName = "anno_func_table_init", anno = "func_init";
382 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_FUNC_TABLE_INIT, structName));
383 newCode.add(ASSIGN_TO_PTR(structName, "size", "INTERFACE_SIZE"));
384 newCode.add(ASSIGN_TO_PTR(structName, "table", "func_ptr_table"));
385 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
386 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_FUNC_TABLE_INIT));
387 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
388 newCode.add(ANNOTATE(anno));
390 // Pass Happens-before relationship
391 newCode.addAll(generateHBInitAnnotation(semantics));
393 newCode.add(COMMENT("End of Global construct generation in class"));
399 public static ArrayList<String> generateStaticVarDefine(
400 SemanticsChecker semantics, GlobalConstruct construct) {
401 ArrayList<String> newCode = new ArrayList<String>();
402 String className = semantics.getClassName();
403 if (className == null)
404 return newCode; // No need to define any static variables
405 String templateList = semantics.getTemplateStr();
407 if (templateList == null) {
408 varPrefix = className + "::";
410 varPrefix = className + templateList + "::";
412 String templateDecl = semantics.getTemplateFullStr();
413 if (templateList == null) {
414 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
415 for (int i = 0; i < construct.code.declareVar.size(); i++) {
416 VariableDeclaration varDecl = construct.code.declareVar.get(i);
417 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
420 newCode.add(templateDecl);
421 newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
422 for (int i = 0; i < construct.code.declareVar.size(); i++) {
423 VariableDeclaration varDecl = construct.code.declareVar.get(i);
424 newCode.add(templateDecl);
425 newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
431 private static ArrayList<String> generateHBInitAnnotation(
432 SemanticsChecker semantics) {
433 ArrayList<String> newCode = new ArrayList<String>();
434 int hbConditionInitIdx = 0;
435 for (ConditionalInterface left : semantics.getHBConditions().keySet()) {
436 for (ConditionalInterface right : semantics.getHBConditions().get(
438 String structVarName = "hbConditionInit" + hbConditionInitIdx;
439 String annotationVarName = "hb_init" + hbConditionInitIdx;
440 hbConditionInitIdx++;
441 String interfaceNumBefore = Integer
442 .toString(semantics.interface2Num
443 .get(left.interfaceName)), hbLabelNumBefore = Integer
444 .toString(semantics.hbLabel2Num
445 .get(left.hbConditionLabel)), interfaceNumAfter = Integer
446 .toString(semantics.interface2Num
447 .get(right.interfaceName)), hbLabelNumAfter = Integer
448 .toString(semantics.hbLabel2Num
449 .get(right.hbConditionLabel));
450 newCode.add(COMMENT(left + " -> " + right));
452 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
453 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_before",
454 interfaceNumBefore));
455 newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_before",
457 newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
459 newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_after",
462 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, annotationVarName));
463 newCode.add(ASSIGN_TO_PTR(annotationVarName,
464 SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
465 newCode.add(ASSIGN_TO_PTR(annotationVarName,
466 SPEC_ANNOTATION_FIELD_ANNO, structVarName));
467 newCode.add(ANNOTATE(annotationVarName));
473 public static ArrayList<String> generateEntryPointInitCall() {
474 ArrayList<String> newCode = new ArrayList<String>(1);
475 newCode.add("__sequential_init();");
479 public static ArrayList<String> generateInterfaceWrapper(
480 SemanticsChecker semantics, InterfaceConstruct construct) {
481 ArrayList<String> newCode = new ArrayList<String>();
482 String interfaceName = construct.name;
483 // Generate necessary header file (might be redundant but never mind)
484 newCode.add(INCLUDE(HEADER_STDLIB));
485 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
486 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
487 newCode.add(INCLUDE(HEADER_SPEC_LIB));
489 FunctionHeader header = getFunctionHeader(semantics, construct);
490 String interfaceNum = Integer.toString(semantics.interface2Num
491 .get(construct.name));
492 // Rename the interface
493 renameInterface(semantics, construct);
494 InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
496 if (defineConstruct != null) {
497 renameInterface(semantics, defineConstruct);
500 // Generate wrapper header
501 // If it's not in a class, we should declare the wrapper function
502 if (semantics.getClassName() == null) {
503 FunctionHeader renamedHeader = header
504 .getRenamedHeader(SPEC_INTERFACE_WRAPPER);
505 newCode.add(COMMENT("Declaration of the wrapper"));
506 newCode.add(renamedHeader + ";");
509 newCode.add(header.toString() + " {");
510 // Wrapper function body
511 newCode.add(COMMENT("Interface begins"));
513 String structName = "interface_begin";
514 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN, "interface_begin"));
515 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
516 String anno = "annotation_interface_begin";
517 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
518 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
519 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
520 newCode.add(ANNOTATE(anno));
521 // Call original renamed function
522 if (header.returnType.equals("void")) {
523 newCode.add(header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
525 newCode.add(DECLARE_DEFINE(header.returnType, MACRO_RETURN,
526 header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
529 for (String label : construct.hbConditions.keySet()) {
530 String condition = construct.hbConditions.get(label);
531 String hbCondNum = Integer.toString(semantics.hbLabel2Num
533 newCode.add("if " + BRACE(condition) + " {");
534 structName = "hb_condition";
535 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
536 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
538 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
539 anno = "annotation_hb_condition";
540 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
541 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
542 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
543 newCode.add(ANNOTATE(anno));
547 // Also add the true condition if any
548 if (semantics.containsConditionalInterface(new ConditionalInterface(interfaceName, ""))) {
549 structName = "hb_condition";
550 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
551 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
552 newCode.add(ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
553 anno = "annotation_hb_condition";
554 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
555 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
556 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
557 newCode.add(ANNOTATE(anno));
561 String infoStructType = null, infoName = null;
562 if (!header.returnType.equals("void") || header.args.size() > 0) {
563 infoStructType = interfaceName + "_info";
565 newCode.add(DECLARE_DEFINE(infoStructType + "*", infoName,
566 BRACE(infoStructType + "*") + " malloc(sizeof("
567 + infoStructType + "))"));
568 if (!header.returnType.equals("void")) {
569 newCode.add(ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
571 for (int i = 0; i < header.args.size(); i++) {
572 String argName = header.args.get(i).name;
573 newCode.add(ASSIGN_TO_PTR(infoName, argName, argName));
578 structName = "interface_end";
579 anno = "annoation_interface_end";
580 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
581 newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
582 newCode.add(ASSIGN_TO_PTR(structName, "info", infoName));
583 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
584 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
585 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
586 newCode.add(ANNOTATE(anno));
587 // Return __RET__ if it's not void
588 if (!header.returnType.equals("void")) {
589 newCode.add("return " + MACRO_RETURN + ";");
591 // End of the wrapper function
594 // printCode(newCode);
598 // Rename the interface depending on if it's declaration or definition
599 public static void renameInterface(SemanticsChecker semantics,
600 Construct construct) {
601 FunctionHeader header = getFunctionHeader(semantics, construct);
602 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
603 int lineNum = construct.beginLineNum;
604 String headerLine = content.get(construct.beginLineNum);
605 if (headerLine.startsWith("template")) {
606 headerLine = content.get(construct.beginLineNum + 1);
609 String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
611 content.set(lineNum, newLine);
614 public static void addAtomicReturn(SemanticsChecker semantics,
615 Construct construct) {
616 int lineNum = construct.beginLineNum - 1;
617 ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
618 String oldLine = content.get(lineNum);
619 String newLine = "uint64_t " + MACRO_ATOMIC_RETURN + " = " + oldLine;
620 content.set(lineNum, newLine);
623 public static ArrayList<String> generatePotentialCPDefine(
624 SemanticsChecker semantics, PotentialCPDefineConstruct construct) {
625 ArrayList<String> newCode = new ArrayList<String>();
626 // Add atomic return variable if the predicate accesses to it
627 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
628 addAtomicReturn(semantics, construct);
630 // Generate redundant header files
631 newCode.add(COMMENT("Automatically generated code for potential commit point: "
633 newCode.add(COMMENT("Include redundant headers"));
634 newCode.add(INCLUDE(HEADER_STDINT));
635 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
636 newCode.add(INCLUDE(HEADER_SPECANNOTATION));
639 newCode.add("if (" + construct.condition + ") {");
640 String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
641 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE, structName));
642 String labelNum = Integer.toString(semantics.commitPointLabel2Num
643 .get(construct.label));
644 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
645 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
646 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
647 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
648 newCode.add(ANNOTATE(anno));
653 public static ArrayList<String> generateCPDefineCheck(
654 SemanticsChecker semantics, CPDefineCheckConstruct construct) {
655 ArrayList<String> newCode = new ArrayList<String>();
656 // Add atomic return variable if the predicate accesses to it
657 if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
658 addAtomicReturn(semantics, construct);
660 // Generate redundant header files
661 newCode.add(COMMENT("Automatically generated code for commit point define check: "
663 newCode.add(COMMENT("Include redundant headers"));
664 newCode.add(INCLUDE(HEADER_STDINT));
665 newCode.add(INCLUDE(HEADER_CDSANNOTATE));
668 newCode.add("if (" + construct.condition + ") {");
669 String structName = "cp_define_check", anno = "annotation_cp_define_check";
670 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
671 String labelNum = Integer.toString(semantics.commitPointLabel2Num
672 .get(construct.label));
673 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
674 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
675 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
676 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
677 newCode.add(ANNOTATE(anno));
682 public static ArrayList<String> generateCPDefine(
683 SemanticsChecker semantics, CPDefineConstruct construct) {
684 ArrayList<String> newCode = new ArrayList<String>();
685 // Generate redundant header files
686 newCode.add(COMMENT("Automatically generated code for commit point define check: "
690 newCode.add("if (" + construct.condition + ") {");
691 String structName = "cp_define", anno = "annotation_cp_define";
692 newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
693 String labelNum = Integer.toString(semantics.commitPointLabel2Num
694 .get(construct.label));
695 newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
696 newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
697 newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
698 newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
699 newCode.add(ANNOTATE(anno));