8 * The main compiler module, which does the following:
15 * @author <b>Daniel Roy</b> droy (at) mit (dot) edu
19 public class Compiler {
21 public static void main(String[] args) {
23 boolean success = true;
26 printArgInfo(cli); // prints debugging information and warning
29 State.currentState = state;
30 State.debug = cli.debug;
31 State.verbose = cli.verbose;
32 State.infile = cli.infile;
33 State.outfile = cli.outfile;
36 * added: terminates with an error message if no input file
37 * specified at command line
40 System.out.println("\nMCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)");
42 if (cli.infile == null) {
43 System.err.println("\nError: no input file specified");
47 if (cli.target == CLI.ASSEMBLY || cli.target == CLI.DEFAULT) {
49 System.out.println("Compiling " + cli.infile + ".");
52 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
53 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
54 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
56 (new DependencyBuilder(state)).calculate();
59 Vector nodes = new Vector(state.constraintnodes.values());
60 nodes.addAll(state.rulenodes.values());
62 FileOutputStream dotfile;
63 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
64 GraphNode.useEdgeLabels = true;
65 GraphNode.DOTVisitor.visit(dotfile, nodes);
68 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
69 GraphNode.useEdgeLabels = false;
70 GraphNode.DOTVisitor.visit(dotfile, nodes);
72 } catch (Exception e) {
78 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
79 NaiveGenerator ng = new NaiveGenerator(state);
82 } catch (Exception e) {
88 System.out.println("Compilation of " + state.infile + " successful.");
89 System.out.println("#SUCCESS#");
91 } else if (cli.target == CLI.INTER) {
93 System.out.println("Semantic analysis for " + cli.infile + ".");
96 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
97 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
98 success = semantics(state) || error(state, "Semantic analysis failed.");
101 System.out.println("Semantic analysis of " + state.infile + " successful.");
102 System.out.println("#SUCCESS#");
104 } else if (cli.target == CLI.PARSE) {
106 System.out.println("Parsing " + cli.infile + ".");
109 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
110 success = parse(state) || error(state, "Parsing failed.");
113 System.out.println("Parsing of " + state.infile + " successful.");
114 System.out.println("#SUCCESS#");
116 } else if (cli.target == CLI.SCAN) {
118 System.out.println("Scanning " + cli.infile + ".");
121 success = scan(state) || error(state, "Scanning failed.");
124 System.out.println("Scanning of " + state.infile + " successful.");
125 System.out.println("#SUCCESS#");
130 private static void printArgInfo(CLI cli) {
132 System.out.println("Printing debugging information...");
133 System.out.println("Input filename: " + cli.infile);
134 System.out.println("Output filename: " + cli.outfile);
135 System.out.print("Target: ");
139 System.out.println("ASSEMBLY");
142 System.out.println("DEFAULT");
145 System.out.println("INTER");
148 System.out.println("LOWIR");
151 System.out.println("PARSE");
154 System.out.println("SCAN");
157 System.out.println("not recognized");
161 for (int i = 0; i < cli.opts.length; i++) {
163 System.out.println("Optimization");
168 for (int i = 0; i < cli.extraopts.size(); i++) {
169 System.err.println("Warning: optimization \"" +
170 cli.extraopts.elementAt(i) +
171 "\" not recognized");
174 for (int i = 0; i < cli.extras.size(); i++) {
175 System.err.println("Warning: option \"" +
176 cli.extras.elementAt(i) +
177 "\" not recognized");
181 private static boolean error(State state, String error) {
182 System.err.println(error);
184 System.out.println("#ERROR#");
190 public static boolean semantics(State state) {
191 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
192 SemanticChecker checker = new SemanticChecker();
196 ok = checker.check(state, er);
197 } catch (Exception e) {
198 er.report(null, e.toString());
204 er.report(null, "Semantic check failed.");
207 System.out.print(er.toString());
212 public static void debugMessage(int level, String s) {
213 if (State.currentState.verbose >= level) {
214 System.err.println(s);
218 public static boolean parse(State state) {
220 /* parse structure file */
222 debugMessage(1, "Parsing structure file");
224 FileInputStream infile = new FileInputStream(state.infile + ".struct");
225 TDLParser parser = new TDLParser(new Lexer(infile));
226 CUP$TDLParser$actions.debug = state.verbose > 1 ;
227 state.ptStructures = (ParseNode) parser.parse().value;
228 } catch (FileNotFoundException fnfe) {
229 System.err.println("Unable to open file: " + state.infile + ".struct");
231 } catch (Exception e) {
232 // System.out.println(e);
233 // e.printStackTrace();
237 /* parse model file */
239 debugMessage(1, "Parsing model file");
241 FileInputStream infile = new FileInputStream(state.infile + ".model");
242 MDLParser parser = new MDLParser(new Lexer(infile));
243 CUP$MDLParser$actions.debug = state.verbose > 1 ;
244 state.ptModel = (ParseNode) parser.parse().value;
245 } catch (FileNotFoundException fnfe) {
246 System.err.println("Unable to open file: " + state.infile + ".model");
248 } catch (Exception e) {
249 // System.out.println(e);
250 // e.printStackTrace();
254 /* parse space file */
256 debugMessage(1, "Parsing space file");
258 FileInputStream infile = new FileInputStream(state.infile + ".space");
259 SDLParser parser = new SDLParser(new Lexer(infile));
260 CUP$SDLParser$actions.debug = state.verbose > 1 ;
261 state.ptSpace = (ParseNode) parser.parse().value;
262 } catch (FileNotFoundException fnfe) {
263 System.err.println("Unable to open file: " + state.infile + ".space");
265 } catch (Exception e) {
266 // System.out.println(e);
267 // e.printStackTrace();
271 /* parse constraints file */
273 debugMessage(1, "Parsing constraints file");
275 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
276 CDLParser parser = new CDLParser(new Lexer(infile));
277 CUP$CDLParser$actions.debug = state.verbose > 1 ;
278 state.ptConstraints = (ParseNode) parser.parse().value;
279 } catch (FileNotFoundException fnfe) {
280 System.err.println("Unable to open file: " + state.infile + ".constraints");
282 } catch (Exception e) {
283 // System.out.println(e);
284 // e.printStackTrace();
289 !CUP$TDLParser$actions.errors &&
290 !CUP$SDLParser$actions.errors &&
291 !CUP$CDLParser$actions.errors &&
292 !CUP$MDLParser$actions.errors;
295 // if verbosity is on, then output parse trees as .dot files
296 if (success && state.verbose > 0) {
298 FileOutputStream dotfile;
300 dotfile = new FileOutputStream(state.infile + ".struct.dot");
301 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
304 dotfile = new FileOutputStream(state.infile + ".model.dot");
305 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
308 dotfile = new FileOutputStream(state.infile + ".space.dot");
309 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
312 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
313 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
315 } catch (Exception e) {
325 public static boolean scan(State state) {
326 FileInputStream infile = null;
328 boolean errors = false;
329 String files[] = { new String(state.infile + ".struct"),
330 new String(state.infile + ".model"),
331 new String(state.infile + ".constraints"),
332 new String(state.infile + ".space") };
336 for (int i = 0; i < files.length; i++) {
338 String filename = files[i];
341 infile = new FileInputStream(filename);
342 } catch (FileNotFoundException fnfe) {
343 System.err.println("Unable to open file: " + filename);
347 lexer = new Lexer(infile);
352 java_cup.runtime.Symbol symbol;
354 symbol = lexer.next_token();
356 if (symbol.sym == Sym.EOF) {
358 } else if (symbol.sym == Sym.BAD) {
362 if (State.verbose > 2) {
363 System.out.println("Got token: " + symbol.value);
366 } catch (Exception e) {