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