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;
23 public static boolean PRUNEQUANTIFIERS=false;
25 public static void main(String[] args) {
27 boolean success = true;
30 printArgInfo(cli); // prints debugging information and warning
33 State.currentState = state;
34 State.debug = cli.debug;
35 State.verbose = cli.verbose;
36 State.infile = cli.infile;
37 State.outfile = cli.outfile;
40 * added: terminates with an error message if no input file
41 * specified at command line
44 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
46 if (cli.infile == null) {
47 System.err.println("\nError: no input file specified");
52 System.out.println("Compiling " + cli.infile + ".");
55 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
56 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
57 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
60 Termination termination=null;
61 /* Check partition constraints */
62 (new ImplicitSchema(state)).update();
63 termination=new Termination(state);
66 (new DependencyBuilder(state)).calculate();
69 Vector nodes = new Vector(state.constraintnodes.values());
70 nodes.addAll(state.rulenodes.values());
72 FileOutputStream dotfile;
73 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
74 GraphNode.useEdgeLabels = true;
75 GraphNode.DOTVisitor.visit(dotfile, nodes);
78 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
79 GraphNode.useEdgeLabels = false;
80 GraphNode.DOTVisitor.visit(dotfile, nodes);
82 } catch (Exception e) {
88 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
91 // do model optimizations
92 //(new Optimizer(state)).optimize();
94 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
95 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
96 RepairGenerator wg = new RepairGenerator(state,termination);
97 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
101 WorklistGenerator ng = new WorklistGenerator(state);
102 SetInclusion.worklist=true;
103 RelationInclusion.worklist=true;
107 } catch (Exception e) {
113 System.out.println("Compilation of " + state.infile + " successful.");
114 System.out.println("#SUCCESS#");
118 private static void printArgInfo(CLI cli) {
120 System.out.println("Printing debugging information...");
121 System.out.println("Input filename: " + cli.infile);
122 System.out.println("Output filename: " + cli.outfile);
124 for (int i = 0; i < cli.opts.length; i++) {
126 System.out.println("Optimization");
131 for (int i = 0; i < cli.extraopts.size(); i++) {
132 System.err.println("Warning: optimization \"" +
133 cli.extraopts.elementAt(i) +
134 "\" not recognized");
137 for (int i = 0; i < cli.extras.size(); i++) {
138 System.err.println("Warning: option \"" +
139 cli.extras.elementAt(i) +
140 "\" not recognized");
144 private static boolean error(State state, String error) {
145 System.err.println(error);
147 System.out.println("#ERROR#");
153 public static boolean semantics(State state) {
154 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
155 SemanticChecker checker = new SemanticChecker();
159 ok = checker.check(state, er);
160 } catch (Exception e) {
161 er.report(null, e.toString());
167 er.report(null, "Semantic check failed.");
170 System.out.print(er.toString());
175 public static void debugMessage(int level, String s) {
176 if (State.currentState.verbose >= level) {
177 System.err.println(s);
181 public static boolean parse(State state) {
183 /* parse structure file */
185 debugMessage(1, "Parsing structure file");
187 FileInputStream infile = new FileInputStream(state.infile + ".struct");
188 TDLParser parser = new TDLParser(new Lexer(infile));
189 parser.filename = state.infile + ".struct";
190 CUP$TDLParser$actions.debug = state.verbose > 1 ;
191 state.ptStructures = (ParseNode) parser.parse().value;
192 } catch (FileNotFoundException fnfe) {
193 System.err.println("Unable to open file: " + state.infile + ".struct");
195 } catch (Exception e) {
196 // System.out.println(e);
197 // e.printStackTrace();
201 /* parse model file */
203 debugMessage(1, "Parsing model file");
205 FileInputStream infile = new FileInputStream(state.infile + ".model");
206 MDLParser parser = new MDLParser(new Lexer(infile));
207 parser.filename = state.infile + ".model";
208 CUP$MDLParser$actions.debug = state.verbose > 1 ;
209 state.ptModel = (ParseNode) parser.parse().value;
210 } catch (FileNotFoundException fnfe) {
211 System.err.println("Unable to open file: " + state.infile + ".model");
213 } catch (Exception e) {
214 // System.out.println(e);
215 // e.printStackTrace();
219 /* parse space file */
221 debugMessage(1, "Parsing space file");
223 FileInputStream infile = new FileInputStream(state.infile + ".space");
224 SDLParser parser = new SDLParser(new Lexer(infile));
225 parser.filename = state.infile + ".space";
226 CUP$SDLParser$actions.debug = state.verbose > 1 ;
227 state.ptSpace = (ParseNode) parser.parse().value;
228 } catch (FileNotFoundException fnfe) {
229 System.err.println("Unable to open file: " + state.infile + ".space");
231 } catch (Exception e) {
232 System.out.println(e);
237 /* parse constraints file */
239 debugMessage(1, "Parsing constraints file");
241 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
242 CDLParser parser = new CDLParser(new Lexer(infile));
243 parser.filename = state.infile + ".constraints";
244 CUP$CDLParser$actions.debug = state.verbose > 1 ;
245 state.ptConstraints = (ParseNode) parser.parse().value;
246 } catch (FileNotFoundException fnfe) {
247 System.err.println("Unable to open file: " + state.infile + ".constraints");
249 } catch (Exception e) {
250 // System.out.println(e);
251 // e.printStackTrace();
256 !CUP$TDLParser$actions.errors &&
257 !CUP$SDLParser$actions.errors &&
258 !CUP$CDLParser$actions.errors &&
259 !CUP$MDLParser$actions.errors;
262 // if verbosity is on, then output parse trees as .dot files
263 if (success && state.verbose > 0) {
265 FileOutputStream dotfile;
267 dotfile = new FileOutputStream(state.infile + ".struct.dot");
268 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
271 dotfile = new FileOutputStream(state.infile + ".model.dot");
272 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
275 dotfile = new FileOutputStream(state.infile + ".space.dot");
276 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
279 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
280 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
282 } catch (Exception e) {
292 public static boolean scan(State state) {
293 FileInputStream infile = null;
295 boolean errors = false;
296 String files[] = { new String(state.infile + ".struct"),
297 new String(state.infile + ".model"),
298 new String(state.infile + ".constraints"),
299 new String(state.infile + ".space") };
303 for (int i = 0; i < files.length; i++) {
305 String filename = files[i];
308 infile = new FileInputStream(filename);
309 } catch (FileNotFoundException fnfe) {
310 System.err.println("Unable to open file: " + filename);
314 lexer = new Lexer(infile);
319 java_cup.runtime.Symbol symbol;
321 symbol = lexer.next_token();
323 if (symbol.sym == Sym.EOF) {
325 } else if (symbol.sym == Sym.BAD) {
329 if (State.verbose > 2) {
330 System.out.println("Got token: " + symbol.value);
333 } catch (Exception e) {