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