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