generating code
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeGenerator.java
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.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<String> newCode = new ArrayList<String>();
-               
+
                // 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<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");
-//             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);
        }
-       
+
+       // 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()) {
@@ -129,19 +193,35 @@ public class CodeGenerator {
                        end++;
                }
        }
-       
+
        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;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               InterfaceConstruct construct = (InterfaceConstruct) inst.construct;
                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>());
@@ -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<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;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                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;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                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;
-               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
-               
-               
+
                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) {
-                               interface2Code(inst);
+                               try {
+                                       interface2Code(inst);
+                               } catch (InterfaceWrongFormatException e) {
+                                       e.printStackTrace();
+                               }
                        } else if (construct instanceof PotentialCPDefineConstruct) {
                                potentialCP2Code(inst);
                        } else if (construct instanceof CPDefineConstruct) {