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=false;
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.");
57 /* Check partition constraints */
58 (new ImplicitSchema(state)).update();
61 Termination t=new Termination(state);
64 (new DependencyBuilder(state)).calculate();
67 Vector nodes = new Vector(state.constraintnodes.values());
68 nodes.addAll(state.rulenodes.values());
70 FileOutputStream dotfile;
71 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
72 GraphNode.useEdgeLabels = true;
73 GraphNode.DOTVisitor.visit(dotfile, nodes);
76 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
77 GraphNode.useEdgeLabels = false;
78 GraphNode.DOTVisitor.visit(dotfile, nodes);
80 } catch (Exception e) {
86 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
88 // do model optimizations
89 //(new Optimizer(state)).optimize();
91 NaiveGenerator ng = new NaiveGenerator(state);
93 //WorklistGenerator wg = new WorklistGenerator(state);
96 } catch (Exception e) {
102 System.out.println("Compilation of " + state.infile + " successful.");
103 System.out.println("#SUCCESS#");
105 } else if (cli.target == CLI.INTER) {
107 System.out.println("Semantic analysis for " + cli.infile + ".");
110 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
111 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
112 success = semantics(state) || error(state, "Semantic analysis failed.");
115 System.out.println("Semantic analysis of " + state.infile + " successful.");
116 System.out.println("#SUCCESS#");
118 } else if (cli.target == CLI.PARSE) {
120 System.out.println("Parsing " + cli.infile + ".");
123 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
124 success = parse(state) || error(state, "Parsing failed.");
127 System.out.println("Parsing of " + state.infile + " successful.");
128 System.out.println("#SUCCESS#");
130 } else if (cli.target == CLI.SCAN) {
132 System.out.println("Scanning " + cli.infile + ".");
135 success = scan(state) || error(state, "Scanning failed.");
138 System.out.println("Scanning of " + state.infile + " successful.");
139 System.out.println("#SUCCESS#");
144 private static void printArgInfo(CLI cli) {
146 System.out.println("Printing debugging information...");
147 System.out.println("Input filename: " + cli.infile);
148 System.out.println("Output filename: " + cli.outfile);
149 System.out.print("Target: ");
153 System.out.println("ASSEMBLY");
156 System.out.println("DEFAULT");
159 System.out.println("INTER");
162 System.out.println("LOWIR");
165 System.out.println("PARSE");
168 System.out.println("SCAN");
171 System.out.println("not recognized");
175 for (int i = 0; i < cli.opts.length; i++) {
177 System.out.println("Optimization");
182 for (int i = 0; i < cli.extraopts.size(); i++) {
183 System.err.println("Warning: optimization \"" +
184 cli.extraopts.elementAt(i) +
185 "\" not recognized");
188 for (int i = 0; i < cli.extras.size(); i++) {
189 System.err.println("Warning: option \"" +
190 cli.extras.elementAt(i) +
191 "\" not recognized");
195 private static boolean error(State state, String error) {
196 System.err.println(error);
198 System.out.println("#ERROR#");
204 public static boolean semantics(State state) {
205 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
206 SemanticChecker checker = new SemanticChecker();
210 ok = checker.check(state, er);
211 } catch (Exception e) {
212 er.report(null, e.toString());
218 er.report(null, "Semantic check failed.");
221 System.out.print(er.toString());
226 public static void debugMessage(int level, String s) {
227 if (State.currentState.verbose >= level) {
228 System.err.println(s);
232 public static boolean parse(State state) {
234 /* parse structure file */
236 debugMessage(1, "Parsing structure file");
238 FileInputStream infile = new FileInputStream(state.infile + ".struct");
239 TDLParser parser = new TDLParser(new Lexer(infile));
240 CUP$TDLParser$actions.debug = state.verbose > 1 ;
241 state.ptStructures = (ParseNode) parser.parse().value;
242 } catch (FileNotFoundException fnfe) {
243 System.err.println("Unable to open file: " + state.infile + ".struct");
245 } catch (Exception e) {
246 // System.out.println(e);
247 // e.printStackTrace();
251 /* parse model file */
253 debugMessage(1, "Parsing model file");
255 FileInputStream infile = new FileInputStream(state.infile + ".model");
256 MDLParser parser = new MDLParser(new Lexer(infile));
257 CUP$MDLParser$actions.debug = state.verbose > 1 ;
258 state.ptModel = (ParseNode) parser.parse().value;
259 } catch (FileNotFoundException fnfe) {
260 System.err.println("Unable to open file: " + state.infile + ".model");
262 } catch (Exception e) {
263 // System.out.println(e);
264 // e.printStackTrace();
268 /* parse space file */
270 debugMessage(1, "Parsing space file");
272 FileInputStream infile = new FileInputStream(state.infile + ".space");
273 SDLParser parser = new SDLParser(new Lexer(infile));
274 CUP$SDLParser$actions.debug = state.verbose > 1 ;
275 state.ptSpace = (ParseNode) parser.parse().value;
276 } catch (FileNotFoundException fnfe) {
277 System.err.println("Unable to open file: " + state.infile + ".space");
279 } catch (Exception e) {
280 // System.out.println(e);
281 // e.printStackTrace();
285 /* parse constraints file */
287 debugMessage(1, "Parsing constraints file");
289 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
290 CDLParser parser = new CDLParser(new Lexer(infile));
291 CUP$CDLParser$actions.debug = state.verbose > 1 ;
292 state.ptConstraints = (ParseNode) parser.parse().value;
293 } catch (FileNotFoundException fnfe) {
294 System.err.println("Unable to open file: " + state.infile + ".constraints");
296 } catch (Exception e) {
297 // System.out.println(e);
298 // e.printStackTrace();
303 !CUP$TDLParser$actions.errors &&
304 !CUP$SDLParser$actions.errors &&
305 !CUP$CDLParser$actions.errors &&
306 !CUP$MDLParser$actions.errors;
309 // if verbosity is on, then output parse trees as .dot files
310 if (success && state.verbose > 0) {
312 FileOutputStream dotfile;
314 dotfile = new FileOutputStream(state.infile + ".struct.dot");
315 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
318 dotfile = new FileOutputStream(state.infile + ".model.dot");
319 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
322 dotfile = new FileOutputStream(state.infile + ".space.dot");
323 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
326 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
327 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
329 } catch (Exception e) {
339 public static boolean scan(State state) {
340 FileInputStream infile = null;
342 boolean errors = false;
343 String files[] = { new String(state.infile + ".struct"),
344 new String(state.infile + ".model"),
345 new String(state.infile + ".constraints"),
346 new String(state.infile + ".space") };
350 for (int i = 0; i < files.length; i++) {
352 String filename = files[i];
355 infile = new FileInputStream(filename);
356 } catch (FileNotFoundException fnfe) {
357 System.err.println("Unable to open file: " + filename);
361 lexer = new Lexer(infile);
366 java_cup.runtime.Symbol symbol;
368 symbol = lexer.next_token();
370 if (symbol.sym == Sym.EOF) {
372 } else if (symbol.sym == Sym.BAD) {
376 if (State.verbose > 2) {
377 System.out.println("Got token: " + symbol.value);
380 } catch (Exception e) {