fixed commutativity rule
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index 7dcaaa0acec7a605c3bedca3858964d6a9bb6e47..f85ec8e674176997d696414b19517167db700377 100644 (file)
@@ -214,10 +214,15 @@ public class CodeGeneratorUtils {
                                                + 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)));
@@ -408,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())
@@ -546,9 +569,9 @@ public class CodeGeneratorUtils {
                        code.addLine(TabbedLine(
                                        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("");
@@ -594,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);
@@ -616,19 +640,75 @@ public class CodeGeneratorUtils {
                                        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("");
+
                                }
                                // Define @PostCondition
                                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);
@@ -791,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
@@ -922,10 +1030,10 @@ public class CodeGeneratorUtils {
                // The very first assignment "
                res.addLine(DeclareDefine(structName, "*" + inst, "(" + structName
                                + "*) " + methodInst + "->" + SpecNaming.MethodValueField));
-               // Don't leave out the RET field
+               // 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) {
@@ -1072,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("");
@@ -1086,12 +1194,12 @@ public class CodeGeneratorUtils {
                                + DeclareDefine(structName,
                                                "*" + SpecNaming.InterfaceValueInst, SpecNaming.New
                                                                + Brace(structName)));
-               // Don't leave out the RET field
+               // 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
@@ -1122,7 +1230,7 @@ public class CodeGeneratorUtils {
 
                // 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;