edits
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index bb572c18b5ef6eafb285b1982e529f21f9a20bbe..7e4ab29c446c74db5683957664c197fe86600aa7 100644 (file)
@@ -52,7 +52,7 @@ 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";
@@ -65,8 +65,8 @@ public class CodeVariables {
        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 ANNO_COMMUTATIVITY_RULE = "anno_commutativity_rule";
+       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";
@@ -418,10 +418,11 @@ 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(ANNO_COMMUTATIVITY_RULE + "**", "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++) {
@@ -429,21 +430,32 @@ public class CodeVariables {
                        String infoStructType1 = rule.method1 + "_info";
                        String infoStructType2 = rule.method2 + "_info";
                        String condition = rule.condition;
-                       
-                       // Declare
-                       
+                       String conditionFuncName = "CommutativityCondition"
+                                       + Integer.toString(i);
+
                        // Replace the "_M1." and "_M2." with the actual info struct
-                       condition.replaceAll("_M1\\.", infoStructType1 + "->");
-                       condition.replaceAll("_M2\\.", infoStructType2 + "->");
-                       
+                       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(e)
+                       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
@@ -459,10 +471,10 @@ public class CodeVariables {
                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"
@@ -478,13 +490,10 @@ 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));
 
@@ -494,25 +503,27 @@ public class CodeVariables {
                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__"));
+                               + ASSIGN_TO_PTR(structName, "init_func",
+                                               "(void_func_t*) __SPEC_INIT__"));
                newCode.add("\t"
-                               + ASSIGN_TO_PTR(structName, "cleanup_func", "(void*) __SPEC_CLEANUP__"));
+                               + ASSIGN_TO_PTR(structName, "cleanup_func",
+                                               "(void_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_init_table_size",
-                                               "HB_INIT_TABLE_SIZE"));
-               
+                               + ASSIGN_TO_PTR(structName, "hb_rule_table_size",
+                                               "HB_RULE_TABLE_SIZE"));
                newCode.add("\t"
-                               + ASSIGN_TO_PTR(structName, "commutativity_rule_table", "hb_init_table"));
+                               + 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));
@@ -544,7 +555,7 @@ 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"));
                        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));
@@ -553,7 +564,7 @@ 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"));
                        for (int i = 0; i < construct.code.declareVar.size(); i++) {
                                VariableDeclaration varDecl = construct.code.declareVar.get(i);
                                newCode.add(templateDecl);
@@ -572,7 +583,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
@@ -586,7 +597,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)
@@ -605,19 +616,19 @@ 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;
        }
@@ -626,24 +637,52 @@ public class CodeVariables {
                        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"));
+
+               // Users have not defined any commutativity rules
+               if (rules.size() == 0)
+                       return newCode;
+
                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++) {
+                               + 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);
+                       String interfaceNumBefore = Integer
+                                       .toString(semantics.interface2Num.get(rule.method1));
+                       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("hb_init_table[" + i + "]", "hbConditionInit" + i));
+                                       + 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;
        }
 
@@ -858,13 +897,7 @@ public class CodeVariables {
                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"));
-               } else {
-                       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"
                                + ASSIGN_TO_PTR(anno, "type",
@@ -922,13 +955,7 @@ public class CodeVariables {
                                                + 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"));
-               } else {
-                       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"
                                + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE_CHECK));
@@ -966,13 +993,15 @@ public class CodeVariables {
                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 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));
@@ -1023,13 +1052,6 @@ public class CodeVariables {
                                                + 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"));
-               } else {
-                       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"
                                + ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_CP_DEFINE));