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;
24 public static boolean GENERATEDEBUGHOOKS=false;
25 public static boolean GENERATEDEBUGPRINT=false;
26 public static boolean GENERATEINSTRUMENT=false;
28 public static Vector debuggraphs=new Vector();
30 public static void main(String[] args) {
32 boolean success = true;
35 printArgInfo(cli); // prints debugging information and warning
38 State.currentState = state;
39 State.debug = cli.debug;
40 State.verbose = cli.verbose;
41 State.infile = cli.infile;
42 State.outfile = cli.outfile;
45 * added: terminates with an error message if no input file
46 * specified at command line
49 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
51 if (cli.infile == null) {
52 System.err.println("\nError: no input file specified");
57 System.out.println("Compiling " + cli.infile + ".");
60 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
61 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
62 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
65 Termination termination=null;
66 /* Check partition constraints */
67 (new ImplicitSchema(state)).update();
68 termination=new Termination(state);
71 (new DependencyBuilder(state)).calculate();
74 Vector nodes = new Vector(state.constraintnodes.values());
75 nodes.addAll(state.rulenodes.values());
77 FileOutputStream dotfile;
78 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
79 GraphNode.useEdgeLabels = true;
80 GraphNode.DOTVisitor.visit(dotfile, nodes);
83 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
84 GraphNode.useEdgeLabels = false;
85 GraphNode.DOTVisitor.visit(dotfile, nodes);
87 } catch (Exception e) {
93 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
96 // do model optimizations
97 //(new Optimizer(state)).optimize();
99 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
100 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
101 RepairGenerator wg = new RepairGenerator(state,termination);
102 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
106 WorklistGenerator ng = new WorklistGenerator(state);
107 SetInclusion.worklist=true;
108 RelationInclusion.worklist=true;
112 } catch (Exception e) {
118 System.out.println("Compilation of " + state.infile + " successful.");
119 System.out.println("#SUCCESS#");
123 private static void printArgInfo(CLI cli) {
125 System.out.println("Printing debugging information...");
126 System.out.println("Input filename: " + cli.infile);
127 System.out.println("Output filename: " + cli.outfile);
129 for (int i = 0; i < cli.opts.length; i++) {
131 System.out.println("Optimization");
136 for (int i = 0; i < cli.extraopts.size(); i++) {
137 System.err.println("Warning: optimization \"" +
138 cli.extraopts.elementAt(i) +
139 "\" not recognized");
142 for (int i = 0; i < cli.extras.size(); i++) {
143 System.err.println("Warning: option \"" +
144 cli.extras.elementAt(i) +
145 "\" not recognized");
149 private static boolean error(State state, String error) {
150 System.err.println(error);
152 System.out.println("#ERROR#");
158 public static boolean semantics(State state) {
159 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
160 SemanticChecker checker = new SemanticChecker();
164 ok = checker.check(state, er);
165 } catch (Exception e) {
166 er.report(null, e.toString());
172 er.report(null, "Semantic check failed.");
175 System.out.print(er.toString());
180 public static void debugMessage(int level, String s) {
181 if (State.currentState.verbose >= level) {
182 System.err.println(s);
186 public static boolean parse(State state) {
188 /* parse structure file */
190 debugMessage(1, "Parsing structure file");
192 FileInputStream infile = new FileInputStream(state.infile + ".struct");
193 TDLParser parser = new TDLParser(new Lexer(infile));
194 parser.filename = state.infile + ".struct";
195 CUP$TDLParser$actions.debug = state.verbose > 1 ;
196 state.ptStructures = (ParseNode) parser.parse().value;
197 } catch (FileNotFoundException fnfe) {
198 System.err.println("Unable to open file: " + state.infile + ".struct");
200 } catch (Exception e) {
201 System.out.println(e);
206 /* parse model file */
208 debugMessage(1, "Parsing model file");
210 FileInputStream infile = new FileInputStream(state.infile + ".model");
211 MDLParser parser = new MDLParser(new Lexer(infile));
212 parser.filename = state.infile + ".model";
213 CUP$MDLParser$actions.debug = state.verbose > 1 ;
214 state.ptModel = (ParseNode) parser.parse().value;
215 } catch (FileNotFoundException fnfe) {
216 System.err.println("Unable to open file: " + state.infile + ".model");
218 } catch (Exception e) {
219 System.out.println(e);
224 /* parse space file */
226 debugMessage(1, "Parsing space file");
228 FileInputStream infile = new FileInputStream(state.infile + ".space");
229 SDLParser parser = new SDLParser(new Lexer(infile));
230 parser.filename = state.infile + ".space";
231 CUP$SDLParser$actions.debug = state.verbose > 1 ;
232 state.ptSpace = (ParseNode) parser.parse().value;
233 } catch (FileNotFoundException fnfe) {
234 System.err.println("Unable to open file: " + state.infile + ".space");
236 } catch (Exception e) {
237 System.out.println(e);
242 /* parse constraints file */
244 debugMessage(1, "Parsing constraints file");
246 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
247 CDLParser parser = new CDLParser(new Lexer(infile));
248 parser.filename = state.infile + ".constraints";
249 CUP$CDLParser$actions.debug = state.verbose > 1 ;
250 state.ptConstraints = (ParseNode) parser.parse().value;
251 } catch (FileNotFoundException fnfe) {
252 System.err.println("Unable to open file: " + state.infile + ".constraints");
254 } catch (Exception e) {
255 System.out.println(e);
261 !CUP$TDLParser$actions.errors &&
262 !CUP$SDLParser$actions.errors &&
263 !CUP$CDLParser$actions.errors &&
264 !CUP$MDLParser$actions.errors;
267 // if verbosity is on, then output parse trees as .dot files
268 if (success && state.verbose > 0) {
270 FileOutputStream dotfile;
272 dotfile = new FileOutputStream(state.infile + ".struct.dot");
273 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
276 dotfile = new FileOutputStream(state.infile + ".model.dot");
277 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
280 dotfile = new FileOutputStream(state.infile + ".space.dot");
281 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
284 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
285 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
287 } catch (Exception e) {
297 public static boolean scan(State state) {
298 FileInputStream infile = null;
300 boolean errors = false;
301 String files[] = { new String(state.infile + ".struct"),
302 new String(state.infile + ".model"),
303 new String(state.infile + ".constraints"),
304 new String(state.infile + ".space") };
308 for (int i = 0; i < files.length; i++) {
310 String filename = files[i];
313 infile = new FileInputStream(filename);
314 } catch (FileNotFoundException fnfe) {
315 System.err.println("Unable to open file: " + filename);
319 lexer = new Lexer(infile);
324 java_cup.runtime.Symbol symbol;
326 symbol = lexer.next_token();
328 if (symbol.sym == Sym.EOF) {
330 } else if (symbol.sym == Sym.BAD) {
334 if (State.verbose > 2) {
335 System.out.println("Got token: " + symbol.value);
338 } catch (Exception e) {