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");
88 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
89 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
91 // do model optimizations
92 //(new Optimizer(state)).optimize();
94 //NaiveGenerator ng = new NaiveGenerator(state);
96 RepairGenerator wg = new RepairGenerator(state);
97 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
99 } catch (Exception e) {
105 System.out.println("Compilation of " + state.infile + " successful.");
106 System.out.println("#SUCCESS#");
108 } else if (cli.target == CLI.INTER) {
110 System.out.println("Semantic analysis for " + cli.infile + ".");
113 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
114 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
115 success = semantics(state) || error(state, "Semantic analysis failed.");
118 System.out.println("Semantic analysis of " + state.infile + " successful.");
119 System.out.println("#SUCCESS#");
121 } else if (cli.target == CLI.PARSE) {
123 System.out.println("Parsing " + cli.infile + ".");
126 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
127 success = parse(state) || error(state, "Parsing failed.");
130 System.out.println("Parsing of " + state.infile + " successful.");
131 System.out.println("#SUCCESS#");
133 } else if (cli.target == CLI.SCAN) {
135 System.out.println("Scanning " + cli.infile + ".");
138 success = scan(state) || error(state, "Scanning failed.");
141 System.out.println("Scanning of " + state.infile + " successful.");
142 System.out.println("#SUCCESS#");
147 private static void printArgInfo(CLI cli) {
149 System.out.println("Printing debugging information...");
150 System.out.println("Input filename: " + cli.infile);
151 System.out.println("Output filename: " + cli.outfile);
152 System.out.print("Target: ");
156 System.out.println("ASSEMBLY");
159 System.out.println("DEFAULT");
162 System.out.println("INTER");
165 System.out.println("LOWIR");
168 System.out.println("PARSE");
171 System.out.println("SCAN");
174 System.out.println("not recognized");
178 for (int i = 0; i < cli.opts.length; i++) {
180 System.out.println("Optimization");
185 for (int i = 0; i < cli.extraopts.size(); i++) {
186 System.err.println("Warning: optimization \"" +
187 cli.extraopts.elementAt(i) +
188 "\" not recognized");
191 for (int i = 0; i < cli.extras.size(); i++) {
192 System.err.println("Warning: option \"" +
193 cli.extras.elementAt(i) +
194 "\" not recognized");
198 private static boolean error(State state, String error) {
199 System.err.println(error);
201 System.out.println("#ERROR#");
207 public static boolean semantics(State state) {
208 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
209 SemanticChecker checker = new SemanticChecker();
213 ok = checker.check(state, er);
214 } catch (Exception e) {
215 er.report(null, e.toString());
221 er.report(null, "Semantic check failed.");
224 System.out.print(er.toString());
229 public static void debugMessage(int level, String s) {
230 if (State.currentState.verbose >= level) {
231 System.err.println(s);
235 public static boolean parse(State state) {
237 /* parse structure file */
239 debugMessage(1, "Parsing structure file");
241 FileInputStream infile = new FileInputStream(state.infile + ".struct");
242 TDLParser parser = new TDLParser(new Lexer(infile));
243 CUP$TDLParser$actions.debug = state.verbose > 1 ;
244 state.ptStructures = (ParseNode) parser.parse().value;
245 } catch (FileNotFoundException fnfe) {
246 System.err.println("Unable to open file: " + state.infile + ".struct");
248 } catch (Exception e) {
249 // System.out.println(e);
250 // e.printStackTrace();
254 /* parse model file */
256 debugMessage(1, "Parsing model file");
258 FileInputStream infile = new FileInputStream(state.infile + ".model");
259 MDLParser parser = new MDLParser(new Lexer(infile));
260 CUP$MDLParser$actions.debug = state.verbose > 1 ;
261 state.ptModel = (ParseNode) parser.parse().value;
262 } catch (FileNotFoundException fnfe) {
263 System.err.println("Unable to open file: " + state.infile + ".model");
265 } catch (Exception e) {
266 // System.out.println(e);
267 // e.printStackTrace();
271 /* parse space file */
273 debugMessage(1, "Parsing space file");
275 FileInputStream infile = new FileInputStream(state.infile + ".space");
276 SDLParser parser = new SDLParser(new Lexer(infile));
277 CUP$SDLParser$actions.debug = state.verbose > 1 ;
278 state.ptSpace = (ParseNode) parser.parse().value;
279 } catch (FileNotFoundException fnfe) {
280 System.err.println("Unable to open file: " + state.infile + ".space");
282 } catch (Exception e) {
283 // System.out.println(e);
284 // e.printStackTrace();
288 /* parse constraints file */
290 debugMessage(1, "Parsing constraints file");
292 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
293 CDLParser parser = new CDLParser(new Lexer(infile));
294 CUP$CDLParser$actions.debug = state.verbose > 1 ;
295 state.ptConstraints = (ParseNode) parser.parse().value;
296 } catch (FileNotFoundException fnfe) {
297 System.err.println("Unable to open file: " + state.infile + ".constraints");
299 } catch (Exception e) {
300 // System.out.println(e);
301 // e.printStackTrace();
306 !CUP$TDLParser$actions.errors &&
307 !CUP$SDLParser$actions.errors &&
308 !CUP$CDLParser$actions.errors &&
309 !CUP$MDLParser$actions.errors;
312 // if verbosity is on, then output parse trees as .dot files
313 if (success && state.verbose > 0) {
315 FileOutputStream dotfile;
317 dotfile = new FileOutputStream(state.infile + ".struct.dot");
318 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
321 dotfile = new FileOutputStream(state.infile + ".model.dot");
322 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
325 dotfile = new FileOutputStream(state.infile + ".space.dot");
326 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
329 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
330 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
332 } catch (Exception e) {
342 public static boolean scan(State state) {
343 FileInputStream infile = null;
345 boolean errors = false;
346 String files[] = { new String(state.infile + ".struct"),
347 new String(state.infile + ".model"),
348 new String(state.infile + ".constraints"),
349 new String(state.infile + ".space") };
353 for (int i = 0; i < files.length; i++) {
355 String filename = files[i];
358 infile = new FileInputStream(filename);
359 } catch (FileNotFoundException fnfe) {
360 System.err.println("Unable to open file: " + filename);
364 lexer = new Lexer(infile);
369 java_cup.runtime.Symbol symbol;
371 symbol = lexer.next_token();
373 if (symbol.sym == Sym.EOF) {
375 } else if (symbol.sym == Sym.BAD) {
379 if (State.verbose > 2) {
380 System.out.println("Got token: " + symbol.value);
383 } catch (Exception e) {