add more on code generation
[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
11 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
12 import edu.uci.eecs.specCompiler.specExtraction.SpecConstruct;
13 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
14 import edu.uci.eecs.specCompiler.specExtraction.SpecNotMatchException;
15
16 /**
17  * <p>
18  * This class will generate the annotated C code that can run on the current
19  * model checker.
20  * </p>
21  * 
22  * @author peizhaoo
23  * 
24  */
25 public class CodeGenerator {
26         private SemanticsChecker _semantics;
27         private SpecExtractor _extractor;
28
29         private File[] srcFiles;
30
31         private HashMap<File, ArrayList<String>> contents;
32
33         private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
34
35         public CodeGenerator(File[] srcFiles) {
36                 this.srcFiles = srcFiles;
37                 this.contents = new HashMap<File, ArrayList<String>>();
38                 readSrcFiles();
39                 this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
40
41                 _extractor = new SpecExtractor();
42
43                 try {
44                         _extractor.extract(srcFiles);
45                 } catch (SpecNotMatchException e1) {
46                         e1.printStackTrace();
47                 }
48
49                 _semantics = new SemanticsChecker(_extractor.getConstructs());
50                 try {
51                         _semantics.check();
52                         System.out.println(_semantics);
53                 } catch (SemanticsCheckerException e) {
54                         e.printStackTrace();
55                 }
56         }
57
58         private ArrayList<String> readSrcFile(File f) throws IOException {
59                 BufferedReader bf = new BufferedReader(new FileReader(f));
60                 ArrayList<String> content = new ArrayList<String>();
61                 String curLine;
62                 while ((curLine = bf.readLine()) != null) {
63                         content.add(curLine);
64                 }
65                 return content;
66         }
67
68         private void readSrcFiles() {
69                 for (int i = 0; i < srcFiles.length; i++) {
70                         File f = srcFiles[i];
71                         if (!contents.containsKey(f)) {
72                                 try {
73                                         contents.put(f, readSrcFile(f));
74                                 } catch (IOException e) {
75                                         e.printStackTrace();
76                                 }
77                         }
78                 }
79         }
80
81         /**
82          * <p>
83          * Generate all the global code, including the "@DefineVar" in each
84          * "@Interface" define
85          * </p>
86          */
87         private void globalConstruct2Code(SpecConstruct inst) {
88                 int lineNum = inst.endLineNum + 1;
89                 GlobalConstruct construct = (GlobalConstruct) inst.construct; 
90                 ArrayList<String> newCode = new ArrayList<String>();
91                 
92                 // Generate the code in global construct first
93                 String globalCode = construct.code;
94                 int begin = 0, end = 0;
95                 while (end < globalCode.length()) {
96                         if (globalCode.charAt(end) == '\n') {
97                                 String line = globalCode.substring(begin, end);
98                                 newCode.add(line);
99                                 begin = end + 1;
100                         }
101                         end++;
102                 }
103                 
104                 CodeAddition addition = new CodeAddition(lineNum, newCode);
105                 if (!codeAdditions.containsKey(inst.file)) {
106                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
107                 }
108                 codeAdditions.get(inst.file).add(addition);
109         }
110
111         private void interface2Code(SpecConstruct inst) {
112                 int lineNum = inst.endLineNum + 1;
113                 GlobalConstruct construct = (GlobalConstruct) inst.construct; 
114                 ArrayList<String> newCode = new ArrayList<String>();
115                 
116                 
117                 CodeAddition addition = new CodeAddition(lineNum, newCode);
118                 if (!codeAdditions.containsKey(inst.file)) {
119                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
120                 }
121                 codeAdditions.get(inst.file).add(addition);
122         }
123
124         private void potentialCP2Code(SpecConstruct inst) {
125                 int lineNum = inst.endLineNum + 1;
126                 GlobalConstruct construct = (GlobalConstruct) inst.construct; 
127                 ArrayList<String> newCode = new ArrayList<String>();
128                 
129                 
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         private void CPDefine2Code(SpecConstruct inst) {
138                 int lineNum = inst.endLineNum + 1;
139                 GlobalConstruct construct = (GlobalConstruct) inst.construct; 
140                 ArrayList<String> newCode = new ArrayList<String>();
141                 
142                 
143                 CodeAddition addition = new CodeAddition(lineNum, newCode);
144                 if (!codeAdditions.containsKey(inst.file)) {
145                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
146                 }
147                 codeAdditions.get(inst.file).add(addition);
148         }
149
150         private void CPDefineCheck2Code(SpecConstruct inst) {
151                 int lineNum = inst.endLineNum + 1;
152                 GlobalConstruct construct = (GlobalConstruct) inst.construct; 
153                 ArrayList<String> newCode = new ArrayList<String>();
154                 
155                 
156                 CodeAddition addition = new CodeAddition(lineNum, newCode);
157                 if (!codeAdditions.containsKey(inst.file)) {
158                         codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
159                 }
160                 codeAdditions.get(inst.file).add(addition);
161         }
162
163         public void generateCode() {
164
165         }
166
167         public static void main(String[] argvs) {
168                 String homeDir = Environment.HOME_DIRECTORY;
169                 File[] srcFiles = {
170                 // new File(homeDir + "/benchmark/linuxrwlocks/linuxrwlocks.c"),
171                 new File(homeDir
172                                 + "/benchmark/cliffc-hashtable/simplified_cliffc_hashtable.h"),
173                 // new File(homeDir + "/benchmark/ms-queue/my_queue.c")
174                 };
175                 CodeGenerator gen = new CodeGenerator(srcFiles);
176                 gen.generateCode();
177         }
178 }