X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=src%2Fedu%2Fuci%2Feecs%2FcodeGenerator%2FCodeGeneratorUtils.java;h=7dcaaa0acec7a605c3bedca3858964d6a9bb6e47;hb=195f28450704612d09341f8262b31254e7852b62;hp=005397e5c68b200c9ab244ea583dfa894e54a042;hpb=45a94756dec1364837f3b4b116a33894813f8325;p=cdsspec-compiler.git diff --git a/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java b/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java index 005397e..7dcaaa0 100644 --- a/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java +++ b/src/edu/uci/eecs/codeGenerator/CodeGeneratorUtils.java @@ -12,6 +12,7 @@ import java.io.IOException; import edu.uci.eecs.specExtraction.Code; import edu.uci.eecs.specExtraction.CommutativityRule; +import edu.uci.eecs.specExtraction.DefineConstruct; import edu.uci.eecs.specExtraction.EntryConstruct; import edu.uci.eecs.specExtraction.FunctionHeader; import edu.uci.eecs.specExtraction.GlobalConstruct; @@ -147,37 +148,41 @@ public class CodeGeneratorUtils { '_') + "_H"); code.addLine(""); + // FIXME: We have included ad-hoc header files here // System included headers code.addLine(ShortComment("System included headers go here")); - for (String header : SpecNaming.includedHeadersList) { - code.addLine(IncludeHeader(header)); - } + code.addLine(IncludeHeader(SpecNaming.SPECANNOTATION_API)); + code.addLine(IncludeHeader(SpecNaming.STDLIB)); + code.addLine(""); // Users included headers - // FIXME: We don't add user-defined headers, but as a workaround we only add forward class. code.addLine(ShortComment("User included headers go here")); for (String header : headerFiles) { code.addLine(IncludeHeader(header)); } code.addLine(""); - -// code.addLine(ShortComment("Forward declaration goes here")); -// for (String type : extractor.forwardClass) { -// code.addLine("class " + type + ";"); -// } -// code.addLine(""); - - code.addLine("using namespace std;"); + + // Decalre extern "C" --- begin + code.addLine("#ifdef __cplusplus"); + code.addLine("extern \"C\" {"); + code.addLine("#endif"); code.addLine(""); + + // Declare _createOPDefineUnattached + code.addLine(ShortComment("Declare _createOPDefineUnattached")); + // void _createOPDefineUnattached(); + code.addLine("void _createOPDefineUnattached();"); 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)); + // 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 code.addLine(ShortComment("Interface name strings")); for (File file : interfaceListMap.keySet()) { @@ -190,14 +195,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) { @@ -206,68 +203,16 @@ 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 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 StateStruct - code.addLine(ShortComment("Declare customized StateStruct")); - code.addLine("typedef struct " + SpecNaming.StateStruct + " {"); - for (VariableDeclaration decl : globalConstruct.declState) { - code.addLine(TabbedLine(Declare(decl))); - } - code.addLine(""); - code.addLine(TabbedLine("SNAPSHOTALLOC")); - code.addLine("} " + SpecNaming.StateStruct + ";"); - code.addLine(""); - // Declare customized value struct for (File file : interfaceListMap.keySet()) { ArrayList list = interfaceListMap.get(file); 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")) @@ -277,98 +222,23 @@ public class CodeGeneratorUtils { for (VariableDeclaration decl : funcHeader.args) { code.addLine(TabbedLine(Declare(decl))); } - code.addLine("} " + name + ";"); + code.addLine("} " + structName + ";"); code.addLine(""); } } - // 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 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();"); code.addLine(""); + + // Decalre extern "C" --- begin + code.addLine("#ifdef __cplusplus"); + code.addLine("};"); + code.addLine("#endif"); + code.addLine(""); + + // Declare #endif code.addLine("#endif"); return code; @@ -391,6 +261,7 @@ public class CodeGeneratorUtils { Code code = new Code(); String line = null; + Code fieldsInit = null; // Add auto-generated comments code.addLine("/**"); @@ -401,9 +272,67 @@ public class CodeGeneratorUtils { code.addLine(""); code.addLine("#include " + SpecNaming.CDSSpecGeneratedHeader); + code.addLine("#include " + SpecNaming.CDSANNOTATE); + code.addLine("#include " + SpecNaming.SPEC_COMMON); + code.addLine("#include " + SpecNaming.METHODCALL); + code.addLine("#include " + SpecNaming.CDSSPEC); + code.addLine("#include " + SpecNaming.SPECANNOTATION); + code.addLine(""); + code.addLine(""); + + // Declare customized StateStruct + code.addLine(ShortComment("Declare customized StateStruct")); + code.addLine("typedef struct " + SpecNaming.StateStruct + " {"); + for (VariableDeclaration decl : globalConstruct.declState) { + 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(""); + + // 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, @@ -449,6 +378,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()))); @@ -476,15 +408,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()) @@ -518,7 +441,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); @@ -557,25 +484,43 @@ 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)); - 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(""); - code.addLine(TabbedLine(ShortComment("Execute the print-out"))); - // 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")); @@ -591,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("}")); @@ -609,23 +558,25 @@ public class CodeGeneratorUtils { for (File file : interfaceListMap.keySet()) { ArrayList 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 + ") {"); // Initialize value struct fields fieldsInit = GenerateInterfaceFieldsInitialization( - SpecNaming.Method2, "value", construct); + SpecNaming.Method2, SpecNaming.InterfaceValueInst, + construct); fieldsInit.align(1); code.addLines(fieldsInit); @@ -633,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(""); @@ -646,7 +600,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); @@ -654,30 +609,13 @@ public class CodeGeneratorUtils { code.addLine(TabbedLine(ShortComment("Execute PreCondition"))); code.addLines(construct.preCondition); - code.addLine("}"); - 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); + // 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 @PostCondition if (!construct.postCondition.isEmpty()) { @@ -690,7 +628,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); @@ -698,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(""); } @@ -710,12 +653,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("}"); @@ -728,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", @@ -782,16 +735,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)); @@ -838,20 +791,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 @@ -978,14 +917,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 + "->value")); + 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, - SpecNaming.RET, "value->" + SpecNaming.RET)); + SpecNaming.RET, inst + "->" + SpecNaming.RET)); } // For arguments for (VariableDeclaration decl : construct.getFunctionHeader().args) { @@ -1010,28 +950,37 @@ 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 OPDefineUnattached: + code.addLine(prefixTabs + SpecNaming.CreateOPDefineUnattachedFunc + "();"); 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; + case OPClearDefineUnattached: + code.addLine(prefixTabs + + SpecNaming.CreateOPClearDefineUnattachedFunc + "();"); break; default: break; @@ -1058,6 +1007,25 @@ public class CodeGeneratorUtils { return res; } + /** + *

