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;
11 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.Construct;
14 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
15 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
16 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
20 import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
24 * This class will generate the annotated C code that can run on the current
31 public class CodeGenerator {
32 private SemanticsChecker _semantics;
33 private SpecExtractor _extractor;
35 private File[] srcFiles;
37 private HashMap<File, ArrayList<String>> contents;
39 private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
41 public CodeGenerator(File[] srcFiles) {
42 this.srcFiles = srcFiles;
43 this.contents = new HashMap<File, ArrayList<String>>();
45 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
47 _extractor = new SpecExtractor();
50 _extractor.extract(srcFiles);
51 } catch (SpecNotMatchException e1) {
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(SpecConstruct inst) {
94 int lineNum = inst.endLineNum + 1;
95 GlobalConstruct construct = (GlobalConstruct) inst.construct;
96 ArrayList<String> newCode = new ArrayList<String>();
98 // Generate the inner class definition
99 newCode.add("class Sequential {\n");
100 newCode.add("public:\n");
102 // Generate the code in global construct first
103 SequentialDefineSubConstruct globalCode = construct.code;
104 breakCodeLines(newCode, globalCode.declareVar);
105 breakCodeLines(newCode, globalCode.defineFunc);
107 // Generate code from the DefineVar, __COND_SAT__ and __ID__
110 // Generate the end of the inner class definition
112 // printCode(newCode);
114 CodeAddition addition = new CodeAddition(lineNum, newCode);
115 if (!codeAdditions.containsKey(inst.file)) {
116 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
118 codeAdditions.get(inst.file).add(addition);
121 private void breakCodeLines(ArrayList<String> newCode, String code) {
122 int begin = 0, end = 0;
123 while (end < code.length()) {
124 if (code.charAt(end) == '\n') {
125 String line = code.substring(begin, end);
133 private void printCode(ArrayList<String> code) {
134 for (int i = 0; i < code.size(); i++) {
135 System.out.println(code.get(i));
139 private void interface2Code(SpecConstruct inst) {
140 int lineNum = inst.endLineNum + 1;
141 GlobalConstruct construct = (GlobalConstruct) inst.construct;
142 ArrayList<String> newCode = new ArrayList<String>();
145 CodeAddition addition = new CodeAddition(lineNum, newCode);
146 if (!codeAdditions.containsKey(inst.file)) {
147 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
149 codeAdditions.get(inst.file).add(addition);
152 private void potentialCP2Code(SpecConstruct inst) {
153 int lineNum = inst.endLineNum + 1;
154 GlobalConstruct construct = (GlobalConstruct) inst.construct;
155 ArrayList<String> newCode = new ArrayList<String>();
158 CodeAddition addition = new CodeAddition(lineNum, newCode);
159 if (!codeAdditions.containsKey(inst.file)) {
160 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
162 codeAdditions.get(inst.file).add(addition);
165 private void CPDefine2Code(SpecConstruct inst) {
166 int lineNum = inst.endLineNum + 1;
167 GlobalConstruct construct = (GlobalConstruct) inst.construct;
168 ArrayList<String> newCode = new ArrayList<String>();
171 CodeAddition addition = new CodeAddition(lineNum, newCode);
172 if (!codeAdditions.containsKey(inst.file)) {
173 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
175 codeAdditions.get(inst.file).add(addition);
178 private void CPDefineCheck2Code(SpecConstruct inst) {
179 int lineNum = inst.endLineNum + 1;
180 GlobalConstruct construct = (GlobalConstruct) inst.construct;
181 ArrayList<String> newCode = new ArrayList<String>();
184 CodeAddition addition = new CodeAddition(lineNum, newCode);
185 if (!codeAdditions.containsKey(inst.file)) {
186 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
188 codeAdditions.get(inst.file).add(addition);
191 public void generateCode() {
192 for (int i = 0; i < _semantics.constructs.size(); i++) {
193 SpecConstruct inst = _semantics.constructs.get(i);
194 Construct construct = inst.construct;
195 if (construct instanceof GlobalConstruct) {
196 globalConstruct2Code(inst);
197 } else if (construct instanceof InterfaceConstruct) {
198 interface2Code(inst);
199 } else if (construct instanceof PotentialCPDefineConstruct) {
200 potentialCP2Code(inst);
201 } else if (construct instanceof CPDefineConstruct) {
203 } else if (construct instanceof CPDefineCheckConstruct) {
204 CPDefineCheck2Code(inst);
209 public static void main(String[] argvs) {
210 String homeDir = Environment.HOME_DIRECTORY;
212 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
214 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
215 // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
217 CodeGenerator gen = new CodeGenerator(srcFiles);