more
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index 53f40bbcc3ea6135256f56671b5ac55db22a834d..d0a935c64fca960ca68274895ea2b1ea4759d2d9 100644 (file)
@@ -94,6 +94,10 @@ public class CodeVariables {
                return "/* " + comment + " */";
        }
 
+       private static String SHORT_COMMENT(String comment) {
+               return " // " + comment;
+       }
+
        private static String INCLUDE(String header) {
                return "#include " + header;
        }
@@ -408,29 +412,36 @@ public class CodeVariables {
                newCode.add("inline static void __sequential_init() {");
                // Init func_ptr_table
                newCode.add("\t" + COMMENT("Init func_ptr_table"));
-               newCode.add("\t" + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
-                               + semantics.interface2Num.size() + " * 2)"));
+               newCode.add("\t"
+                               + ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
+                                               + semantics.interface2Num.size() + " * 2)"));
                for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
                        String interfaceNum = Integer.toString(semantics.interface2Num
                                        .get(interfaceName));
-                       newCode.add("\t" + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
-                                       "(void*) &" + interfaceName + "_id"));
-                       newCode.add("\t" + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
-                                       "(void*) &" + interfaceName + "_check_action"));
+                       newCode.add("\t"
+                                       + ASSIGN("func_ptr_table[2 * " + interfaceNum + "]",
+                                                       "(void*) &" + interfaceName + "_id"));
+                       newCode.add("\t"
+                                       + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
+                                                       "(void*) &" + interfaceName + "_check_action"));
                }
                // Init Happens-before rules table
                newCode.addAll(generateHBInitAnnotation(semantics));
 
                // Pass init info, including function table info & HB rules
-               newCode.add("\t" + COMMENT("Pass init info, including function table info & HB rules"));
+               newCode.add("\t"
+                               + COMMENT("Pass init info, including function table info & HB rules"));
                String structName = "anno_init", anno = "init";
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "func_table_size",
-                               "INTERFACE_SIZE"));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "hb_init_table_size",
-                               "HB_INIT_TABLE_SIZE"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "func_table_size", "INTERFACE_SIZE"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "hb_init_table", "hb_init_table"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "hb_init_table_size",
+                                               "HB_INIT_TABLE_SIZE"));
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INIT));
                newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
@@ -443,7 +454,7 @@ public class CodeVariables {
                newCode.add("}");
                newCode.add(COMMENT("End of Global construct generation in class"));
 
-               //printCode(newCode);
+               // printCode(newCode);
                return newCode;
        }
 
@@ -504,61 +515,49 @@ public class CodeVariables {
                                                                .get(right.hbConditionLabel));
                                newCode.add("\t" + COMMENT(left + " -> " + right));
 
-                               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
-                                               structVarName));
-                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
-                                               "interface_num_before", interfaceNumBefore));
-                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
-                                               "hb_condition_num_before", hbLabelNumBefore));
-                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName, "interface_num_after",
-                                               interfaceNumAfter));
-                               newCode.add("\t" + ASSIGN_TO_PTR(structVarName,
-                                               "hb_condition_num_after", hbLabelNumAfter));
-
-                               // newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
-                               // annotationVarName));
-                               // newCode.add(ASSIGN_TO_PTR(annotationVarName,
-                               // SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
-                               // newCode.add(ASSIGN_TO_PTR(annotationVarName,
-                               // SPEC_ANNOTATION_FIELD_ANNO, structVarName));
-                               // newCode.add(ANNOTATE(annotationVarName));
+                               newCode.add("\t"
+                                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
+                               newCode.add("\t"
+                                               + ASSIGN_TO_PTR(structVarName, "interface_num_before",
+                                                               interfaceNumBefore)
+                                               + SHORT_COMMENT(left.interfaceName));
+                               newCode.add("\t"
+                                               + ASSIGN_TO_PTR(structVarName,
+                                                               "hb_condition_num_before", hbLabelNumBefore)
+                                               + SHORT_COMMENT(left.hbConditionLabel));
+                               newCode.add("\t"
+                                               + ASSIGN_TO_PTR(structVarName, "interface_num_after",
+                                                               interfaceNumAfter)
+                                               + SHORT_COMMENT(right.interfaceName));
+                               newCode.add("\t"
+                                               + ASSIGN_TO_PTR(structVarName,
+                                                               "hb_condition_num_after", hbLabelNumAfter)
+                                               + SHORT_COMMENT(right.hbConditionLabel));
                        }
                }
                // Init hb_init_table
                newCode.add("\t" + COMMENT("Init hb_init_table"));
