generating code
authorPeizhao Ou <peizhaoo@uci.edu>
Thu, 17 Oct 2013 02:18:25 +0000 (19:18 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Thu, 17 Oct 2013 02:18:25 +0000 (19:18 -0700)
.dir-locals.el [deleted file]
.gitignore [deleted file]
src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java [new file with mode: 0644]
src/edu/uci/eecs/specCompiler/codeGenerator/InterfaceWrongFormatException.java [new file with mode: 0644]
src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java
test.cc [new file with mode: 0644]

diff --git a/.dir-locals.el b/.dir-locals.el
deleted file mode 100644 (file)
index ce85e5f..0000000
+++ /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 (file)
index 8df9862..0000000
+++ /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
index ab3e7ec9b7b50ba0f5ae2f4a160ed69841a9c305..8f4537a43a6b33a39d65be969978e3a660be00c8 100644 (file)
@@ -7,7 +7,9 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 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;
 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;
         */
        private void globalConstruct2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
                ArrayList<String> newCode = new ArrayList<String>();
-               
+
                // Generate the inner class definition
                // Generate the inner class definition
-               newCode.add("class Sequential {\n");
+               newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n");
                newCode.add("public:\n");
                newCode.add("public:\n");
-               
+
                // Generate the code in global construct first
                SequentialDefineSubConstruct globalCode = construct.code;
                breakCodeLines(newCode, globalCode.declareVar);
                // 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__
                // 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<DefineVar> 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<String> 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 + "<enum "
+                               + CodeVariables.SPEC_INTERFACE_ENUM + "> "
+                               + 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 + "<enum "
+                               + CodeVariables.SPEC_INTERFACE_ENUM + ">();");
+               // 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");
                // 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<CodeAddition>());
                }
                codeAdditions.get(inst.file).add(addition);
        }
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
                }
                codeAdditions.get(inst.file).add(addition);
        }
-       
+
+       // Break the code (String) into multiple lines and add it to newCode
        private void breakCodeLines(ArrayList<String> newCode, String code) {
                int begin = 0, end = 0;
                while (end < code.length()) {
        private void breakCodeLines(ArrayList<String> newCode, String code) {
                int begin = 0, end = 0;
                while (end < code.length()) {
@@ -129,19 +193,35 @@ public class CodeGenerator {
                        end++;
                }
        }
                        end++;
                }
        }
-       
+
        private void printCode(ArrayList<String> code) {
                for (int i = 0; i < code.size(); i++) {
                        System.out.println(code.get(i));
                }
        }
 
        private void printCode(ArrayList<String> 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;
                int lineNum = inst.endLineNum + 1;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               InterfaceConstruct construct = (InterfaceConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
                ArrayList<String> newCode = new ArrayList<String>();
+
+               // Rename the interface name
+               File file = inst.file;
+               ArrayList<String> 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<CodeAddition>());
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
@@ -149,12 +229,43 @@ public class CodeGenerator {
                codeAdditions.get(inst.file).add(addition);
        }
 
                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<String> 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;
        private void potentialCP2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
@@ -164,10 +275,9 @@ public class CodeGenerator {
 
        private void CPDefine2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
 
        private void CPDefine2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
@@ -177,10 +287,9 @@ public class CodeGenerator {
 
        private void CPDefineCheck2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
 
        private void CPDefineCheck2Code(SpecConstruct inst) {
                int lineNum = inst.endLineNum + 1;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
                        codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
@@ -195,7 +304,11 @@ public class CodeGenerator {
                        if (construct instanceof GlobalConstruct) {
                                globalConstruct2Code(inst);
                        } else if (construct instanceof InterfaceConstruct) {
                        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) {
                        } 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 (file)
index 0000000..301cd12
--- /dev/null
@@ -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 (file)
index 0000000..9fdd84a
--- /dev/null
@@ -0,0 +1,7 @@
+package edu.uci.eecs.specCompiler.codeGenerator;
+
+public class InterfaceWrongFormatException extends Exception {
+       public InterfaceWrongFormatException(String msg) {
+               super(msg);
+       }
+}
index 8f02b409dad58d7f999446c558800fb26a700afa..3bd801ce1a323f2b526ce102aecb07df4e80b505 100644 (file)
@@ -19,6 +19,7 @@ public class SemanticsChecker {
        public final HashMap<String, SpecConstruct> CPLabel2Construct;
        public final HashMap<String, SpecConstruct> potentialCPLabel2Construct;
        public final HashMap<String, SpecConstruct> interfaceName2Construct;
        public final HashMap<String, SpecConstruct> CPLabel2Construct;
        public final HashMap<String, SpecConstruct> potentialCPLabel2Construct;
        public final HashMap<String, SpecConstruct> interfaceName2Construct;
+       public final HashMap<String, ArrayList<InterfaceConstruct>> CPLabel2InterfaceConstruct;
        public final HashSet<DefineVar> defineVars;
 
        public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
        public final HashSet<DefineVar> defineVars;
 
        public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
@@ -26,6 +27,7 @@ public class SemanticsChecker {
                this.CPLabel2Construct = new HashMap<String, SpecConstruct>();
                this.potentialCPLabel2Construct = new HashMap<String, SpecConstruct>();
                this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
                this.CPLabel2Construct = new HashMap<String, SpecConstruct>();
                this.potentialCPLabel2Construct = new HashMap<String, SpecConstruct>();
                this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
+               this.CPLabel2InterfaceConstruct = new HashMap<String, ArrayList<InterfaceConstruct>>();
                this.defineVars = new HashSet<DefineVar>();
        }
 
                this.defineVars = new HashSet<DefineVar>();
        }
 
@@ -68,6 +70,14 @@ public class SemanticsChecker {
                                        DefineVar var = iConstruct.action.defineVars.get(j);
                                        var.renameVarName("__" + iConstruct.name + "_" + var.varName + "__");
                                }
                                        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<InterfaceConstruct>());
+                                       }
+                                       CPLabel2InterfaceConstruct.get(label).add(iConstruct);
+                               }
                        }
                }
 
                        }
                }
 
diff --git a/test.cc b/test.cc
new file mode 100644 (file)
index 0000000..c7cb0db
--- /dev/null
+++ b/test.cc
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+
+template<typename T>
+class A {
+       private:
+       int outer;
+       class B {
+               private:
+                       vector<int> v;
+                       T str;
+               public:
+                       typedef struct C {
+                               T x;
+                       } C_t;
+
+                       enum interface_t {put, get};
+                       B() {
+                               v = vector<int>();
+                               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<T>::interface_t inter;
+               struct B::C_t c = b.func();
+               enum B::interface_t a = B::put;
+               vector<enum B::interface_t> 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<string> 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;
+}