3569e2011abe88c7ef908d8d4eebf6f2eb13ecbf
[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.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.SpecConstruct;
22 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
23 import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
24
25 /**
26  * <p>
27  * This class will generate the annotated C code that can run on the current
28  * model checker.
29  * </p>
30  * 
31  * @author peizhaoo
32  * 
33  */
34 public class CodeGenerator {
35         private SemanticsChecker _semantics;
36         private SpecExtractor _extractor;
37
38         private File[] srcFiles;
39
40         private HashMap<File, ArrayList<String>> contents;
41
42         private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
43
44         private ArrayList<String> globalContent;
45
46         public CodeGenerator(File[] srcFiles) {
47                 this.srcFiles = srcFiles;
48                 this.contents = new HashMap<File, ArrayList<String>>();
49                 this.globalContent = null;
50                 readSrcFiles();
51                 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
52
53                 _extractor = new SpecExtractor();
54
55                 try {
56                         _extractor.extract(srcFiles);
57                 } catch (SpecNotMatchException e1) {
58                         e1.printStackTrace();
59                 }
60
61                 _semantics = new SemanticsChecker(_extractor.getConstructs());
62                 try {
63                         _semantics.check();
64                         System.out.println(_semantics);
65                 } catch (SemanticsCheckerException e) {
66                         e.printStackTrace();
67                 }
68         }
69
70         private ArrayList<String> readSrcFile(File f) throws IOException {
71                 BufferedReader bf = new BufferedReader(new FileReader(f));
72                 ArrayList<String> content = new ArrayList<String>();
73                 String curLine;
74                 while ((curLine = bf.readLine()) != null) {
75                         content.add(curLine);
76                 }
77                 return content;
78         }
79
80         private void readSrcFiles() {
81                 for (int i = 0; i < srcFiles.length; i++) {
82                         File f = srcFiles[i];
83                         if (!contents.containsKey(f)) {
84                                 try {
85                                         contents.put(f, readSrcFile(f));
86                                 } catch (IOException e) {
87                                         e.printStackTrace();
88                                 }
89                         }
90                 }
91         }
92
93         /**
94          * <p>
95          * Generate all the global code, including the "@DefineVar" in each
96          * "@Interface" define
97          * </p>
98          */
99         private void globalConstruct2Code(SpecConstruct inst) {
100                 GlobalConstruct construct = (GlobalConstruct) inst.construct;
101                 ArrayList<String> newCode = CodeVariables.generateGlobalVarDeclaration(
102                                 _semantics, construct);
103                 // Record the global content array to generate the new file
104                 globalContent = newCode;
105         }
106
107         // Mainly rename and wrap the interface
108         private void interface2Code(SpecConstruct inst)
109                         throws InterfaceWrongFormatException {
110                 int lineNum = inst.endLineNum + 1;
111                 InterfaceConstruct construct = (InterfaceConstruct) inst.construct;
112                 
113                 // Rename the interface name
114                 File file = inst.file;
115                 String funcDecl = inst.interfaceDeclBody;
116                 // Rename the function declaration
117                 String funcName = renameInterface(inst);
118                 // Also rename the function definition if it's separated from the
119                 // declaration
120                 SpecConstruct definition = _semantics.interfaceName2DefineConstruct
121                                 .get(construct.name);
122                 if (definition != null) {
123                         String funcDefintionName = renameInterface(definition);
124                         assert (funcDefintionName.equals(funcName));
125                 }
126
127                 // Generate new wrapper
128                 ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(_semantics, inst);
129                 // Add it to the codeAdditions
130                 CodeAddition addition = new CodeAddition(lineNum, newCode);
131                 if (!codeAdditions.containsKey(inst.file)) {
132                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
133                 }
134                 codeAdditions.get(inst.file).add(addition);
135         }
136
137         // Returns the function name that has been renamed and replace the old line
138         private String renameInterface(SpecConstruct inst)
139                         throws InterfaceWrongFormatException {
140                 String funcDecl = inst.interfaceDeclBody;
141                 ArrayList<String> content = contents.get(inst.file);
142
143                 // Depending on "(" to find the function name, so it doesn't matter if
144                 // there's any template
145                 int beginIdx = funcDecl.indexOf('(');
146                 if (beginIdx == -1) {
147                         throw new InterfaceWrongFormatException(funcDecl
148                                         + "\n has wrong format!");
149                 }
150                 IDExtractor idExtractor = new IDExtractor(funcDecl, beginIdx);
151                 String funcName = idExtractor.getPrevID();
152                 int idBeginIdx = idExtractor.getIDBeginIdx(),
153                                 idEndIdx = idExtractor.getIDEndIdx(),
154                                 idLineBeginIdx = idExtractor.lineBeginIdxOfID(),
155                                 idLineEndIdx = idExtractor.lineEndIdxOfID();
156                 String newLine = funcDecl.substring(idLineBeginIdx, idBeginIdx)
157                                 + CodeVariables.SPEC_INTERFACE_WRAPPER + funcName
158                                 + funcDecl.substring(idEndIdx + 1, idLineEndIdx + 1);
159                 
160                 int lineNumOfID = idExtractor.lineNumOfID();
161                 // Be careful: lineNum - 1 -> index of content array
162                 content.set(inst.endLineNum + lineNumOfID, newLine);
163                 return funcName;
164         }
165
166         private void potentialCP2Code(SpecConstruct inst) {
167                 int lineNum = inst.endLineNum + 1;
168                 GlobalConstruct construct = (GlobalConstruct) inst.construct;
169                 ArrayList<String> newCode = new ArrayList<String>();
170
171                 CodeAddition addition = new CodeAddition(lineNum, newCode);
172                 if (!codeAdditions.containsKey(inst.file)) {
173                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
174                 }
175                 codeAdditions.get(inst.file).add(addition);
176         }
177
178         private void CPDefine2Code(SpecConstruct inst) {
179                 int lineNum = inst.endLineNum + 1;
180                 GlobalConstruct construct = (GlobalConstruct) inst.construct;
181                 ArrayList<String> newCode = new ArrayList<String>();
182
183                 CodeAddition addition = new CodeAddition(lineNum, newCode);
184                 if (!codeAdditions.containsKey(inst.file)) {
185                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
186                 }
187                 codeAdditions.get(inst.file).add(addition);
188         }
189
190         private void CPDefineCheck2Code(SpecConstruct inst) {
191                 int lineNum = inst.endLineNum + 1;
192                 GlobalConstruct construct = (GlobalConstruct) inst.construct;
193                 ArrayList<String> newCode = new ArrayList<String>();
194
195                 CodeAddition addition = new CodeAddition(lineNum, newCode);
196                 if (!codeAdditions.containsKey(inst.file)) {
197                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
198                 }
199                 codeAdditions.get(inst.file).add(addition);
200         }
201
202         public void generateCode() {
203                 for (int i = 0; i < _semantics.constructs.size(); i++) {
204                         SpecConstruct inst = _semantics.constructs.get(i);
205                         Construct construct = inst.construct;
206                         if (construct instanceof GlobalConstruct) {
207                                 globalConstruct2Code(inst);
208                         } else if (construct instanceof InterfaceConstruct) {
209                                 try {
210                                         interface2Code(inst);
211                                 } catch (InterfaceWrongFormatException e) {
212                                         e.printStackTrace();
213                                 }
214                         } else if (construct instanceof PotentialCPDefineConstruct) {
215                                 // potentialCP2Code(inst);
216                         } else if (construct instanceof CPDefineConstruct) {
217                                 // CPDefine2Code(inst);
218                         } else if (construct instanceof CPDefineCheckConstruct) {
219                                 // CPDefineCheck2Code(inst);
220                         }
221                 }
222         }
223
224         public static void main(String[] argvs) {
225                 String homeDir = Environment.HOME_DIRECTORY;
226                 File[] srcFiles = {
227                 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
228                 new File(homeDir
229                                 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"), };
230                 // new File(homeDir + "/benchmark/ms-queue/my_queue.c"),
231                 // new File(homeDir + "/benchmark/ms-queue/my_queue.h") };
232                 CodeGenerator gen = new CodeGenerator(srcFiles);
233                 gen.generateCode();
234         }
235 }