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