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.SpecConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
19 import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
23 * This class will generate the annotated C code that can run on the current
30 public class CodeGenerator {
31 private SemanticsChecker _semantics;
32 private SpecExtractor _extractor;
34 private File[] srcFiles;
36 private HashMap<File, ArrayList<String>> contents;
38 private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
40 public CodeGenerator(File[] srcFiles) {
41 this.srcFiles = srcFiles;
42 this.contents = new HashMap<File, ArrayList<String>>();
44 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
46 _extractor = new SpecExtractor();
49 _extractor.extract(srcFiles);
50 } catch (SpecNotMatchException e1) {
54 _semantics = new SemanticsChecker(_extractor.getConstructs());
57 System.out.println(_semantics);
58 } catch (SemanticsCheckerException e) {
63 private ArrayList<String> readSrcFile(File f) throws IOException {
64 BufferedReader bf = new BufferedReader(new FileReader(f));
65 ArrayList<String> content = new ArrayList<String>();
67 while ((curLine = bf.readLine()) != null) {
73 private void readSrcFiles() {
74 for (int i = 0; i < srcFiles.length; i++) {
76 if (!contents.containsKey(f)) {
78 contents.put(f, readSrcFile(f));
79 } catch (IOException e) {
88 * Generate all the global code, including the "@DefineVar" in each
92 private void globalConstruct2Code(SpecConstruct inst) {
93 int lineNum = inst.endLineNum + 1;
94 GlobalConstruct construct = (GlobalConstruct) inst.construct;
95 ArrayList<String> newCode = new ArrayList<String>();
97 // Generate the code in global construct first
98 String globalCode = construct.code;
99 int begin = 0, end = 0;
100 while (end < globalCode.length()) {
101 if (globalCode.charAt(end) == '\n') {
102 String line = globalCode.substring(begin, end);
109 // Generate code from the DefineVar and __COND_SAT__
112 CodeAddition addition = new CodeAddition(lineNum, newCode);
113 if (!codeAdditions.containsKey(inst.file)) {
114 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
116 codeAdditions.get(inst.file).add(addition);
119 private void interface2Code(SpecConstruct inst) {
120 int lineNum = inst.endLineNum + 1;
121 GlobalConstruct construct = (GlobalConstruct) inst.construct;
122 ArrayList<String> newCode = new ArrayList<String>();
125 CodeAddition addition = new CodeAddition(lineNum, newCode);
126 if (!codeAdditions.containsKey(inst.file)) {
127 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
129 codeAdditions.get(inst.file).add(addition);
132 private void potentialCP2Code(SpecConstruct inst) {
133 int lineNum = inst.endLineNum + 1;
134 GlobalConstruct construct = (GlobalConstruct) inst.construct;
135 ArrayList<String> newCode = new ArrayList<String>();
138 CodeAddition addition = new CodeAddition(lineNum, newCode);
139 if (!codeAdditions.containsKey(inst.file)) {
140 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
142 codeAdditions.get(inst.file).add(addition);
145 private void CPDefine2Code(SpecConstruct inst) {
146 int lineNum = inst.endLineNum + 1;
147 GlobalConstruct construct = (GlobalConstruct) inst.construct;
148 ArrayList<String> newCode = new ArrayList<String>();
151 CodeAddition addition = new CodeAddition(lineNum, newCode);
152 if (!codeAdditions.containsKey(inst.file)) {
153 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
155 codeAdditions.get(inst.file).add(addition);
158 private void CPDefineCheck2Code(SpecConstruct inst) {
159 int lineNum = inst.endLineNum + 1;
160 GlobalConstruct construct = (GlobalConstruct) inst.construct;
161 ArrayList<String> newCode = new ArrayList<String>();
164 CodeAddition addition = new CodeAddition(lineNum, newCode);
165 if (!codeAdditions.containsKey(inst.file)) {
166 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
168 codeAdditions.get(inst.file).add(addition);
171 public void generateCode() {
172 for (int i = 0; i < _semantics.constructs.size(); i++) {
173 SpecConstruct inst = _semantics.constructs.get(i);
174 Construct construct = inst.construct;
175 if (construct instanceof GlobalConstruct) {
176 globalConstruct2Code(inst);
177 } else if (construct instanceof InterfaceConstruct) {
178 interface2Code(inst);
179 } else if (construct instanceof PotentialCPDefineConstruct) {
180 potentialCP2Code(inst);
181 } else if (construct instanceof CPDefineConstruct) {
183 } else if (construct instanceof CPDefineCheckConstruct) {
184 CPDefineCheck2Code(inst);
189 public static void main(String[] argvs) {
190 String homeDir = Environment.HOME_DIRECTORY;
192 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
194 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
195 // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
197 CodeGenerator gen = new CodeGenerator(srcFiles);