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 void main(String[] args) {
30 boolean success = true;
33 printArgInfo(cli); // prints debugging information and warning
36 State.currentState = state;
37 State.debug = cli.debug;
38 State.verbose = cli.verbose;
39 State.infile = cli.infile;
40 State.outfile = cli.outfile;
43 * added: terminates with an error message if no input file
44 * specified at command line
47 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
49 if (cli.infile == null) {
50 System.err.println("\nError: no input file specified");
55 System.out.println("Compiling " + cli.infile + ".");
58 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
59 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
60 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
63 Termination termination=null;
64 /* Check partition constraints */
65 (new ImplicitSchema(state)).update();
66 termination=new Termination(state);
69 (new DependencyBuilder(state)).calculate();
72 Vector nodes = new Vector(state.constraintnodes.values());
73 nodes.addAll(state.rulenodes.values());
75 FileOutputStream dotfile;
76 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
77 GraphNode.useEdgeLabels = true;
78 GraphNode.DOTVisitor.visit(dotfile, nodes);
81 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
82 GraphNode.useEdgeLabels = false;
83 GraphNode.DOTVisitor.visit(dotfile, nodes);
85 } catch (Exception e) {
91 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
94 // do model optimizations
95 //(new Optimizer(state)).optimize();
97 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
98 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
99 RepairGenerator wg = new RepairGenerator(state,termination);
100 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
104 WorklistGenerator ng = new WorklistGenerator(state);
105 SetInclusion.worklist=true;
106 RelationInclusion.worklist=true;
110 } catch (Exception e) {
116 System.out.println("Compilation of " + state.infile + " successful.");
117 System.out.println("#SUCCESS#");
121 private static void printArgInfo(CLI cli) {
123 System.out.println("Printing debugging information...");
124 System.out.println("Input filename: " + cli.infile);
125 System.out.println("Output filename: " + cli.outfile);
127 for (int i = 0; i < cli.opts.length; i++) {
129 System.out.println("Optimization");
134 for (int i = 0; i < cli.extraopts.size(); i++) {
135 System.err.println("Warning: optimization \"" +
136 cli.extraopts.elementAt(i) +
137 "\" not recognized");
140 for (int i = 0; i < cli.extras.size(); i++) {
141 System.err.println("Warning: option \"" +
142 cli.extras.elementAt(i) +
143 "\" not recognized");
147 private static boolean error(State state, String error) {
148 System.err.println(error);
150 System.out.println("#ERROR#");
156 public static boolean semantics(State state) {
157 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
158 SemanticChecker checker = new SemanticChecker();
162 ok = checker.check(state, er);
163 } catch (Exception e) {
164 er.report(null, e.toString());
170 er.report(null, "Semantic check failed.");
173 System.out.print(er.toString());
178 public static void debugMessage(int level, String s) {
179 if (State.currentState.verbose >= level) {
180 System.err.println(s);
184 public static boolean parse(State state) {
186 /* parse structure file */
188 debugMessage(1, "Parsing structure file");
190 FileInputStream infile = new FileInputStream(state.infile + ".struct");
191 TDLParser parser = new TDLParser(new Lexer(infile));
192 parser.filename = state.infile + ".struct";
193 CUP$TDLParser$actions.debug = state.verbose > 1 ;
194 state.ptStructures = (ParseNode) parser.parse().value;
195 } catch (FileNotFoundException fnfe) {
196 System.err.println("Unable to open file: " + state.infile + ".struct");
198 } catch (Exception e) {
199 System.out.println(e);
204 /* parse model file */
206 debugMessage(1, "Parsing model file");
208 FileInputStream infile = new FileInputStream(state.infile + ".model");
209 MDLParser parser = new MDLParser(new Lexer(infile));
210 parser.filename = state.infile + ".model";
211 CUP$MDLParser$actions.debug = state.verbose > 1 ;
212 state.ptModel = (ParseNode) parser.parse().value;
213 } catch (FileNotFoundException fnfe) {
214 System.err.println("Unable to open file: " + state.infile + ".model");
216 } catch (Exception e) {
217 System.out.println(e);
222 /* parse space file */
224 debugMessage(1, "Parsing space file");
226 FileInputStream infile = new FileInputStream(state.infile + ".space");
227 SDLParser parser = new SDLParser(new Lexer(infile));
228 parser.filename = state.infile + ".space";
229 CUP$SDLParser$actions.debug = state.verbose > 1 ;
230 state.ptSpace = (ParseNode) parser.parse().value;
231 } catch (FileNotFoundException fnfe) {
232 System.err.println("Unable to open file: " + state.infile + ".space");
234 } catch (Exception e) {
235 System.out.println(e);
240 /* parse constraints file */
242 debugMessage(1, "Parsing constraints file");
244 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
245 CDLParser parser = new CDLParser(new Lexer(infile));
246 parser.filename = state.infile + ".constraints";
247 CUP$CDLParser$actions.debug = state.verbose > 1 ;
248 state.ptConstraints = (ParseNode) parser.parse().value;
249 } catch (FileNotFoundException fnfe) {
250 System.err.println("Unable to open file: " + state.infile + ".constraints");
252 } catch (Exception e) {
253 System.out.println(e);
259 !CUP$TDLParser$actions.errors &&
260 !CUP$SDLParser$actions.errors &&
261 !CUP$CDLParser$actions.errors &&
262 !CUP$MDLParser$actions.errors;
265 // if verbosity is on, then output parse trees as .dot files
266 if (success && state.verbose > 0) {
268 FileOutputStream dotfile;
270 dotfile = new FileOutputStream(state.infile + ".struct.dot");
271 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
274 dotfile = new FileOutputStream(state.infile + ".model.dot");
275 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
278 dotfile = new FileOutputStream(state.infile + ".space.dot");
279 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
282 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
283 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
285 } catch (Exception e) {
295 public static boolean scan(State state) {
296 FileInputStream infile = null;
298 boolean errors = false;
299 String files[] = { new String(state.infile + ".struct"),
300 new String(state.infile + ".model"),
301 new String(state.infile + ".constraints"),
302 new String(state.infile + ".space") };
306 for (int i = 0; i < files.length; i++) {
308 String filename = files[i];
311 infile = new FileInputStream(filename);
312 } catch (FileNotFoundException fnfe) {
313 System.err.println("Unable to open file: " + filename);
317 lexer = new Lexer(infile);
322 java_cup.runtime.Symbol symbol;
324 symbol = lexer.next_token();
326 if (symbol.sym == Sym.EOF) {
328 } else if (symbol.sym == Sym.BAD) {
332 if (State.verbose > 2) {
333 System.out.println("Got token: " + symbol.value);
336 } catch (Exception e) {