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 public static boolean REPAIR=true;
22 public static void main(String[] args) {
24 boolean success = true;
27 printArgInfo(cli); // prints debugging information and warning
30 State.currentState = state;
31 State.debug = cli.debug;
32 State.verbose = cli.verbose;
33 State.infile = cli.infile;
34 State.outfile = cli.outfile;
37 * added: terminates with an error message if no input file
38 * specified at command line
41 System.out.println("\nMCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)");
43 if (cli.infile == null) {
44 System.err.println("\nError: no input file specified");
48 if (cli.target == CLI.ASSEMBLY || cli.target == CLI.DEFAULT) {
50 System.out.println("Compiling " + cli.infile + ".");
53 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
54 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
55 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
58 (new ImplicitSchema(state)).update();
61 (new DependencyBuilder(state)).calculate();
64 Vector nodes = new Vector(state.constraintnodes.values());
65 nodes.addAll(state.rulenodes.values());
67 FileOutputStream dotfile;
68 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
69 GraphNode.useEdgeLabels = true;
70 GraphNode.DOTVisitor.visit(dotfile, nodes);
73 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
74 GraphNode.useEdgeLabels = false;
75 GraphNode.DOTVisitor.visit(dotfile, nodes);
77 } catch (Exception e) {
83 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
85 // do model optimizations
86 //(new Optimizer(state)).optimize();
88 NaiveGenerator ng = new NaiveGenerator(state);
90 //WorklistGenerator wg = new WorklistGenerator(state);
93 } catch (Exception e) {
99 System.out.println("Compilation of " + state.infile + " successful.");
100 System.out.println("#SUCCESS#");
102 } else if (cli.target == CLI.INTER) {
104 System.out.println("Semantic analysis for " + cli.infile + ".");
107 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
108 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
109 success = semantics(state) || error(state, "Semantic analysis failed.");
112 System.out.println("Semantic analysis of " + state.infile + " successful.");
113 System.out.println("#SUCCESS#");
115 } else if (cli.target == CLI.PARSE) {
117 System.out.println("Parsing " + cli.infile + ".");
120 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
121 success = parse(state) || error(state, "Parsing failed.");
124 System.out.println("Parsing of " + state.infile + " successful.");
125 System.out.println("#SUCCESS#");
127 } else if (cli.target == CLI.SCAN) {
129 System.out.println("Scanning " + cli.infile + ".");
132 success = scan(state) || error(state, "Scanning failed.");
135 System.out.println("Scanning of " + state.infile + " successful.");
136 System.out.println("#SUCCESS#");
141 private static void printArgInfo(CLI cli) {
143 System.out.println("Printing debugging information...");
144 System.out.println("Input filename: " + cli.infile);
145 System.out.println("Output filename: " + cli.outfile);
146 System.out.print("Target: ");
150 System.out.println("ASSEMBLY");
153 System.out.println("DEFAULT");
156 System.out.println("INTER");
159 System.out.println("LOWIR");
162 System.out.println("PARSE");
165 System.out.println("SCAN");
168 System.out.println("not recognized");
172 for (int i = 0; i < cli.opts.length; i++) {
174 System.out.println("Optimization");
179 for (int i = 0; i < cli.extraopts.size(); i++) {
180 System.err.println("Warning: optimization \"" +
181 cli.extraopts.elementAt(i) +
182 "\" not recognized");
185 for (int i = 0; i < cli.extras.size(); i++) {
186 System.err.println("Warning: option \"" +
187 cli.extras.elementAt(i) +
188 "\" not recognized");
192 private static boolean error(State state, String error) {
193 System.err.println(error);
195 System.out.println("#ERROR#");
201 public static boolean semantics(State state) {
202 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
203 SemanticChecker checker = new SemanticChecker();
207 ok = checker.check(state, er);
208 } catch (Exception e) {
209 er.report(null, e.toString());
215 er.report(null, "Semantic check failed.");
218 System.out.print(er.toString());
223 public static void debugMessage(int level, String s) {
224 if (State.currentState.verbose >= level) {
225 System.err.println(s);
229 public static boolean parse(State state) {
231 /* parse structure file */
233 debugMessage(1, "Parsing structure file");
235 FileInputStream infile = new FileInputStream(state.infile + ".struct");
236 TDLParser parser = new TDLParser(new Lexer(infile));
237 CUP$TDLParser$actions.debug = state.verbose > 1 ;
238 state.ptStructures = (ParseNode) parser.parse().value;
239 } catch (FileNotFoundException fnfe) {
240 System.err.println("Unable to open file: " + state.infile + ".struct");
242 } catch (Exception e) {
243 // System.out.println(e);
244 // e.printStackTrace();
248 /* parse model file */
250 debugMessage(1, "Parsing model file");
252 FileInputStream infile = new FileInputStream(state.infile + ".model");
253 MDLParser parser = new MDLParser(new Lexer(infile));
254 CUP$MDLParser$actions.debug = state.verbose > 1 ;
255 state.ptModel = (ParseNode) parser.parse().value;
256 } catch (FileNotFoundException fnfe) {
257 System.err.println("Unable to open file: " + state.infile + ".model");
259 } catch (Exception e) {
260 // System.out.println(e);
261 // e.printStackTrace();
265 /* parse space file */
267 debugMessage(1, "Parsing space file");
269 FileInputStream infile = new FileInputStream(state.infile + ".space");
270 SDLParser parser = new SDLParser(new Lexer(infile));
271 CUP$SDLParser$actions.debug = state.verbose > 1 ;
272 state.ptSpace = (ParseNode) parser.parse().value;
273 } catch (FileNotFoundException fnfe) {
274 System.err.println("Unable to open file: " + state.infile + ".space");
276 } catch (Exception e) {
277 // System.out.println(e);
278 // e.printStackTrace();
282 /* parse constraints file */
284 debugMessage(1, "Parsing constraints file");
286 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
287 CDLParser parser = new CDLParser(new Lexer(infile));
288 CUP$CDLParser$actions.debug = state.verbose > 1 ;
289 state.ptConstraints = (ParseNode) parser.parse().value;
290 } catch (FileNotFoundException fnfe) {
291 System.err.println("Unable to open file: " + state.infile + ".constraints");
293 } catch (Exception e) {
294 // System.out.println(e);
295 // e.printStackTrace();
300 !CUP$TDLParser$actions.errors &&
301 !CUP$SDLParser$actions.errors &&
302 !CUP$CDLParser$actions.errors &&
303 !CUP$MDLParser$actions.errors;
306 // if verbosity is on, then output parse trees as .dot files
307 if (success && state.verbose > 0) {
309 FileOutputStream dotfile;
311 dotfile = new FileOutputStream(state.infile + ".struct.dot");
312 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
315 dotfile = new FileOutputStream(state.infile + ".model.dot");
316 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
319 dotfile = new FileOutputStream(state.infile + ".space.dot");
320 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
323 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
324 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
326 } catch (Exception e) {
336 public static boolean scan(State state) {
337 FileInputStream infile = null;
339 boolean errors = false;
340 String files[] = { new String(state.infile + ".struct"),
341 new String(state.infile + ".model"),
342 new String(state.infile + ".constraints"),
343 new String(state.infile + ".space") };
347 for (int i = 0; i < files.length; i++) {
349 String filename = files[i];
352 infile = new FileInputStream(filename);
353 } catch (FileNotFoundException fnfe) {
354 System.err.println("Unable to open file: " + filename);
358 lexer = new Lexer(infile);
363 java_cup.runtime.Symbol symbol;
365 symbol = lexer.next_token();
367 if (symbol.sym == Sym.EOF) {
369 } else if (symbol.sym == Sym.BAD) {
373 if (State.verbose > 2) {
374 System.out.println("Got token: " + symbol.value);
377 } catch (Exception e) {