edits
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index cdc8771fdf5ed5cdbb15663e28d54025353d3dba..bb572c18b5ef6eafb285b1982e529f21f9a20bbe 100644 (file)
@@ -10,6 +10,7 @@ import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
 import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CommutativityRule;
 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
@@ -21,9 +22,12 @@ import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
 /**
- * <p> Defines a list of commonly used constant strings. </p>
+ * <p>
+ * Defines a list of commonly used constant strings.
+ * </p>
+ * 
  * @author peizhaoo
- *
+ * 
  */
 public class CodeVariables {
        // C++ code or library
@@ -62,6 +66,7 @@ public class CodeVariables {
 
        public static final String ANNO_INIT = "anno_init";
        public static final String ANNO_HB_INIT = "anno_hb_init";
+       public static final String ANNO_COMMUTATIVITY_RULE = "anno_commutativity_rule";
        public static final String ANNO_INTERFACE_BEGIN = "anno_interface_begin";
        public static final String ANNO_INTERFACE_END = "anno_interface_end";
        public static final String ANNO_POTENTIAL_CP_DEFINE = "anno_potential_cp_define";
@@ -414,10 +419,50 @@ public class CodeVariables {
                // Happens-before initialization rules
                // Should make it static
                newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
+               
+               // Declare the Commutativity Rule table
+               newCode.add("static " + DECLARE(ANNO_COMMUTATIVITY_RULE + "**", "commutativity_rule_table"));
+               // Define the Commutativity Rule condition functions
+               ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
+               for (int i = 0; i < rules.size(); i++) {
+                       CommutativityRule rule = rules.get(i);
+                       String infoStructType1 = rule.method1 + "_info";
+                       String infoStructType2 = rule.method2 + "_info";
+                       String condition = rule.condition;
+                       
+                       // Declare
+                       
+                       // Replace the "_M1." and "_M2." with the actual info struct
+                       condition.replaceAll("_M1\\.", infoStructType1 + "->");
+                       condition.replaceAll("_M2\\.", infoStructType2 + "->");
+                       
+                       // Cast the "void*" type to the actual info type
+                       
+                       newCode.add(e)
+               }
+               
+
+               newCode.add("");
+               
+               // Beginning initialization
+               // Define the __SPEC_INIT__ function to initialize user-defined
+               // variables
+               newCode.add(COMMENT("Initialization of sequential varialbes"));
+               newCode.add("static void __SPEC_INIT__() {");
+               addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
+               newCode.add("}");
+               newCode.add("");
 
+               // Define the __SPEC_CLEAN__ function for clean-up
+               newCode.add(COMMENT("Cleanup routine of sequential variables"));
+               newCode.add("static void __SPEC_CLEANUP__() {");
+               addAllCodeWithIndent(newCode, construct.code.cleanupCode, "\t");
+               newCode.add("}");
                newCode.add("");
+               
                newCode.add(COMMENT("Define function for sequential code initialization"));
                newCode.add("inline static void __sequential_init() {");
+               
                // Init func_ptr_table
                newCode.add("\t" + COMMENT("Init func_ptr_table"));
                newCode.add("\t"
@@ -433,14 +478,25 @@ public class CodeVariables {
                                        + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
                                                        "(void*) &" + interfaceName + "_check_action"));
                }
+               
+               // Init Commutativity rules table
+               
+               
                // Init Happens-before rules table
                newCode.addAll(generateHBInitAnnotation(semantics));
+               
+               // Init Commutativity rules table
+               newCode.addAll(generateCommutativityAnnotation(semantics));
 
                // Pass init info, including function table info & HB rules
                newCode.add("\t"
-                               + COMMENT("Pass init info, including function table info & HB rules"));
+                               + COMMENT("Pass init info, including function table info & HB rules & Commutativity Rules"));
                String structName = "anno_init", anno = "init";
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(ANNO_INIT, structName));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "init_func", "(void*) __SPEC_INIT__"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "cleanup_func", "(void*) __SPEC_CLEANUP__"));
                newCode.add("\t"
                                + ASSIGN_TO_PTR(structName, "func_table", "func_ptr_table"));
                newCode.add("\t"
@@ -450,16 +506,22 @@ public class CodeVariables {
                newCode.add("\t"
                                + ASSIGN_TO_PTR(structName, "hb_init_table_size",
                                                "HB_INIT_TABLE_SIZE"));
+               
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "commutativity_rule_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));
                newCode.add("\t" + ANNOTATE(semantics, anno));
 
                newCode.add("");
-               // Init user-defined variables
-               addAllCodeWithIndent(newCode, construct.code.initVar, "\t");
-
                newCode.add("}");
+               newCode.add("");
+
                newCode.add(COMMENT("End of Global construct generation in class"));
 
                // printCode(newCode);
@@ -560,6 +622,31 @@ public class CodeVariables {
                return newCode;
        }
 
+       private static ArrayList<String> generateCommutativityAnnotation(
+                       SemanticsChecker semantics) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
+               for (CommutativityRule rule : rules) {
+
+               }
+
+               // Init commutativity_rule_table
+               newCode.add("\t" + COMMENT("Init commutativity_rule_table"));
+               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)));
+               for (int i = 0; i < hbConditionInitIdx; 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("\t" + "__sequential_init();");
@@ -597,7 +684,9 @@ public class CodeVariables {
                newCode.add("\t"
                                + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum)
                                + SHORT_COMMENT(construct.name));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_name", "\"" + construct.name + "\""));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "interface_name", "\""
