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