fixed some bugs
authorPeizhao Ou <peizhaoo@uci.edu>
Fri, 6 Dec 2013 05:39:35 +0000 (21:39 -0800)
committerPeizhao Ou <peizhaoo@uci.edu>
Fri, 6 Dec 2013 05:39:35 +0000 (21:39 -0800)
grammer/spec_compiler.jj
src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
src/edu/uci/eecs/specCompiler/codeGenerator/CodeVariables.java
src/edu/uci/eecs/specCompiler/specExtraction/FunctionHeader.java
src/edu/uci/eecs/specCompiler/specExtraction/ParserUtils.java

index cbe2e28d6240ab8a6853df36d0c68c7d647ce15a..f914094a1f249af4838c3aeea860edf07897f742 100644 (file)
@@ -181,7 +181,7 @@ import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
                        return null;
                }
 
-               public static ArrayList<String> getTemplateArg(String line)
+               public static ArrayList<VariableDeclaration> getTemplateArg(String line)
                throws ParseException {
                        InputStream input = new ByteArrayInputStream(line.getBytes());
                        SpecParser parser = new SpecParser(input);
@@ -433,7 +433,6 @@ SKIP : {
        <OR_EQUALS: "|=">
 |
        <AND_EQUALS: "&=">
-
 |
        <SEMI_COLON: ";">
 |
@@ -583,13 +582,11 @@ String ParameterizedName() :
 
 FunctionHeader FuncDecl() :
 {
-       ArrayList<VariableDeclaration> templateList;
        String ret;
        QualifiedName funcName;
        ArrayList<VariableDeclaration> args;
 }
 {
-aa
        (<STATIC> | <INLINE>)*
        ret = Type() 
        funcName = ParseQualifiedName() 
index d38bba7f645c6191ff6b73bed4cf477477cda73e..66bdca4ab408d740edb6a444ad684fc9fc48ee02 100644 (file)
@@ -89,15 +89,48 @@ public class CodeGenerator {
 
        // Mainly rename and wrap the interface
        private void interface2Code(InterfaceConstruct construct) {
-               ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(
-                               _semantics, construct);
-               int lineNum = construct.beginLineNum;
-               // Add it to the codeAdditions
-               CodeAddition addition = new CodeAddition(lineNum, newCode);
-               if (!codeAdditions.containsKey(construct.file)) {
-                       codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+               // First rename the interface
+               CodeVariables.renameInterface(_semantics, construct);
+
+               // If there's no define construct for it, we generate the wrapper just
+               // in place without declaration
+               InterfaceDefineConstruct defineConstruct = _semantics.interfaceName2DefineConstruct
+                               .get(construct.name);
+               ArrayList<String> newCode;
+               int lineNum;
+               CodeAddition addition;
+               // Then generate the wrapper if necessary
+               if (defineConstruct != null) { // Need to have a wrapper declaration
+                       newCode = CodeVariables.generateInterfaceWrapperDeclaration(_semantics, construct);
+                       lineNum = construct.beginLineNum;
+                       // Add the wrapper declaration
+                       addition = new CodeAddition(lineNum, newCode);
+                       if (!codeAdditions.containsKey(construct.file)) {
+                               codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+                       }
+                       codeAdditions.get(construct.file).add(addition);
+                       
+                       // Add the wrapper definition
+                       newCode = CodeVariables.generateInterfaceWrapperDefinition(_semantics, construct);
+                       lineNum = defineConstruct.beginLineNum;
+                       // Add the wrapper declaration
+                       addition = new CodeAddition(lineNum, newCode);
+                       if (!codeAdditions.containsKey(defineConstruct.file)) {
+                               codeAdditions.put(defineConstruct.file, new ArrayList<CodeAddition>());
+                       }
+                       codeAdditions.get(defineConstruct.file).add(addition);
+               } else { // No declaration needed
+                       // Last generate the definition
+                       newCode = CodeVariables.generateInterfaceWrapperDefinition(_semantics, construct);
+                       lineNum = construct.beginLineNum;
+                       // Add the wrapper declaration
+                       addition = new CodeAddition(lineNum, newCode);
+                       if (!codeAdditions.containsKey(construct.file)) {
+                               codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+                       }
+                       codeAdditions.get(construct.file).add(addition);
                }
-               codeAdditions.get(construct.file).add(addition);
+               
        }
 
        private void potentialCPDefine2Code(PotentialCPDefineConstruct construct) {
@@ -158,9 +191,10 @@ public class CodeGenerator {
                int curSrcLine = 0;
                for (int i = 0; i < additions.size(); i++) {
                        CodeAddition addition = additions.get(i);
-                       if (curSrcLine <  addition.lineNum) {
+                       if (curSrcLine < addition.lineNum) {
                                // Be careful, subList is the interval [begin, end)
-                               newContent.addAll(content.subList(curSrcLine, addition.lineNum));
+                               newContent
+                                               .addAll(content.subList(curSrcLine, addition.lineNum));
                                curSrcLine = addition.lineNum;
                        }
                        newContent.addAll(addition.newCode);
@@ -206,13 +240,14 @@ public class CodeGenerator {
        public static void main(String[] argvs) {
                String homeDir = Environment.HOME_DIRECTORY;
                File[] srcFiles = {
-//                             new File(Environment.MODEL_CHECKER_TEST_DIR + "/backup_linuxrwlocks.c") };
-//              new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c") };
+               // new File(Environment.MODEL_CHECKER_TEST_DIR +
+               // "/backup_linuxrwlocks.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.c") };
-//             new File(homeDir + "/benchmark/test/test.c") };
+               // new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
+               // new File(homeDir + "/benchmark/ms-queue/my_queue.c") };
+               // new File(homeDir + "/benchmark/test/test.c") };
                CodeGenerator gen = new CodeGenerator(srcFiles);
                gen.generateCode();
        }
index e6dfc1dae68cb5aef131ca6fc9520c5ef9fccee2..0e4a712e4c75e23c9794f11baa5a9b5057d44c02 100644 (file)
@@ -36,7 +36,7 @@ public class CodeVariables {
        public static final String HEADER_COMMON = "<common.h>";
        public static final String HEADER_SPECANNOTATION = "<specannotation.h>";
        public static final String HEADER_CDSTRACE = "<cdstrace.h>";
-//     public static final String CDSAnnotate = "cdsannotate";
+       // public static final String CDSAnnotate = "cdsannotate";
        public static final String CDSAnnotate = "_Z11cdsannotatemPv";
        public static final String CDSAnnotateType = "SPEC_ANALYSIS";
        public static final String IDType = "call_id_t";
@@ -134,15 +134,17 @@ public class CodeVariables {
                        String val) {
                return structName + "->" + field + " = " + val + ";";
        }
-       
-       private static String ASSIGN_PTR_TO_PTR(String structName, String field, String val) {
+
+       private static String ASSIGN_PTR_TO_PTR(String structName, String field,
+                       String val) {
                return structName + "->" + field + " = &" + val + ";";
        }
 
        private static String STRUCT_NEW_DECLARE_DEFINE(String type, String name) {
-               return "struct " + type + " *" + name + " = (struct " + type + "*) malloc(sizeof(struct " + type + "));";
+               return "struct " + type + " *" + name + " = (struct " + type
+                               + "*) malloc(sizeof(struct " + type + "));";
        }
-       
+
        private static String DECLARE(String type, String name) {
                return type + " " + name + ";";
        }
@@ -264,13 +266,20 @@ public class CodeVariables {
        private static FunctionHeader getFunctionHeader(SemanticsChecker semantics,
                        Construct construct) {
                ArrayList<String> content = semantics.srcFilesInfo.get(construct.file).content;
-               String headerLine = content.get(construct.beginLineNum);
+               String headerLine = content.get(construct.beginLineNum), templateLine = null;
                if (headerLine.startsWith("template")) {
+                       templateLine = headerLine;
                        headerLine = content.get(construct.beginLineNum + 1);
                }
                headerLine = headerLine.substring(0, headerLine.indexOf(')') + 1);
                try {
-                       return SpecParser.parseFuncHeader(headerLine);
+                       FunctionHeader header = SpecParser.parseFuncHeader(headerLine);
+                       if (templateLine != null) {
+                               ArrayList<VariableDeclaration> templateArgs = SpecParser
+                                               .getTemplateArg(templateLine);
+                               header.setTemplateList(templateArgs);
+                       }
+                       return header;
                } catch (ParseException e) {
                        e.printStackTrace();
                }
@@ -363,8 +372,8 @@ public class CodeVariables {
                newCode.add("static void __sequential_init() {");
                // Init func_ptr_table
                newCode.add(COMMENT("Init func_ptr_table"));
-               newCode.add(ASSIGN("func_ptr_table",
-                               "(void**) malloc(sizeof(void*) * " + semantics.interface2Num.size() + " * 2)"));
+               newCode.add(ASSIGN("func_ptr_table", "(void**) malloc(sizeof(void*) * "
+                               + semantics.interface2Num.size() + " * 2)"));
                for (String interfaceName : semantics.interfaceName2Construct.keySet()) {
                        String interfaceNum = Integer.toString(semantics.interface2Num
                                        .get(interfaceName));
@@ -449,17 +458,19 @@ public class CodeVariables {
                                                                .get(right.hbConditionLabel));
                                newCode.add(COMMENT(left + " -> " + right));
 
-                               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT, structVarName));
-                               newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_before",
-                                               interfaceNumBefore));
-                               newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_before",
-                                               hbLabelNumBefore));
+                               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_INIT,
+                                               structVarName));
+                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                                               "interface_num_before", interfaceNumBefore));
+                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                                               "hb_condition_num_before", hbLabelNumBefore));
                                newCode.add(ASSIGN_TO_PTR(structVarName, "interface_num_after",
                                                interfaceNumAfter));
-                               newCode.add(ASSIGN_TO_PTR(structVarName, "hb_condition_num_after",
-                                               hbLabelNumAfter));
+                               newCode.add(ASSIGN_TO_PTR(structVarName,
+                                               "hb_condition_num_after", hbLabelNumAfter));
 
-                               newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, annotationVarName));
+                               newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION,
+                                               annotationVarName));
                                newCode.add(ASSIGN_TO_PTR(annotationVarName,
                                                SPEC_ANNOTATION_FIELD_TYPE, SPEC_ANNO_TYPE_HB_INIT));
                                newCode.add(ASSIGN_TO_PTR(annotationVarName,
@@ -476,7 +487,22 @@ public class CodeVariables {
                return newCode;
        }
 
-       public static ArrayList<String> generateInterfaceWrapper(
+       // Only generate the declaration of the wrapper, don't do any renaming
+       public static ArrayList<String> generateInterfaceWrapperDeclaration(
+                       SemanticsChecker semantics, InterfaceConstruct construct) {
+               ArrayList<String> newCode = new ArrayList<String>();
+               FunctionHeader header = getFunctionHeader(semantics, construct);
+               newCode.add(COMMENT("Declaration of the wrapper"));
+               String templateStr = header.getTemplateFullStr();
+               newCode.add(templateStr);
+               newCode.add(header.getFuncStr() + ";");
+               newCode.add("");
+
+               return newCode;
+       }
+
+       // Only generate the definition of the wrapper, don't do any renaming
+       public static ArrayList<String> generateInterfaceWrapperDefinition(
                        SemanticsChecker semantics, InterfaceConstruct construct) {
                ArrayList<String> newCode = new ArrayList<String>();
                String interfaceName = construct.name;
@@ -489,29 +515,15 @@ public class CodeVariables {
                FunctionHeader header = getFunctionHeader(semantics, construct);
                String interfaceNum = Integer.toString(semantics.interface2Num
                                .get(construct.name));
-               // Rename the interface
-               renameInterface(semantics, construct);
-               InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
-                               .get(interfaceName);
-               if (defineConstruct != null) {
-                       renameInterface(semantics, defineConstruct);
-               }
-
-               // Generate wrapper header
-               // If it's not in a class, we should declare the wrapper function
-               if (semantics.getClassName() == null) {
-                       FunctionHeader renamedHeader = header
-                                       .getRenamedHeader(SPEC_INTERFACE_WRAPPER);
-                       newCode.add(COMMENT("Declaration of the wrapper"));
-                       newCode.add(renamedHeader + ";");
-                       newCode.add("");
-               }
-               newCode.add(header.toString() + " {");
+                       
+               newCode.add(header.getTemplateFullStr());
+               newCode.add(header.getFuncStr() + " {");
                // Wrapper function body
                newCode.add(COMMENT("Interface begins"));
                // Interface begin
                String structName = "interface_begin";
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN, "interface_begin"));
+               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_INTERFACE_BEGIN,
+                               "interface_begin"));
                newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
                String anno = "annotation_interface_begin";
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
@@ -545,7 +557,8 @@ public class CodeVariables {
                        newCode.add("");
                }
                // Also add the true condition if any
-               if (semantics.containsConditionalInterface(new ConditionalInterface(interfaceName, ""))) {
+               if (semantics.containsConditionalInterface(new ConditionalInterface(
+                               interfaceName, ""))) {
                        structName = "hb_condition";
                        newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_HB_CONDITION, structName));
                        newCode.add(ASSIGN_TO_PTR(structName, "interface_num", interfaceNum));
@@ -595,7 +608,7 @@ public class CodeVariables {
                return newCode;
        }
 
-       // Rename the interface depending on if it's declaration or definition
+       // Rename the interface name for declaration or definition
        public static void renameInterface(SemanticsChecker semantics,
                        Construct construct) {
                FunctionHeader header = getFunctionHeader(semantics, construct);
@@ -607,7 +620,21 @@ public class CodeVariables {
                        lineNum++;
                }
                String newLine = header.getRenamedHeader(SPEC_INTERFACE_WRAPPER)
-                               .toString() + " {";
+                               .toString();
+
+               if (construct instanceof InterfaceConstruct) {
+                       InterfaceConstruct iConstruct = (InterfaceConstruct) construct;
+                       InterfaceDefineConstruct defineConstruct = semantics.interfaceName2DefineConstruct
+                                       .get(iConstruct.name);
+                       if (defineConstruct != null) { // There is a defineConstruct
+                               newLine = newLine + " ;";
+                               renameInterface(semantics, defineConstruct);
+                       } else { // This is a declare & define construct
+                               newLine = newLine + " {";
+                       }
+               } else {
+                       newLine = newLine + " {";
+               }
                content.set(lineNum, newLine);
        }
 
@@ -638,12 +665,14 @@ public class CodeVariables {
                // Add annotation
                newCode.add("if (" + construct.condition + ") {");
                String structName = "potential_cp_define", anno = "annotation_potential_cp_define";
-               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE, structName));
+               newCode.add(STRUCT_NEW_DECLARE_DEFINE(ANNO_POTENTIAL_CP_DEFINE,
+                               structName));
                String labelNum = Integer.toString(semantics.commitPointLabel2Num
                                .get(construct.label));
                newCode.add(ASSIGN_TO_PTR(structName, "label_num", labelNum));
                newCode.add(STRUCT_NEW_DECLARE_DEFINE(SPEC_ANNOTATION, anno));
-               newCode.add(ASSIGN_TO_PTR(anno, "type", SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
+               newCode.add(ASSIGN_TO_PTR(anno, "type",
+                               SPEC_ANNO_TYPE_POTENTIAL_CP_DEFINE));
                newCode.add(ASSIGN_TO_PTR(anno, "annotation", structName));
                newCode.add(ANNOTATE(anno));
                newCode.add("}");
index 60f7700c2459139a0bd042bf001547c7a8961a8a..769087dcd7a6298f172b6e1e5006251cd6c7547c 100644 (file)
@@ -2,20 +2,69 @@ package edu.uci.eecs.specCompiler.specExtraction;
 
 import java.util.ArrayList;
 
+import com.sun.xml.internal.ws.wsdl.parser.ParserUtil;
+
 public class FunctionHeader {
-       public final ArrayList<VariableDeclaration> templateList;
+       private ArrayList<VariableDeclaration> templateList;
        
        public final String returnType;
        public final QualifiedName qualifiedName;
        public final ArrayList<VariableDeclaration> args;
 
-       public FunctionHeader(ArrayList<VariableDeclaration> templateList, String returnType, QualifiedName qualifiedName,
+       public FunctionHeader(String returnType, QualifiedName qualifiedName,
                        ArrayList<VariableDeclaration> args) {
-               this.templateList = templateList;
+               this.templateList = null;
                this.returnType = returnType;
                this.qualifiedName = qualifiedName;
                this.args = args;
        }
+       
+       public void setTemplateList(ArrayList<VariableDeclaration> templateList) {
+               this.templateList = templateList;
+       }
+       
+       public ArrayList<VariableDeclaration> getTemplateList() {
+               return this.templateList;
+       }
+       
+       public String getTemplateFullStr() {
+               String templateStr = "";
+               if (templateList.size() == 0)
+                       return templateStr;
+               VariableDeclaration decl;
+               decl = templateList.get(0);
+               templateStr = "<" + decl.type + " " + decl.name;
+               for (int i = 1; i < templateList.size(); i++) {
+                       decl = templateList.get(i);
+                       templateStr = templateStr + ", " + decl.type + " " + decl.name;
+               }
+               templateStr = templateStr + ">";
+               return templateStr;
+       }
+       
+       public String getTemplateArgStr() {
+               String templateStr = null;
+               if (templateList.size() == 0)
+                       return templateStr;
+               templateStr = "<" + templateList.get(0).name;
+               for (int i = 1; i < templateList.size(); i++) {
+                       templateStr = templateStr + ", " + templateList.get(i);
+               }
+               templateStr = templateStr + ">";
+               return templateStr;
+       }
+       
+       public String getFuncStr() {
+               String res = returnType + " " + qualifiedName.fullName + "(";
+               if (args.size() >= 1) {
+                       res = res + args.get(0);
+               }
+               for (int i = 1; i < args.size(); i++) {
+                       res = res + ", " + args.get(i);
+               }
+               res = res + ")";
+               return res;
+       }
 
        public String toString() {
                String res = returnType + " " + qualifiedName.fullName + "(";
@@ -32,7 +81,7 @@ public class FunctionHeader {
        public FunctionHeader getRenamedHeader(String prefix) {
                String newFullName = qualifiedName.qualifiedName + prefix + "_"
                                + qualifiedName.bareName;
-               FunctionHeader newHeader = new FunctionHeader(templateList, returnType,
+               FunctionHeader newHeader = new FunctionHeader(returnType,
                                new QualifiedName(newFullName), args);
                return newHeader;
        }
index 2cae9b5d8a55e52abd23b79e2ac2efc51183fffd..4df375c6a40f8cf57adf33b46efe242ec046a846 100644 (file)
@@ -48,10 +48,10 @@ public class ParserUtils {
        public static String getTemplateStr(String templateLine) {
                String templateStr = null;
                try {
-                       ArrayList<String> args = SpecParser.getTemplateArg(templateLine);
-                       templateStr = "<" + args.get(1);
-                       for (int i = 1; i < args.size() / 2; i++) {
-                               templateStr = templateStr + ", " + args.get(i * 2 + 1);
+                       ArrayList<VariableDeclaration> templateArgs = SpecParser.getTemplateArg(templateLine);
+                       templateStr = "<" + templateArgs.get(0).name;
+                       for (int i = 1; i < templateArgs.size(); i++) {
+                               templateStr = templateStr + ", " + templateArgs.get(i);
                        }
                        templateStr = templateStr + ">";
                } catch (ParseException e) {