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");
80 // do model optimizations
81 (new Optimizer(state)).optimize();
83 NaiveGenerator ng = new NaiveGenerator(state);
85 //WorklistGenerator wg = new WorklistGenerator(state);
88 } catch (Exception e) {
94 System.out.println("Compilation of " + state.infile + " successful.");
95 System.out.println("#SUCCESS#");
97 } else if (cli.target == CLI.INTER) {
99 System.out.println("Semantic analysis for " + cli.infile + ".");
102 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
103 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
104 success = semantics(state) || error(state, "Semantic analysis failed.");
107 System.out.println("Semantic analysis of " + state.infile + " successful.");
108 System.out.println("#SUCCESS#");
110 } else if (cli.target == CLI.PARSE) {
112 System.out.println("Parsing " + cli.infile + ".");
115 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
116 success = parse(state) || error(state, "Parsing failed.");
119 System.out.println("Parsing of " + state.infile + " successful.");
120 System.out.println("#SUCCESS#");
122 } else if (cli.target == CLI.SCAN) {
124 System.out.println("Scanning " + cli.infile + ".");
127 success = scan(state) || error(state, "Scanning failed.");
130 System.out.println("Scanning of " + state.infile + " successful.");
131 System.out.println("#SUCCESS#");
136 private static void printArgInfo(CLI cli) {
138 System.out.println("Printing debugging information...");
139 System.out.println("Input filename: " + cli.infile);
140 System.out.println("Output filename: " + cli.outfile);
141 System.out.print("Target: ");
145 System.out.println("ASSEMBLY");
148 System.out.println("DEFAULT");
151 System.out.println("INTER");
154 System.out.println("LOWIR");
157 System.out.println("PARSE");
160 System.out.println("SCAN");
163 System.out.println("not recognized");
167 for (int i = 0; i < cli.opts.length; i++) {
169 System.out.println("Optimization");
174 for (int i = 0; i < cli.extraopts.size(); i++) {
175 System.err.println("Warning: optimization \"" +
176 cli.extraopts.elementAt(i) +
177 "\" not recognized");
180 for (int i = 0; i < cli.extras.size(); i++) {
181 System.err.println("Warning: option \"" +
182 cli.extras.elementAt(i) +
183 "\" not recognized");
187 private static boolean error(State state, String error) {
188 System.err.println(error);
190 System.out.println("#ERROR#");
196 public static boolean semantics(State state) {
197 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
198 SemanticChecker checker = new SemanticChecker();
202 ok = checker.check(state, er);
203 } catch (Exception e) {
204 er.report(null, e.toString());
210 er.report(null, "Semantic check failed.");
213 System.out.print(er.toString());
218 public static void debugMessage(int level, String s) {
219 if (State.currentState.verbose >= level) {
220 System.err.println(s);
224 public static boolean parse(State state) {
226 /* parse structure file */
228 debugMessage(1, "Parsing structure file");
230 FileInputStream infile = new FileInputStream(state.infile + ".struct");
231 TDLParser parser = new TDLParser(new Lexer(infile));
232 CUP$TDLParser$actions.debug = state.verbose > 1 ;
233 state.ptStructures = (ParseNode) parser.parse().value;
234 } catch (FileNotFoundException fnfe) {
235 System.err.println("Unable to open file: " + state.infile + ".struct");
237 } catch (Exception e) {
238 // System.out.println(e);
239 // e.printStackTrace();
243 /* parse model file */
245 debugMessage(1, "Parsing model file");
247 FileInputStream infile = new FileInputStream(state.infile + ".model");
248 MDLParser parser = new MDLParser(new Lexer(infile));
249 CUP$MDLParser$actions.debug = state.verbose > 1 ;
250 state.ptModel = (ParseNode) parser.parse().value;
251 } catch (FileNotFoundException fnfe) {
252 System.err.println("Unable to open file: " + state.infile + ".model");
254 } catch (Exception e) {
255 // System.out.println(e);
256 // e.printStackTrace();
260 /* parse space file */
262 debugMessage(1, "Parsing space file");
264 FileInputStream infile = new FileInputStream(state.infile + ".space");
265 SDLParser parser = new SDLParser(new Lexer(infile));
266 CUP$SDLParser$actions.debug = state.verbose > 1 ;
267 state.ptSpace = (ParseNode) parser.parse().value;
268 } catch (FileNotFoundException fnfe) {
269 System.err.println("Unable to open file: " + state.infile + ".space");
271 } catch (Exception e) {
272 // System.out.println(e);
273 // e.printStackTrace();
277 /* parse constraints file */
279 debugMessage(1, "Parsing constraints file");
281 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
282 CDLParser parser = new CDLParser(new Lexer(infile));
283 CUP$CDLParser$actions.debug = state.verbose > 1 ;
284 state.ptConstraints = (ParseNode) parser.parse().value;
285 } catch (FileNotFoundException fnfe) {
286 System.err.println("Unable to open file: " + state.infile + ".constraints");
288 } catch (Exception e) {
289 // System.out.println(e);
290 // e.printStackTrace();
295 !CUP$TDLParser$actions.errors &&
296 !CUP$SDLParser$actions.errors &&
297 !CUP$CDLParser$actions.errors &&
298 !CUP$MDLParser$actions.errors;
301 // if verbosity is on, then output parse trees as .dot files
302 if (success && state.verbose > 0) {
304 FileOutputStream dotfile;
306 dotfile = new FileOutputStream(state.infile + ".struct.dot");
307 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
310 dotfile = new FileOutputStream(state.infile + ".model.dot");
311 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
314 dotfile = new FileOutputStream(state.infile + ".space.dot");
315 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
318 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
319 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
321 } catch (Exception e) {
331 public static boolean scan(State state) {
332 FileInputStream infile = null;
334 boolean errors = false;
335 String files[] = { new String(state.infile + ".struct"),
336 new String(state.infile + ".model"),
337 new String(state.infile + ".constraints"),
338 new String(state.infile + ".space") };
342 for (int i = 0; i < files.length; i++) {
344 String filename = files[i];
347 infile = new FileInputStream(filename);
348 } catch (FileNotFoundException fnfe) {
349 System.err.println("Unable to open file: " + filename);
353 lexer = new Lexer(infile);
358 java_cup.runtime.Symbol symbol;
360 symbol = lexer.next_token();
362 if (symbol.sym == Sym.EOF) {
364 } else if (symbol.sym == Sym.BAD) {
368 if (State.verbose > 2) {
369 System.out.println("Got token: " + symbol.value);
372 } catch (Exception e) {