From: Peizhao Ou Date: Tue, 15 Oct 2013 23:44:25 +0000 (-0700) Subject: add more on code generation X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e6d5d77e01de375b41a74e5b4c68b734b3f978cb;p=cdsspec-compiler.git add more on code generation --- diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java new file mode 100644 index 0000000..6359d82 --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeAddition.java @@ -0,0 +1,13 @@ +package edu.uci.eecs.specCompiler.codeGenerator; + +import java.util.ArrayList; + +public class CodeAddition { + public final int lineNum; + public final ArrayList newCode; + + public CodeAddition(int lineNum, ArrayList newCode) { + this.lineNum = lineNum; + this.newCode = newCode; + } +} diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java index faac44c..cbd8d7f 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java @@ -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; /** *

@@ -14,6 +23,156 @@ import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct; * */ public class CodeGenerator { - ArrayList _constructs; - + private SemanticsChecker _semantics; + private SpecExtractor _extractor; + + private File[] srcFiles; + + private HashMap> contents; + + private HashMap> codeAdditions; + + public CodeGenerator(File[] srcFiles) { + this.srcFiles = srcFiles; + this.contents = new HashMap>(); + readSrcFiles(); + this.codeAdditions = new HashMap>(); + + _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 readSrcFile(File f) throws IOException { + BufferedReader bf = new BufferedReader(new FileReader(f)); + ArrayList content = new ArrayList(); + 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(); + } + } + } + } + + /** + *

+ * Generate all the global code, including the "@DefineVar" in each + * "@Interface" define + *

+ */ + private void globalConstruct2Code(SpecConstruct inst) { + int lineNum = inst.endLineNum + 1; + GlobalConstruct construct = (GlobalConstruct) inst.construct; + ArrayList newCode = new ArrayList(); + + // 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()); + } + codeAdditions.get(inst.file).add(addition); + } + + private void interface2Code(SpecConstruct inst) { + int lineNum = inst.endLineNum + 1; + GlobalConstruct construct = (GlobalConstruct) inst.construct; + ArrayList newCode = new ArrayList(); + + + CodeAddition addition = new CodeAddition(lineNum, newCode); + if (!codeAdditions.containsKey(inst.file)) { + codeAdditions.put(inst.file, new ArrayList()); + } + codeAdditions.get(inst.file).add(addition); + } + + private void potentialCP2Code(SpecConstruct inst) { + int lineNum = inst.endLineNum + 1; + GlobalConstruct construct = (GlobalConstruct) inst.construct; + ArrayList newCode = new ArrayList(); + + + CodeAddition addition = new CodeAddition(lineNum, newCode); + if (!codeAdditions.containsKey(inst.file)) { + codeAdditions.put(inst.file, new ArrayList()); + } + codeAdditions.get(inst.file).add(addition); + } + + private void CPDefine2Code(SpecConstruct inst) { + int lineNum = inst.endLineNum + 1; + GlobalConstruct construct = (GlobalConstruct) inst.construct; + ArrayList newCode = new ArrayList(); + + + CodeAddition addition = new CodeAddition(lineNum, newCode); + if (!codeAdditions.containsKey(inst.file)) { + codeAdditions.put(inst.file, new ArrayList()); + } + codeAdditions.get(inst.file).add(addition); + } + + private void CPDefineCheck2Code(SpecConstruct inst) { + int lineNum = inst.endLineNum + 1; + GlobalConstruct construct = (GlobalConstruct) inst.construct; + ArrayList newCode = new ArrayList(); + + + CodeAddition addition = new CodeAddition(lineNum, newCode); + if (!codeAdditions.containsKey(inst.file)) { + codeAdditions.put(inst.file, new ArrayList()); + } + 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 index 0000000..f4e75cc --- /dev/null +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/Environment.java @@ -0,0 +1,5 @@ +package edu.uci.eecs.specCompiler.codeGenerator; + +public class Environment { + public static String HOME_DIRECTORY = System.getProperty("user.dir"); +} diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java b/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java index f053ba4..8f02b40 100644 --- a/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java +++ b/src/edu/uci/eecs/specCompiler/codeGenerator/SemanticsChecker.java @@ -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 constructs; - public final HashMap label2Construct; + public final HashMap CPLabel2Construct; + public final HashMap potentialCPLabel2Construct; public final HashMap interfaceName2Construct; + public final HashSet defineVars; public SemanticsChecker(ArrayList constructs) { this.constructs = constructs; - this.label2Construct = new HashMap(); + this.CPLabel2Construct = new HashMap(); + this.potentialCPLabel2Construct = new HashMap(); this.interfaceName2Construct = new HashMap(); + this.defineVars = new HashSet(); } - 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> hbConditions = theConstruct.hbRelations; for (ConditionalInterface left : hbConditions.keySet()) { HashSet 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(); + } } diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java index b8b8689..0522a48 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/ActionSubConstruct.java @@ -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 defineVars; public final String code; - + public ActionSubConstruct(ArrayList 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"); diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java index eb4bdb2..3c462c1 100644 --- a/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java +++ b/src/edu/uci/eecs/specCompiler/specExtraction/SpecExtractor.java @@ -28,7 +28,7 @@ public class SpecExtractor { private int _beginLineNum, _endLineNum; private String _beginLine; - SpecExtractor() { + public SpecExtractor() { _constructs = new ArrayList(); } @@ -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); } } }