-               newCode.add("\t" + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
-                               + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
-                               + hbConditionInitIdx + ")"));
+               newCode.add("\t"
+                               + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
+                                               + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
+                                               + hbConditionInitIdx + ")"));
                // Define HB_INIT_TABLE_SIZE
-               newCode.add("\t" + DEFINE("HB_INIT_TABLE_SIZE",
-                               Integer.toString(hbConditionInitIdx)));
+               newCode.add("\t"
+                               + DEFINE("HB_INIT_TABLE_SIZE",
+                                               Integer.toString(hbConditionInitIdx)));
                for (int i = 0; i < hbConditionInitIdx; i++) {
-                       newCode.add("\t" + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit"
-                                       + i));
+                       newCode.add("\t"
+                                       + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
                }
                return newCode;
        }
 
        public static ArrayList<String> generateEntryPointInitCall() {
                ArrayList<String> newCode = new ArrayList<String>();
-               newCode.add("__sequential_init();");
+               newCode.add("\t" + "__sequential_init();");
                return newCode;
        }
 
-       // Only generate the declaration of the wrapper, don't do any renaming
-       // public static ArrayList<String> generateInterfaceWrapperDeclaration(
-       // SemanticsChecker semantics, InterfaceConstruct construct) {
-       // ArrayList<String> newCode = new ArrayList<String>();
-       // FunctionHeader header = getFunctionHeader(semantics, construct);
-       // newCode.add(COMMENT("Declaration of the wrapper"));
-       // String templateStr = header.getTemplateFullStr();
-       // newCode.add(templateStr);
-       // newCode.add(header.getFuncStr() + ";");
-       // newCode.add("");
-       //
-       // return newCode;
-       // }
-
        public static ArrayList<String> generateInterfaceWrapperDeclaration(
                        SemanticsChecker semantics, InterfaceConstruct construct) {
                FunctionHeader header = getFunctionHeader(semantics, construct);
@@ -584,21 +583,27 @@ public class CodeVariables {
                newCode.add("\t" + COMMENT("Interface begins"));
                // Interface begin
                String structName = "interface_begin";
-               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
-                               "interface_begin"));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+               newCode.add("\t"
+                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
+                                               "interface_begin"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
+                               + SHORT_COMMENT(construct.name));
 
                String anno = "annotation_interface_begin";
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_BEGIN));
                newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                newCode.add("\t" + ANNOTATE(semantics, anno));
                // Call original renamed function
                if (header.returnType.equals("void")) {
-                       newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER) + ";");
+                       newCode.add("\t" + header.getRenamedCall(SPEC_INTERFACE_WRAPPER)
+                                       + ";");
                } else {
-                       newCode.add("\t" + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
-                                       header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
+                       newCode.add("\t"
+                                       + DECLARE_DEFINE(header.returnType, MACRO_RETURN,
+                                                       header.getRenamedCall(SPEC_INTERFACE_WRAPPER)));
                }
                // HB conditions
                for (String label : construct.hbConditions.keySet()) {
@@ -607,13 +612,19 @@ public class CodeVariables {
                                        .get(label));
                        newCode.add("\t" + "if " + BRACE(condition) + " {");
                        structName = "hb_condition";
-                       newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
-
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
+                       newCode.add("\t\t"
+                                       + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
+                                       + SHORT_COMMENT(construct.name));
+
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "hb_condition_num", hbCondNum));
                        anno = "annotation_hb_condition";
