Change to the spec...missed a consistency property. Adding timing option.
[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 TIME=false;
29     
30     public static Vector debuggraphs=new Vector();
31
32     public static void main(String[] args) {
33         State state = null;
34         boolean success = true;
35         CLI cli = new CLI();
36         cli.parse(args);
37         printArgInfo(cli); // prints debugging information and warning
38
39         state = new State();
40         State.currentState = state;
41         State.debug = cli.debug;
42         State.verbose = cli.verbose;
43         State.infile = cli.infile;
44         State.outfile = cli.outfile;
45
46         /*
47          * added: terminates with an error message if no input file
48          * specified at command line
49          */
50
51         System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
52
53         if (cli.infile == null) {
54             System.err.println("\nError: no input file specified");
55             System.exit(-1);
56         }
57         
58         if (state.debug) {
59             System.out.println("Compiling " + cli.infile + ".");
60         }
61         
62         success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
63         success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
64         success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
65         
66         
67         state.setanalysis=new SetAnalysis(state);
68         Termination termination=null;
69         /* Check partition constraints */
70         (new ImplicitSchema(state)).update();
71         termination=new Termination(state);
72         
73         state.printall();
74         (new DependencyBuilder(state)).calculate();
75         
76         try {
77             Vector nodes = new Vector(state.constraintnodes.values());
78             nodes.addAll(state.rulenodes.values());
79             
80             FileOutputStream dotfile;
81             dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
82             GraphNode.useEdgeLabels = true;
83             GraphNode.DOTVisitor.visit(dotfile, nodes);
84             dotfile.close();
85             
86             dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
87             GraphNode.useEdgeLabels = false;
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 + ".cc");
97             
98             
99             // do model optimizations
100             //(new Optimizer(state)).optimize();
101             
102             FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
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 }
351