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.");
60 /* Check partition constraints */
61 (new ImplicitSchema(state)).update();
62 Termination t=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");
89 // do model optimizations
90 //(new Optimizer(state)).optimize();
92 NaiveGenerator ng = new NaiveGenerator(state);
94 //WorklistGenerator wg = new WorklistGenerator(state);
97 } catch (Exception e) {
103 System.out.println("Compilation of " + state.infile + " successful.");
104 System.out.println("#SUCCESS#");
106 } else if (cli.target == CLI.INTER) {
108 System.out.println("Semantic analysis for " + cli.infile + ".");
111 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
112 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
113 success = semantics(state) || error(state, "Semantic analysis failed.");
116 System.out.println("Semantic analysis of " + state.infile + " successful.");
117 System.out.println("#SUCCESS#");
119 } else if (cli.target == CLI.PARSE) {
121 System.out.println("Parsing " + cli.infile + ".");
124 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
125 success = parse(state) || error(state, "Parsing failed.");
128 System.out.println("Parsing of " + state.infile + " successful.");
129 System.out.println("#SUCCESS#");
131 } else if (cli.target == CLI.SCAN) {
133 System.out.println("Scanning " + cli.infile + ".");
136 success = scan(state) || error(state, "Scanning failed.");
139 System.out.println("Scanning of " + state.infile + " successful.");
140 System.out.println("#SUCCESS#");
145 private static void printArgInfo(CLI cli) {
147 System.out.println("Printing debugging information...");
148 System.out.println("Input filename: " + cli.infile);
149 System.out.println("Output filename: " + cli.outfile);
150 System.out.print("Target: ");
154 System.out.println("ASSEMBLY");
157 System.out.println("DEFAULT");
160 System.out.println("INTER");
163 System.out.println("LOWIR");
166 System.out.println("PARSE");
169 System.out.println("SCAN");
172 System.out.println("not recognized");
176 for (int i = 0; i < cli.opts.length; i++) {
178 System.out.println("Optimization");
183 for (int i = 0; i < cli.extraopts.size(); i++) {
184 System.err.println("Warning: optimization \"" +
185 cli.extraopts.elementAt(i) +
186 "\" not recognized");
189 for (int i = 0; i < cli.extras.size(); i++) {
190 System.err.println("Warning: option \"" +
191 cli.extras.elementAt(i) +
192 "\" not recognized");
196 private static boolean error(State state, String error) {
197 System.err.println(error);
199 System.out.println("#ERROR#");
205 public static boolean semantics(State state) {
206 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
207 SemanticChecker checker = new SemanticChecker();
211 ok = checker.check(state, er);
212 } catch (Exception e) {
213 er.report(null, e.toString());
219 er.report(null, "Semantic check failed.");
222 System.out.print(er.toString());
227 public static void debugMessage(int level, String s) {
228 if (State.currentState.verbose >= level) {
229 System.err.println(s);
233 public static boolean parse(State state) {
235 /* parse structure file */
237 debugMessage(1, "Parsing structure file");
239 FileInputStream infile = new FileInputStream(state.infile + ".struct");
240 TDLParser parser = new TDLParser(new Lexer(infile));
241 CUP$TDLParser$actions.debug = state.verbose > 1 ;
242 state.ptStructures = (ParseNode) parser.parse().value;
243 } catch (FileNotFoundException fnfe) {
244 System.err.println("Unable to open file: " + state.infile + ".struct");
246 } catch (Exception e) {
247 // System.out.println(e);
248 // e.printStackTrace();
252 /* parse model file */
254 debugMessage(1, "Parsing model file");
256 FileInputStream infile = new FileInputStream(state.infile + ".model");
257 MDLParser parser = new MDLParser(new Lexer(infile));
258 CUP$MDLParser$actions.debug = state.verbose > 1 ;
259 state.ptModel = (ParseNode) parser.parse().value;
260 } catch (FileNotFoundException fnfe) {
261 System.err.println("Unable to open file: " + state.infile + ".model");
263 } catch (Exception e) {
264 // System.out.println(e);
265 // e.printStackTrace();
269 /* parse space file */
271 debugMessage(1, "Parsing space file");
273 FileInputStream infile = new FileInputStream(state.infile + ".space");
274 SDLParser parser = new SDLParser(new Lexer(infile));
275 CUP$SDLParser$actions.debug = state.verbose > 1 ;
276 state.ptSpace = (ParseNode) parser.parse().value;
277 } catch (FileNotFoundException fnfe) {
278 System.err.println("Unable to open file: " + state.infile + ".space");
280 } catch (Exception e) {
281 // System.out.println(e);
282 // e.printStackTrace();
286 /* parse constraints file */
288 debugMessage(1, "Parsing constraints file");
290 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
291 CDLParser parser = new CDLParser(new Lexer(infile));
292 CUP$CDLParser$actions.debug = state.verbose > 1 ;
293 state.ptConstraints = (ParseNode) parser.parse().value;
294 } catch (FileNotFoundException fnfe) {
295 System.err.println("Unable to open file: " + state.infile + ".constraints");
297 } catch (Exception e) {
298 // System.out.println(e);
299 // e.printStackTrace();
304 !CUP$TDLParser$actions.errors &&
305 !CUP$SDLParser$actions.errors &&
306 !CUP$CDLParser$actions.errors &&
307 !CUP$MDLParser$actions.errors;
310 // if verbosity is on, then output parse trees as .dot files
311 if (success && state.verbose > 0) {
313 FileOutputStream dotfile;
315 dotfile = new FileOutputStream(state.infile + ".struct.dot");
316 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
319 dotfile = new FileOutputStream(state.infile + ".model.dot");
320 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
323 dotfile = new FileOutputStream(state.infile + ".space.dot");
324 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
327 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
328 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
330 } catch (Exception e) {
340 public static boolean scan(State state) {
341 FileInputStream infile = null;
343 boolean errors = false;
344 String files[] = { new String(state.infile + ".struct"),
345 new String(state.infile + ".model"),
346 new String(state.infile + ".constraints"),
347 new String(state.infile + ".space") };
351 for (int i = 0; i < files.length; i++) {
353 String filename = files[i];
356 infile = new FileInputStream(filename);
357 } catch (FileNotFoundException fnfe) {
358 System.err.println("Unable to open file: " + filename);
362 lexer = new Lexer(infile);
367 java_cup.runtime.Symbol symbol;
369 symbol = lexer.next_token();
371 if (symbol.sym == Sym.EOF) {
373 } else if (symbol.sym == Sym.BAD) {
377 if (State.verbose > 2) {
378 System.out.println("Got token: " + symbol.value);
381 } catch (Exception e) {