-                       newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
+                       newCode.add("\t\t"
+                                       + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
                        newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                        newCode.add("\t\t" + ANNOTATE(semantics, anno));
                        newCode.add("\t" + "}");
@@ -623,12 +634,17 @@ public class CodeVariables {
                if (semantics.containsConditionalInterface(new ConditionalInterface(
                                interfaceName, ""))) {
                        structName = "hb_condition";
-                       newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
-                       newCode.add("\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
-                       newCode.add("\t" + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
+                       newCode.add("\t"
+                                       + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
+                                       + SHORT_COMMENT(construct.name));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR(structName, "hb_condition_num", "0"));
                        anno = "annotation_hb_condition";
                        newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-                       newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_HB_CONDITION));
                        newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                        newCode.add("\t" + ANNOTATE(semantics, anno));
                        newCode.add("");
@@ -638,11 +654,13 @@ public class CodeVariables {
                if (!header.returnType.equals("void") || header.args.size() > 0) {
                        infoStructType = interfaceName + "_info";
                        infoName = "info";
-                       newCode.add("\t" + DECLARE_DEFINE(infoStructType + "*", infoName,
-                                       BRACE(infoStructType + "*") + " malloc(sizeof("
-                                                       + infoStructType + "))"));
+                       newCode.add("\t"
+                                       + DECLARE_DEFINE(infoStructType + "*", infoName,
+                                                       BRACE(infoStructType + "*") + " malloc(sizeof("
+                                                                       + infoStructType + "))"));
                        if (!header.returnType.equals("void")) {
-                               newCode.add("\t" + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
+                               newCode.add("\t"
+                                               + ASSIGN_TO_PTR(infoName, MACRO_RETURN, MACRO_RETURN));
                        }
                        for (int i = 0; i < header.args.size(); i++) {
                                String argName = header.args.get(i).name;
@@ -653,11 +671,15 @@ public class CodeVariables {
                }
                structName = "interface_end";
                anno = "annoation_interface_end";
-               newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
-               newCode.add("\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+               newCode.add("\t"
+                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_END, structName));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
+                               + SHORT_COMMENT(construct.name));
                newCode.add("\t" + ASSIGN_TO_PTR(structName, "info", infoName));
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add("\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_INTERFACE_END));
                newCode.add("\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                newCode.add("\t" + ANNOTATE(semantics, anno));
                // Return __RET__ if it's not void
@@ -684,6 +706,7 @@ public class CodeVariables {
                }
                String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
                                .toString();
+               String oldLine = content.get(lineNum + 1);
 
                if (construct instanceof InterfaceConstruct) {
                        InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
@@ -693,12 +716,15 @@ public class CodeVariables {
                                newLine = newLine + " ;";
                                renameInterface(semantics, defineConstruct);
                        } else { // This is a declare & define construct
-                               newLine = newLine + " {";
+                               if (oldLine.indexOf('{') != -1)
+                                       newLine = newLine + " {";
                        }
                } else {
-                       newLine = newLine + " {";
+                       if (oldLine.indexOf('{') != -1)
+                               newLine = newLine + " {";
                }
-               content.set(lineNum, newLine);
+
+               content.set(lineNum + 1, newLine);
        }
 
        public static void addAtomicReturn(SemanticsChecker semantics,
@@ -718,23 +744,26 @@ public class CodeVariables {
                        addAtomicReturn(semantics, construct);
                }
                // Generate redundant header files
-               newCode.add(COMMENT("Automatically generated code for potential commit point: "
-                               + construct.label));
+               newCode.add("\t"
+                               + COMMENT("Automatically generated code for potential commit point: "
+                                               + construct.label));
                newCode.add("");
                // Add annotation
-               newCode.add("if (" + construct.condition + ") {");
+               newCode.add("\t" + "if (" + construct.condition + ") {");
                String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
-                               structName));
+               newCode.add("\t\t"
+                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
+                                               structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
-               newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add(ASSIGN_TO_PTR(anno, "type",
-                               SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
-               newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
-               newCode.add(ANNOTATE(semantics, anno));
-               newCode.add("}");
+               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
+               newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(anno, "type",
+                                               SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
+               newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
+               newCode.add("\t\t" + ANNOTATE(semantics, anno));
+               newCode.add("\t" + "}");
                return newCode;
        }
 
@@ -746,18 +775,21 @@ public class CodeVariables {
                        addAtomicReturn(semantics, construct);
                }
                // Generate redundant header files
-               newCode.add("\t" + COMMENT("Automatically generated code for commit point define check: "
-                               + construct.label));
+               newCode.add("\t"
+                               + COMMENT("Automatically generated code for commit point define check: "
+                                               + construct.label));
                newCode.add("");
                // Add annotation
                newCode.add("\t" + "if (" + construct.condition + ") {");
                String structName = "cp_define_check", anno = "annotation_cp_define_check";
-               newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
+               newCode.add("\t\t"
+                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
                newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
                newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                newCode.add("\t\t" + ANNOTATE(semantics, anno));
                newCode.add("\t" + "}");
@@ -768,23 +800,27 @@ public class CodeVariables {
                        SemanticsChecker semantics, CPDefineConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
                // Generate redundant header files
-               newCode.add("\t" + COMMENT("Automatically generated code for commit point define: "
-                               + construct.label));
+               newCode.add("\t"
+                               + COMMENT("Automatically generated code for commit point define: "
+                                               + construct.label));
                newCode.add("");
                // Add annotation
                newCode.add("\t" + "if (" + construct.condition + ") {");
                String structName = "cp_define", anno = "annotation_cp_define";
-               newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
+               newCode.add("\t\t"
+                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
                String potentialLabelNum = Integer
                                .toString(semantics.commitPointLabel2Num
                                                .get(construct.potentialCPLabel));
                newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
-                               potentialLabelNum));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
+                                               potentialLabelNum));
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));
                newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
                newCode.add("\t\t" + ANNOTATE(semantics, anno));
                newCode.add("\t" + "}");