--- /dev/null
+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;
+ }
+}
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>
*
*/
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();
+ }
}
--- /dev/null
+package edu.uci.eecs.specCompiler.codeGenerator;
+
+public class Environment {
+ public static String HOME_DIRECTORY = System.getProperty("user.dir");
+}
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;
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
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;
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();
+ }
}
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");
private int _beginLineNum, _endLineNum;
private String _beginLine;
- SpecExtractor() {
+ public SpecExtractor() {
_constructs = new ArrayList<SpecConstruct>();
}
specText.toString(), file,
_beginLineNum, _endLineNum, inst);
}
+ _constructs.add(specConstruct);
specText = new StringBuilder();
- System.out.println(specConstruct);
+// System.out.println(specConstruct);
}
}
} else {
specText.toString(), file,
_beginLineNum, _endLineNum, inst);
}
- System.out.println(specConstruct);
+ _constructs.add(specConstruct);
specText = new StringBuilder();
+// System.out.println(specConstruct);
}
}
}