8 * The main compiler module, which does the following:
15 * @author <b>Daniel Roy</b> droy (at) mit (dot) edu
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 public static boolean REJECTLENGTH=false;
33 public static boolean PRINTREPAIRS=false;
34 public static boolean EXACTALLOCATION=false;
36 public static Vector debuggraphs=new Vector();
38 public static void main(String[] args) {
40 boolean success = true;
43 printArgInfo(cli); // prints debugging information and warning
46 State.currentState = state;
47 State.debug = cli.debug;
48 State.verbose = cli.verbose;
49 State.infile = cli.infile;
50 State.outfile = cli.outfile;
53 * added: terminates with an error message if no input file
54 * specified at command line
57 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
59 if (cli.infile == null) {
60 System.err.println("\nError: no input file specified");
65 System.out.println("Compiling " + cli.infile + ".");
68 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
69 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
70 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
73 state.setanalysis=new SetAnalysis(state);
74 Termination termination=null;
75 /* Check partition constraints */
76 (new ImplicitSchema(state)).update();
77 termination=new Termination(state);
80 (new DependencyBuilder(state)).calculate();
83 Vector nodes = new Vector(state.constraintnodes.values());
84 nodes.addAll(state.rulenodes.values());
86 FileOutputStream dotfile;
88 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
89 GraphNode.DOTVisitor.visit(dotfile, nodes);
91 } catch (Exception e) {
97 FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
100 // do model optimizations
101 //(new Optimizer(state)).optimize();
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");
110 WorklistGenerator ng = new WorklistGenerator(state);
111 SetInclusion.worklist=true;
112 RelationInclusion.worklist=true;
116 } catch (Exception e) {
122 System.out.println("Compilation of " + state.infile + " successful.");
123 System.out.println("#SUCCESS#");
127 private static void printArgInfo(CLI cli) {
129 System.out.println("Printing debugging information...");
130 System.out.println("Input filename: " + cli.infile);
131 System.out.println("Output filename: " + cli.outfile);
133 for (int i = 0; i < cli.opts.length; i++) {
135 System.out.println("Optimization");
140 for (int i = 0; i < cli.extraopts.size(); i++) {
141 System.err.println("Warning: optimization \"" +
142 cli.extraopts.elementAt(i) +
143 "\" not recognized");
146 for (int i = 0; i < cli.extras.size(); i++) {
147 System.err.println("Warning: option \"" +
148 cli.extras.elementAt(i) +
149 "\" not recognized");
153 private static boolean error(State state, String error) {
154 System.err.println(error);
156 System.out.println("#ERROR#");
162 public static boolean semantics(State state) {
163 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
164 SemanticChecker checker = new SemanticChecker();
168 ok = checker.check(state, er);
169 } catch (Exception e) {
170 er.report(null, e.toString());
176 er.report(null, "Semantic check failed.");
179 System.out.print(er.toString());
184 public static void debugMessage(int level, String s) {
185 if (State.currentState.verbose >= level) {
186 System.err.println(s);
190 public static boolean parse(State state) {
192 /* parse structure file */
194 debugMessage(1, "Parsing structure file");
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");
204 } catch (Exception e) {
205 System.out.println(e);
210 /* parse model file */
212 debugMessage(1, "Parsing model file");
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");
222 } catch (Exception e) {
223 System.out.println(e);
228 /* parse space file */
230 debugMessage(1, "Parsing space file");
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");
240 } catch (Exception e) {
241 System.out.println(e);
246 /* parse constraints file */
248 debugMessage(1, "Parsing constraints file");
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");
258 } catch (Exception e) {
259 System.out.println(e);
265 !CUP$TDLParser$actions.errors &&
266 !CUP$SDLParser$actions.errors &&
267 !CUP$CDLParser$actions.errors &&
268 !CUP$MDLParser$actions.errors;
271 // if verbosity is on, then output parse trees as .dot files
272 if (success && state.verbose > 0) {
274 FileOutputStream dotfile;
276 dotfile = new FileOutputStream(state.infile + ".struct.dot");
277 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
280 dotfile = new FileOutputStream(state.infile + ".model.dot");
281 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
284 dotfile = new FileOutputStream(state.infile + ".space.dot");
285 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
288 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
289 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
291 } catch (Exception e) {
301 public static boolean scan(State state) {
302 FileInputStream infile = null;
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") };
312 for (int i = 0; i < files.length; i++) {
314 String filename = files[i];
317 infile = new FileInputStream(filename);
318 } catch (FileNotFoundException fnfe) {
319 System.err.println("Unable to open file: " + filename);
323 lexer = new Lexer(infile);
328 java_cup.runtime.Symbol symbol;
330 symbol = lexer.next_token();
332 if (symbol.sym == Sym.EOF) {
334 } else if (symbol.sym == Sym.BAD) {
338 if (State.verbose > 2) {
339 System.out.println("Got token: " + symbol.value);
342 } catch (Exception e) {