add more on code generation
authorPeizhao Ou <peizhaoo@uci.edu>
Tue, 15 Oct 2013 23:44:25 +0000 (16:44 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Tue, 15 Oct 2013 23:44:25 +0000 (16:44 -0700)
src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java [new file with mode: 0644]
src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
src/edu/uci/eecs/specCompiler/codeGenerator/Environment.java [new file with mode: 0644]
src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java
src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java
src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java

diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java
new file mode 100644 (file)
index 0000000..6359d82
--- /dev/null
@@ -0,0 +1,13 @@
+package edu.uci.eecs.specCompiler.codeGenerator;
+
+import java.util.ArrayList;
+
+public class CodeAddition {
+       public final int lineNum;
+       public final ArrayList<String> newCode;
+       
+       public CodeAddition(int lineNum, ArrayList<String> newCode) {
+               this.lineNum = lineNum;
+               this.newCode = newCode;
+       }
+}
index faac44c220849f22c6ba13c657b9f27e209e2dc1..cbd8d7f414baf1cfab99fcef7da9b483ba279df8 100644 (file)
@@ -1,8 +1,17 @@
 package edu.uci.eecs.specCompiler.codeGenerator;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 
+import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
+import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
 
 /**
  * <p>
@@ -14,6 +23,156 @@ import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
  * 
  */
 public class CodeGenerator {
-       ArrayList<SpecConstruct> _constructs;
-       
+       private SemanticsChecker _semantics;
+       private SpecExtractor _extractor;
+
+       private File[] srcFiles;
+
+       private HashMap<File, ArrayList<String>> contents;
+
+       private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
+
+       public CodeGenerator(File[] srcFiles) {
+               this.srcFiles = srcFiles;
+               this.contents = new HashMap<File, ArrayList<String>>();
+               readSrcFiles();
+               this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
+
+               _extractor = new SpecExtractor();
+
+               try {
+                       _extractor.extract(srcFiles);
+               } catch (SpecNotMatchException e1) {
+                       e1.printStackTrace();
+               }
+
+               _semantics = new SemanticsChecker(_extractor.getConstructs());
+               try {
+                       _semantics.check();
+                       System.out.println(_semantics);
+               } catch (SemanticsCheckerException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       private ArrayList<String> readSrcFile(File f) throws IOException {
+               BufferedReader bf = new BufferedReader(new FileReader(f));
+               ArrayList<String> content = new ArrayList<String>();
+               String curLine;
+               while ((curLine = bf.readLine()) != null) {
+                       content.add(curLine);
+               }
+               return content;
+       }
+
+       private void readSrcFiles() {
+               for (int i = 0; i < srcFiles.length; i++) {
+                       File f = srcFiles[i];
+                       if (!contents.containsKey(f)) {
+                               try {
+                                       contents.put(f, readSrcFile(f));
+                               } catch (IOException e) {
+                                       e.printStackTrace();
+                               }
+                       }
+               }
+       }
+
+       /**
+        * <p>
+        * Generate all the global code, including the "@DefineVar" in each
+        * "@Interface" define
+        * </p>
+        */
+       private void globalConstruct2Code(SpecConstruct inst) {
+               int lineNum = inst.endLineNum + 1;
+               GlobalConstruct construct = (GlobalConstruct) inst.construct; 
+               ArrayList<String> newCode = new ArrayList<String>();
+               
+               // Generate the code in global construct first
+               String globalCode = construct.code;
+               int begin = 0, end = 0;
+               while (end < globalCode.length()) {
+                       if (globalCode.charAt(end) == '\n') {
+                               String line = globalCode.substring(begin, end);
+                               newCode.add(line);
+                               begin = end + 1;
+                       }
+                       end++;
+               }
+               
+               CodeAddition addition = new CodeAddition(lineNum, newCode);
+               if (!codeAdditions.containsKey(inst.file)) {
+                       codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       private void interface2Code(SpecConstruct inst) {
+               int lineNum = inst.endLineNum + 1;
+               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>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       private void potentialCP2Code(SpecConstruct inst) {
+               int lineNum = inst.endLineNum + 1;
+               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>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       private void CPDefine2Code(SpecConstruct inst) {
+               int lineNum = inst.endLineNum + 1;
+               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>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       private void CPDefineCheck2Code(SpecConstruct inst) {
+               int lineNum = inst.endLineNum + 1;
+               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>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       public void generateCode() {
+
+       }
+
+       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")
+               };
+               CodeGenerator gen = new CodeGenerator(srcFiles);
+               gen.generateCode();
+       }
 }
diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/Environment.java b/src/edu/uci/eecs/specCompiler/codeGenerator/Environment.java
new file mode 100644 (file)
index 0000000..f4e75cc
--- /dev/null
@@ -0,0 +1,5 @@
+package edu.uci.eecs.specCompiler.codeGenerator;
+
+public class Environment {
+       public static String HOME_DIRECTORY = System.getProperty("user.dir");
+}
index f053ba4186f2424373b4646875e420a59a20eaf9..8f02b409dad58d7f999446c558800fb26a700afa 100644 (file)
@@ -4,7 +4,9 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 
+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;
@@ -14,33 +16,46 @@ import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
 
 public class SemanticsChecker {
        public final ArrayList<SpecConstruct> constructs;
-       public final HashMap<String, SpecConstruct> label2Construct;
+       public final HashMap<String, SpecConstruct> CPLabel2Construct;
+       public final HashMap<String, SpecConstruct> potentialCPLabel2Construct;
        public final HashMap<String, SpecConstruct> interfaceName2Construct;
+       public final HashSet<DefineVar> defineVars;
 
        public SemanticsChecker(ArrayList<SpecConstruct> constructs) {
                this.constructs = constructs;
-               this.label2Construct = new HashMap<String, SpecConstruct>();
+               this.CPLabel2Construct = new HashMap<String, SpecConstruct>();
+               this.potentialCPLabel2Construct = new HashMap<String, SpecConstruct>();
                this.interfaceName2Construct = new HashMap<String, SpecConstruct>();
+               this.defineVars = new HashSet<DefineVar>();
        }
 
-       private void checkHBLabelConsistency(String interfaceName, String label)
+       private void checkHBLabelConsistency(ConditionalInterface inst)
                        throws SemanticsCheckerException {
+               String interfaceName = inst.interfaceName,
+                               label = inst.hbConditionLabel;
                if (!interfaceName2Construct.containsKey(interfaceName)) {
                        throw new SemanticsCheckerException(
-                                       "In global construct, no interface \""
-                                                       + interfaceName + "\"!");
-               } else {
+                                       "In global construct, no interface \"" + interfaceName
+                                                       + "\"!");
+               } else if (!label.equals("")){
                        InterfaceConstruct iConstruct = (InterfaceConstruct) interfaceName2Construct
                                        .get(interfaceName).construct;
                        if (!iConstruct.hbConditions.containsKey(label)) {
                                throw new SemanticsCheckerException("Interface "
-                                               + interfaceName
-                                               + " doesn't contain HB_codition: " + label
-                                               + "!");
+                                               + interfaceName + " doesn't contain HB_codition: "
+                                               + label + "!");
                        }
                }
        }
 
+       private void checkLabelDuplication(Construct construct, String label)
+                       throws SemanticsCheckerException {
+               if (potentialCPLabel2Construct.containsKey(label) ||
+                               CPLabel2Construct.containsKey(label))
+                       throw new SemanticsCheckerException("In construct: " + construct
+                                       + "\"" + label + "\" has duplication.");
+       }
+
        public void check() throws SemanticsCheckerException {
                boolean hasGlobalConstruct = false;
                // First grab the information from the interface
@@ -49,9 +64,14 @@ public class SemanticsChecker {
                        if (inst instanceof InterfaceConstruct) {
                                InterfaceConstruct iConstruct = (InterfaceConstruct) inst;
                                interfaceName2Construct.put(iConstruct.name, constructs.get(i));
+                               for (int j = 0; j < iConstruct.action.defineVars.size(); j++) {
+                                       DefineVar var = iConstruct.action.defineVars.get(j);
+                                       var.renameVarName("__" + iConstruct.name + "_" + var.varName + "__");
+                               }
                        }
                }
 
+               String label;
                for (int i = 0; i < constructs.size(); i++) {
                        SpecConstruct inst = constructs.get(i);
                        Construct construct = inst.construct;
@@ -63,22 +83,49 @@ public class SemanticsChecker {
                                        throw new SemanticsCheckerException(
                                                        "More than one global construct!");
                                }
-
                                HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbConditions = theConstruct.hbRelations;
                                for (ConditionalInterface left : hbConditions.keySet()) {
                                        HashSet<ConditionalInterface> set = hbConditions.get(left);
-                                       checkHBLabelConsistency(left.interfaceName, left.hbConditionLabel);
+                                       checkHBLabelConsistency(left);
                                        for (ConditionalInterface right : set) {
-                                               checkHBLabelConsistency(right.interfaceName, right.hbConditionLabel);
+                                               checkHBLabelConsistency(right);
                                        }
                                }
                        } else if (construct instanceof PotentialCPDefineConstruct) {
                                PotentialCPDefineConstruct theConstruct = (PotentialCPDefineConstruct) construct;
-                               label2Construct.put(theConstruct.label, inst);
+                               label = theConstruct.label;
+                               checkLabelDuplication(construct, label);
+                               potentialCPLabel2Construct.put(label, inst);
                        } else if (construct instanceof CPDefineCheckConstruct) {
                                CPDefineCheckConstruct theConstruct = (CPDefineCheckConstruct) construct;
-                               label2Construct.put(theConstruct.label, inst);
+                               label = theConstruct.label;
+                               checkLabelDuplication(construct, label);
+                               CPLabel2Construct.put(label, inst);
+                       } else if (construct instanceof CPDefineConstruct) {
+                               CPDefineConstruct theConstruct = (CPDefineConstruct) construct;
+                               label = theConstruct.label;
+                               checkLabelDuplication(construct, label);
+                               CPLabel2Construct.put(label, inst);
                        }
                }
        }
+       
+       public String toString() {
+               StringBuilder sb = new StringBuilder();
+               sb.append("Interface name 2 Construct:\n");
+               for (String interfaceName : interfaceName2Construct.keySet()) {
+                       sb.append(interfaceName + "\t" + interfaceName2Construct.get(interfaceName) + "\n");
+               }
+               
+               sb.append("Potential commit point label 2 Construct:\n");
+               for (String label : potentialCPLabel2Construct.keySet()) {
+                       sb.append(label + "\t" + potentialCPLabel2Construct.get(label) + "\n");
+               }
+               
+               sb.append("Commit point label 2 Construct:\n");
+               for (String label : CPLabel2Construct.keySet()) {
+                       sb.append(label + "\t" + CPLabel2Construct.get(label) + "\n");
+               }
+               return sb.toString();
+       }
 }
index b8b8689512fb9a104efe069104704069b9283e0e..0522a48aebc1f8b9fe67171c21c4b6e93b7643fc 100644 (file)
@@ -7,30 +7,44 @@ public class ActionSubConstruct {
                public final String varType;
                public final String varName;
                public final String varExpr;
-               
+               private String newVarName;
+
                public DefineVar(String varType, String varName, String varExpr) {
                        this.varType = varType;
                        this.varName = varName;
                        this.varExpr = varExpr;
+                       this.newVarName = null;
+               }
+
+               public void renameVarName(String newName) {
+                       this.newVarName = newName;
+               }
+
+               public String getNewVarName() {
+                       return this.newVarName;
                }
-               
+
                public String toString() {
-                       return varType + " " + varName + " = " + varExpr;
+                       if (newVarName == null)
+                               return varType + " " + varName + " = " + varExpr;
+                       else
+                               return varType + " " + varName + "(" + newVarName + ")" + " = "
+                                               + varExpr;
                }
        }
-       
+
        public final ArrayList<DefineVar> defineVars;
        public final String code;
-       
+
        public ActionSubConstruct(ArrayList<DefineVar> defineVars, String code) {
                this.code = code;
                this.defineVars = defineVars;
        }
-       
+
        public void addDefineVar(DefineVar defineVar) {
                defineVars.add(defineVar);
        }
-       
+
        public String toString() {
                StringBuilder sb = new StringBuilder();
                sb.append("@Action:\n");
index eb4bdb2d5d6ea704da9c7b1c5f858906b7613152..3c462c17b4735e5a425326420c556163f82dcda5 100644 (file)
@@ -28,7 +28,7 @@ public class SpecExtractor {
        private int _beginLineNum, _endLineNum;
        private String _beginLine;
 
-       SpecExtractor() {
+       public SpecExtractor() {
                _constructs = new ArrayList<SpecConstruct>();
        }
 
@@ -81,8 +81,9 @@ public class SpecExtractor {
                                                                                specText.toString(), file,
                                                                                _beginLineNum, _endLineNum, inst);
                                                        }
+                                                       _constructs.add(specConstruct);
                                                        specText = new StringBuilder();
-                                                       System.out.println(specConstruct);
+//                                                     System.out.println(specConstruct);
                                                }
                                        }
                                } else {
@@ -107,8 +108,9 @@ public class SpecExtractor {
                                                                        specText.toString(), file,
                                                                        _beginLineNum, _endLineNum, inst);
                                                }
-                                               System.out.println(specConstruct);
+                                               _constructs.add(specConstruct);
                                                specText = new StringBuilder();
+//                                             System.out.println(specConstruct);
                                        }
                                }
                        }