add Commit point clear construct
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index d0a935c64fca960ca68274895ea2b1ea4759d2d9..fb1b520202f77d755c22a73f2d6936e009468842 100644 (file)
@@ -7,6 +7,7 @@ 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.ConditionalInterface;
@@ -60,6 +61,7 @@ public class CodeVariables {
        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
@@ -766,7 +768,26 @@ public class CodeVariables {
                newCode.add("\t" + "}");
                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 +801,58 @@ 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, "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));
+               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 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 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_DEFINE_CHECK));
@@ -796,6 +862,16 @@ public class CodeVariables {
                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,6 +887,7 @@ 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));
@@ -818,6 +895,7 @@ public class CodeVariables {
                newCode.add("\t\t"
                                + ASSIGN_TO_PTR(structName, "potential_cp_label_num",
                                                potentialLabelNum));
+               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));