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