1 package edu.uci.eecs.specCompiler.codeGenerator;
3 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Iterator;
12 import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
13 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
14 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
16 import edu.uci.eecs.specCompiler.specExtraction.Construct;
17 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
25 * This class will generate the annotated C code that can run on the current
32 public class CodeGenerator {
33 private SemanticsChecker _semantics;
34 private SpecExtractor _extractor;
36 private File[] srcFiles;
38 private HashMap<File, ArrayList<String>> contents;
40 private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
42 private ArrayList<String> globalContent;
44 public CodeGenerator(File[] srcFiles) {
45 this.srcFiles = srcFiles;
46 this.contents = new HashMap<File, ArrayList<String>>();
47 this.globalContent = null;
49 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
51 _extractor = new SpecExtractor();
53 _extractor.extract(srcFiles);
55 _semantics = new SemanticsChecker(_extractor.getConstructs());
58 System.out.println(_semantics);
59 } catch (SemanticsCheckerException e) {
64 private ArrayList<String> readSrcFile(File f) throws IOException {
65 BufferedReader bf = new BufferedReader(new FileReader(f));
66 ArrayList<String> content = new ArrayList<String>();
68 while ((curLine = bf.readLine()) != null) {
74 private void readSrcFiles() {
75 for (int i = 0; i < srcFiles.length; i++) {
77 if (!contents.containsKey(f)) {
79 contents.put(f, readSrcFile(f));
80 } catch (IOException e) {
89 * Generate all the global code, including the "@DefineVar" in each
93 private void globalConstruct2Code(GlobalConstruct construct) {
94 ArrayList<String> newCode = CodeVariables.generateGlobalVarDeclaration(
95 _semantics, construct);
96 // Record the global content array to generate the new file
97 globalContent = newCode;
100 // Mainly rename and wrap the interface
101 private void interface2Code(InterfaceConstruct construct)
102 throws InterfaceWrongFormatException {
103 int lineNum = construct.begin + 1;
105 // Rename the interface name
106 File file = inst.file;
107 String funcDecl = inst.interfaceDeclBody;
108 // Rename the function declaration
109 String funcName = renameInterface(inst);
110 // Also rename the function definition if it's separated from the
112 SpecConstruct definition = _semantics.interfaceName2DefineConstruct
113 .get(construct.name);
114 if (definition != null) {
115 String funcDefintionName = renameInterface(definition);
116 assert (funcDefintionName.equals(funcName));
119 // Generate new wrapper
120 ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(
122 // Add it to the codeAdditions
123 CodeAddition addition = new CodeAddition(lineNum, newCode);
124 if (!codeAdditions.containsKey(inst.file)) {
125 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
127 codeAdditions.get(inst.file).add(addition);
130 // Returns the function name that has been renamed and replace the old line
131 private String renameInterface(Construct inst)
132 throws InterfaceWrongFormatException {
133 String funcDecl = inst.interfaceDeclBody;
134 ArrayList<String> content = contents.get(inst.file);
136 // Depending on "(" to find the function name, so it doesn't matter if
137 // there's any template
138 int beginIdx = funcDecl.indexOf('(');
139 if (beginIdx == -1) {
140 throw new InterfaceWrongFormatException(funcDecl
141 + "\n has wrong format!");
143 IDExtractor idExtractor = new IDExtractor(funcDecl, beginIdx);
144 String funcName = idExtractor.getPrevID();
145 int idBeginIdx = idExtractor.getIDBeginIdx(), idEndIdx = idExtractor
146 .getIDEndIdx(), idLineBeginIdx = idExtractor.lineBeginIdxOfID(), idLineEndIdx = idExtractor
148 String newLine = funcDecl.substring(idLineBeginIdx, idBeginIdx)
149 + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName
150 + funcDecl.substring(idEndIdx + 1, idLineEndIdx + 1);
152 int lineNumOfID = idExtractor.lineNumOfID();
153 // Be careful: lineNum - 1 -> index of content array
154 content.set(inst.endLineNum + lineNumOfID, newLine);
158 private void potentialCPDefine2Code(PotentialCPDefineConstruct construct) {
159 int lineNum = construct.beginLineNum;
160 ArrayList<String> newCode = new ArrayList<String>();
162 CodeAddition addition = new CodeAddition(lineNum, newCode);
163 if (!codeAdditions.containsKey(construct.file)) {
164 codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
166 codeAdditions.get(construct.file).add(addition);
169 private void CPDefine2Code(CPDefineConstruct construct) {
170 int lineNum = construct.beginLineNum;
171 ArrayList<String> newCode = new ArrayList<String>();
173 CodeAddition addition = new CodeAddition(lineNum, newCode);
174 if (!codeAdditions.containsKey(construct.file)) {
175 codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
177 codeAdditions.get(construct.file).add(addition);
180 private void CPDefineCheck2Code(CPDefineCheckConstruct construct) {
181 int lineNum = construct.beginLineNum;
182 ArrayList<String> newCode = new ArrayList<String>();
184 CodeAddition addition = new CodeAddition(lineNum, newCode);
185 if (!codeAdditions.containsKey(construct.file)) {
186 codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
188 codeAdditions.get(construct.file).add(addition);
191 public void generateCode() {
192 for (int i = 0; i < _semantics.constructs.size(); i++) {
193 Construct construct = _semantics.constructs.get(i);
194 if (construct instanceof GlobalConstruct) {
195 globalConstruct2Code((GlobalConstruct) construct);
196 } else if (construct instanceof InterfaceConstruct) {
198 interface2Code((InterfaceConstruct) construct);
199 } catch (InterfaceWrongFormatException e) {
202 } else if (construct instanceof PotentialCPDefineConstruct) {
203 // potentialCP2Code(inst);
204 } else if (construct instanceof CPDefineConstruct) {
205 // CPDefine2Code(inst);
206 } else if (construct instanceof CPDefineCheckConstruct) {
207 // CPDefineCheck2Code(inst);
212 public static void main(String[] argvs) {
213 String homeDir = Environment.HOME_DIRECTORY;
215 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
217 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"), };
218 // new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
219 // new File(homeDir + "/benchmark/ms-queue/my_queue.h") };
220 CodeGenerator gen = new CodeGenerator(srcFiles);