more changes
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeGenerator.java
index 8f4537a43a6b33a39d65be969978e3a660be00c8..05a76a79e8d113534e5f956b498ff8084bb61f37 100644 (file)
@@ -12,6 +12,7 @@ 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.ConditionalInterface;
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
@@ -97,21 +98,20 @@ public class CodeGenerator {
                GlobalConstruct construct = (GlobalConstruct) inst.construct;
                ArrayList<String> newCode = new ArrayList<String>();
 
-               // Generate the inner class definition
-               newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n");
-               newCode.add("public:\n");
+               // Generate all sequential variables into a struct
+               newCode.add("struct " + CodeVariables.SPEC_STRUCT + " {\n");
 
                // Generate the code in global construct first
                SequentialDefineSubConstruct globalCode = construct.code;
                breakCodeLines(newCode, globalCode.declareVar);
 
                // Generate code from the DefineVar, __COND_SAT__ and __ID__
+               // The hashtable in the contract can only contains pointers or integers
                // __COND_SAT__
-               newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
-                               + "> " + CodeVariables.SPEC_CONDITION + ";");
+               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_CONDITION
+                               + ";");
                // __ID__
-               newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
-                               + "> " + CodeVariables.SPEC_ID + ";");
+               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_ID + ";");
 
                // DefineVars
                for (String interfaceName : _semantics.interfaceName2Construct.keySet()) {
@@ -120,58 +120,46 @@ public class CodeGenerator {
                        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() + ";");
+                               newCode.add(CodeVariables.SPEC_HASHTABLE + 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 + ";");
+               newCode.add(CodeVariables.SPEC_HASHTABLE + CodeVariables.SPEC_INTERFACE
+                               + ";");
 
-               // Generate constructor (the place to initialize everything!)
-               newCode.add("\n");
-               newCode.add(CodeVariables.SPEC_CLASS + "() {");
+               // End of the struct
+               newCode.add("}");
 
+               // FIXME: Constructor should be modified and put in the right place
+               // Generate constructor (the place to initialize everything!)
                breakCodeLines(newCode, globalCode.initVar);
                // __COND_SAT__
-               newCode.add(CodeVariables.SPEC_CONDITION + " = "
-                               + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
-                               + ">();");
+               newCode.add("init_table(&" + CodeVariables.SPEC_CONDITION + ");");
                // __ID__
-               newCode.add(CodeVariables.SPEC_ID + " = "
-                               + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
-                               + ">();");
+               newCode.add("init_table(&" + 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("init_table(&" + var.getNewVarName() + ");");
+                       }
+               }
                // __interface
-               newCode.add(CodeVariables.SPEC_INTERFACE + " = "
-                               + CodeVariables.SPEC_HASHTABLE + "<enum "
-                               + CodeVariables.SPEC_INTERFACE_ENUM + ">();");
-               // FIXME: Pass the happens-before relationship check here
-               newCode.add("}");
+               newCode.add("init_table(&" + CodeVariables.SPEC_INTERFACE + ");");
+
+               // Pass the happens-before relationship check here
+               newCode.addAll(CodeVariables.generateHBInitAnnotation(_semantics));
 
+               newCode.add("\n");
+               
                // Generate the sequential functions
                breakCodeLines(newCode, globalCode.defineFunc);
 
-               // Generate the end of the inner class definition
-               newCode.add("};\n");
                printCode(newCode);
 
                CodeAddition addition = new CodeAddition(lineNum, newCode);
@@ -209,18 +197,27 @@ public class CodeGenerator {
 
                // Rename the interface name
                File file = inst.file;
-               ArrayList<String> content = contents.get(file);
                String funcDecl = inst.interfaceDeclBody;
-               String funcName = renameInterface(funcDecl, content, lineNum);
+               // Rename the function declaration
+               String funcName = renameInterface(inst);
+               // Also rename the function definition if it's separated from the
+               // declaration
+               SpecConstruct definition = _semantics.interfaceName2DefineConstruct
+                               .get(construct.name);
+               if (definition != null) {
+                       String funcDefintionName = renameInterface(definition);
+                       assert (funcDefintionName.equals(funcName));
+               }
 
                // Generate new wrapper
                breakCodeLines(newCode, funcDecl);
+
                newCode.add("{");
-               
-               // Generate 
-               
+
+               // Generate
+
                // FIXME: Add Happens-before check here
-               
+
                newCode.add("}");
                CodeAddition addition = new CodeAddition(lineNum, newCode);
                if (!codeAdditions.containsKey(inst.file)) {
@@ -230,8 +227,13 @@ public class CodeGenerator {
        }
 
        // 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 {
+       private String renameInterface(SpecConstruct inst)
+                       throws InterfaceWrongFormatException {
+               String funcDecl = inst.interfaceDeclBody;
+               ArrayList<String> content = contents.get(inst.file);
+
+               // Depending on "(" to find the function name, so it doesn't matter if
+               // there's any template
                int begin = 0, end = funcDecl.indexOf('(');
                if (end == -1) {
                        throw new InterfaceWrongFormatException(funcDecl
@@ -240,24 +242,31 @@ public class CodeGenerator {
                end--;
                while (end > 0) {
                        char ch = funcDecl.charAt(end);
-                       if (ch == '\n' || ch == '\t' || ch == ' ')
-                               continue;
+                       if (ch == '_' || (ch >= 'a' && ch <= 'z')
+                                       || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
+                               break;
+                       }
+                       end--;
                }
                begin = end;
                while (begin > 0) {
                        char ch = funcDecl.charAt(begin);
                        if (ch == '_' || (ch >= 'a' && ch <= 'z')
                                        || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
+                               begin--;
                                continue;
                        }
+                       break;
                }
                String funcName = funcDecl.substring(begin + 1, end + 1), newLine;
                int lineBreakIdx = funcDecl.indexOf('\n');
-               int firstLineBreak = lineBreakIdx == -1 ? funcDecl.length() : lineBreakIdx;  
+               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);
+               // Be careful: lineNum - 1 -> index of content array
+               content.set(inst.endLineNum, newLine);
                return funcName;
        }
 
@@ -310,11 +319,11 @@ public class CodeGenerator {
                                        e.printStackTrace();
                                }
                        } else if (construct instanceof PotentialCPDefineConstruct) {
-                               potentialCP2Code(inst);
+                               // potentialCP2Code(inst);
                        } else if (construct instanceof CPDefineConstruct) {
-                               CPDefine2Code(inst);
+                               // CPDefine2Code(inst);
                        } else if (construct instanceof CPDefineCheckConstruct) {
-                               CPDefineCheck2Code(inst);
+                               // CPDefineCheck2Code(inst);
                        }
                }
        }
@@ -322,11 +331,12 @@ public class CodeGenerator {
        public static void main(String[] argvs) {
                String homeDir = Environment.HOME_DIRECTORY;
                File[] srcFiles = {
-               // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
-               new File(homeDir
-                               + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
-               // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
-               };
+                               // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
+                               // new File(homeDir
+                               // +
+                               // "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
+                               new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
+                               new File(homeDir + "/benchmark/ms-queue/my_queue.h") };
                CodeGenerator gen = new CodeGenerator(srcFiles);
                gen.generateCode();
        }