+ * This function generates the code to be inserted right after the "@Define" + * construct (instrumentation code) + *

+ * + * @param construct + * The corresponding entry construct + * @return + */ + public static Code Generate4Define(DefineConstruct construct) { + Code code = new Code(); + code.addLine(""); + code.addLine("/********** User-defined code in annotation (BEGIN) **********/"); + code.addLines(construct.code); + code.addLine("/********** User-defined code in annotation (END) **********/"); + return code; + } + /** *

* This function generates the new interface wrapper code to be inserted @@ -1072,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); @@ -1089,11 +1058,11 @@ public class CodeGeneratorUtils { } else { code.addLine(beginLine); } - // Instrument with the INTERFACE_BEGIN annotation + // Instrument with the INTERFACE_BEGIN annotations code.addLine(prefixTabs + "\t" + ShortComment("Instrument with the INTERFACE_BEGIN annotation")); - // AnnoInterfaceInfo *info = _createInterfaceBeginAnnotation(_DEQ_str); + // CAnnoInterfaceInfo info = _createInterfaceBeginAnnotation(_DEQ_str); code.addLine(prefixTabs + "\t" + DeclareDefine(SpecNaming.AnnoInterfaceInfo, "*" @@ -1112,28 +1081,45 @@ public class CodeGeneratorUtils { code.addLine(prefixTabs + "\t" + ShortComment("Initialize the value struct")); // The very first assignment " - code.addLine(prefixTabs + "\t" - + DeclareDefine(name, "*value", "new " + name)); + code.addLine(prefixTabs + + "\t" + + DeclareDefine(structName, + "*" + SpecNaming.InterfaceValueInst, SpecNaming.New + + Brace(structName))); // 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 + // _setInterfaceBeginAnnotationValue(info, value); code.addLine(prefixTabs + "\t" + ShortComment("Store the value info into the current MethodCall")); code.addLine(prefixTabs + "\t" - + AssignToPtr(SpecNaming.AnnoInterfaceInfoInst, "value", - "value")); + + SpecNaming.SetInterfaceBeginAnnoValueFunc + + Brace(SpecNaming.AnnoInterfaceInfoInst + ", " + + 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 + ";");