From 275b0c45df6810492526210aff9c914848fbdf3b Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Wed, 16 Oct 2013 19:18:25 -0700 Subject: [PATCH] generating code --- .dir-locals.el | 1 - .gitignore | 15 -- .../codeGenerator/CodeGenerator.java | 161 +++++++++++++++--- .../codeGenerator/CodeVariables.java | 29 ++++ .../InterfaceWrongFormatException.java | 7 + .../codeGenerator/SemanticsChecker.java | 10 ++ test.cc | 81 +++++++++ 7 files changed, 264 insertions(+), 40 deletions(-) delete mode 100644 .dir-locals.el delete mode 100644 .gitignore create mode 100644 src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java create mode 100644 src/edu/uci/eecs/specCompiler/codeGenerator/InterfaceWrongFormatException.java create mode 100644 test.cc diff --git a/.dir-locals.el b/.dir-locals.el deleted file mode 100644 index ce85e5f..0000000 --- a/.dir-locals.el +++ /dev/null @@ -1 +0,0 @@ -((nil . ((indent-tabs-mode . t)))) \ No newline at end of file diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 8df9862..0000000 --- a/.gitignore +++ /dev/null @@ -1,15 +0,0 @@ -# generic types -*.o -*.swp -*.swo -*.so -*~ -*.dot -.*.d -*.pdf - -# files in this directory -/tags -/doc/docs -/benchmarks -/README.html diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java index ab3e7ec..8f4537a 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java @@ -7,7 +7,9 @@ import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; +import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar; import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct; import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct; import edu.uci.eecs.specCompiler.specExtraction.Construct; @@ -92,32 +94,94 @@ public class CodeGenerator { */ private void globalConstruct2Code(SpecConstruct inst) { int lineNum = inst.endLineNum + 1; - GlobalConstruct construct = (GlobalConstruct) inst.construct; + GlobalConstruct construct = (GlobalConstruct) inst.construct; ArrayList newCode = new ArrayList(); - + // Generate the inner class definition - newCode.add("class Sequential {\n"); + newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n"); newCode.add("public:\n"); - + // Generate the code in global construct first SequentialDefineSubConstruct globalCode = construct.code; breakCodeLines(newCode, globalCode.declareVar); - breakCodeLines(newCode, globalCode.defineFunc); - + // Generate code from the DefineVar, __COND_SAT__ and __ID__ - - + // __COND_SAT__ + newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN + + "> " + CodeVariables.SPEC_CONDITION + ";"); + // __ID__ + newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG + + "> " + CodeVariables.SPEC_ID + ";"); + + // DefineVars + for (String interfaceName : _semantics.interfaceName2Construct.keySet()) { + InterfaceConstruct iConstruct = (InterfaceConstruct) _semantics.interfaceName2Construct + .get(interfaceName).construct; + ArrayList defineVars = iConstruct.action.defineVars; + for (int i = 0; i < defineVars.size(); i++) { + DefineVar var = defineVars.get(i); + newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + var.varType + + "> " + var.getNewVarName() + ";"); + } + } + + // Enum of all interface + String enumDefinition = "enum " + CodeVariables.SPEC_INTERFACE_ENUM + + " {"; + Iterator iter = _semantics.interfaceName2Construct.keySet() + .iterator(); + String interfaceName; + if (iter.hasNext()) { + interfaceName = iter.next(); + enumDefinition = enumDefinition + "_" + interfaceName + "_"; + } + while (iter.hasNext()) { + interfaceName = iter.next(); + enumDefinition = enumDefinition + ", _" + interfaceName + "_"; + } + enumDefinition = enumDefinition + "};"; + newCode.add(enumDefinition); + + // __interface + newCode.add(CodeVariables.SPEC_HASHTABLE + " " + + CodeVariables.SPEC_INTERFACE + ";"); + + // Generate constructor (the place to initialize everything!) + newCode.add("\n"); + newCode.add(CodeVariables.SPEC_CLASS + "() {"); + + breakCodeLines(newCode, globalCode.initVar); + // __COND_SAT__ + newCode.add(CodeVariables.SPEC_CONDITION + " = " + + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN + + ">();"); + // __ID__ + newCode.add(CodeVariables.SPEC_ID + " = " + + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG + + ">();"); + // __interface + newCode.add(CodeVariables.SPEC_INTERFACE + " = " + + CodeVariables.SPEC_HASHTABLE + "();"); + // FIXME: Pass the happens-before relationship check here + newCode.add("}"); + + // Generate the sequential functions + breakCodeLines(newCode, globalCode.defineFunc); + // Generate the end of the inner class definition newCode.add("};\n"); -// printCode(newCode); - + printCode(newCode); + CodeAddition addition = new CodeAddition(lineNum, newCode); if (!codeAdditions.containsKey(inst.file)) { codeAdditions.put(inst.file, new ArrayList()); } codeAdditions.get(inst.file).add(addition); } - + + // Break the code (String) into multiple lines and add it to newCode private void breakCodeLines(ArrayList newCode, String code) { int begin = 0, end = 0; while (end < code.length()) { @@ -129,19 +193,35 @@ public class CodeGenerator { end++; } } - + private void printCode(ArrayList code) { for (int i = 0; i < code.size(); i++) { System.out.println(code.get(i)); } } - private void interface2Code(SpecConstruct inst) { + // Mainly rename and wrap the interface + private void interface2Code(SpecConstruct inst) + throws InterfaceWrongFormatException { int lineNum = inst.endLineNum + 1; - GlobalConstruct construct = (GlobalConstruct) inst.construct; + InterfaceConstruct construct = (InterfaceConstruct) inst.construct; ArrayList newCode = new ArrayList(); + + // Rename the interface name + File file = inst.file; + ArrayList content = contents.get(file); + String funcDecl = inst.interfaceDeclBody; + String funcName = renameInterface(funcDecl, content, lineNum); + + // Generate new wrapper + breakCodeLines(newCode, funcDecl); + newCode.add("{"); + + // Generate + // FIXME: Add Happens-before check here + newCode.add("}"); CodeAddition addition = new CodeAddition(lineNum, newCode); if (!codeAdditions.containsKey(inst.file)) { codeAdditions.put(inst.file, new ArrayList()); @@ -149,12 +229,43 @@ public class CodeGenerator { codeAdditions.get(inst.file).add(addition); } + // Returns the function name that has been renamed and replace the old line + private String renameInterface(String funcDecl, ArrayList content, + int lineNum) throws InterfaceWrongFormatException { + int begin = 0, end = funcDecl.indexOf('('); + if (end == -1) { + throw new InterfaceWrongFormatException(funcDecl + + "\n has wrong format!"); + } + end--; + while (end > 0) { + char ch = funcDecl.charAt(end); + if (ch == '\n' || ch == '\t' || ch == ' ') + continue; + } + begin = end; + while (begin > 0) { + char ch = funcDecl.charAt(begin); + if (ch == '_' || (ch >= 'a' && ch <= 'z') + || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) { + continue; + } + } + String funcName = funcDecl.substring(begin + 1, end + 1), newLine; + int lineBreakIdx = funcDecl.indexOf('\n'); + int firstLineBreak = lineBreakIdx == -1 ? funcDecl.length() : lineBreakIdx; + newLine = funcDecl.substring(0, begin + 1) + + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName + + funcDecl.substring(end + 1, firstLineBreak); + content.set(lineNum, newLine); + return funcName; + } + private void potentialCP2Code(SpecConstruct inst) { int lineNum = inst.endLineNum + 1; - GlobalConstruct construct = (GlobalConstruct) inst.construct; + GlobalConstruct construct = (GlobalConstruct) inst.construct; ArrayList newCode = new ArrayList(); - - + CodeAddition addition = new CodeAddition(lineNum, newCode); if (!codeAdditions.containsKey(inst.file)) { codeAdditions.put(inst.file, new ArrayList()); @@ -164,10 +275,9 @@ public class CodeGenerator { private void CPDefine2Code(SpecConstruct inst) { int lineNum = inst.endLineNum + 1; - GlobalConstruct construct = (GlobalConstruct) inst.construct; + GlobalConstruct construct = (GlobalConstruct) inst.construct; ArrayList newCode = new ArrayList(); - - + CodeAddition addition = new CodeAddition(lineNum, newCode); if (!codeAdditions.containsKey(inst.file)) { codeAdditions.put(inst.file, new ArrayList()); @@ -177,10 +287,9 @@ public class CodeGenerator { private void CPDefineCheck2Code(SpecConstruct inst) { int lineNum = inst.endLineNum + 1; - GlobalConstruct construct = (GlobalConstruct) inst.construct; + GlobalConstruct construct = (GlobalConstruct) inst.construct; ArrayList newCode = new ArrayList(); - - + CodeAddition addition = new CodeAddition(lineNum, newCode); if (!codeAdditions.containsKey(inst.file)) { codeAdditions.put(inst.file, new ArrayList()); @@ -195,7 +304,11 @@ public class CodeGenerator { if (construct instanceof GlobalConstruct) { globalConstruct2Code(inst); } else if (construct instanceof InterfaceConstruct) { - interface2Code(inst); + try { + interface2Code(inst); + } catch (InterfaceWrongFormatException e) { + e.printStackTrace(); + } } else if (construct instanceof PotentialCPDefineConstruct) { potentialCP2Code(inst); } else if (construct instanceof CPDefineConstruct) { diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java new file mode 100644 index 0000000..301cd12 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java @@ -0,0 +1,29 @@ +package edu.uci.eecs.specCompiler.codeGenerator; + +public class CodeVariables { + // C++ code or library + public static final String ThreadIDType = "thread_id_t"; + public static final String BOOLEAN = "bool"; + + // Specification variables + public static final String SPEC_CLASS = "Sequential"; + public static final String SPEC_INSTANCE = "__sequential"; + public static final String SPEC_CONDITION = "__cond"; + public static final String SPEC_ID = "__id"; + public static final String SPEC_INTERFACE_ENUM = "_interface_t"; + public static final String SPEC_INTERFACE = "__interface"; + + public static final String SPEC_INTERFACE_WRAPPER = "__wrapper_"; + + // Specification library + public static final String SPEC_QUEUE = "spec_queue"; + public static final String SPEC_STACK = "spec_stack"; + public static final String SPEC_HASHTABLE = "spec_hashtable"; + public static final String SPEC_TAG = "spec_tag"; + + // Macro + public static final String MACRO_ID = "__ID__"; + public static final String MACRO_COND = "__COND_SAT__"; + public static final String MACRO_RETURN = "__RET__"; + +} diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/InterfaceWrongFormatException.java b/src/edu/uci/eecs/specCompiler/codeGenerator/InterfaceWrongFormatException.java new file mode 100644 index 0000000..9fdd84a --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/InterfaceWrongFormatException.java @@ -0,0 +1,7 @@ +package edu.uci.eecs.specCompiler.codeGenerator; + +public class InterfaceWrongFormatException extends Exception { + public InterfaceWrongFormatException(String msg) { + super(msg); + } +} diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java b/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java index 8f02b40..3bd801c 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java @@ -19,6 +19,7 @@ public class SemanticsChecker { public final HashMap CPLabel2Construct; public final HashMap potentialCPLabel2Construct; public final HashMap interfaceName2Construct; + public final HashMap> CPLabel2InterfaceConstruct; public final HashSet defineVars; public SemanticsChecker(ArrayList constructs) { @@ -26,6 +27,7 @@ public class SemanticsChecker { this.CPLabel2Construct = new HashMap(); this.potentialCPLabel2Construct = new HashMap(); this.interfaceName2Construct = new HashMap(); + this.CPLabel2InterfaceConstruct = new HashMap>(); this.defineVars = new HashSet(); } @@ -68,6 +70,14 @@ public class SemanticsChecker { DefineVar var = iConstruct.action.defineVars.get(j); var.renameVarName("__" + iConstruct.name + "_" + var.varName + "__"); } + + for (int j = 0; j < iConstruct.commitPointSet.size(); j++) { + String label = iConstruct.commitPointSet.get(j); + if (!CPLabel2InterfaceConstruct.containsKey(label)) { + CPLabel2InterfaceConstruct.put(label, new ArrayList()); + } + CPLabel2InterfaceConstruct.get(label).add(iConstruct); + } } } diff --git a/test.cc b/test.cc new file mode 100644 index 0000000..c7cb0db --- /dev/null +++ b/test.cc @@ -0,0 +1,81 @@ +#include +#include +#include + +using namespace std; + + +template +class A { + private: + int outer; + class B { + private: + vector v; + T str; + public: + typedef struct C { + T x; + } C_t; + + enum interface_t {put, get}; + B() { + v = vector(); + str = "abc"; + + } + + void _pushBack(int a) { + cout << str << endl; + v.push_back(a); + } + + int _size() { + return v.size(); + } + + C_t func() { + char *cStr = "struct_ab"; + C_t c; + c.x = cStr; + return c; + } + } b; + + public: + A() { + } + + void pushBack(int a) { + //printf("Size: %d\n", b.size()); + b._pushBack(a); + //printf("Size: %d\n", b.size()); + } + + int size() { + //B::interface_t inter; + struct B::C_t c = b.func(); + enum B::interface_t a = B::put; + vector ve(3); + ve.push_back(B::put); + cout << "Size: " << ve.size() << endl; + cout << b.func().x << endl; + return b._size(); + } +}; + +int main() { + #define __COND_SAT__ a.size() + A a; + a.pushBack(1); + if (__COND_SAT__ != 0) { + cout << "Size greater than 0" << endl; + } + #undef __COND_SAT__ + + bool __COND_SAT__ = false; + if (!__COND_SAT__) { + cout << "False!" << endl; + } + return 0; +} -- 2.34.1