changes to ms-queue spec
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index d0a935c64fca960ca68274895ea2b1ea4759d2d9..0ee2ee0c9e7c13eee1261db84ae7f1ca92108658 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;
@@ -19,6 +20,11 @@ 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>";
@@ -48,6 +54,7 @@ public class CodeVariables {
        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";
@@ -60,6 +67,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
@@ -589,6 +597,7 @@ 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 +766,7 @@ 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",
@@ -766,7 +776,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 +809,17 @@ 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));
@@ -795,7 +828,59 @@ 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
+        * </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_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 +896,17 @@ 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));