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