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 /* Set this flag to false to turn repairs off */
21 public static boolean REPAIR=true;
23 public static void main(String[] args) {
25 boolean success = true;
28 printArgInfo(cli); // prints debugging information and warning
31 State.currentState = state;
32 State.debug = cli.debug;
33 State.verbose = cli.verbose;
34 State.infile = cli.infile;
35 State.outfile = cli.outfile;
38 * added: terminates with an error message if no input file
39 * specified at command line
42 System.out.println("\nMCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)");
44 if (cli.infile == null) {
45 System.err.println("\nError: no input file specified");
49 if (cli.target == CLI.ASSEMBLY || cli.target == CLI.DEFAULT) {
51 System.out.println("Compiling " + cli.infile + ".");
54 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
55 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
56 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
59 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);
103 SetInclusion.worklist=true;
104 RelationInclusion.worklist=true;
108 } catch (Exception e) {
114 System.out.println("Compilation of " + state.infile + " successful.");
115 System.out.println("#SUCCESS#");
117 } else if (cli.target == CLI.INTER) {
119 System.out.println("Semantic analysis for " + cli.infile + ".");
122 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
123 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
124 success = semantics(state) || error(state, "Semantic analysis failed.");
127 System.out.println("Semantic analysis of " + state.infile + " successful.");
128 System.out.println("#SUCCESS#");
130 } else if (cli.target == CLI.PARSE) {
132 System.out.println("Parsing " + cli.infile + ".");
135 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
136 success = parse(state) || error(state, "Parsing failed.");
139 System.out.println("Parsing of " + state.infile + " successful.");
140 System.out.println("#SUCCESS#");
142 } else if (cli.target == CLI.SCAN) {
144 System.out.println("Scanning " + cli.infile + ".");
147 success = scan(state) || error(state, "Scanning failed.");
150 System.out.println("Scanning of " + state.infile + " successful.");
151 System.out.println("#SUCCESS#");
156 private static void printArgInfo(CLI cli) {
158 System.out.println("Printing debugging information...");
159 System.out.println("Input filename: " + cli.infile);
160 System.out.println("Output filename: " + cli.outfile);
161 System.out.print("Target: ");
165 System.out.println("ASSEMBLY");
168 System.out.println("DEFAULT");
171 System.out.println("INTER");
174 System.out.println("LOWIR");
177 System.out.println("PARSE");
180 System.out.println("SCAN");
183 System.out.println("not recognized");
187 for (int i = 0; i < cli.opts.length; i++) {
189 System.out.println("Optimization");
194 for (int i = 0; i < cli.extraopts.size(); i++) {
195 System.err.println("Warning: optimization \"" +
196 cli.extraopts.elementAt(i) +
197 "\" not recognized");
200 for (int i = 0; i < cli.extras.size(); i++) {
201 System.err.println("Warning: option \"" +
202 cli.extras.elementAt(i) +
203 "\" not recognized");
207 private static boolean error(State state, String error) {
208 System.err.println(error);
210 System.out.println("#ERROR#");
216 public static boolean semantics(State state) {
217 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
218 SemanticChecker checker = new SemanticChecker();
222 ok = checker.check(state, er);
223 } catch (Exception e) {
224 er.report(null, e.toString());
230 er.report(null, "Semantic check failed.");
233 System.out.print(er.toString());
238 public static void debugMessage(int level, String s) {
239 if (State.currentState.verbose >= level) {
240 System.err.println(s);
244 public static boolean parse(State state) {
246 /* parse structure file */
248 debugMessage(1, "Parsing structure file");
250 FileInputStream infile = new FileInputStream(state.infile + ".struct");
251 TDLParser parser = new TDLParser(new Lexer(infile));
252 parser.filename = state.infile + ".struct";
253 CUP$TDLParser$actions.debug = state.verbose > 1 ;
254 state.ptStructures = (ParseNode) parser.parse().value;
255 } catch (FileNotFoundException fnfe) {
256 System.err.println("Unable to open file: " + state.infile + ".struct");
258 } catch (Exception e) {
259 // System.out.println(e);
260 // e.printStackTrace();
264 /* parse model file */
266 debugMessage(1, "Parsing model file");
268 FileInputStream infile = new FileInputStream(state.infile + ".model");
269 MDLParser parser = new MDLParser(new Lexer(infile));
270 parser.filename = state.infile + ".model";
271 CUP$MDLParser$actions.debug = state.verbose > 1 ;
272 state.ptModel = (ParseNode) parser.parse().value;
273 } catch (FileNotFoundException fnfe) {
274 System.err.println("Unable to open file: " + state.infile + ".model");
276 } catch (Exception e) {
277 // System.out.println(e);
278 // e.printStackTrace();
282 /* parse space file */
284 debugMessage(1, "Parsing space file");
286 FileInputStream infile = new FileInputStream(state.infile + ".space");
287 SDLParser parser = new SDLParser(new Lexer(infile));
288 parser.filename = state.infile + ".space";
289 CUP$SDLParser$actions.debug = state.verbose > 1 ;
290 state.ptSpace = (ParseNode) parser.parse().value;
291 } catch (FileNotFoundException fnfe) {
292 System.err.println("Unable to open file: " + state.infile + ".space");
294 } catch (Exception e) {
295 System.out.println(e);
300 /* parse constraints file */
302 debugMessage(1, "Parsing constraints file");
304 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
305 CDLParser parser = new CDLParser(new Lexer(infile));
306 parser.filename = state.infile + ".constraints";
307 CUP$CDLParser$actions.debug = state.verbose > 1 ;
308 state.ptConstraints = (ParseNode) parser.parse().value;
309 } catch (FileNotFoundException fnfe) {
310 System.err.println("Unable to open file: " + state.infile + ".constraints");
312 } catch (Exception e) {
313 // System.out.println(e);
314 // e.printStackTrace();
319 !CUP$TDLParser$actions.errors &&
320 !CUP$SDLParser$actions.errors &&
321 !CUP$CDLParser$actions.errors &&
322 !CUP$MDLParser$actions.errors;
325 // if verbosity is on, then output parse trees as .dot files
326 if (success && state.verbose > 0) {
328 FileOutputStream dotfile;
330 dotfile = new FileOutputStream(state.infile + ".struct.dot");
331 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
334 dotfile = new FileOutputStream(state.infile + ".model.dot");
335 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
338 dotfile = new FileOutputStream(state.infile + ".space.dot");
339 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
342 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
343 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
345 } catch (Exception e) {
355 public static boolean scan(State state) {
356 FileInputStream infile = null;
358 boolean errors = false;
359 String files[] = { new String(state.infile + ".struct"),
360 new String(state.infile + ".model"),
361 new String(state.infile + ".constraints"),
362 new String(state.infile + ".space") };
366 for (int i = 0; i < files.length; i++) {
368 String filename = files[i];
371 infile = new FileInputStream(filename);
372 } catch (FileNotFoundException fnfe) {
373 System.err.println("Unable to open file: " + filename);
377 lexer = new Lexer(infile);
382 java_cup.runtime.Symbol symbol;
384 symbol = lexer.next_token();
386 if (symbol.sym == Sym.EOF) {
388 } else if (symbol.sym == Sym.BAD) {
392 if (State.verbose > 2) {
393 System.out.println("Got token: " + symbol.value);
396 } catch (Exception e) {