+                                               + construct.name + "\""));
 
                String anno = "annotation_interface_begin";
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
@@ -766,11 +855,15 @@ public class CodeVariables {
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
                newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_name", "\"" + construct.label + "\""));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "label_name", "\""
+                                               + construct.label + "\""));
                if (construct.isAdditionalOrderingPoint) {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
                } else {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
                }
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"
@@ -781,8 +874,9 @@ public class CodeVariables {
                newCode.add("\t" + "}");
                return newCode;
        }
-       
-       public static String getCPInterfaceNum(SemanticsChecker semantics, String commitPointLabel) {
+
+       public static String getCPInterfaceNum(SemanticsChecker semantics,
+                       String commitPointLabel) {
                HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
                InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
                String interfaceName = iConstruct.name;
@@ -793,8 +887,8 @@ public class CodeVariables {
 
        /**
         * <p>
-        * Commit point define check should be unique to each interface, meaning that they
-        * are not shared between different interfaces
+        * Commit point define check should be unique to each interface, meaning
+        * that they are not shared between different interfaces
         * </p>
         * 
         * @param semantics
@@ -814,7 +908,7 @@ public class CodeVariables {
                                                + 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"
@@ -823,12 +917,17 @@ public class CodeVariables {
                                .get(construct.label));
                String interfaceNum = getCPInterfaceNum(semantics, construct.label);
                newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_name", "\"" + construct.label + "\""));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "label_name", "\""
+                                               + construct.label + "\""));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
                if (construct.isAdditionalOrderingPoint) {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
                } else {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
                }
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"
@@ -838,19 +937,19 @@ public class CodeVariables {
                newCode.add("\t" + "}");
                return newCode;
        }
-       
+
        /**
         * <p>
-        * Commit point define check should be unique to each interface, meaning that they
-        * are not shared between different interfaces
+        * Commit point define check should be unique to each interface, meaning
+        * that they are not shared between different interfaces
         * </p>
         * 
         * @param semantics
         * @param construct
         * @return
         */
-       public static ArrayList<String> generateCPClear(
-                       SemanticsChecker semantics, CPClearConstruct construct) {
+       public static ArrayList<String> generateCPClear(SemanticsChecker semantics,
+                       CPClearConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
                // Add atomic return variable if the predicate accesses to it
                if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
@@ -862,16 +961,18 @@ public class CodeVariables {
                                                + construct.label));
                newCode.add("");
                // Add annotation
-               
+
                newCode.add("\t" + "if (" + construct.condition + ") {");
                String structName = "cp_clear", anno = "annotation_cp_clear";
                newCode.add("\t\t"
                                + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_CLEAR, structName));
-//             String labelNum = Integer.toString(semantics.commitPointLabel2Num
-//                             .get(construct.label));
-//             String interfaceNum = getCPInterfaceNum(semantics, construct.label);
-//             newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num", labelNum));
-//             newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+               // String labelNum = Integer.toString(semantics.commitPointLabel2Num
+               // .get(construct.label));
+               // String interfaceNum = getCPInterfaceNum(semantics, construct.label);
+               // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_num",
+               // labelNum));
+               // newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num",
+               // interfaceNum));
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"
                                + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_CLEAR));
@@ -911,16 +1012,23 @@ public class CodeVariables {
                                .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, "label_name", "\"" + construct.label + "\""));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "label_name", "\""
+                                               + construct.label + "\""));
                newCode.add("\t\t"
                                + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
                                                potentialLabelNum));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "potential_label_name", "\"" + construct.potentialCPLabel + "\""));
-               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "potential_label_name", "\""
+                                               + construct.potentialCPLabel + "\""));
+               newCode.add("\t\t"
+                               + ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
                if (construct.isAdditionalOrderingPoint) {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "true"));
                } else {
-                       newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
+                       newCode.add("\t\t"
+                                       + ASSIGN_TO_PTR(structName, "is_additional_point", "false"));
                }
                newCode.add("\t\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"