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