add destructors
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeGeneratorUtils.java
index 2914d49b6d7e1b41ac40beeae61acd11f96896f2..694695cf48ca9db30cae73666101a9ccb75ef066 100644 (file)
@@ -170,10 +170,6 @@ public class CodeGeneratorUtils {
                code.addLine("");
 
                code.addLine(ShortComment("Declaration of some c-strings (CSTR)"));
-               code.addLine(ShortComment("A special empty string"));
-               code.addLine(Declare("extern " + SpecNaming.CString,
-                               SpecNaming.EmptyCString));
-               code.addLine("");
 
                // Interface name strings
                code.addLine(ShortComment("Interface name strings"));
@@ -187,14 +183,6 @@ public class CodeGeneratorUtils {
                }
                code.addLine("");
 
-               // Commutativity rule strings
-               code.addLine(ShortComment("Commutativity rule strings"));
-               for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
-                       code.addLine(Declare("extern " + SpecNaming.CString,
-                                       SpecNaming.AppendStr(SpecNaming.Commutativity + i)));
-               }
-               code.addLine("");
-
                // Ordering points label strings
                code.addLine(ShortComment("Ordering points label strings"));
                for (String label : OPLabelSet) {
@@ -203,48 +191,6 @@ public class CodeGeneratorUtils {
                }
                code.addLine("");
 
-               // Special function name strings
-               code.addLine(ShortComment("Special function name strings"));
-               code.addLine(Declare("extern " + SpecNaming.CString,
-                               SpecNaming.AppendStr(SpecNaming.InitalState)));
-               code.addLine(Declare("extern " + SpecNaming.CString,
-                               SpecNaming.AppendStr(SpecNaming.CopyState)));
-               code.addLine(Declare("extern " + SpecNaming.CString,
-                               SpecNaming.AppendStr(SpecNaming.FinalState)));
-               code.addLine(Declare("extern " + SpecNaming.CString,
-                               SpecNaming.AppendStr(SpecNaming.PrintState)));
-               code.addLine("");
-
-               // Interface name strings
-               for (File file : interfaceListMap.keySet()) {
-                       ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
-                       for (InterfaceConstruct construct : list) {
-                               String name = construct.getName();
-                               code.addLine(ShortComment(name + " function strings"));
-                               // Transition
-                               String tmpFunc = name + "_" + SpecNaming.Transition;
-                               code.addLine(Declare("extern " + SpecNaming.CString,
-                                               SpecNaming.AppendStr(tmpFunc)));
-                               // PreCondition
-                               tmpFunc = name + "_" + SpecNaming.PreCondition;
-                               code.addLine(Declare("extern " + SpecNaming.CString,
-                                               SpecNaming.AppendStr(tmpFunc)));
-                               // SideEffect
-                               tmpFunc = name + "_" + SpecNaming.SideEffect;
-                               code.addLine(Declare("extern " + SpecNaming.CString,
-                                               SpecNaming.AppendStr(tmpFunc)));
-                               // PostCondition
-                               tmpFunc = name + "_" + SpecNaming.PostCondition;
-                               code.addLine(Declare("extern " + SpecNaming.CString,
-                                               SpecNaming.AppendStr(tmpFunc)));
-                               // Print
-                               tmpFunc = name + "_" + SpecNaming.PrintValue;
-                               code.addLine(Declare("extern " + SpecNaming.CString,
-                                               SpecNaming.AppendStr(tmpFunc)));
-                               code.addLine("");
-                       }
-               }
-
                // Declare customized value struct
                for (File file : interfaceListMap.keySet()) {
                        ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
@@ -268,89 +214,6 @@ public class CodeGeneratorUtils {
                        }
                }
 
-               // Declare @Initial
-               code.addLine(ShortComment("Declare @" + SpecNaming.InitalState));
-               code.addLine("void _" + SpecNaming.InitalState.toLowerCase() + "("
-                               + SpecNaming.Method + " " + SpecNaming.Method1 + ");");
-               code.addLine("");
-               // Declare @Copy
-               code.addLine(ShortComment("Declare @" + SpecNaming.CopyState));
-               code.addLine("void _" + SpecNaming.CopyState.toLowerCase() + "("
-                               + SpecNaming.Method + " " + "dest, " + SpecNaming.Method
-                               + " src);");
-               code.addLine("");
-               // Declare @Print
-               code.addLine(ShortComment("Declare @" + SpecNaming.PrintState));
-               if (!globalConstruct.printState.isEmpty()) {
-                       code.addLine("void _" + SpecNaming.PrintState.toLowerCase() + "("
-                                       + SpecNaming.Method + " " + SpecNaming.Method1 + ");");
-                       code.addLine("");
-               }
-
-               // Declare @Commutativity
-               code.addLine(ShortComment("Declare commutativity checking functions"));
-               for (int i = 1; i <= globalConstruct.commutativityRules.size(); i++) {
-                       code.addLine("bool _check" + SpecNaming.Commutativity + i + "("
-                                       + SpecNaming.Method + " m1, " + SpecNaming.Method + " m2);");
-               }
-               code.addLine("");
-
-               // Declare customized interface functions
-               for (File file : interfaceListMap.keySet()) {
-                       ArrayList<InterfaceConstruct> list = interfaceListMap.get(file);
-                       for (InterfaceConstruct construct : list) {
-                               // Declare interface functions
-                               String name = construct.getName();
-                               code.addLine("/**********    " + name
-                                               + " functions    **********/");
-                               // Declare @Transition for INTERFACE
-                               code.addLine(ShortComment("Declare @" + SpecNaming.Transition
-                                               + " for " + name));
-                               code.addLine("void _" + name + "_" + SpecNaming.Transition
-                                               + "(" + SpecNaming.Method + " " + SpecNaming.Method1
-                                               + ", " + SpecNaming.Method + " " + SpecNaming.Method2
-                                               + ");");
-                               code.addLine("");
-                               // Declare @PreCondition
-                               if (!construct.preCondition.isEmpty()) {
-                                       code.addLine(ShortComment("Declare @"
-                                                       + SpecNaming.PreCondition + " for " + name));
-                                       code.addLine("bool _" + name + "_"
-                                                       + SpecNaming.PreCondition + "(" + SpecNaming.Method
-                                                       + " " + SpecNaming.Method1 + ");");
-                                       code.addLine("");
-                               }
-                               // Declare @SideEffect
-                               if (!construct.sideEffect.isEmpty()) {
-                                       code.addLine(ShortComment("Declare @"
-                                                       + SpecNaming.SideEffect + " for " + name));
-                                       code.addLine("void _" + name + "_" + SpecNaming.SideEffect
-                                                       + "(" + SpecNaming.Method + " "
-                                                       + SpecNaming.Method1 + ");");
-                                       code.addLine("");
-                               }
-                               // Declare @PostCondition
-                               if (!construct.postCondition.isEmpty()) {
-                                       code.addLine(ShortComment("Declare @"
-                                                       + SpecNaming.PostCondition + " for " + name));
-                                       code.addLine("bool _" + name + "_"
-                                                       + SpecNaming.PostCondition + "("
-                                                       + SpecNaming.Method + " " + SpecNaming.Method1
-                                                       + ");");
-                                       code.addLine("");
-                               }
-                               // Declare @Print
-                               if (!construct.print.isEmpty()) {
-                                       code.addLine(ShortComment("Declare @"
-                                                       + SpecNaming.PrintValue + " for " + name));
-                                       code.addLine("void _" + name + "_" + SpecNaming.PrintValue
-                                                       + "(" + SpecNaming.Method + " "
-                                                       + SpecNaming.Method1 + ");");
-                                       code.addLine("");
-                               }
-                       }
-               }
-
                // Declare INIT annotation instrumentation function
                code.addLine(ShortComment("Declare INIT annotation instrumentation function"));
                code.addLine("void _createInitAnnotation();");
@@ -410,7 +273,23 @@ public class CodeGeneratorUtils {
                        code.addLine(TabbedLine(Declare(decl)));
                }
                code.addLine("");
+               // Define state destructor
+               code.addLine(TabbedLine(ShortComment("Define state destructor")));
+               code.addLine(TabbedLine("~" + SpecNaming.StateStruct + "() {"));
+               if (!globalConstruct.autoGenClear) {
+                       code.addLine(TabbedLine(
+                                       ShortComment("Execute user-defined state clear code"), 2));
+               } else {
+                       code.addLine(TabbedLine(
+                                       ShortComment("Execute auto-generated state clear code"), 2));
+               }
+               globalConstruct.clearState.align(2);
+               code.addLines(globalConstruct.clearState);
+               code.addLine(TabbedLine("}"));
+               code.addLine("");
+
                code.addLine(TabbedLine("SNAPSHOTALLOC"));
+               code.addLine("");
                code.addLine("} " + SpecNaming.StateStruct + ";");
                code.addLine("");
                code.addLine("");
@@ -460,6 +339,9 @@ public class CodeGeneratorUtils {
                code.addLine(DeclareDefine(SpecNaming.CString,
                                SpecNaming.AppendStr(SpecNaming.CopyState), Quote("_"
                                                + SpecNaming.CopyState.toLowerCase())));
+               code.addLine(DeclareDefine(SpecNaming.CString,
+                               SpecNaming.AppendStr(SpecNaming.ClearState), Quote("_"
+                                               + SpecNaming.ClearState.toLowerCase())));
                code.addLine(DeclareDefine(SpecNaming.CString,
                                SpecNaming.AppendStr(SpecNaming.FinalState), Quote("_"
                                                + SpecNaming.FinalState.toLowerCase())));
@@ -487,15 +369,6 @@ public class CodeGeneratorUtils {
                                        code.addLine(DeclareDefine(SpecNaming.CString,
                                                        SpecNaming.AppendStr(tmpFunc),
                                                        SpecNaming.EmptyCString));
-                               // SideEffect
-                               tmpFunc = name + "_" + SpecNaming.SideEffect;
-                               if (!construct.sideEffect.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())
@@ -529,7 +402,11 @@ public class CodeGeneratorUtils {
                        code.addLine(TabbedLine("#define " + decl.name + " "
                                        + SpecNaming.StateInst + "->" + decl.name));
                }
-               code.addLine(TabbedLine(ShortComment("User-defined intial state code")));
+               if (!globalConstruct.autoGenInitial)
+                       code.addLine(TabbedLine(ShortComment("User-defined state intialization code")));
+               else
+                       // Auto-generated the initialization function
+                       code.addLine(TabbedLine(ShortComment("Auto-generated state intialization code")));
                // Align the code with one tab
                globalConstruct.initState.align(1);
                code.addLines(globalConstruct.initState);
@@ -568,6 +445,21 @@ public class CodeGeneratorUtils {
                code.addLine("}");
                code.addLine("");
 
+               // Define @Clear
+               code.addLine(ShortComment("Define @" + SpecNaming.ClearState));
+               code.addLine("void _" + SpecNaming.ClearState.toLowerCase() + "("
+                               + SpecNaming.Method + " " + SpecNaming.Method1 + ") {");
+               // Retrieve the state
+               code.addLine(TabbedLine(ShortComment("Retrieve the state")));
+               code.addLine(TabbedLine(DeclareDefine(SpecNaming.StateStruct, "*"
+                               + SpecNaming.StateInst, "(" + SpecNaming.StateStruct + "*) "
+                               + SpecNaming.Method1 + "->state")));
+               // Explicitly call the state destructor
+               code.addLine(TabbedLine(ShortComment("Explicitly call the state destructor")));
+               code.addLine(TabbedLine("delete " + SpecNaming.StateInst + ";"));
+               code.addLine("}");
+               code.addLine("");
+
                // Define @Print
                if (!globalConstruct.printState.isEmpty()) {
                        code.addLine(ShortComment("Define @" + SpecNaming.PrintState));
@@ -580,7 +472,12 @@ public class CodeGeneratorUtils {
                        fieldsInit.align(1);
                        code.addLines(fieldsInit);
                        code.addLine("");
-                       code.addLine(TabbedLine(ShortComment("Execute the print-out")));
+                       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);
@@ -636,7 +533,8 @@ public class CodeGeneratorUtils {
 
                                // Initialize value struct fields
                                fieldsInit = GenerateInterfaceFieldsInitialization(
-                                               SpecNaming.Method2, "value", construct);
+                                               SpecNaming.Method2, SpecNaming.InterfaceValueInst,
+                                               construct);
                                fieldsInit.align(1);
                                code.addLines(fieldsInit);
 
@@ -657,7 +555,8 @@ public class CodeGeneratorUtils {
 
                                        // Initialize value struct fields
                                        fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, "value", construct);
+                                                       SpecNaming.Method1, SpecNaming.InterfaceValueInst,
+                                                       construct);
                                        fieldsInit.align(1);
                                        code.addLines(fieldsInit);
 
@@ -669,27 +568,6 @@ public class CodeGeneratorUtils {
                                        code.addLine("");
 
                                }
-                               // Define @SideEffect
-                               if (!construct.sideEffect.isEmpty()) {
-                                       code.addLine(ShortComment("Define @"
-                                                       + SpecNaming.SideEffect + " for " + name));
-                                       code.addLine("void _" + name + "_" + SpecNaming.SideEffect
-                                                       + "(" + SpecNaming.Method + " "
-                                                       + SpecNaming.Method1 + ") {");
-
-                                       // Initialize value struct fields
-                                       fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, "value", construct);
-                                       fieldsInit.align(1);
-                                       code.addLines(fieldsInit);
-
-                                       construct.sideEffect.align(1);
-                                       code.addLine(TabbedLine(ShortComment("Execute SideEffect")));
-                                       code.addLines(construct.sideEffect);
-
-                                       code.addLine("}");
-                                       code.addLine("");
-                               }
                                // Define @PostCondition
                                if (!construct.postCondition.isEmpty()) {
                                        code.addLine(ShortComment("Define @"
@@ -701,7 +579,8 @@ public class CodeGeneratorUtils {
 
                                        // Initialize value struct fields
                                        fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, "value", construct);
+                                                       SpecNaming.Method1, SpecNaming.InterfaceValueInst,
+                                                       construct);
                                        fieldsInit.align(1);
                                        code.addLines(fieldsInit);
 
@@ -721,12 +600,17 @@ public class CodeGeneratorUtils {
                                                        + SpecNaming.Method1 + ") {");
                                        // Initialize value struct fields
                                        fieldsInit = GenerateInterfaceFieldsInitialization(
-                                                       SpecNaming.Method1, "value", construct);
+                                                       SpecNaming.Method1, SpecNaming.InterfaceValueInst,
+                                                       construct);
                                        fieldsInit.align(1);
                                        code.addLines(fieldsInit);
 
                                        construct.print.align(1);
-                                       code.addLine(TabbedLine(ShortComment("Execute Print")));
+                                       if (!construct.autoGenPrint)
+                                               code.addLine(TabbedLine(ShortComment("Execute user-defined value printing code")));
+                                       else
+                                               // Auto-generated the value printing function
+                                               code.addLine(TabbedLine(ShortComment("Execute auto-generated value printing code")));
                                        code.addLines(construct.print);
 
                                        code.addLine("}");
@@ -793,16 +677,16 @@ public class CodeGeneratorUtils {
                                + SpecNaming.AppendStr(SpecNaming.CopyState) + ", "
                                + SpecNaming.CopyState.toUpperCase() + ", " + "(void*) _"
                                + SpecNaming.CopyState.toLowerCase() + "),", 2));
+               // new NamedFunction(_Clear_str, CLEAR, (void*) _clear),
+               code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
+                               + SpecNaming.AppendStr(SpecNaming.ClearState) + ", "
+                               + SpecNaming.ClearState.toUpperCase() + ", " + "(void*) _"
+                               + SpecNaming.ClearState.toLowerCase() + "),", 2));
                // new NamedFunction(_Print_str, PRINT_STATE, (void*) _print),
-               line = "new " + SpecNaming.NamedFunction + "("
+               code.addLine(TabbedLine("new " + SpecNaming.NamedFunction + "("
                                + SpecNaming.AppendStr(SpecNaming.PrintState) + ", "
-                               + SpecNaming.PrintStateType + ", " + "(void*)";
-               if (globalConstruct.printState.isEmpty()) {
-                       line = line + SpecNaming.NullFunc + "),";
-               } else {
-                       line = line + "_" + SpecNaming.PrintState.toLowerCase() + "),";
-               }
-               code.addLine(TabbedLine(line, 2));
+                               + SpecNaming.PrintStateType + ", " + "(void*) _"
+                               + SpecNaming.PrintState.toLowerCase() + "),", 2));
                // commuteRules, CommuteRuleSize);
                code.addLine(TabbedLine(SpecNaming.CommutativityRuleInst + ", "
                                + SpecNaming.CommutativityRuleSizeInst + ");", 2));
@@ -849,20 +733,6 @@ public class CodeGeneratorUtils {
                                                        + "),";
                                }
                                code.addLine(TabbedLine(line, 2));
-                               // SideEffect
-                               line = "new "
-                                               + SpecNaming.NamedFunction
-                                               + "("
-                                               + SpecNaming.AppendStr(name + "_"
-                                                               + SpecNaming.SideEffect) + ", "
-                                               + SpecNaming.SideEffectType + ", (void*) ";
-                               if (construct.sideEffect.isEmpty()) {
-                                       line = line + SpecNaming.NullFunc + "),";
-                               } else {
-                                       line = line + "_" + name + "_" + SpecNaming.SideEffect
-                                                       + "),";
-                               }
-                               code.addLine(TabbedLine(line, 2));
                                // PostCondition
                                line = "new "
                                                + SpecNaming.NamedFunction
@@ -992,11 +862,11 @@ public class CodeGeneratorUtils {
                res.addLine(ShortComment("Initialize fields for " + name));
                // The very first assignment "
                res.addLine(DeclareDefine(name, "*" + inst, "(" + name + "*) "
-                               + methodInst + "->value"));
+                               + methodInst + "->" + SpecNaming.MethodValueField));
                // Don't leave out the RET field
                if (!construct.getFunctionHeader().isReturnVoid()) {
                        res.addLine(DeclareDefine(construct.getFunctionHeader().returnType,
-                                       SpecNaming.RET, "value->" + SpecNaming.RET));
+                                       SpecNaming.RET, inst + "->" + SpecNaming.RET));
                }
                // For arguments
                for (VariableDeclaration decl : construct.getFunctionHeader().args) {
@@ -1021,28 +891,29 @@ public class CodeGeneratorUtils {
                String curLine = construct.annotation;
                String label = construct.label;
                String prefixTabs = curLine.substring(0, curLine.indexOf("/**"));
-               code.addLine(prefixTabs + "if (" + construct.condition + ")");
+               if (!construct.condition.equals("true")) {
+                       code.addLine(prefixTabs + "if (" + construct.condition + ")");
+                       prefixTabs = prefixTabs + "\t";
+               }
+
                switch (construct.type) {
                case OPDefine:
-                       code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPDefineAnnoFunc
-                                       + "();");
+                       code.addLine(prefixTabs + SpecNaming.CreateOPDefineAnnoFunc + "();");
                        break;
                case PotentialOP:
-                       code.addLine(prefixTabs + "\t"
-                                       + SpecNaming.CreatePotentialOPAnnoFunc + "("
-                                       + SpecNaming.AppendStr(label) + ");");
+                       code.addLine(prefixTabs + SpecNaming.CreatePotentialOPAnnoFunc
+                                       + "(" + SpecNaming.AppendStr(label) + ");");
                        break;
                case OPCheck:
-                       code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPCheckAnnoFunc
-                                       + "(" + SpecNaming.AppendStr(label) + ");");
+                       code.addLine(prefixTabs + SpecNaming.CreateOPCheckAnnoFunc + "("
+                                       + SpecNaming.AppendStr(label) + ");");
                        break;
                case OPClear:
-                       code.addLine(prefixTabs + "\t" + SpecNaming.CreateOPClearAnnoFunc
-                                       + "();");
+                       code.addLine(prefixTabs + SpecNaming.CreateOPClearAnnoFunc + "();");
                        break;
                case OPClearDefine:
-                       code.addLine(prefixTabs + "\t"
-                                       + SpecNaming.CreateOPClearDefineAnnoFunc + "();");
+                       code.addLine(prefixTabs + SpecNaming.CreateOPClearDefineAnnoFunc
+                                       + "();");
                        break;
                default:
                        break;
@@ -1142,16 +1013,22 @@ public class CodeGeneratorUtils {
                code.addLine(prefixTabs + "\t"
                                + ShortComment("Initialize the value struct"));
                // The very first assignment "
-               code.addLine(prefixTabs + "\t"
-                               + DeclareDefine(name, "*value", SpecNaming.New + Brace(name)));
+               code.addLine(prefixTabs
+                               + "\t"
+                               + DeclareDefine(name, "*" + SpecNaming.InterfaceValueInst,
+                                               SpecNaming.New + Brace(name)));
                // Don't leave out the RET field
                if (!construct.getFunctionHeader().isReturnVoid())
-                       code.addLine(prefixTabs + "\t"
-                                       + AssignToPtr("value", SpecNaming.RET, SpecNaming.RET));
+                       code.addLine(prefixTabs
+                                       + "\t"
+                                       + AssignToPtr(SpecNaming.InterfaceValueInst,
+                                                       SpecNaming.RET, SpecNaming.RET));
                // For arguments
                for (VariableDeclaration decl : construct.getFunctionHeader().args)
-                       code.addLine(prefixTabs + "\t"
-                                       + AssignToPtr("value", decl.name, decl.name));
+                       code.addLine(prefixTabs
+                                       + "\t"
+                                       + AssignToPtr(SpecNaming.InterfaceValueInst, decl.name,
+                                                       decl.name));
                code.addLine("");
 
                // Store the value info into the current MethodCall
@@ -1159,9 +1036,11 @@ public class CodeGeneratorUtils {
                code.addLine(prefixTabs
                                + "\t"
                                + ShortComment("Store the value info into the current MethodCall"));
-               code.addLine(prefixTabs + "\t"
+               code.addLine(prefixTabs
+                               + "\t"
                                + SpecNaming.SetInterfaceBeginAnnoValueFunc
-                               + Brace(SpecNaming.AnnoInterfaceInfoInst + ", value") + ";");
+                               + Brace(SpecNaming.AnnoInterfaceInfoInst + ", "
+                                               + SpecNaming.InterfaceValueInst) + ";");
                code.addLine("");
 
                // Return if necessary