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.Construct;
16 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
17 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
18 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
19 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
20 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
21 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
22 import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
26 * This class will generate the annotated C code that can run on the current
33 public class CodeGenerator {
34 private SemanticsChecker _semantics;
35 private SpecExtractor _extractor;
37 private File[] srcFiles;
39 private HashMap<File, ArrayList<String>> contents;
41 private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
43 public CodeGenerator(File[] srcFiles) {
44 this.srcFiles = srcFiles;
45 this.contents = new HashMap<File, ArrayList<String>>();
47 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
49 _extractor = new SpecExtractor();
52 _extractor.extract(srcFiles);
53 } catch (SpecNotMatchException e1) {
57 _semantics = new SemanticsChecker(_extractor.getConstructs());
60 System.out.println(_semantics);
61 } catch (SemanticsCheckerException e) {
66 private ArrayList<String> readSrcFile(File f) throws IOException {
67 BufferedReader bf = new BufferedReader(new FileReader(f));
68 ArrayList<String> content = new ArrayList<String>();
70 while ((curLine = bf.readLine()) != null) {
76 private void readSrcFiles() {
77 for (int i = 0; i < srcFiles.length; i++) {
79 if (!contents.containsKey(f)) {
81 contents.put(f, readSrcFile(f));
82 } catch (IOException e) {
91 * Generate all the global code, including the "@DefineVar" in each
95 private void globalConstruct2Code(SpecConstruct inst) {
96 int lineNum = inst.endLineNum + 1;
97 GlobalConstruct construct = (GlobalConstruct) inst.construct;
98 ArrayList<String> newCode = new ArrayList<String>();
100 // Generate the inner class definition
101 newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n");
102 newCode.add("public:\n");
104 // Generate the code in global construct first
105 SequentialDefineSubConstruct globalCode = construct.code;
106 breakCodeLines(newCode, globalCode.declareVar);
108 // Generate code from the DefineVar, __COND_SAT__ and __ID__
110 newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
111 + "> " + CodeVariables.SPEC_CONDITION + ";");
113 newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
114 + "> " + CodeVariables.SPEC_ID + ";");
117 for (String interfaceName : _semantics.interfaceName2Construct.keySet()) {
118 InterfaceConstruct iConstruct = (InterfaceConstruct) _semantics.interfaceName2Construct
119 .get(interfaceName).construct;
120 ArrayList<DefineVar> defineVars = iConstruct.action.defineVars;
121 for (int i = 0; i < defineVars.size(); i++) {
122 DefineVar var = defineVars.get(i);
123 newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + var.varType
124 + "> " + var.getNewVarName() + ";");
128 // Enum of all interface
129 String enumDefinition = "enum " + CodeVariables.SPEC_INTERFACE_ENUM
131 Iterator<String> iter = _semantics.interfaceName2Construct.keySet()
133 String interfaceName;
134 if (iter.hasNext()) {
135 interfaceName = iter.next();
136 enumDefinition = enumDefinition + "_" + interfaceName + "_";
138 while (iter.hasNext()) {
139 interfaceName = iter.next();
140 enumDefinition = enumDefinition + ", _" + interfaceName + "_";
142 enumDefinition = enumDefinition + "};";
143 newCode.add(enumDefinition);
146 newCode.add(CodeVariables.SPEC_HASHTABLE + "<enum "
147 + CodeVariables.SPEC_INTERFACE_ENUM + "> "
148 + CodeVariables.SPEC_INTERFACE + ";");
150 // Generate constructor (the place to initialize everything!)
152 newCode.add(CodeVariables.SPEC_CLASS + "() {");
154 breakCodeLines(newCode, globalCode.initVar);
156 newCode.add(CodeVariables.SPEC_CONDITION + " = "
157 + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
160 newCode.add(CodeVariables.SPEC_ID + " = "
161 + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
164 newCode.add(CodeVariables.SPEC_INTERFACE + " = "
165 + CodeVariables.SPEC_HASHTABLE + "<enum "
166 + CodeVariables.SPEC_INTERFACE_ENUM + ">();");
167 // FIXME: Pass the happens-before relationship check here
170 // Generate the sequential functions
171 breakCodeLines(newCode, globalCode.defineFunc);
173 // Generate the end of the inner class definition
177 CodeAddition addition = new CodeAddition(lineNum, newCode);
178 if (!codeAdditions.containsKey(inst.file)) {
179 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
181 codeAdditions.get(inst.file).add(addition);
184 // Break the code (String) into multiple lines and add it to newCode
185 private void breakCodeLines(ArrayList<String> newCode, String code) {
186 int begin = 0, end = 0;
187 while (end < code.length()) {
188 if (code.charAt(end) == '\n') {
189 String line = code.substring(begin, end);
197 private void printCode(ArrayList<String> code) {
198 for (int i = 0; i < code.size(); i++) {
199 System.out.println(code.get(i));
203 // Mainly rename and wrap the interface
204 private void interface2Code(SpecConstruct inst)
205 throws InterfaceWrongFormatException {
206 int lineNum = inst.endLineNum + 1;
207 InterfaceConstruct construct = (InterfaceConstruct) inst.construct;
208 ArrayList<String> newCode = new ArrayList<String>();
210 // Rename the interface name
211 File file = inst.file;
212 ArrayList<String> content = contents.get(file);
213 String funcDecl = inst.interfaceDeclBody;
214 String funcName = renameInterface(funcDecl, content, lineNum);
216 // Generate new wrapper
217 breakCodeLines(newCode, funcDecl);
222 // FIXME: Add Happens-before check here
225 CodeAddition addition = new CodeAddition(lineNum, newCode);
226 if (!codeAdditions.containsKey(inst.file)) {
227 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
229 codeAdditions.get(inst.file).add(addition);
232 // Returns the function name that has been renamed and replace the old line
233 private String renameInterface(String funcDecl, ArrayList<String> content,
234 int lineNum) throws InterfaceWrongFormatException {
235 int begin = 0, end = funcDecl.indexOf('(');
237 throw new InterfaceWrongFormatException(funcDecl
238 + "\n has wrong format!");
242 char ch = funcDecl.charAt(end);
243 if (ch == '\n' || ch == '\t' || ch == ' ')
248 char ch = funcDecl.charAt(begin);
249 if (ch == '_' || (ch >= 'a' && ch <= 'z')
250 || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
254 String funcName = funcDecl.substring(begin + 1, end + 1), newLine;
255 int lineBreakIdx = funcDecl.indexOf('\n');
256 int firstLineBreak = lineBreakIdx == -1 ? funcDecl.length() : lineBreakIdx;
257 newLine = funcDecl.substring(0, begin + 1)
258 + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName
259 + funcDecl.substring(end + 1, firstLineBreak);
260 content.set(lineNum, newLine);
264 private void potentialCP2Code(SpecConstruct inst) {
265 int lineNum = inst.endLineNum + 1;
266 GlobalConstruct construct = (GlobalConstruct) inst.construct;
267 ArrayList<String> newCode = new ArrayList<String>();
269 CodeAddition addition = new CodeAddition(lineNum, newCode);
270 if (!codeAdditions.containsKey(inst.file)) {
271 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
273 codeAdditions.get(inst.file).add(addition);
276 private void CPDefine2Code(SpecConstruct inst) {
277 int lineNum = inst.endLineNum + 1;
278 GlobalConstruct construct = (GlobalConstruct) inst.construct;
279 ArrayList<String> newCode = new ArrayList<String>();
281 CodeAddition addition = new CodeAddition(lineNum, newCode);
282 if (!codeAdditions.containsKey(inst.file)) {
283 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
285 codeAdditions.get(inst.file).add(addition);
288 private void CPDefineCheck2Code(SpecConstruct inst) {
289 int lineNum = inst.endLineNum + 1;
290 GlobalConstruct construct = (GlobalConstruct) inst.construct;
291 ArrayList<String> newCode = new ArrayList<String>();
293 CodeAddition addition = new CodeAddition(lineNum, newCode);
294 if (!codeAdditions.containsKey(inst.file)) {
295 codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
297 codeAdditions.get(inst.file).add(addition);
300 public void generateCode() {
301 for (int i = 0; i < _semantics.constructs.size(); i++) {
302 SpecConstruct inst = _semantics.constructs.get(i);
303 Construct construct = inst.construct;
304 if (construct instanceof GlobalConstruct) {
305 globalConstruct2Code(inst);
306 } else if (construct instanceof InterfaceConstruct) {
308 interface2Code(inst);
309 } catch (InterfaceWrongFormatException e) {
312 } else if (construct instanceof PotentialCPDefineConstruct) {
313 potentialCP2Code(inst);
314 } else if (construct instanceof CPDefineConstruct) {
316 } else if (construct instanceof CPDefineCheckConstruct) {
317 CPDefineCheck2Code(inst);
322 public static void main(String[] argvs) {
323 String homeDir = Environment.HOME_DIRECTORY;
325 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
327 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
328 // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
330 CodeGenerator gen = new CodeGenerator(srcFiles);