lots of changes
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeGenerator.java
index faac44c220849f22c6ba13c657b9f27e209e2dc1..399c036b15661ab3876d27ccb405f1383dd662de 100644 (file)
@@ -1,8 +1,24 @@
 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 java.util.Iterator;
 
-import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
+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;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
 
 /**
  * <p>
@@ -14,6 +30,194 @@ 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;
+
+       private ArrayList<String> globalContent;
+
+       public CodeGenerator(File[] srcFiles) {
+               this.srcFiles = srcFiles;
+               this.contents = new HashMap<File, ArrayList<String>>();
+               this.globalContent = null;
+               readSrcFiles();
+               this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
+
+               _extractor = new SpecExtractor();
+
+               _extractor.extract(srcFiles);
+
+               _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(GlobalConstruct construct) {
+               ArrayList<String> newCode = CodeVariables.generateGlobalVarDeclaration(
+                               _semantics, construct);
+               // Record the global content array to generate the new file
+               globalContent = newCode;
+       }
+
+       // Mainly rename and wrap the interface
+       private void interface2Code(InterfaceConstruct construct)
+                       throws InterfaceWrongFormatException {
+               int lineNum = construct.begin + 1;
+
+               // Rename the interface name
+               File file = inst.file;
+               String funcDecl = inst.interfaceDeclBody;
+               // Rename the function declaration
+               String funcName = renameInterface(inst);
+               // Also rename the function definition if it's separated from the
+               // declaration
+               SpecConstruct definition = _semantics.interfaceName2DefineConstruct
+                               .get(construct.name);
+               if (definition != null) {
+                       String funcDefintionName = renameInterface(definition);
+                       assert (funcDefintionName.equals(funcName));
+               }
+
+               // Generate new wrapper
+               ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(
+                               _semantics, inst);
+               // Add it to the codeAdditions
+               CodeAddition addition = new CodeAddition(lineNum, newCode);
+               if (!codeAdditions.containsKey(inst.file)) {
+                       codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
+               }
+               codeAdditions.get(inst.file).add(addition);
+       }
+
+       // Returns the function name that has been renamed and replace the old line
+       private String renameInterface(Construct inst)
+                       throws InterfaceWrongFormatException {
+               String funcDecl = inst.interfaceDeclBody;
+               ArrayList<String> content = contents.get(inst.file);
+
+               // Depending on "(" to find the function name, so it doesn't matter if
+               // there's any template
+               int beginIdx = funcDecl.indexOf('(');
+               if (beginIdx == -1) {
+                       throw new InterfaceWrongFormatException(funcDecl
+                                       + "\n has wrong format!");
+               }
+               IDExtractor idExtractor = new IDExtractor(funcDecl, beginIdx);
+               String funcName = idExtractor.getPrevID();
+               int idBeginIdx = idExtractor.getIDBeginIdx(), idEndIdx = idExtractor
+                               .getIDEndIdx(), idLineBeginIdx = idExtractor.lineBeginIdxOfID(), idLineEndIdx = idExtractor
+                               .lineEndIdxOfID();
+               String newLine = funcDecl.substring(idLineBeginIdx, idBeginIdx)
+                               + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName
+                               + funcDecl.substring(idEndIdx + 1, idLineEndIdx + 1);
+
+               int lineNumOfID = idExtractor.lineNumOfID();
+               // Be careful: lineNum - 1 -> index of content array
+               content.set(inst.endLineNum + lineNumOfID, newLine);
+               return funcName;
+       }
+
+       private void potentialCPDefine2Code(PotentialCPDefineConstruct construct) {
+               int lineNum = construct.beginLineNum;
+               ArrayList<String> newCode = new ArrayList<String>();
+
+               CodeAddition addition = new CodeAddition(lineNum, newCode);
+               if (!codeAdditions.containsKey(construct.file)) {
+                       codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+               }
+               codeAdditions.get(construct.file).add(addition);
+       }
+
+       private void CPDefine2Code(CPDefineConstruct construct) {
+               int lineNum = construct.beginLineNum;
+               ArrayList<String> newCode = new ArrayList<String>();
+
+               CodeAddition addition = new CodeAddition(lineNum, newCode);
+               if (!codeAdditions.containsKey(construct.file)) {
+                       codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+               }
+               codeAdditions.get(construct.file).add(addition);
+       }
+
+       private void CPDefineCheck2Code(CPDefineCheckConstruct construct) {
+               int lineNum = construct.beginLineNum;
+               ArrayList<String> newCode = new ArrayList<String>();
+
+               CodeAddition addition = new CodeAddition(lineNum, newCode);
+               if (!codeAdditions.containsKey(construct.file)) {
+                       codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
+               }
+               codeAdditions.get(construct.file).add(addition);
+       }
+
+       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) {
+                               try {
+                                       interface2Code((InterfaceConstruct) construct);
+                               } catch (InterfaceWrongFormatException e) {
+                                       e.printStackTrace();
+                               }
+                       } else if (construct instanceof PotentialCPDefineConstruct) {
+                               // potentialCP2Code(inst);
+                       } else if (construct instanceof CPDefineConstruct) {
+                               // CPDefine2Code(inst);
+                       } else if (construct instanceof CPDefineCheckConstruct) {
+                               // CPDefineCheck2Code(inst);
+                       }
+               }
+       }
+
+       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"),
+               // new File(homeDir + "/benchmark/ms-queue/my_queue.h") };
+               CodeGenerator gen = new CodeGenerator(srcFiles);
+               gen.generateCode();
+       }
 }