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=false;
23 public static void main(String[] args) {
25 boolean success = true;
28 printArgInfo(cli); // prints debugging information and warning
31 State.currentState = state;
32 State.debug = cli.debug;
33 State.verbose = cli.verbose;
34 State.infile = cli.infile;
35 State.outfile = cli.outfile;
38 * added: terminates with an error message if no input file
39 * specified at command line
42 System.out.println("\nMCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)");
44 if (cli.infile == null) {
45 System.err.println("\nError: no input file specified");
49 if (cli.target == CLI.ASSEMBLY || cli.target == CLI.DEFAULT) {
51 System.out.println("Compiling " + cli.infile + ".");
54 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
55 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
56 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
59 Termination termination=null;
60 /* Check partition constraints */
61 (new ImplicitSchema(state)).update();
62 termination=new Termination(state);
65 (new DependencyBuilder(state)).calculate();
68 Vector nodes = new Vector(state.constraintnodes.values());
69 nodes.addAll(state.rulenodes.values());
71 FileOutputStream dotfile;
72 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
73 GraphNode.useEdgeLabels = true;
74 GraphNode.DOTVisitor.visit(dotfile, nodes);
77 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
78 GraphNode.useEdgeLabels = false;
79 GraphNode.DOTVisitor.visit(dotfile, nodes);
81 } catch (Exception e) {
87 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
90 // do model optimizations
91 //(new Optimizer(state)).optimize();
95 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
96 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
97 RepairGenerator wg = new RepairGenerator(state,termination);
98 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
102 WorklistGenerator ng = new WorklistGenerator(state);
103 SetInclusion.worklist=true;
104 RelationInclusion.worklist=true;
108 } catch (Exception e) {
114 System.out.println("Compilation of " + state.infile + " successful.");
115 System.out.println("#SUCCESS#");
117 } else if (cli.target == CLI.INTER) {
119 System.out.println("Semantic analysis for " + cli.infile + ".");
122 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
123 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
124 success = semantics(state) || error(state, "Semantic analysis failed.");
127 System.out.println("Semantic analysis of " + state.infile + " successful.");
128 System.out.println("#SUCCESS#");
130 } else if (cli.target == CLI.PARSE) {
132 System.out.println("Parsing " + cli.infile + ".");
135 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
136 success = parse(state) || error(state, "Parsing failed.");
139 System.out.println("Parsing of " + state.infile + " successful.");
140 System.out.println("#SUCCESS#");
142 } else if (cli.target == CLI.SCAN) {
144 System.out.println("Scanning " + cli.infile + ".");
147 success = scan(state) || error(state, "Scanning failed.");
150 System.out.println("Scanning of " + state.infile + " successful.");
151 System.out.println("#SUCCESS#");
156 private static void printArgInfo(CLI cli) {
158 System.out.println("Printing debugging information...");
159 System.out.println("Input filename: " + cli.infile);
160 System.out.println("Output filename: " + cli.outfile);
161 System.out.print("Target: ");
165 System.out.println("ASSEMBLY");
168 System.out.println("DEFAULT");
171 System.out.println("INTER");
174 System.out.println("LOWIR");
177 System.out.println("PARSE");
180 System.out.println("SCAN");
183 System.out.println("not recognized");
187 for (int i = 0; i < cli.opts.length; i++) {
189 System.out.println("Optimization");
194 for (int i = 0; i < cli.extraopts.size(); i++) {
195 System.err.println("Warning: optimization \"" +
196 cli.extraopts.elementAt(i) +
197 "\" not recognized");
200 for (int i = 0; i < cli.extras.size(); i++) {
201 System.err.println("Warning: option \"" +
202 cli.extras.elementAt(i) +
203 "\" not recognized");
207 private static boolean error(State state, String error) {
208 System.err.println(error);
210 System.out.println("#ERROR#");
216 public static boolean semantics(State state) {
217 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
218 SemanticChecker checker = new SemanticChecker();
222 ok = checker.check(state, er);
223 } catch (Exception e) {
224 er.report(null, e.toString());
230 er.report(null, "Semantic check failed.");
233 System.out.print(er.toString());
238 public static void debugMessage(int level, String s) {
239 if (State.currentState.verbose >= level) {
240 System.err.println(s);
244 public static boolean parse(State state) {
246 /* parse structure file */
248 debugMessage(1, "Parsing structure file");
250 FileInputStream infile = new FileInputStream(state.infile + ".struct");
251 TDLParser parser = new TDLParser(new Lexer(infile));
252 CUP$TDLParser$actions.debug = state.verbose > 1 ;
253 state.ptStructures = (ParseNode) parser.parse().value;
254 } catch (FileNotFoundException fnfe) {
255 System.err.println("Unable to open file: " + state.infile + ".struct");
257 } catch (Exception e) {
258 // System.out.println(e);
259 // e.printStackTrace();
263 /* parse model file */
265 debugMessage(1, "Parsing model file");
267 FileInputStream infile = new FileInputStream(state.infile + ".model");
268 MDLParser parser = new MDLParser(new Lexer(infile));
269 parser.filename = state.infile + ".model";
270 CUP$MDLParser$actions.debug = state.verbose > 1 ;
271 state.ptModel = (ParseNode) parser.parse().value;
272 } catch (FileNotFoundException fnfe) {
273 System.err.println("Unable to open file: " + state.infile + ".model");
275 } catch (Exception e) {
276 // System.out.println(e);
277 // e.printStackTrace();
281 /* parse space file */
283 debugMessage(1, "Parsing space file");
285 FileInputStream infile = new FileInputStream(state.infile + ".space");
286 SDLParser parser = new SDLParser(new Lexer(infile));
287 CUP$SDLParser$actions.debug = state.verbose > 1 ;
288 state.ptSpace = (ParseNode) parser.parse().value;
289 } catch (FileNotFoundException fnfe) {
290 System.err.println("Unable to open file: " + state.infile + ".space");
292 } catch (Exception e) {
293 System.out.println(e);
298 /* parse constraints file */
300 debugMessage(1, "Parsing constraints file");
302 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
303 CDLParser parser = new CDLParser(new Lexer(infile));
304 CUP$CDLParser$actions.debug = state.verbose > 1 ;
305 state.ptConstraints = (ParseNode) parser.parse().value;
306 } catch (FileNotFoundException fnfe) {
307 System.err.println("Unable to open file: " + state.infile + ".constraints");
309 } catch (Exception e) {
310 // System.out.println(e);
311 // e.printStackTrace();
316 !CUP$TDLParser$actions.errors &&
317 !CUP$SDLParser$actions.errors &&
318 !CUP$CDLParser$actions.errors &&
319 !CUP$MDLParser$actions.errors;
322 // if verbosity is on, then output parse trees as .dot files
323 if (success && state.verbose > 0) {
325 FileOutputStream dotfile;
327 dotfile = new FileOutputStream(state.infile + ".struct.dot");
328 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
331 dotfile = new FileOutputStream(state.infile + ".model.dot");
332 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
335 dotfile = new FileOutputStream(state.infile + ".space.dot");
336 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
339 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
340 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
342 } catch (Exception e) {
352 public static boolean scan(State state) {
353 FileInputStream infile = null;
355 boolean errors = false;
356 String files[] = { new String(state.infile + ".struct"),
357 new String(state.infile + ".model"),
358 new String(state.infile + ".constraints"),
359 new String(state.infile + ".space") };
363 for (int i = 0; i < files.length; i++) {
365 String filename = files[i];
368 infile = new FileInputStream(filename);
369 } catch (FileNotFoundException fnfe) {
370 System.err.println("Unable to open file: " + filename);
374 lexer = new Lexer(infile);
379 java_cup.runtime.Symbol symbol;
381 symbol = lexer.next_token();
383 if (symbol.sym == Sym.EOF) {
385 } else if (symbol.sym == Sym.BAD) {
389 if (State.verbose > 2) {
390 System.out.println("Got token: " + symbol.value);
393 } catch (Exception e) {