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("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
44 if (cli.infile == null) {
45 System.err.println("\nError: no input file specified");
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 Termination termination=null;
59 /* Check partition constraints */
60 (new ImplicitSchema(state)).update();
61 termination=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");
89 // do model optimizations
90 //(new Optimizer(state)).optimize();
92 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
93 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
94 RepairGenerator wg = new RepairGenerator(state,termination);
95 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
99 WorklistGenerator ng = new WorklistGenerator(state);
100 SetInclusion.worklist=true;
101 RelationInclusion.worklist=true;
105 } catch (Exception e) {
111 System.out.println("Compilation of " + state.infile + " successful.");
112 System.out.println("#SUCCESS#");
116 private static void printArgInfo(CLI cli) {
118 System.out.println("Printing debugging information...");
119 System.out.println("Input filename: " + cli.infile);
120 System.out.println("Output filename: " + cli.outfile);
122 for (int i = 0; i < cli.opts.length; i++) {
124 System.out.println("Optimization");
129 for (int i = 0; i < cli.extraopts.size(); i++) {
130 System.err.println("Warning: optimization \"" +
131 cli.extraopts.elementAt(i) +
132 "\" not recognized");
135 for (int i = 0; i < cli.extras.size(); i++) {
136 System.err.println("Warning: option \"" +
137 cli.extras.elementAt(i) +
138 "\" not recognized");
142 private static boolean error(State state, String error) {
143 System.err.println(error);
145 System.out.println("#ERROR#");
151 public static boolean semantics(State state) {
152 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
153 SemanticChecker checker = new SemanticChecker();
157 ok = checker.check(state, er);
158 } catch (Exception e) {
159 er.report(null, e.toString());
165 er.report(null, "Semantic check failed.");
168 System.out.print(er.toString());
173 public static void debugMessage(int level, String s) {
174 if (State.currentState.verbose >= level) {
175 System.err.println(s);
179 public static boolean parse(State state) {
181 /* parse structure file */
183 debugMessage(1, "Parsing structure file");
185 FileInputStream infile = new FileInputStream(state.infile + ".struct");
186 TDLParser parser = new TDLParser(new Lexer(infile));
187 parser.filename = state.infile + ".struct";
188 CUP$TDLParser$actions.debug = state.verbose > 1 ;
189 state.ptStructures = (ParseNode) parser.parse().value;
190 } catch (FileNotFoundException fnfe) {
191 System.err.println("Unable to open file: " + state.infile + ".struct");
193 } catch (Exception e) {
194 // System.out.println(e);
195 // e.printStackTrace();
199 /* parse model file */
201 debugMessage(1, "Parsing model file");
203 FileInputStream infile = new FileInputStream(state.infile + ".model");
204 MDLParser parser = new MDLParser(new Lexer(infile));
205 parser.filename = state.infile + ".model";
206 CUP$MDLParser$actions.debug = state.verbose > 1 ;
207 state.ptModel = (ParseNode) parser.parse().value;
208 } catch (FileNotFoundException fnfe) {
209 System.err.println("Unable to open file: " + state.infile + ".model");
211 } catch (Exception e) {
212 // System.out.println(e);
213 // e.printStackTrace();
217 /* parse space file */
219 debugMessage(1, "Parsing space file");
221 FileInputStream infile = new FileInputStream(state.infile + ".space");
222 SDLParser parser = new SDLParser(new Lexer(infile));
223 parser.filename = state.infile + ".space";
224 CUP$SDLParser$actions.debug = state.verbose > 1 ;
225 state.ptSpace = (ParseNode) parser.parse().value;
226 } catch (FileNotFoundException fnfe) {
227 System.err.println("Unable to open file: " + state.infile + ".space");
229 } catch (Exception e) {
230 System.out.println(e);
235 /* parse constraints file */
237 debugMessage(1, "Parsing constraints file");
239 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
240 CDLParser parser = new CDLParser(new Lexer(infile));
241 parser.filename = state.infile + ".constraints";
242 CUP$CDLParser$actions.debug = state.verbose > 1 ;
243 state.ptConstraints = (ParseNode) parser.parse().value;
244 } catch (FileNotFoundException fnfe) {
245 System.err.println("Unable to open file: " + state.infile + ".constraints");
247 } catch (Exception e) {
248 // System.out.println(e);
249 // e.printStackTrace();
254 !CUP$TDLParser$actions.errors &&
255 !CUP$SDLParser$actions.errors &&
256 !CUP$CDLParser$actions.errors &&
257 !CUP$MDLParser$actions.errors;
260 // if verbosity is on, then output parse trees as .dot files
261 if (success && state.verbose > 0) {
263 FileOutputStream dotfile;
265 dotfile = new FileOutputStream(state.infile + ".struct.dot");
266 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
269 dotfile = new FileOutputStream(state.infile + ".model.dot");
270 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
273 dotfile = new FileOutputStream(state.infile + ".space.dot");
274 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
277 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
278 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
280 } catch (Exception e) {
290 public static boolean scan(State state) {
291 FileInputStream infile = null;
293 boolean errors = false;
294 String files[] = { new String(state.infile + ".struct"),
295 new String(state.infile + ".model"),
296 new String(state.infile + ".constraints"),
297 new String(state.infile + ".space") };
301 for (int i = 0; i < files.length; i++) {
303 String filename = files[i];
306 infile = new FileInputStream(filename);
307 } catch (FileNotFoundException fnfe) {
308 System.err.println("Unable to open file: " + filename);
312 lexer = new Lexer(infile);
317 java_cup.runtime.Symbol symbol;
319 symbol = lexer.next_token();
321 if (symbol.sym == Sym.EOF) {
323 } else if (symbol.sym == Sym.BAD) {
327 if (State.verbose > 2) {
328 System.out.println("Got token: " + symbol.value);
331 } catch (Exception e) {