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 (new ImplicitSchema(state)).update();
59 Termination t=new Termination(state);
62 (new DependencyBuilder(state)).calculate();
65 Vector nodes = new Vector(state.constraintnodes.values());
66 nodes.addAll(state.rulenodes.values());
68 FileOutputStream dotfile;
69 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
70 GraphNode.useEdgeLabels = true;
71 GraphNode.DOTVisitor.visit(dotfile, nodes);
74 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
75 GraphNode.useEdgeLabels = false;
76 GraphNode.DOTVisitor.visit(dotfile, nodes);
78 } catch (Exception e) {
84 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
86 // do model optimizations
87 //(new Optimizer(state)).optimize();
89 NaiveGenerator ng = new NaiveGenerator(state);
91 //WorklistGenerator wg = new WorklistGenerator(state);
94 } catch (Exception e) {
100 System.out.println("Compilation of " + state.infile + " successful.");
101 System.out.println("#SUCCESS#");
103 } else if (cli.target == CLI.INTER) {
105 System.out.println("Semantic analysis for " + cli.infile + ".");
108 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
109 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
110 success = semantics(state) || error(state, "Semantic analysis failed.");
113 System.out.println("Semantic analysis of " + state.infile + " successful.");
114 System.out.println("#SUCCESS#");
116 } else if (cli.target == CLI.PARSE) {
118 System.out.println("Parsing " + cli.infile + ".");
121 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
122 success = parse(state) || error(state, "Parsing failed.");
125 System.out.println("Parsing of " + state.infile + " successful.");
126 System.out.println("#SUCCESS#");
128 } else if (cli.target == CLI.SCAN) {
130 System.out.println("Scanning " + cli.infile + ".");
133 success = scan(state) || error(state, "Scanning failed.");
136 System.out.println("Scanning of " + state.infile + " successful.");
137 System.out.println("#SUCCESS#");
142 private static void printArgInfo(CLI cli) {
144 System.out.println("Printing debugging information...");
145 System.out.println("Input filename: " + cli.infile);
146 System.out.println("Output filename: " + cli.outfile);
147 System.out.print("Target: ");
151 System.out.println("ASSEMBLY");
154 System.out.println("DEFAULT");
157 System.out.println("INTER");
160 System.out.println("LOWIR");
163 System.out.println("PARSE");
166 System.out.println("SCAN");
169 System.out.println("not recognized");
173 for (int i = 0; i < cli.opts.length; i++) {
175 System.out.println("Optimization");
180 for (int i = 0; i < cli.extraopts.size(); i++) {
181 System.err.println("Warning: optimization \"" +
182 cli.extraopts.elementAt(i) +
183 "\" not recognized");
186 for (int i = 0; i < cli.extras.size(); i++) {
187 System.err.println("Warning: option \"" +
188 cli.extras.elementAt(i) +
189 "\" not recognized");
193 private static boolean error(State state, String error) {
194 System.err.println(error);
196 System.out.println("#ERROR#");
202 public static boolean semantics(State state) {
203 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
204 SemanticChecker checker = new SemanticChecker();
208 ok = checker.check(state, er);
209 } catch (Exception e) {
210 er.report(null, e.toString());
216 er.report(null, "Semantic check failed.");
219 System.out.print(er.toString());
224 public static void debugMessage(int level, String s) {
225 if (State.currentState.verbose >= level) {
226 System.err.println(s);
230 public static boolean parse(State state) {
232 /* parse structure file */
234 debugMessage(1, "Parsing structure file");
236 FileInputStream infile = new FileInputStream(state.infile + ".struct");
237 TDLParser parser = new TDLParser(new Lexer(infile));
238 CUP$TDLParser$actions.debug = state.verbose > 1 ;
239 state.ptStructures = (ParseNode) parser.parse().value;
240 } catch (FileNotFoundException fnfe) {
241 System.err.println("Unable to open file: " + state.infile + ".struct");
243 } catch (Exception e) {
244 // System.out.println(e);
245 // e.printStackTrace();
249 /* parse model file */
251 debugMessage(1, "Parsing model file");
253 FileInputStream infile = new FileInputStream(state.infile + ".model");
254 MDLParser parser = new MDLParser(new Lexer(infile));
255 CUP$MDLParser$actions.debug = state.verbose > 1 ;
256 state.ptModel = (ParseNode) parser.parse().value;
257 } catch (FileNotFoundException fnfe) {
258 System.err.println("Unable to open file: " + state.infile + ".model");
260 } catch (Exception e) {
261 // System.out.println(e);
262 // e.printStackTrace();
266 /* parse space file */
268 debugMessage(1, "Parsing space file");
270 FileInputStream infile = new FileInputStream(state.infile + ".space");
271 SDLParser parser = new SDLParser(new Lexer(infile));
272 CUP$SDLParser$actions.debug = state.verbose > 1 ;
273 state.ptSpace = (ParseNode) parser.parse().value;
274 } catch (FileNotFoundException fnfe) {
275 System.err.println("Unable to open file: " + state.infile + ".space");
277 } catch (Exception e) {
278 // System.out.println(e);
279 // e.printStackTrace();
283 /* parse constraints file */
285 debugMessage(1, "Parsing constraints file");
287 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
288 CDLParser parser = new CDLParser(new Lexer(infile));
289 CUP$CDLParser$actions.debug = state.verbose > 1 ;
290 state.ptConstraints = (ParseNode) parser.parse().value;
291 } catch (FileNotFoundException fnfe) {
292 System.err.println("Unable to open file: " + state.infile + ".constraints");
294 } catch (Exception e) {
295 // System.out.println(e);
296 // e.printStackTrace();
301 !CUP$TDLParser$actions.errors &&
302 !CUP$SDLParser$actions.errors &&
303 !CUP$CDLParser$actions.errors &&
304 !CUP$MDLParser$actions.errors;
307 // if verbosity is on, then output parse trees as .dot files
308 if (success && state.verbose > 0) {
310 FileOutputStream dotfile;
312 dotfile = new FileOutputStream(state.infile + ".struct.dot");
313 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
316 dotfile = new FileOutputStream(state.infile + ".model.dot");
317 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
320 dotfile = new FileOutputStream(state.infile + ".space.dot");
321 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
324 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
325 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
327 } catch (Exception e) {
337 public static boolean scan(State state) {
338 FileInputStream infile = null;
340 boolean errors = false;
341 String files[] = { new String(state.infile + ".struct"),
342 new String(state.infile + ".model"),
343 new String(state.infile + ".constraints"),
344 new String(state.infile + ".space") };
348 for (int i = 0; i < files.length; i++) {
350 String filename = files[i];
353 infile = new FileInputStream(filename);
354 } catch (FileNotFoundException fnfe) {
355 System.err.println("Unable to open file: " + filename);
359 lexer = new Lexer(infile);
364 java_cup.runtime.Symbol symbol;
366 symbol = lexer.next_token();
368 if (symbol.sym == Sym.EOF) {
370 } else if (symbol.sym == Sym.BAD) {
374 if (State.verbose > 2) {
375 System.out.println("Got token: " + symbol.value);
378 } catch (Exception e) {