fixed bugs
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeVariables.java
index dd815db37bdb92ca9cec78dbe89dd9c628689cff..8e87cd5c4a1b1534c1904f742cd6d125283c75eb 100644 (file)
@@ -163,7 +163,7 @@ public class CodeVariables {
                        String idCode) {
                ArrayList<String> code = new ArrayList<String>();
                code.add("static " + IDType + " " + interfaceName + "_id() {");
-               if (idCode != null) {
+               if (!idCode.equals("")) {
                        code.add(DECLARE_DEFINE(IDType, MACRO_ID, idCode));
                } else {
                        code.add(DECLARE_DEFINE(IDType, MACRO_ID, DEFAULT_ID));
@@ -177,7 +177,7 @@ public class CodeVariables {
                        InterfaceConstruct construct, FunctionHeader header) {
                String interfaceName = construct.name;
                ArrayList<String> code = new ArrayList<String>();
-               code.add("bool " + interfaceName + "_check_action(void *info, "
+               code.add("static bool " + interfaceName + "_check_action(void *info, "
                                + IDType + " " + MACRO_ID + ") {");
                code.add(DECLARE("bool", "check_passed"));
                String infoStructType = interfaceName + "_info", infoStructName = "theInfo";
@@ -192,17 +192,28 @@ public class CodeVariables {
                }
                code.add("");
                // __COND_SAT
-               code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
+               if (!construct.condition.equals("")) {
+                       code.add(DECLARE_DEFINE("bool", MACRO_COND, construct.condition));
+               }
                // Check
-               code.add(ASSIGN("check_passed", construct.check));
-               code.add("if (!check_passed) return false;");
+               if (!construct.check.equals("")) {
+                       code.add(ASSIGN("check_passed", construct.check));
+                       code.add("if (!check_passed) return false;");
+               }
                // Action
-               code.addAll(construct.action);
+               if (construct.action.size() > 0) {
+                       code.addAll(construct.action);
+               }
                // Post_check
-               code.add(ASSIGN("check_passed", construct.postCheck));
-               code.add("if (!check_passed) return false;");
+               if (!construct.postCheck.equals("")) {
+                       code.add(ASSIGN("check_passed", construct.postCheck));
+                       code.add("if (!check_passed) return false;");
+               }
                // Post_action
-               code.addAll(construct.postAction);
+               if (construct.postAction.size() > 0) {
+                       code.addAll(construct.postAction);
+               }
+
                code.add("}");
 
                return code;
@@ -223,12 +234,9 @@ public class CodeVariables {
                funcDefine.set(0, headLine);
        }
 
-       private static void makeVariablesStatic(ArrayList<String> varDecls) {
-               for (int i = 0; i < varDecls.size(); i++) {
-                       String varDecl = varDecls.get(i);
-                       varDecl = "static " + varDecl;
-                       varDecls.set(i, varDecl);
-               }
+       private static String makeVariablesStatic(VariableDeclaration varDecl) {
+               String res = "static " + varDecl.type + " " + varDecl.name + ";";
+               return res;
        }
 
        private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
@@ -238,6 +246,7 @@ public class CodeVariables {
                if (headerLine.startsWith("template")) {
                        headerLine = content.get(construct.beginLineNum + 1);
                }
+               headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
                try {
                        return SpecParser.parseFuncHeader(headerLine);
                } catch (ParseException e) {
@@ -252,7 +261,7 @@ public class CodeVariables {
                HashSet<String> allHeaders = getAllHeaders(semantics);
 
                // All headers needed by the interface decalration
-               newCode.add(COMMENT("/* Include all the header files that contains the interface declaration */"));
+               newCode.add(COMMENT("Include all the header files that contains the interface declaration"));
                for (String header : allHeaders) {
                        newCode.add(INCLUDE(header));
                }
@@ -264,6 +273,14 @@ public class CodeVariables {
                newCode.add("");
 
                SequentialDefineSubConstruct code = construct.code;
+               // User-defined structs first
+               newCode.add(COMMENT("All other user-defined structs"));
+               ArrayList<ArrayList<String>> declareStructs = code.declareStructs;
+               for (int i = 0; i < declareStructs.size(); i++) {
+                       ArrayList<String> declareStruct = declareStructs.get(i);
+                       newCode.addAll(declareStruct);
+                       newCode.add("");
+               }
                // User-defined functions
                newCode.add(COMMENT("All other user-defined functions"));
                ArrayList<ArrayList<String>> defineFuncs = code.defineFuncs;
@@ -309,7 +326,8 @@ public class CodeVariables {
                ArrayList<VariableDeclaration> varDecls = code.declareVar;
                for (int i = 0; i < varDecls.size(); i++) {
                        VariableDeclaration varDecl = varDecls.get(i);
-                       newCode.add(DECLARE(varDecl.type, varDecl.name));
+                       // Don't forget to make them static
+                       newCode.add(makeVariablesStatic(varDecl));
                }
 
                newCode.add("");
@@ -335,7 +353,7 @@ public class CodeVariables {
                newCode.add("}");
                newCode.add(COMMENT("End of Global construct generation in class"));
 
-               // printCode(newCode);
+               printCode(newCode);
                return newCode;
        }
 
@@ -413,6 +431,12 @@ public class CodeVariables {
                return newCode;
        }
 
+       public static ArrayList<String> generateEntryPointInitCall() {
+               ArrayList<String> newCode = new ArrayList<String>(1);
+               newCode.add("__sequential_init();");
+               return newCode;
+       }
+
        public static ArrayList<String> generateInterfaceWrapper(
                        SemanticsChecker semantics, InterfaceConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
@@ -428,11 +452,12 @@ public class CodeVariables {
                                .get(construct.name));
                // Rename the interface
                renameInterface(semantics, construct);
-               InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct.get(interfaceName);
+               InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
+                               .get(interfaceName);
                if (defineConstruct != null) {
                        renameInterface(semantics, defineConstruct);
                }
-               
+
                // Generate wrapper header
                newCode.add(header.toString() + " {");
                // Wrapper function body
@@ -544,7 +569,7 @@ public class CodeVariables {
                newCode.add("}");
                return newCode;
        }
-       
+
        public static ArrayList<String> generateCPDefineCheck(
                        SemanticsChecker semantics, CPDefineCheckConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
@@ -573,7 +598,7 @@ public class CodeVariables {
                newCode.add("}");
                return newCode;
        }
-       
+
        public static ArrayList<String> generateCPDefine(
                        SemanticsChecker semantics, CPDefineConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();