fixed commutativity rule
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index 694695cf48ca9db30cae73666101a9ccb75ef066..f85ec8e674176997d696414b19517167db700377 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,19 +209,25 @@ 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"))
+                               // C_RET & also the S_RET
+                               // The idea is that we store the S_RET in the __value struct, and every time we access/update
+                               // S_RET, we actually access "__value->S_RET" (good for read and write)...
+                               if (!funcHeader.returnType.equals("void")) {
                                        code.addLine(TabbedLine(Declare(funcHeader.returnType,
-                                                       SpecNaming.RET)));
+                                                       SpecNaming.C_RET)));
+                                       code.addLine(TabbedLine(Declare(funcHeader.returnType,
+                                                       SpecNaming.S_RET)));
+                               }
                                // Arguments
                                for (VariableDeclaration decl : funcHeader.args) {
                                        code.addLine(TabbedLine(Declare(decl)));
                                }
-                               code.addLine("} " + name + ";");
+                               code.addLine("} " + structName + ";");
                                code.addLine("");
                        }
                }
@@ -248,6 +266,7 @@ public class CodeGeneratorUtils {
 
                Code code = new Code();
                String line = null;
+               Code fieldsInit = null;
 
                // Add auto-generated comments
                code.addLine("/**");
@@ -294,6 +313,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,
@@ -369,6 +413,24 @@ public class CodeGeneratorUtils {
                                        code.addLine(DeclareDefine(SpecNaming.CString,
                                                        SpecNaming.AppendStr(tmpFunc),
                                                        SpecNaming.EmptyCString));
+                               // JustifyingPrecondition
+                               tmpFunc = name + "_" + SpecNaming.JustifyingPrecondition;
+                               if (!construct.justifyingPrecondition.isEmpty())
+                                       code.addLine(DeclareDefine(SpecNaming.CString,
+                                                       SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
+                               else
+                                       code.addLine(DeclareDefine(SpecNaming.CString,
+                                                       SpecNaming.AppendStr(tmpFunc),
+                                                       SpecNaming.EmptyCString));
+                               // JustifyingPostcondition
+                               tmpFunc = name + "_" + SpecNaming.JustifyingPostcondition;
+                               if (!construct.justifyingPostcondition.isEmpty())
+                                       code.addLine(DeclareDefine(SpecNaming.CString,
+                                                       SpecNaming.AppendStr(tmpFunc), Quote("_" + tmpFunc)));
+                               else
+                                       code.addLine(DeclareDefine(SpecNaming.CString,
+                                                       SpecNaming.AppendStr(tmpFunc),
+                                                       SpecNaming.EmptyCString));
                                // PostCondition
                                tmpFunc = name + "_" + SpecNaming.PostCondition;
                                if (!construct.postCondition.isEmpty())
@@ -461,29 +523,27 @@ public class CodeGeneratorUtils {
                code.addLine("");
 
                // Define @Print
-               if (!globalConstruct.printState.isEmpty()) {
-                       code.addLine(ShortComment("Define @" + SpecNaming.PrintState));
-                       code.addLine("void _" + SpecNaming.PrintState.toLowerCase() + "("
-                                       + SpecNaming.Method + " " + SpecNaming.Method1 + ") {");
-
-                       // Initialize state struct fields
-                       Code fieldsInit = GenerateStateFieldsInitialization(
-                                       SpecNaming.Method1, SpecNaming.StateInst, globalConstruct);
-                       fieldsInit.align(1);
-                       code.addLines(fieldsInit);
-                       code.addLine("");
-                       if (!globalConstruct.autoGenPrint)
-                               code.addLine(TabbedLine(ShortComment("Execute user-defined state printing code")));
-                       else
-                               // Auto-generated the copy function
-                               code.addLine(TabbedLine(ShortComment("Execute auto-generated state printing code")));
-
-                       // Align the code with one tab
-                       globalConstruct.printState.align(1);
-                       code.addLines(globalConstruct.printState);
-                       code.addLine("}");
-                       code.addLine("");
-               }
+               code.addLine(ShortComment("Define @" + SpecNaming.PrintState));
+               code.addLine("void _" + SpecNaming.PrintState.toLowerCase() + "("
+                               + SpecNaming.Method + " " + SpecNaming.Method1 + ") {");
+
+               // Initialize state struct fields
+               fieldsInit = GenerateStateFieldsInitialization(SpecNaming.Method1,
+                               SpecNaming.StateInst, globalConstruct);
+               fieldsInit.align(1);
+               code.addLines(fieldsInit);
+               code.addLine("");
+               if (!globalConstruct.autoGenPrint)
+                       code.addLine(TabbedLine(ShortComment("Execute user-defined state printing code")));
+               else
+                       // Auto-generated the copy function
+                       code.addLine(TabbedLine(ShortComment("Execute auto-generated state printing code")));
+
+               // Align the code with one tab
+               globalConstruct.printState.align(1);
+               code.addLines(globalConstruct.printState);
+               code.addLine("}");
+               code.addLine("");
 
                // Define @Commutativity
                code.addLine(ShortComment("Define commutativity checking functions"));
@@ -499,15 +559,19 @@ 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("return !(" + rule.condition + ");", 2));
                        code.addLine(TabbedLine("}"));
-                       code.addLine(TabbedLine("return false;"));
+                       code.addLine(TabbedLine("return true;"));
 
                        code.addLine("}");
                        code.addLine("");
@@ -517,16 +581,17 @@ public class CodeGeneratorUtils {
                for (File file : interfaceListMap.keySet()) {
                        ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
                        for (InterfaceConstruct construct : list) {
-                               Code fieldsInit = null;
+                               fieldsInit = null;
 
                                // 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
                                                + ") {");
@@ -542,6 +607,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("");
 
@@ -549,13 +617,14 @@ public class CodeGeneratorUtils {
                                if (!construct.preCondition.isEmpty()) {
                                        code.addLine(ShortComment("Define @"
                                                        + SpecNaming.PreCondition + " for " + name));
-                                       code.addLine("bool _" + name + "_"
-                                                       + SpecNaming.PreCondition + "(" + SpecNaming.Method
-                                                       + " " + SpecNaming.Method1 + ") {");
+                                       code.addLine("bool _" + name + "_" + SpecNaming.PreCondition
+                                                       + "(" + SpecNaming.Method + " " + SpecNaming.Method1
+                                                       + ", " + SpecNaming.Method + " " + SpecNaming.Method2
+                                                       + ") {");
 
                                        // Initialize value struct fields
                                        fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, SpecNaming.InterfaceValueInst,
+                                                       SpecNaming.Method2, SpecNaming.InterfaceValueInst,
                                                        construct);
                                        fieldsInit.align(1);
                                        code.addLines(fieldsInit);
@@ -564,6 +633,66 @@ 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("");
+
+                               }
+                               // Define @JustifyingPrecondition
+                               if (!construct.justifyingPrecondition.isEmpty()) {
+                                       code.addLine(ShortComment("Define @"
+                                                       + SpecNaming.JustifyingPrecondition + " for " + name));
+                                       code.addLine("bool _" + name + "_" + SpecNaming.JustifyingPrecondition
+                                                       + "(" + SpecNaming.Method + " " + SpecNaming.Method1
+                                                       + ", " + SpecNaming.Method + " " + SpecNaming.Method2
+                                                       + ") {");
+
+                                       // Initialize value struct fields
+                                       fieldsInit = GenerateInterfaceFieldsInitialization(
+                                                       SpecNaming.Method2, SpecNaming.InterfaceValueInst,
+                                                       construct);
+                                       fieldsInit.align(1);
+                                       code.addLines(fieldsInit);
+
+                                       construct.justifyingPrecondition.align(1);
+                                       code.addLine(TabbedLine(ShortComment("Execute JustifyingPrecondition")));
+                                       code.addLines(construct.justifyingPrecondition);
+
+                                       // By default, we will return true for @JustifyingPrecondition
+                                       code.addLine(TabbedLine(ShortComment("By default @JustifyingPrecondition returns true")));
+                                       code.addLine(TabbedLine("return true;"));
+
+                                       code.addLine("}");
+                                       code.addLine("");
+
+                               }
+                               // Define @JustifyingPostcondition
+                               if (!construct.justifyingPostcondition.isEmpty()) {
+                                       code.addLine(ShortComment("Define @"
+                                                       + SpecNaming.JustifyingPostcondition + " for " + name));
+                                       code.addLine("bool _" + name + "_" + SpecNaming.JustifyingPostcondition
+                                                       + "(" + SpecNaming.Method + " " + SpecNaming.Method1
+                                                       + ", " + SpecNaming.Method + " " + SpecNaming.Method2
+                                                       + ") {");
+
+                                       // Initialize value struct fields
+                                       fieldsInit = GenerateInterfaceFieldsInitialization(
+                                                       SpecNaming.Method2, SpecNaming.InterfaceValueInst,
+                                                       construct);
+                                       fieldsInit.align(1);
+                                       code.addLines(fieldsInit);
+
+                                       construct.justifyingPostcondition.align(1);
+                                       code.addLine(TabbedLine(ShortComment("Execute JustifyingPostcondition")));
+                                       code.addLines(construct.justifyingPostcondition);
+
+                                       // By default, we will return true for @JustifyingPostcondition
+                                       code.addLine(TabbedLine(ShortComment("By default @JustifyingPostcondition returns true")));
+                                       code.addLine(TabbedLine("return true;"));
+
                                        code.addLine("}");
                                        code.addLine("");
 
@@ -572,14 +701,14 @@ public class CodeGeneratorUtils {
                                if (!construct.postCondition.isEmpty()) {
                                        code.addLine(ShortComment("Define @"
                                                        + SpecNaming.PostCondition + " for " + name));
-                                       code.addLine("bool _" + name + "_"
-                                                       + SpecNaming.PostCondition + "("
-                                                       + SpecNaming.Method + " " + SpecNaming.Method1
+                                       code.addLine("bool _" + name + "_" + SpecNaming.PostCondition
+                                                       + "(" + SpecNaming.Method + " " + SpecNaming.Method1
+                                                       + ", " + SpecNaming.Method + " " + SpecNaming.Method2
                                                        + ") {");
 
                                        // Initialize value struct fields
                                        fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, SpecNaming.InterfaceValueInst,
+                                                       SpecNaming.Method2, SpecNaming.InterfaceValueInst,
                                                        construct);
                                        fieldsInit.align(1);
                                        code.addLines(fieldsInit);
@@ -588,6 +717,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("");
                                }
@@ -623,6 +756,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",
@@ -733,6 +871,34 @@ public class CodeGeneratorUtils {
                                                        + "),";
                                }
                                code.addLine(TabbedLine(line, 2));
+                               // JustifyingPrecondition
+                               line = "new "
+                                               + SpecNaming.NamedFunction
+                                               + "("
+                                               + SpecNaming.AppendStr(name + "_"
+                                                               + SpecNaming.JustifyingPrecondition) + ", "
+                                               + SpecNaming.JustifyingPreconditionType + ", (void*) ";
+                               if (construct.justifyingPrecondition.isEmpty()) {
+                                       line = line + SpecNaming.NullFunc + "),";
+                               } else {
+                                       line = line + "_" + name + "_" + SpecNaming.JustifyingPrecondition
+                                                       + "),";
+                               }
+                               code.addLine(TabbedLine(line, 2));
+                               // JustifyingPostcondition
+                               line = "new "
+                                               + SpecNaming.NamedFunction
+                                               + "("
+                                               + SpecNaming.AppendStr(name + "_"
+                                                               + SpecNaming.JustifyingPostcondition) + ", "
+                                               + SpecNaming.JustifyingPostconditionType + ", (void*) ";
+                               if (construct.justifyingPostcondition.isEmpty()) {
+                                       line = line + SpecNaming.NullFunc + "),";
+                               } else {
+                                       line = line + "_" + name + "_" + SpecNaming.JustifyingPostcondition
+                                                       + "),";
+                               }
+                               code.addLine(TabbedLine(line, 2));
                                // PostCondition
                                line = "new "
                                                + SpecNaming.NamedFunction
@@ -859,14 +1025,15 @@ 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));
-               // Don't leave out the RET field
+               res.addLine(DeclareDefine(structName, "*" + inst, "(" + structName
+                               + "*) " + methodInst + "->" + SpecNaming.MethodValueField));
+               // Don't leave out the C_RET field
                if (!construct.getFunctionHeader().isReturnVoid()) {
                        res.addLine(DeclareDefine(construct.getFunctionHeader().returnType,
-                                       SpecNaming.RET, inst + "->" + SpecNaming.RET));
+                                       SpecNaming.C_RET, inst + "->" + SpecNaming.C_RET));
                }
                // For arguments
                for (VariableDeclaration decl : construct.getFunctionHeader().args) {
@@ -900,6 +1067,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) + ");");
@@ -915,6 +1086,10 @@ public class CodeGeneratorUtils {
                        code.addLine(prefixTabs + SpecNaming.CreateOPClearDefineAnnoFunc
                                        + "();");
                        break;
+               case OPClearDefineUnattached:
+                       code.addLine(prefixTabs
+                                       + SpecNaming.CreateOPClearDefineUnattachedFunc + "();");
+                       break;
                default:
                        break;
                }
@@ -973,6 +1148,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);
@@ -1004,7 +1180,7 @@ public class CodeGeneratorUtils {
                // Call the actual function
                code.addLine(prefixTabs + "\t"
                                + ShortComment("Call the actual function"));
-               // bool RET = dequeue_ORIGINAL__(q, retVal, reclaimNode);
+               // bool C_RET = dequeue_ORIGINAL__(q, retVal, reclaimNode);
                code.addLine(prefixTabs + "\t"
                                + construct.getFunctionHeader().getRenamedCall() + ";");
                code.addLine("");
@@ -1015,14 +1191,15 @@ public class CodeGeneratorUtils {
                // The very first assignment "
                code.addLine(prefixTabs
                                + "\t"
-                               + DeclareDefine(name, "*" + SpecNaming.InterfaceValueInst,
-                                               SpecNaming.New + Brace(name)));
-               // Don't leave out the RET field
+                               + DeclareDefine(structName,
+                                               "*" + SpecNaming.InterfaceValueInst, SpecNaming.New
+                                                               + Brace(structName)));
+               // Don't leave out the C_RET field
                if (!construct.getFunctionHeader().isReturnVoid())
                        code.addLine(prefixTabs
                                        + "\t"
                                        + AssignToPtr(SpecNaming.InterfaceValueInst,
-                                                       SpecNaming.RET, SpecNaming.RET));
+                                                       SpecNaming.C_RET, SpecNaming.C_RET));
                // For arguments
                for (VariableDeclaration decl : construct.getFunctionHeader().args)
                        code.addLine(prefixTabs
@@ -1043,9 +1220,17 @@ 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 + ";");
+                       code.addLine(prefixTabs + "\treturn " + SpecNaming.C_RET + ";");
                code.addLine(prefixTabs + "}");
 
                return code;