X-Git-Url: http://plrg.eecs.uci.edu/git/?p=cdsspec-compiler.git;a=blobdiff_plain;f=src%2Fedu%2Fuci%2Feecs%2FspecCompiler%2FcodeGenerator%2FCodeGenerator.java;h=4acbfb623e57735f0eb8dd66cf9bde72d48c03fc;hp=faac44c220849f22c6ba13c657b9f27e209e2dc1;hb=903198262494ab58a885eed90d6310ea7ec917ac;hpb=70fd401b67750e8efbd73540e1affa7f65c61c1c
diff --git a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
index faac44c..4acbfb6 100644
--- a/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
+++ b/src/edu/uci/eecs/specCompiler/codeGenerator/CodeGenerator.java
@@ -1,8 +1,33 @@
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.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
-import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
+import edu.uci.eecs.specCompiler.specExtraction.Construct;
+import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
+import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ParserUtils;
+import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
+import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
/**
*
@@ -14,6 +39,299 @@ import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
*
*/
public class CodeGenerator {
- ArrayList _constructs;
+ private SemanticsChecker _semantics;
+ private SpecExtractor _extractor;
+
+ private File[] srcFiles;
+
+ private HashMap srcFilesInfo;
+
+ private HashMap> codeAdditions;
+ private String homeDir;
+
+ public CodeGenerator(File[] srcFiles) {
+ this.srcFiles = srcFiles;
+ _extractor = new SpecExtractor();
+ _extractor.extract(srcFiles);
+
+ this.srcFilesInfo = _extractor.srcFilesInfo;
+
+ this.codeAdditions = new HashMap>();
+
+ _semantics = new SemanticsChecker(_extractor);
+ try {
+ _semantics.check();
+ System.out.println(_semantics);
+ } catch (SemanticsCheckerException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ *
+ * Generate all the global code, including the "@DefineVar" in each
+ * "@Interface" define
+ *
+ */
+ private void globalConstruct2Code(GlobalConstruct construct) {
+ ArrayList newCode = CodeVariables.generateGlobalVarDeclaration(
+ _semantics, construct);
+ // Add it to the codeAdditions
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ CodeAddition addition = new CodeAddition(construct.beginLineNum,
+ newCode);
+ codeAdditions.get(construct.file).add(addition);
+ newCode = CodeVariables.generateStaticVarDefine(_semantics, construct);
+ if (newCode.size() > 0) {
+ addition = new CodeAddition(
+ _semantics.getClassEndConstruct().beginLineNum, newCode);
+ codeAdditions.get(construct.file).add(addition);
+ }
+ }
+
+ // Wrap the interface and then renaem it
+ private void interface2Code(InterfaceConstruct 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 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());
+ }
+ 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());
+ }
+ codeAdditions.get(defineConstruct.file).add(addition);
+ } else { // No declaration needed but should add forward declaration in
+ // Class
+ // Last generate the definition
+ newCode = new ArrayList();
+ if (_semantics.getOption("CLASS") == null) {
+ newCode.addAll(CodeVariables
+ .generateInterfaceWrapperDeclaration(_semantics,
+ construct));
+ }
+ newCode.addAll(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());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ // Don't forget to rename the interface
+ CodeVariables.renameInterface(_semantics, construct);
+ }
+
+ private void potentialCPDefine2Code(PotentialCPDefineConstruct construct) {
+ int lineNum = construct.beginLineNum;
+ ArrayList newCode = CodeVariables.generatePotentialCPDefine(
+ _semantics, construct);
+
+ CodeAddition addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ private void CPDefine2Code(CPDefineConstruct construct) {
+ int lineNum = construct.beginLineNum;
+ ArrayList newCode = CodeVariables.generateCPDefine(_semantics,
+ construct);
+
+ CodeAddition addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ private void CPDefineCheck2Code(CPDefineCheckConstruct construct) {
+ int lineNum = construct.beginLineNum;
+ ArrayList newCode = CodeVariables.generateCPDefineCheck(
+ _semantics, construct);
+
+ CodeAddition addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ private void CPClear2Code(CPClearConstruct construct) {
+ int lineNum = construct.beginLineNum;
+ ArrayList newCode = CodeVariables.generateCPClear(
+ _semantics, construct);
+
+ CodeAddition addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ /**
+ * private void ClassEnd2Code(ClassEndConstruct construct) { int lineNum =
+ * construct.beginLineNum; ArrayList newCode =
+ * CodeVariables.generateStaticVarDefine(_semantics,
+ * _semantics.getGlobalConstruct());
+ *
+ * CodeAddition addition = new CodeAddition(lineNum, newCode); if
+ * (!codeAdditions.containsKey(construct.file)) {
+ * codeAdditions.put(construct.file, new ArrayList()); }
+ * codeAdditions.get(construct.file).add(addition); }
+ */
+
+ private void EntryPoint2Code(EntryPointConstruct construct) {
+ int lineNum = construct.beginLineNum;
+ ArrayList newCode = new ArrayList();
+ newCode.addAll(CodeVariables.generateEntryPointInitCall());
+
+ CodeAddition addition = new CodeAddition(lineNum, newCode);
+ if (!codeAdditions.containsKey(construct.file)) {
+ codeAdditions.put(construct.file, new ArrayList());
+ }
+ codeAdditions.get(construct.file).add(addition);
+ }
+
+ private ArrayList insertAnnotation2Src(
+ ArrayList additions, ArrayList content) {
+ int totalSize = content.size();
+ for (int i = 0; i < additions.size(); i++) {
+ totalSize += additions.size();
+ }
+ ArrayList newContent = new ArrayList(totalSize);
+ int curSrcLine = 0;
+ for (int i = 0; i < additions.size(); i++) {
+ CodeAddition addition = additions.get(i);
+ if (curSrcLine < addition.lineNum) {
+ // Be careful, subList is the interval [begin, end)
+ newContent
+ .addAll(content.subList(curSrcLine, addition.lineNum));
+ curSrcLine = addition.lineNum;
+ }
+ newContent.addAll(addition.newCode);
+ }
+ newContent.addAll(content.subList(curSrcLine, content.size()));
+ return newContent;
+ }
+
+ public void generateCode() {
+ for (int i = 0; i < _semantics.constructs.size(); i++) {
+ Construct construct = _semantics.constructs.get(i);
+ if (construct instanceof GlobalConstruct) {
+ globalConstruct2Code((GlobalConstruct) construct);
+ } else if (construct instanceof InterfaceConstruct) {
+ interface2Code((InterfaceConstruct) construct);
+ } else if (construct instanceof PotentialCPDefineConstruct) {
+ potentialCPDefine2Code((PotentialCPDefineConstruct) construct);
+ } else if (construct instanceof CPDefineConstruct) {
+ CPDefine2Code((CPDefineConstruct) construct);
+ } else if (construct instanceof CPDefineCheckConstruct) {
+ CPDefineCheck2Code((CPDefineCheckConstruct) construct);
+ } else if (construct instanceof CPClearConstruct) {
+ CPClear2Code((CPClearConstruct) construct);
+ } else if (construct instanceof EntryPointConstruct) {
+ EntryPoint2Code((EntryPointConstruct) construct);
+ }
+ }
+
+ // ClassEndConstruct endConstruct = _semantics.getClassEndConstruct();
+ // if (endConstruct != null) {
+ // ClassEnd2Code(endConstruct);
+ // }
+
+ // Sort code additions
+ for (File file : codeAdditions.keySet()) {
+ ArrayList additions = codeAdditions.get(file);
+
+ if (additions.size() == 0) // Simply do nothing
+ continue;
+ ArrayList content = _semantics.srcFilesInfo.get(file).content;
+ Collections.sort(additions, CodeAddition.lineNumComparator);
+ // Insert generated annotation to the source files
+ ArrayList newContent = insertAnnotation2Src(additions,
+ content);
+ // Write it back to file
+ ParserUtils.write2File(file, newContent);
+ }
+ }
+
+ public static void main(String[] argvs) {
+ String homeDir = Environment.HOME_DIRECTORY;
+
+ File[] srcLinuxRWLocks = { new File(homeDir
+ + "/benchmark/linuxrwlocks/linuxrwlocks.c") };
+
+ File[] srcHashtable = {
+ new File(homeDir
+ + "/benchmark/cliffc-hashtable/cliffc_hashtable.h"),
+ new File(homeDir + "/benchmark/cliffc-hashtable/main.cc") };
+
+ File[] srcMSQueue = {
+ new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
+ new File(homeDir + "/benchmark/ms-queue/testcase.c"),
+ new File(homeDir + "/benchmark/ms-queue/testcase1.c"),
+ new File(homeDir + "/benchmark/ms-queue/main.c"),
+ new File(homeDir + "/benchmark/ms-queue/my_queue.h") };
+
+ File[] srcRCU = { new File(homeDir
+ + "/benchmark/read-copy-update/rcu.cc") };
+
+ File[] srcDeque = {
+ new File(homeDir + "/benchmark/chase-lev-deque-bugfix/deque.c"),
+ new File(homeDir + "/benchmark/chase-lev-deque-bugfix/main.c"),
+ new File(homeDir + "/benchmark/chase-lev-deque-bugfix/deque.h") };
+
+ File[] srcMCSLock = {
+ new File(homeDir + "/benchmark/mcs-lock/mcs-lock.cc"),
+ new File(homeDir + "/benchmark/mcs-lock/mcs-lock.h") };
+
+ File[] srcSPSCQueue = {
+ new File(homeDir + "/benchmark/spsc-bugfix/spsc-queue.cc"),
+ new File(homeDir + "/benchmark/spsc-bugfix/eventcount.h"),
+ new File(homeDir + "/benchmark/spsc-bugfix/queue.h") };
+
+ File[] srcMPMCQueue = {
+ new File(homeDir + "/benchmark/mpmc-queue/mpmc-queue.h"),
+ new File(homeDir + "/benchmark/mpmc-queue/mpmc-queue.cc") };
+//
+// File[][] sources = { srcLinuxRWLocks, srcMSQueue, srcRCU,
+// srcDeque, srcMCSLock, srcSPSCQueue, srcMPMCQueue, srcHashtable };
+
+ File[][] sources = {srcMSQueue, srcMPMCQueue };
+ // Compile all the benchmarks
+ for (int i = 0; i < sources.length; i++) {
+ CodeGenerator gen = new CodeGenerator(sources[i]);
+ gen.generateCode();
+ }
+ }
}