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 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);
106 } catch (Exception e) {
112 System.out.println("Compilation of " + state.infile + " successful.");
113 System.out.println("#SUCCESS#");
115 } else if (cli.target == CLI.INTER) {
117 System.out.println("Semantic analysis for " + cli.infile + ".");
120 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
121 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
122 success = semantics(state) || error(state, "Semantic analysis failed.");
125 System.out.println("Semantic analysis of " + state.infile + " successful.");
126 System.out.println("#SUCCESS#");
128 } else if (cli.target == CLI.PARSE) {
130 System.out.println("Parsing " + cli.infile + ".");
133 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
134 success = parse(state) || error(state, "Parsing failed.");
137 System.out.println("Parsing of " + state.infile + " successful.");
138 System.out.println("#SUCCESS#");
140 } else if (cli.target == CLI.SCAN) {
142 System.out.println("Scanning " + cli.infile + ".");
145 success = scan(state) || error(state, "Scanning failed.");
148 System.out.println("Scanning of " + state.infile + " successful.");
149 System.out.println("#SUCCESS#");
154 private static void printArgInfo(CLI cli) {
156 System.out.println("Printing debugging information...");
157 System.out.println("Input filename: " + cli.infile);
158 System.out.println("Output filename: " + cli.outfile);
159 System.out.print("Target: ");
163 System.out.println("ASSEMBLY");
166 System.out.println("DEFAULT");
169 System.out.println("INTER");
172 System.out.println("LOWIR");
175 System.out.println("PARSE");
178 System.out.println("SCAN");
181 System.out.println("not recognized");
185 for (int i = 0; i < cli.opts.length; i++) {
187 System.out.println("Optimization");
192 for (int i = 0; i < cli.extraopts.size(); i++) {
193 System.err.println("Warning: optimization \"" +
194 cli.extraopts.elementAt(i) +
195 "\" not recognized");
198 for (int i = 0; i < cli.extras.size(); i++) {
199 System.err.println("Warning: option \"" +
200 cli.extras.elementAt(i) +
201 "\" not recognized");
205 private static boolean error(State state, String error) {
206 System.err.println(error);
208 System.out.println("#ERROR#");
214 public static boolean semantics(State state) {
215 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
216 SemanticChecker checker = new SemanticChecker();
220 ok = checker.check(state, er);
221 } catch (Exception e) {
222 er.report(null, e.toString());
228 er.report(null, "Semantic check failed.");
231 System.out.print(er.toString());
236 public static void debugMessage(int level, String s) {
237 if (State.currentState.verbose >= level) {
238 System.err.println(s);
242 public static boolean parse(State state) {
244 /* parse structure file */
246 debugMessage(1, "Parsing structure file");
248 FileInputStream infile = new FileInputStream(state.infile + ".struct");
249 TDLParser parser = new TDLParser(new Lexer(infile));
250 CUP$TDLParser$actions.debug = state.verbose > 1 ;
251 state.ptStructures = (ParseNode) parser.parse().value;
252 } catch (FileNotFoundException fnfe) {
253 System.err.println("Unable to open file: " + state.infile + ".struct");
255 } catch (Exception e) {
256 // System.out.println(e);
257 // e.printStackTrace();
261 /* parse model file */
263 debugMessage(1, "Parsing model file");
265 FileInputStream infile = new FileInputStream(state.infile + ".model");
266 MDLParser parser = new MDLParser(new Lexer(infile));
267 CUP$MDLParser$actions.debug = state.verbose > 1 ;
268 state.ptModel = (ParseNode) parser.parse().value;
269 } catch (FileNotFoundException fnfe) {
270 System.err.println("Unable to open file: " + state.infile + ".model");
272 } catch (Exception e) {
273 // System.out.println(e);
274 // e.printStackTrace();
278 /* parse space file */
280 debugMessage(1, "Parsing space file");
282 FileInputStream infile = new FileInputStream(state.infile + ".space");
283 SDLParser parser = new SDLParser(new Lexer(infile));
284 CUP$SDLParser$actions.debug = state.verbose > 1 ;
285 state.ptSpace = (ParseNode) parser.parse().value;
286 } catch (FileNotFoundException fnfe) {
287 System.err.println("Unable to open file: " + state.infile + ".space");
289 } catch (Exception e) {
290 // System.out.println(e);
291 // e.printStackTrace();
295 /* parse constraints file */
297 debugMessage(1, "Parsing constraints file");
299 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
300 CDLParser parser = new CDLParser(new Lexer(infile));
301 CUP$CDLParser$actions.debug = state.verbose > 1 ;
302 state.ptConstraints = (ParseNode) parser.parse().value;
303 } catch (FileNotFoundException fnfe) {
304 System.err.println("Unable to open file: " + state.infile + ".constraints");
306 } catch (Exception e) {
307 // System.out.println(e);
308 // e.printStackTrace();
313 !CUP$TDLParser$actions.errors &&
314 !CUP$SDLParser$actions.errors &&
315 !CUP$CDLParser$actions.errors &&
316 !CUP$MDLParser$actions.errors;
319 // if verbosity is on, then output parse trees as .dot files
320 if (success && state.verbose > 0) {
322 FileOutputStream dotfile;
324 dotfile = new FileOutputStream(state.infile + ".struct.dot");
325 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
328 dotfile = new FileOutputStream(state.infile + ".model.dot");
329 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
332 dotfile = new FileOutputStream(state.infile + ".space.dot");
333 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
336 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
337 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
339 } catch (Exception e) {
349 public static boolean scan(State state) {
350 FileInputStream infile = null;
352 boolean errors = false;
353 String files[] = { new String(state.infile + ".struct"),
354 new String(state.infile + ".model"),
355 new String(state.infile + ".constraints"),
356 new String(state.infile + ".space") };
360 for (int i = 0; i < files.length; i++) {
362 String filename = files[i];
365 infile = new FileInputStream(filename);
366 } catch (FileNotFoundException fnfe) {
367 System.err.println("Unable to open file: " + filename);
371 lexer = new Lexer(infile);
376 java_cup.runtime.Symbol symbol;
378 symbol = lexer.next_token();
380 if (symbol.sym == Sym.EOF) {
382 } else if (symbol.sym == Sym.BAD) {
386 if (State.verbose > 2) {
387 System.out.println("Got token: " + symbol.value);
390 } catch (Exception e) {