add INTERFACE_END
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index 04f3be95f4fba9983f04473a7e84f082159a7dc3..7dcaaa0acec7a605c3bedca3858964d6a9bb6e47 100644 (file)
@@ -169,6 +169,18 @@ public class CodeGeneratorUtils {
                code.addLine("#endif");
                code.addLine("");
 
+               // Declare _createOPDefineUnattached
+               code.addLine(ShortComment("Declare _createOPDefineUnattached"));
+               // void _createOPDefineUnattached();
+               code.addLine("void _createOPDefineUnattached();");
+               code.addLine("");
+
+               // Declare _createOPClearDefineUnattached
+               code.addLine(ShortComment("Declare _createOPClearDefineUnattached"));
+               // void _createOPClearDefineUnattached();
+               code.addLine("void _createOPClearDefineUnattached();");
+               code.addLine("");
+
                code.addLine(ShortComment("Declaration of some c-strings (CSTR)"));
 
                // Interface name strings
@@ -197,9 +209,10 @@ public class CodeGeneratorUtils {
                        for (InterfaceConstruct construct : list) {
                                // Declare custom value struct for the interface
                                String name = construct.getName();
+                               String structName = construct.getStructName();
                                code.addLine(ShortComment("Declare custom value struct for "
                                                + name));
-                               code.addLine("typedef struct " + name + " {");
+                               code.addLine("typedef struct " + structName + " {");
                                FunctionHeader funcHeader = construct.getFunctionHeader();
                                // RET
                                if (!funcHeader.returnType.equals("void"))
@@ -209,7 +222,7 @@ public class CodeGeneratorUtils {
                                for (VariableDeclaration decl : funcHeader.args) {
                                        code.addLine(TabbedLine(Declare(decl)));
                                }
-                               code.addLine("} " + name + ";");
+                               code.addLine("} " + structName + ";");
                                code.addLine("");
                        }
                }
@@ -295,6 +308,31 @@ public class CodeGeneratorUtils {
                code.addLine("");
                code.addLine("");
 
+               // Define the fake ordering point operation
+               code.addLine(ShortComment("Define the fake ordering point operation"));
+               // atomic_int _FAKE_OP_;
+               code.addLine("atomic_int " + SpecNaming.FakeOP + ";");
+               code.addLine("");
+
+               // Define _createOPDefineUnattached
+               code.addLine(ShortComment("Define _createOPDefineUnattached"));
+               code.addLine("void " + SpecNaming.CreateOPDefineUnattachedFunc + "() {");
+               code.addLine(TabbedLine(ShortComment("A load acting as the fake OP")));
+               code.addLine(TabbedLine(SpecNaming.FakeOP
+                               + ".load(memory_order_relaxed);"));
+               code.addLine(TabbedLine(SpecNaming.CreateOPDefineAnnoFunc + "();"));
+               code.addLine("}");
+
+               // Define _createOPClearDefineUnattached
+               code.addLine(ShortComment("Define _createOPClearDefineUnattached"));
+               code.addLine("void " + SpecNaming.CreateOPClearDefineUnattachedFunc
+                               + "() {");
+               code.addLine(TabbedLine(ShortComment("A load acting as the fake OP")));
+               code.addLine(TabbedLine(SpecNaming.FakeOP
+                               + ".load(memory_order_relaxed);"));
+               code.addLine(TabbedLine(SpecNaming.CreateOPClearDefineAnnoFunc + "();"));
+               code.addLine("}");
+
                code.addLine(ShortComment("Definition of some c-strings (CSTR)"));
                code.addLine(ShortComment("A special empty string"));
                code.addLine(DeclareDefine(SpecNaming.CString, SpecNaming.EmptyCString,
@@ -498,11 +536,15 @@ public class CodeGeneratorUtils {
                                        + SpecNaming.AppendStr(rule.method2) + ") {"));
                        // Initialize M1 & M2 in commutativity rule
                        // e.g. ENQ *M1 = (ENQ*) m1->value;
+                       String structName1 = InterfaceConstruct
+                                       .createStructName(rule.method1);
+                       String structName2 = InterfaceConstruct
+                                       .createStructName(rule.method2);
                        code.addLine(TabbedLine(
-                                       DeclareDefine(rule.method1, "*M1", "(" + rule.method1
+                                       DeclareDefine(structName1, "*M1", "(" + structName1
                                                        + "*) m1->value"), 2));
                        code.addLine(TabbedLine(
-                                       DeclareDefine(rule.method2, "*M2", "(" + rule.method2
+                                       DeclareDefine(structName2, "*M2", "(" + structName2
                                                        + "*) m2->value"), 2));
                        code.addLine(TabbedLine("return " + rule.condition + ";", 2));
                        code.addLine(TabbedLine("}"));
@@ -520,12 +562,13 @@ public class CodeGeneratorUtils {
 
                                // Define interface functions
                                String name = construct.getName();
+                               String structName = construct.getStructName();
                                code.addLine("/**********    " + name
                                                + " functions    **********/");
                                // Define @Transition for INTERFACE
                                code.addLine(ShortComment("Define @" + SpecNaming.Transition
                                                + " for " + name));
-                               code.addLine("void _" + name + "_" + SpecNaming.Transition
+                               code.addLine("bool _" + name + "_" + SpecNaming.Transition
                                                + "(" + SpecNaming.Method + " " + SpecNaming.Method1
                                                + ", " + SpecNaming.Method + " " + SpecNaming.Method2
                                                + ") {");
@@ -541,6 +584,9 @@ public class CodeGeneratorUtils {
                                code.addLine(TabbedLine(ShortComment("Execute Transition")));
                                code.addLines(construct.transition);
 
+                               // By default, we will return true for state transition
+                               code.addLine(TabbedLine(ShortComment("By default @Transition returns true")));
+                               code.addLine(TabbedLine("return true;"));
                                code.addLine("}");
                                code.addLine("");
 
@@ -563,6 +609,10 @@ public class CodeGeneratorUtils {
                                        code.addLine(TabbedLine(ShortComment("Execute PreCondition")));
                                        code.addLines(construct.preCondition);
 
+                                       // By default, we will return true for @PreCondition
+                                       code.addLine(TabbedLine(ShortComment("By default @PreCondition returns true")));
+                                       code.addLine(TabbedLine("return true;"));
+
                                        code.addLine("}");
                                        code.addLine("");
 
@@ -587,6 +637,10 @@ public class CodeGeneratorUtils {
                                        code.addLine(TabbedLine(ShortComment("Execute PostCondition")));
                                        code.addLines(construct.postCondition);
 
+                                       // By default, we will return true for @PostCondition
+                                       code.addLine(TabbedLine(ShortComment("By default @PostCondition returns true")));
+                                       code.addLine(TabbedLine("return true;"));
+
                                        code.addLine("}");
                                        code.addLine("");
                                }
@@ -622,6 +676,11 @@ public class CodeGeneratorUtils {
                code.addLine(ShortComment("Define INIT annotation instrumentation function"));
                code.addLine("void _createInitAnnotation() {");
 
+               // Init the fake ordering point place holder
+               code.addLine(TabbedLine(ShortComment("Init the fake ordering point place holder")));
+               code.addLine(TabbedLine(SpecNaming.FakeOP
+                               + ".store(1, memory_order_relaxed);"));
+
                // Init commutativity rules
                code.addLine(TabbedLine(ShortComment("Init commutativity rules")));
                code.addLine(TabbedLine(DeclareDefine("int",
@@ -858,10 +917,11 @@ public class CodeGeneratorUtils {
                        String inst, InterfaceConstruct construct) {
                Code res = new Code();
                String name = construct.getName();
+               String structName = construct.getStructName();
                res.addLine(ShortComment("Initialize fields for " + name));
                // The very first assignment "
-               res.addLine(DeclareDefine(name, "*" + inst, "(" + name + "*) "
-                               + methodInst + "->" + SpecNaming.MethodValueField));
+               res.addLine(DeclareDefine(structName, "*" + inst, "(" + structName
+                               + "*) " + methodInst + "->" + SpecNaming.MethodValueField));
                // Don't leave out the RET field
                if (!construct.getFunctionHeader().isReturnVoid()) {
                        res.addLine(DeclareDefine(construct.getFunctionHeader().returnType,
@@ -899,6 +959,10 @@ public class CodeGeneratorUtils {
                case OPDefine:
                        code.addLine(prefixTabs + SpecNaming.CreateOPDefineAnnoFunc + "();");
                        break;
+               case OPDefineUnattached:
+                       code.addLine(prefixTabs + SpecNaming.CreateOPDefineUnattachedFunc
+                                       + "();");
+                       break;
                case PotentialOP:
                        code.addLine(prefixTabs + SpecNaming.CreatePotentialOPAnnoFunc
                                        + "(" + SpecNaming.AppendStr(label) + ");");
@@ -914,6 +978,10 @@ public class CodeGeneratorUtils {
                        code.addLine(prefixTabs + SpecNaming.CreateOPClearDefineAnnoFunc
                                        + "();");
                        break;
+               case OPClearDefineUnattached:
+                       code.addLine(prefixTabs
+                                       + SpecNaming.CreateOPClearDefineUnattachedFunc + "();");
+                       break;
                default:
                        break;
                }
@@ -972,6 +1040,7 @@ public class CodeGeneratorUtils {
                Code code = new Code();
 
                String name = construct.getName();
+               String structName = construct.getStructName();
                String beginLine = construct.getFunctionHeader().getHeaderLine();
                Pattern regexpSpace = Pattern.compile("^(\\s*)\\S.*$");
                Matcher matcherSpace = regexpSpace.matcher(beginLine);
@@ -1014,8 +1083,9 @@ public class CodeGeneratorUtils {
                // The very first assignment "
                code.addLine(prefixTabs
                                + "\t"
-                               + DeclareDefine(name, "*" + SpecNaming.InterfaceValueInst,
-                                               SpecNaming.New + Brace(name)));
+                               + DeclareDefine(structName,
+                                               "*" + SpecNaming.InterfaceValueInst, SpecNaming.New
+                                                               + Brace(structName)));
                // Don't leave out the RET field
                if (!construct.getFunctionHeader().isReturnVoid())
                        code.addLine(prefixTabs
@@ -1042,6 +1112,14 @@ public class CodeGeneratorUtils {
                                                + SpecNaming.InterfaceValueInst) + ";");
                code.addLine("");
 
+               // Instrument with the INTERFACE_END annotations
+               code.addLine(prefixTabs + "\t"
+                               + ShortComment("Instrument with the INTERFACE_END annotation"));
+               // _createInterfaceEndAnnotation(_DEQ_str);
+               code.addLine(prefixTabs + "\t"
+                               + SpecNaming.CreateInterfaceEndAnnoFunc
+                               + Brace(SpecNaming.AppendStr(name)) + ";");
+
                // Return if necessary
                if (!construct.getFunctionHeader().isReturnVoid())
                        code.addLine(prefixTabs + "\treturn " + SpecNaming.RET + ";");