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;
22 public static boolean AGGRESSIVESEARCH=false;
24 public static void main(String[] args) {
26 boolean success = true;
29 printArgInfo(cli); // prints debugging information and warning
32 State.currentState = state;
33 State.debug = cli.debug;
34 State.verbose = cli.verbose;
35 State.infile = cli.infile;
36 State.outfile = cli.outfile;
39 * added: terminates with an error message if no input file
40 * specified at command line
43 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
45 if (cli.infile == null) {
46 System.err.println("\nError: no input file specified");
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();
93 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
94 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
95 RepairGenerator wg = new RepairGenerator(state,termination);
96 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
100 WorklistGenerator ng = new WorklistGenerator(state);
101 SetInclusion.worklist=true;
102 RelationInclusion.worklist=true;
106 } catch (Exception e) {
112 System.out.println("Compilation of " + state.infile + " successful.");
113 System.out.println("#SUCCESS#");
117 private static void printArgInfo(CLI cli) {
119 System.out.println("Printing debugging information...");
120 System.out.println("Input filename: " + cli.infile);
121 System.out.println("Output filename: " + cli.outfile);
123 for (int i = 0; i < cli.opts.length; i++) {
125 System.out.println("Optimization");
130 for (int i = 0; i < cli.extraopts.size(); i++) {
131 System.err.println("Warning: optimization \"" +
132 cli.extraopts.elementAt(i) +
133 "\" not recognized");
136 for (int i = 0; i < cli.extras.size(); i++) {
137 System.err.println("Warning: option \"" +
138 cli.extras.elementAt(i) +
139 "\" not recognized");
143 private static boolean error(State state, String error) {
144 System.err.println(error);
146 System.out.println("#ERROR#");
152 public static boolean semantics(State state) {
153 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
154 SemanticChecker checker = new SemanticChecker();
158 ok = checker.check(state, er);
159 } catch (Exception e) {
160 er.report(null, e.toString());
166 er.report(null, "Semantic check failed.");
169 System.out.print(er.toString());
174 public static void debugMessage(int level, String s) {
175 if (State.currentState.verbose >= level) {
176 System.err.println(s);
180 public static boolean parse(State state) {
182 /* parse structure file */
184 debugMessage(1, "Parsing structure file");
186 FileInputStream infile = new FileInputStream(state.infile + ".struct");
187 TDLParser parser = new TDLParser(new Lexer(infile));
188 parser.filename = state.infile + ".struct";
189 CUP$TDLParser$actions.debug = state.verbose > 1 ;
190 state.ptStructures = (ParseNode) parser.parse().value;
191 } catch (FileNotFoundException fnfe) {
192 System.err.println("Unable to open file: " + state.infile + ".struct");
194 } catch (Exception e) {
195 // System.out.println(e);
196 // e.printStackTrace();
200 /* parse model file */
202 debugMessage(1, "Parsing model file");
204 FileInputStream infile = new FileInputStream(state.infile + ".model");
205 MDLParser parser = new MDLParser(new Lexer(infile));
206 parser.filename = state.infile + ".model";
207 CUP$MDLParser$actions.debug = state.verbose > 1 ;
208 state.ptModel = (ParseNode) parser.parse().value;
209 } catch (FileNotFoundException fnfe) {
210 System.err.println("Unable to open file: " + state.infile + ".model");
212 } catch (Exception e) {
213 // System.out.println(e);
214 // e.printStackTrace();
218 /* parse space file */
220 debugMessage(1, "Parsing space file");
222 FileInputStream infile = new FileInputStream(state.infile + ".space");
223 SDLParser parser = new SDLParser(new Lexer(infile));
224 parser.filename = state.infile + ".space";
225 CUP$SDLParser$actions.debug = state.verbose > 1 ;
226 state.ptSpace = (ParseNode) parser.parse().value;
227 } catch (FileNotFoundException fnfe) {
228 System.err.println("Unable to open file: " + state.infile + ".space");
230 } catch (Exception e) {
231 System.out.println(e);
236 /* parse constraints file */
238 debugMessage(1, "Parsing constraints file");
240 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
241 CDLParser parser = new CDLParser(new Lexer(infile));
242 parser.filename = state.infile + ".constraints";
243 CUP$CDLParser$actions.debug = state.verbose > 1 ;
244 state.ptConstraints = (ParseNode) parser.parse().value;
245 } catch (FileNotFoundException fnfe) {
246 System.err.println("Unable to open file: " + state.infile + ".constraints");
248 } catch (Exception e) {
249 // System.out.println(e);
250 // e.printStackTrace();
255 !CUP$TDLParser$actions.errors &&
256 !CUP$SDLParser$actions.errors &&
257 !CUP$CDLParser$actions.errors &&
258 !CUP$MDLParser$actions.errors;
261 // if verbosity is on, then output parse trees as .dot files
262 if (success && state.verbose > 0) {
264 FileOutputStream dotfile;
266 dotfile = new FileOutputStream(state.infile + ".struct.dot");
267 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
270 dotfile = new FileOutputStream(state.infile + ".model.dot");
271 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
274 dotfile = new FileOutputStream(state.infile + ".space.dot");
275 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
278 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
279 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
281 } catch (Exception e) {
291 public static boolean scan(State state) {
292 FileInputStream infile = null;
294 boolean errors = false;
295 String files[] = { new String(state.infile + ".struct"),
296 new String(state.infile + ".model"),
297 new String(state.infile + ".constraints"),
298 new String(state.infile + ".space") };
302 for (int i = 0; i < files.length; i++) {
304 String filename = files[i];
307 infile = new FileInputStream(filename);
308 } catch (FileNotFoundException fnfe) {
309 System.err.println("Unable to open file: " + filename);
313 lexer = new Lexer(infile);
318 java_cup.runtime.Symbol symbol;
320 symbol = lexer.next_token();
322 if (symbol.sym == Sym.EOF) {
324 } else if (symbol.sym == Sym.BAD) {
328 if (State.verbose > 2) {
329 System.out.println("Got token: " + symbol.value);
332 } catch (Exception e) {