edits
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index d0a935c64fca960ca68274895ea2b1ea4759d2d9..2653383326d5bc17719adbb09fc0085b37f772ff 100644 (file)
@@ -7,8 +7,10 @@ import java.io.File;
 
 import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
 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;
@@ -19,6 +21,14 @@ import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
 
+/**
+ * <p>
+ * Defines a list of commonly used constant strings.
+ * </p>
+ * 
+ * @author peizhaoo
+ * 
+ */
 public class CodeVariables {
        // C++ code or library
        public static final String HEADER_STDLIB = "<stdlib.h>";
@@ -42,24 +52,27 @@ public class CodeVariables {
 
        public static final String SPEC_ANNO_TYPE = "spec_anno_type";
        public static final String SPEC_ANNO_TYPE_INIT = "INIT";
-       public static final String SPEC_ANNO_TYPE_HB_INIT = "HB_INIT";
+       public static final String SPEC_ANNO_TYPE_HB_RULE = "HB_RULE";
        public static final String SPEC_ANNO_TYPE_INTERFACE_BEGIN = "INTERFACE_BEGIN";
        public static final String SPEC_ANNO_TYPE_HB_CONDITION = "HB_CONDITION";
        public static final String SPEC_ANNO_TYPE_INTERFACE_END = "INTERFACE_END";
        public static final String SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE = "POTENTIAL_CP_DEFINE";
        public static final String SPEC_ANNO_TYPE_CP_DEFINE_CHECK = "CP_DEFINE_CHECK";
+       public static final String SPEC_ANNO_TYPE_CP_CLEAR = "CP_CLEAR";
        public static final String SPEC_ANNO_TYPE_CP_DEFINE = "CP_DEFINE";
        public static final String SPEC_ANNOTATION = "spec_annotation";
        public static final String SPEC_ANNOTATION_FIELD_TYPE = "type";
        public static final String SPEC_ANNOTATION_FIELD_ANNO = "annotation";
 
        public static final String ANNO_INIT = "anno_init";
-       public static final String ANNO_HB_INIT = "anno_hb_init";
+       public static final String HB_RULE = "hb_rule";
+       public static final String COMMUTATIVITY_RULE = "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";
        public static final String ANNO_CP_DEFINE = "anno_cp_define";
        public static final String ANNO_CP_DEFINE_CHECK = "anno_cp_define_check";
+       public static final String ANNO_CP_CLEAR = "anno_cp_clear";
        public static final String ANNO_HB_CONDITION = "anno_hb_condition";
 
        // Specification variables
@@ -405,11 +418,68 @@ public class CodeVariables {
                newCode.add("static " + DECLARE("void**", "func_ptr_table"));
                // Happens-before initialization rules
                // Should make it static
-               newCode.add("static " + DECLARE(ANNO_HB_INIT + "**", "hb_init_table"));
+               newCode.add("static " + DECLARE(HB_RULE + "**", "hb_rule_table"));
+
+               // Declare the Commutativity Rule table
+               newCode.add("static "
+                               + DECLARE(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;
+                       String conditionFuncName = "CommutativityCondition"
+                                       + Integer.toString(i);
+
+                       // Replace the "_M1." and "_M2." with the actual info struct
+                       condition = condition.replaceAll("_Method1 \\.", "_info1->");
+                       condition = condition.replaceAll("_Method2 \\.", "_info2->");
+
+                       // Declare the signature of the condition function
+                       newCode.add("inline static bool " + conditionFuncName
+                                       + "(void *info1, void *info2) {");
+
+                       // Cast the "void*" type to the actual info type
+                       newCode.add("\t"
+                                       + DECLARE_DEFINE(infoStructType1, "*_info1", "("
+                                                       + infoStructType1 + "*) info1"));
+                       newCode.add("\t"
+                                       + DECLARE_DEFINE(infoStructType2, "*_info2", "("
+                                                       + infoStructType2 + "*) info2"));
+                       newCode.add("\treturn " + condition + ";");
+
+                       // End of the condition function
+                       newCode.add("}");
+               }
 
                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 bool __SPEC_CLEANUP__() {");
+               if (construct.code.cleanupCode.size() > 0) {
+                       addAllCodeWithIndent(newCode, construct.code.cleanupCode, "\t");        
+               } else {
+                       newCode.add("\treturn true;"); // If not specified return true
+               }
+               
+               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"
@@ -425,33 +495,49 @@ public class CodeVariables {
                                        + ASSIGN("func_ptr_table[2 * " + interfaceNum + " + 1]",
                                                        "(void*) &" + interfaceName + "_check_action"));
                }
+
                // 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_func_t) __SPEC_INIT__"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "cleanup_func",
+                                               "(cleanup_func_t) __SPEC_CLEANUP__"));
                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"));
+                               + ASSIGN_TO_PTR(structName, "hb_rule_table", "hb_rule_table"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "hb_rule_table_size",
+                                               "HB_RULE_TABLE_SIZE"));
+               newCode.add("\t"
+                               + ASSIGN_TO_PTR(structName, "commutativity_rule_table",
+                                               "commutativity_rule_table"));
                newCode.add("\t"
-                               + ASSIGN_TO_PTR(structName, "hb_init_table_size",
-                                               "HB_INIT_TABLE_SIZE"));
+                               + ASSIGN_TO_PTR(structName, "commutativity_rule_table_size",
+                                               Integer.toString(rules.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);
@@ -474,7 +560,8 @@ public class CodeVariables {
                String templateDecl = semantics.getTemplateFullStr();
                if (templateList == null) {
                        newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
-                       newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
+                       newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
+                       newCode.add(DECLARE("commutativity_rule**", varPrefix + "commutativity_rule_table"));
                        for (int i = 0; i < construct.code.declareVar.size(); i++) {
                                VariableDeclaration varDecl = construct.code.declareVar.get(i);
                                newCode.add(DECLARE(varDecl.type, varPrefix + varDecl.name));
@@ -483,7 +570,9 @@ public class CodeVariables {
                        newCode.add(templateDecl);
                        newCode.add(DECLARE("void**", varPrefix + "func_ptr_table"));
                        newCode.add(templateDecl);
-                       newCode.add(DECLARE("anno_hb_init**", varPrefix + "hb_init_table"));
+                       newCode.add(DECLARE("hb_rule**", varPrefix + "hb_rule_table"));
+                       newCode.add(templateDecl);
+                       newCode.add(DECLARE("commutativity_rule**", varPrefix + "commutativity_rule_table"));
                        for (int i = 0; i < construct.code.declareVar.size(); i++) {
                                VariableDeclaration varDecl = construct.code.declareVar.get(i);
                                newCode.add(templateDecl);
@@ -502,7 +591,7 @@ public class CodeVariables {
                        for (ConditionalInterface right : semantics.getHBConditions().get(
                                        left)) {
                                String structVarName = "hbConditionInit" + hbConditionInitIdx;
-                               // String annotationVarName = "hb_init" + hbConditionInitIdx;
+                               // String annotationVarName = "hb_rule" + hbConditionInitIdx;
                                hbConditionInitIdx++;
                                String interfaceNumBefore = Integer
                                                .toString(semantics.interface2Num
@@ -516,7 +605,7 @@ public class CodeVariables {
                                newCode.add("\t" + COMMENT(left + " -> " + right));
 
                                newCode.add("\t"
-                                               + STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
+                                               + STRUCT_NEW_DECLARE_DEFINE(HB_RULE, structVarName));
                                newCode.add("\t"
                                                + ASSIGN_TO_PTR(structVarName, "interface_num_before",
                                                                interfaceNumBefore)
@@ -535,23 +624,79 @@ public class CodeVariables {
                                                + SHORT_COMMENT(right.hbConditionLabel));
                        }
                }
-               // Init hb_init_table
-               newCode.add("\t" + COMMENT("Init hb_init_table"));
+               // Init hb_rule_table
+               newCode.add("\t" + COMMENT("Init hb_rule_table"));
                newCode.add("\t"
-                               + ASSIGN("hb_init_table", "(" + ANNO_HB_INIT
-                                               + "**) malloc(sizeof(" + ANNO_HB_INIT + "*) * "
+                               + ASSIGN("hb_rule_table", "(" + HB_RULE
+                                               + "**) malloc(sizeof(" + HB_RULE + "*) * "
                                                + hbConditionInitIdx + ")"));
-               // Define HB_INIT_TABLE_SIZE
+               // Define HB_RULE_TABLE_SIZE
                newCode.add("\t"
-                               + DEFINE("HB_INIT_TABLE_SIZE",
+                               + DEFINE("HB_RULE_TABLE_SIZE",
                                                Integer.toString(hbConditionInitIdx)));
                for (int i = 0; i < hbConditionInitIdx; i++) {
                        newCode.add("\t"
-                                       + ASSIGN("hb_init_table[" + i + "]", "hbConditionInit" + i));
+                                       + ASSIGN("hb_rule_table[" + i + "]", "hbConditionInit" + i));
                }
                return newCode;
        }
 
+       private static ArrayList<String> generateCommutativityAnnotation(
+                       SemanticsChecker semantics) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               ArrayList<CommutativityRule> rules = semantics.getGlobalConstruct().commutativityRules;
+
+               // Init commutativity_rule_table
+               newCode.add("\t" + COMMENT("Init commutativity_rule_table"));
+
+               // Users have not defined any commutativity rules
+               if (rules.size() == 0)
+                       return newCode;
+
+               newCode.add("\t"
+                               + ASSIGN("commutativity_rule_table", "(" + COMMUTATIVITY_RULE
+                                               + "**) malloc(sizeof(" + COMMUTATIVITY_RULE + "*) * "
+                                               + rules.size() + ")"));
+
+               // Declare a rule pointer
+               newCode.add("\t" + DECLARE("commutativity_rule*", "rule"));
+
+               for (int i = 0; i < rules.size(); i++) {
+                       CommutativityRule rule = rules.get(i);
+                       Integer method = semantics.interface2Num.get(rule.method1);
+                       if (method == null) {
+                               System.out.println("Wrong method label in commutativity rule: " + rule.method1);
+                       }
+                       String interfaceNumBefore = Integer.toString(method);
+                       String interfaceNumAfter = Integer.toString(semantics.interface2Num
+                                       .get(rule.method2));
+                       String conditionFuncName = "CommutativityCondition" + i;
+
+                       // Construct a new rule
+                       newCode.add("\t"
+                                       + ASSIGN("rule",
+                                                       "(commutativity_rule*) malloc (sizeof(commutativity_rule))"));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR("rule", "interface_num_before",
+                                                       interfaceNumBefore));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR("rule", "interface_num_after",
+                                                       interfaceNumAfter));
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR("rule", "rule",
+                                                       "\"" + rule.condition + "\""));
+                       
+                       newCode.add("\t"
+                                       + ASSIGN_TO_PTR("rule", "condition", conditionFuncName));
+
+                       // Assign the rule to the corresponding commutativity table slot
+                       newCode.add("\t"
+                                       + ASSIGN("commutativity_rule_table[" + i + "]", "rule"));
+               }
+
+               return newCode;
+       }
+
        public static ArrayList<String> generateEntryPointInitCall() {
                ArrayList<String> newCode = new ArrayList<String>();
                newCode.add("\t" + "__sequential_init();");
@@ -589,6 +734,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 + "\""));
 
                String anno = "annotation_interface_begin";
                newCode.add("\t" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
@@ -757,6 +905,10 @@ 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" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"
                                + ASSIGN_TO_PTR(anno, "type",
@@ -767,6 +919,26 @@ public class CodeVariables {
                return newCode;
        }
 
+       public static String getCPInterfaceNum(SemanticsChecker semantics,
+                       String commitPointLabel) {
+               HashMap<String, InterfaceConstruct> cp2Interface = semantics.CPLabel2InterfaceConstruct;
+               InterfaceConstruct iConstruct = cp2Interface.get(commitPointLabel);
+               String interfaceName = iConstruct.name;
+               String interfaceNum = Integer.toString(semantics.interface2Num
+                               .get(interfaceName));
+               return interfaceNum;
+       }
+
+       /**
+        * <p>
+        * 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> generateCPDefineCheck(
                        SemanticsChecker semantics, CPDefineCheckConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
@@ -780,13 +952,21 @@ 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"
                                + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE_CHECK, 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, "label_name", "\""
+                                               + construct.label + "\""));
+               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_DEFINE_CHECK));
@@ -796,6 +976,62 @@ public class CodeVariables {
                return newCode;
        }
 
+       /**
+        * <p>
+        * 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) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               // Add atomic return variable if the predicate accesses to it
+               if (construct.condition.indexOf(MACRO_ATOMIC_RETURN) != -1) {
+                       addAtomicReturn(semantics, construct);
+               }
+               // Generate redundant header files
+               newCode.add("\t"
+                               + COMMENT("Automatically generated code for commit point clear: "
+                                               + 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 labelName = construct.label;
+               newCode.add("\t\t" + ASSIGN_TO_PTR(structName, "label_name",
+                               "\"" + labelName + "\""));
+               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_CLEAR));
+               newCode.add("\t\t" + ASSIGN_TO_PTR(anno, "annotation", structName));
+               newCode.add("\t\t" + ANNOTATE(semantics, anno));
+               newCode.add("\t" + "}");
+               return newCode;
+       }
+
+       /**
+        * <p>
+        * Commit point define 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> generateCPDefine(
                        SemanticsChecker semantics, CPDefineConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
@@ -811,13 +1047,22 @@ public class CodeVariables {
                                + STRUCT_NEW_DECLARE_DEFINE(ANNO_CP_DEFINE, structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
+               String interfaceNum = getCPInterfaceNum(semantics, 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, "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" + STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
                newCode.add("\t\t"
                                + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));