generating code
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeGenerator.java
1 package edu.uci.eecs.specCompiler.codeGenerator;
2
3 import java.io.BufferedReader;
4 import java.io.File;
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;
11
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;
23
24 /**
25  * <p>
26  * This class will generate the annotated C code that can run on the current
27  * model checker.
28  * </p>
29  * 
30  * @author peizhaoo
31  * 
32  */
33 public class CodeGenerator {
34         private SemanticsChecker _semantics;
35         private SpecExtractor _extractor;
36
37         private File[] srcFiles;
38
39         private HashMap<File, ArrayList<String>> contents;
40
41         private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
42
43         public CodeGenerator(File[] srcFiles) {
44                 this.srcFiles = srcFiles;
45                 this.contents = new HashMap<File, ArrayList<String>>();
46                 readSrcFiles();
47                 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
48
49                 _extractor = new SpecExtractor();
50
51                 try {
52                         _extractor.extract(srcFiles);
53                 } catch (SpecNotMatchException e1) {
54                         e1.printStackTrace();
55                 }
56
57                 _semantics = new SemanticsChecker(_extractor.getConstructs());
58                 try {
59                         _semantics.check();
60                         System.out.println(_semantics);
61                 } catch (SemanticsCheckerException e) {
62                         e.printStackTrace();
63                 }
64         }
65
66         private ArrayList<String> readSrcFile(File f) throws IOException {
67                 BufferedReader bf = new BufferedReader(new FileReader(f));
68                 ArrayList<String> content = new ArrayList<String>();
69                 String curLine;
70                 while ((curLine = bf.readLine()) != null) {
71                         content.add(curLine);
72                 }
73                 return content;
74         }
75
76         private void readSrcFiles() {
77                 for (int i = 0; i < srcFiles.length; i++) {
78                         File f = srcFiles[i];
79                         if (!contents.containsKey(f)) {
80                                 try {
81                                         contents.put(f, readSrcFile(f));
82                                 } catch (IOException e) {
83                                         e.printStackTrace();
84                                 }
85                         }
86                 }
87         }
88
89         /**
90          * <p>
91          * Generate all the global code, including the "@DefineVar" in each
92          * "@Interface" define
93          * </p>
94          */
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>();
99
100                 // Generate the inner class definition
101                 newCode.add("class " + CodeVariables.SPEC_CLASS + " {\n");
102                 newCode.add("public:\n");
103
104                 // Generate the code in global construct first
105                 SequentialDefineSubConstruct globalCode = construct.code;
106                 breakCodeLines(newCode, globalCode.declareVar);
107
108                 // Generate code from the DefineVar, __COND_SAT__ and __ID__
109                 // __COND_SAT__
110                 newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
111                                 + "> " + CodeVariables.SPEC_CONDITION + ";");
112                 // __ID__
113                 newCode.add(CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
114                                 + "> " + CodeVariables.SPEC_ID + ";");
115
116                 // DefineVars
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() + ";");
125                         }
126                 }
127
128                 // Enum of all interface
129                 String enumDefinition = "enum " + CodeVariables.SPEC_INTERFACE_ENUM
130                                 + " {";
131                 Iterator<String> iter = _semantics.interfaceName2Construct.keySet()
132                                 .iterator();
133                 String interfaceName;
134                 if (iter.hasNext()) {
135                         interfaceName = iter.next();
136                         enumDefinition = enumDefinition + "_" + interfaceName + "_";
137                 }
138                 while (iter.hasNext()) {
139                         interfaceName = iter.next();
140                         enumDefinition = enumDefinition + ", _" + interfaceName + "_";
141                 }
142                 enumDefinition = enumDefinition + "};";
143                 newCode.add(enumDefinition);
144
145                 // __interface
146                 newCode.add(CodeVariables.SPEC_HASHTABLE + "<enum "
147                                 + CodeVariables.SPEC_INTERFACE_ENUM + "> "
148                                 + CodeVariables.SPEC_INTERFACE + ";");
149
150                 // Generate constructor (the place to initialize everything!)
151                 newCode.add("\n");
152                 newCode.add(CodeVariables.SPEC_CLASS + "() {");
153
154                 breakCodeLines(newCode, globalCode.initVar);
155                 // __COND_SAT__
156                 newCode.add(CodeVariables.SPEC_CONDITION + " = "
157                                 + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.BOOLEAN
158                                 + ">();");
159                 // __ID__
160                 newCode.add(CodeVariables.SPEC_ID + " = "
161                                 + CodeVariables.SPEC_HASHTABLE + "<" + CodeVariables.SPEC_TAG
162                                 + ">();");
163                 // __interface
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
168                 newCode.add("}");
169
170                 // Generate the sequential functions
171                 breakCodeLines(newCode, globalCode.defineFunc);
172
173                 // Generate the end of the inner class definition
174                 newCode.add("};\n");
175                 printCode(newCode);
176
177                 CodeAddition addition = new CodeAddition(lineNum, newCode);
178                 if (!codeAdditions.containsKey(inst.file)) {
179                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
180                 }
181                 codeAdditions.get(inst.file).add(addition);
182         }
183
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);
190                                 newCode.add(line);
191                                 begin = end + 1;
192                         }
193                         end++;
194                 }
195         }
196
197         private void printCode(ArrayList<String> code) {
198                 for (int i = 0; i < code.size(); i++) {
199                         System.out.println(code.get(i));
200                 }
201         }
202
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>();
209
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);
215
216                 // Generate new wrapper
217                 breakCodeLines(newCode, funcDecl);
218                 newCode.add("{");
219                 
220                 // Generate 
221                 
222                 // FIXME: Add Happens-before check here
223                 
224                 newCode.add("}");
225                 CodeAddition addition = new CodeAddition(lineNum, newCode);
226                 if (!codeAdditions.containsKey(inst.file)) {
227                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
228                 }
229                 codeAdditions.get(inst.file).add(addition);
230         }
231
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('(');
236                 if (end == -1) {
237                         throw new InterfaceWrongFormatException(funcDecl
238                                         + "\n has wrong format!");
239                 }
240                 end--;
241                 while (end > 0) {
242                         char ch = funcDecl.charAt(end);
243                         if (ch == '\n' || ch == '\t' || ch == ' ')
244                                 continue;
245                 }
246                 begin = end;
247                 while (begin > 0) {
248                         char ch = funcDecl.charAt(begin);
249                         if (ch == '_' || (ch >= 'a' && ch <= 'z')
250                                         || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
251                                 continue;
252                         }
253                 }
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);
261                 return funcName;
262         }
263
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>();
268
269                 CodeAddition addition = new CodeAddition(lineNum, newCode);
270                 if (!codeAdditions.containsKey(inst.file)) {
271                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
272                 }
273                 codeAdditions.get(inst.file).add(addition);
274         }
275
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>();
280
281                 CodeAddition addition = new CodeAddition(lineNum, newCode);
282                 if (!codeAdditions.containsKey(inst.file)) {
283                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
284                 }
285                 codeAdditions.get(inst.file).add(addition);
286         }
287
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>();
292
293                 CodeAddition addition = new CodeAddition(lineNum, newCode);
294                 if (!codeAdditions.containsKey(inst.file)) {
295                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
296                 }
297                 codeAdditions.get(inst.file).add(addition);
298         }
299
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) {
307                                 try {
308                                         interface2Code(inst);
309                                 } catch (InterfaceWrongFormatException e) {
310                                         e.printStackTrace();
311                                 }
312                         } else if (construct instanceof PotentialCPDefineConstruct) {
313                                 potentialCP2Code(inst);
314                         } else if (construct instanceof CPDefineConstruct) {
315                                 CPDefine2Code(inst);
316                         } else if (construct instanceof CPDefineCheckConstruct) {
317                                 CPDefineCheck2Code(inst);
318                         }
319                 }
320         }
321
322         public static void main(String[] argvs) {
323                 String homeDir = Environment.HOME_DIRECTORY;
324                 File[] srcFiles = {
325                 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
326                 new File(homeDir
327                                 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
328                 // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
329                 };
330                 CodeGenerator gen = new CodeGenerator(srcFiles);
331                 gen.generateCode();
332         }
333 }