6151f2d3ecf586449ed8fa21fe938d85dfcfa9bd
[repair.git] / Repair / RepairCompiler / MCC / Compiler.java
1 package MCC;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.IR.*;
6
7 /**
8  * The main compiler module, which does the following:
9  * <ul>
10  *  <li>
11  *   nothing.
12  *  </li>
13  * <ul>
14  *
15  * @author <b>Daniel Roy</b> droy (at) mit (dot) edu
16  * @version %I, %G
17  */
18
19 public class Compiler {
20     /* Set this flag to false to turn repairs off */
21     public static boolean REPAIR=true;
22     public static boolean AGGRESSIVESEARCH=false;
23     public static boolean PRUNEQUANTIFIERS=false;
24     public static boolean GENERATEDEBUGHOOKS=false;
25     public static boolean GENERATEDEBUGPRINT=false;
26     public static boolean GENERATEINSTRUMENT=false;
27     public static boolean ALLOCATECPLUSPLUS=false;
28     public static boolean OMITCOMP=false;
29     public static boolean MERGENODES=false;
30     public static boolean TIME=false;
31     public static boolean DEBUGGRAPH=false;
32     public static boolean REJECTLENGTH=false;
33     public static boolean PRINTREPAIRS=false;
34
35     public static Vector debuggraphs=new Vector();
36
37     public static void main(String[] args) {
38         State state = null;
39         boolean success = true;
40         CLI cli = new CLI();
41         cli.parse(args);
42         printArgInfo(cli); // prints debugging information and warning
43
44         state = new State();
45         State.currentState = state;
46         State.debug = cli.debug;
47         State.verbose = cli.verbose;
48         State.infile = cli.infile;
49         State.outfile = cli.outfile;
50
51         /*
52          * added: terminates with an error message if no input file
53          * specified at command line
54          */
55
56         System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
57
58         if (cli.infile == null) {
59             System.err.println("\nError: no input file specified");
60             System.exit(-1);
61         }
62
63         if (state.debug) {
64             System.out.println("Compiling " + cli.infile + ".");
65         }
66
67         success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
68         success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
69         success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
70
71
72         state.setanalysis=new SetAnalysis(state);
73         Termination termination=null;
74         /* Check partition constraints */
75         (new ImplicitSchema(state)).update();
76         termination=new Termination(state);
77
78         state.printall();
79         (new DependencyBuilder(state)).calculate();
80
81         try {
82             Vector nodes = new Vector(state.constraintnodes.values());
83             nodes.addAll(state.rulenodes.values());
84
85             FileOutputStream dotfile;
86
87             dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
88             GraphNode.DOTVisitor.visit(dotfile, nodes);
89             dotfile.close();
90         } catch (Exception e) {
91             e.printStackTrace();
92             System.exit(-1);
93         }
94
95         try {
96             FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
97
98
99             // do model optimizations
100             //(new Optimizer(state)).optimize();
101
102             FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.c");
103             FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
104             RepairGenerator wg = new RepairGenerator(state,termination);
105             wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
106             gcode2.close();
107             gcode3.close();
108             /*          } else {
109                         WorklistGenerator ng = new WorklistGenerator(state);
110                     SetInclusion.worklist=true;
111                     RelationInclusion.worklist=true;
112                     ng.generate(gcode);
113                     }*/
114             gcode.close();
115         } catch (Exception e) {
116             e.printStackTrace();
117             System.exit(-1);
118         }
119
120         if (state.debug) {
121             System.out.println("Compilation of " + state.infile + " successful.");
122             System.out.println("#SUCCESS#");
123         }
124     }
125
126     private static void printArgInfo(CLI cli) {
127         if (cli.debug) {
128             System.out.println("Printing debugging information...");
129             System.out.println("Input filename: " + cli.infile);
130             System.out.println("Output filename: " + cli.outfile);
131
132             for (int i = 0; i < cli.opts.length; i++) {
133                 if (cli.opts[i]) {
134                     System.out.println("Optimization");
135                 }
136             }
137         }
138
139         for (int i = 0; i < cli.extraopts.size(); i++) {
140             System.err.println("Warning: optimization \"" +
141                                cli.extraopts.elementAt(i) +
142                                "\" not recognized");
143         }
144
145         for (int i = 0; i < cli.extras.size(); i++) {
146             System.err.println("Warning: option \"" +
147                                cli.extras.elementAt(i) +
148                                "\" not recognized");
149         }
150     }
151
152     private static boolean error(State state, String error) {
153         System.err.println(error);
154         if (state.debug) {
155             System.out.println("#ERROR#");
156         }
157         System.exit(-1);
158         return false;
159     }
160
161     public static boolean semantics(State state) {
162         SimpleIRErrorReporter er = new SimpleIRErrorReporter();
163         SemanticChecker checker = new SemanticChecker();
164         boolean ok = true;
165
166         try {
167             ok = checker.check(state, er);
168         } catch (Exception e) {
169             er.report(null, e.toString());
170             e.printStackTrace();
171             er.error = true;
172         }
173
174         if (!ok) {
175             er.report(null, "Semantic check failed.");
176         }
177
178         System.out.print(er.toString());
179
180         return !er.error;
181     }
182
183     public static void debugMessage(int level, String s) {
184         if (State.currentState.verbose >= level) {
185             System.err.println(s);
186         }
187     }
188
189     public static boolean parse(State state) {
190
191         /* parse structure file */
192         try {
193             debugMessage(1, "Parsing structure file");
194             LineCount.reset();
195             FileInputStream infile = new FileInputStream(state.infile + ".struct");
196             TDLParser parser = new TDLParser(new Lexer(infile));
197             parser.filename = state.infile + ".struct";
198             CUP$TDLParser$actions.debug = state.verbose > 1 ;
199             state.ptStructures = (ParseNode) parser.parse().value;
200         } catch (FileNotFoundException fnfe) {
201             System.err.println("Unable to open file: " + state.infile + ".struct");
202             System.exit(-1);
203         } catch (Exception e) {
204             System.out.println(e);
205             e.printStackTrace();
206             return false;
207         }
208
209         /* parse model file */
210         try {
211             debugMessage(1, "Parsing model file");
212             LineCount.reset();
213             FileInputStream infile = new FileInputStream(state.infile + ".model");
214             MDLParser parser = new MDLParser(new Lexer(infile));
215             parser.filename = state.infile + ".model";
216             CUP$MDLParser$actions.debug = state.verbose > 1 ;
217             state.ptModel = (ParseNode) parser.parse().value;
218         } catch (FileNotFoundException fnfe) {
219             System.err.println("Unable to open file: " + state.infile + ".model");
220             System.exit(-1);
221         } catch (Exception e) {
222             System.out.println(e);
223             e.printStackTrace();
224             return false;
225         }
226
227         /* parse space file */
228         try {
229             debugMessage(1, "Parsing space file");
230             LineCount.reset();
231             FileInputStream infile = new FileInputStream(state.infile + ".space");
232             SDLParser parser = new SDLParser(new Lexer(infile));
233             parser.filename = state.infile + ".space";
234             CUP$SDLParser$actions.debug = state.verbose > 1 ;
235             state.ptSpace = (ParseNode) parser.parse().value;
236         } catch (FileNotFoundException fnfe) {
237             System.err.println("Unable to open file: " + state.infile + ".space");
238             System.exit(-1);
239         } catch (Exception e) {
240             System.out.println(e);
241             e.printStackTrace();
242             return false;
243         }
244
245         /* parse constraints file */
246         try {
247             debugMessage(1, "Parsing constraints file");
248             LineCount.reset();
249             FileInputStream infile = new FileInputStream(state.infile + ".constraints");
250             CDLParser parser = new CDLParser(new Lexer(infile));
251             parser.filename = state.infile + ".constraints";
252             CUP$CDLParser$actions.debug = state.verbose > 1 ;
253             state.ptConstraints = (ParseNode) parser.parse().value;
254         } catch (FileNotFoundException fnfe) {
255             System.err.println("Unable to open file: " + state.infile + ".constraints");
256             System.exit(-1);
257         } catch (Exception e) {
258             System.out.println(e);
259             e.printStackTrace();
260             return false;
261         }
262
263         boolean success =
264             !CUP$TDLParser$actions.errors &&
265             !CUP$SDLParser$actions.errors &&
266             !CUP$CDLParser$actions.errors &&
267             !CUP$MDLParser$actions.errors;
268
269
270         // if verbosity is on, then output parse trees as .dot files
271         if (success && state.verbose > 0) {
272             try {
273                 FileOutputStream dotfile;
274
275                 dotfile = new FileOutputStream(state.infile + ".struct.dot");
276                 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
277                 dotfile.close();
278
279                 dotfile = new FileOutputStream(state.infile + ".model.dot");
280                 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
281                 dotfile.close();
282
283                 dotfile = new FileOutputStream(state.infile + ".space.dot");
284                 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
285                 dotfile.close();
286
287                 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
288                 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
289                 dotfile.close();
290             } catch (Exception e) {
291                 e.printStackTrace();
292                 return false;
293             }
294         }
295
296         return success;
297     }
298
299
300     public static boolean scan(State state) {
301         FileInputStream infile = null;
302         Lexer lexer;
303         boolean errors = false;
304         String files[] = { new String(state.infile + ".struct"),
305                            new String(state.infile + ".model"),
306                            new String(state.infile + ".constraints"),
307                            new String(state.infile + ".space") };
308
309
310
311         for (int i = 0; i < files.length; i++) {
312
313             String filename = files[i];
314
315             try {
316                 infile = new FileInputStream(filename);
317             } catch (FileNotFoundException fnfe) {
318                 System.err.println("Unable to open file: " + filename);
319                 System.exit(-1);
320             }
321
322             lexer = new Lexer(infile);
323
324
325             try {
326                 while (true) {
327                     java_cup.runtime.Symbol symbol;
328
329                     symbol = lexer.next_token();
330
331                     if (symbol.sym == Sym.EOF) {
332                         break;
333                     } else if (symbol.sym == Sym.BAD) {
334                         errors = true;
335                     }
336
337                     if (State.verbose > 2) {
338                         System.out.println("Got token: " + symbol.value);
339                     }
340                 }
341             } catch (Exception e) {
342                 return false;
343             }
344         }
345
346         return !errors;
347     }
348
349
350 }