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