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;
27 public static void main(String[] args) {
29 boolean success = true;
32 printArgInfo(cli); // prints debugging information and warning
35 State.currentState = state;
36 State.debug = cli.debug;
37 State.verbose = cli.verbose;
38 State.infile = cli.infile;
39 State.outfile = cli.outfile;
42 * added: terminates with an error message if no input file
43 * specified at command line
46 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
48 if (cli.infile == null) {
49 System.err.println("\nError: no input file specified");
54 System.out.println("Compiling " + cli.infile + ".");
57 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
58 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
59 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
62 Termination termination=null;
63 /* Check partition constraints */
64 (new ImplicitSchema(state)).update();
65 termination=new Termination(state);
68 (new DependencyBuilder(state)).calculate();
71 Vector nodes = new Vector(state.constraintnodes.values());
72 nodes.addAll(state.rulenodes.values());
74 FileOutputStream dotfile;
75 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
76 GraphNode.useEdgeLabels = true;
77 GraphNode.DOTVisitor.visit(dotfile, nodes);
80 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
81 GraphNode.useEdgeLabels = false;
82 GraphNode.DOTVisitor.visit(dotfile, nodes);
84 } catch (Exception e) {
90 FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
93 // do model optimizations
94 //(new Optimizer(state)).optimize();
96 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
97 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
98 RepairGenerator wg = new RepairGenerator(state,termination);
99 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
103 WorklistGenerator ng = new WorklistGenerator(state);
104 SetInclusion.worklist=true;
105 RelationInclusion.worklist=true;
109 } catch (Exception e) {
115 System.out.println("Compilation of " + state.infile + " successful.");
116 System.out.println("#SUCCESS#");
120 private static void printArgInfo(CLI cli) {
122 System.out.println("Printing debugging information...");
123 System.out.println("Input filename: " + cli.infile);
124 System.out.println("Output filename: " + cli.outfile);
126 for (int i = 0; i < cli.opts.length; i++) {
128 System.out.println("Optimization");
133 for (int i = 0; i < cli.extraopts.size(); i++) {
134 System.err.println("Warning: optimization \"" +
135 cli.extraopts.elementAt(i) +
136 "\" not recognized");
139 for (int i = 0; i < cli.extras.size(); i++) {
140 System.err.println("Warning: option \"" +
141 cli.extras.elementAt(i) +
142 "\" not recognized");
146 private static boolean error(State state, String error) {
147 System.err.println(error);
149 System.out.println("#ERROR#");
155 public static boolean semantics(State state) {
156 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
157 SemanticChecker checker = new SemanticChecker();
161 ok = checker.check(state, er);
162 } catch (Exception e) {
163 er.report(null, e.toString());
169 er.report(null, "Semantic check failed.");
172 System.out.print(er.toString());
177 public static void debugMessage(int level, String s) {
178 if (State.currentState.verbose >= level) {
179 System.err.println(s);
183 public static boolean parse(State state) {
185 /* parse structure file */
187 debugMessage(1, "Parsing structure file");
189 FileInputStream infile = new FileInputStream(state.infile + ".struct");
190 TDLParser parser = new TDLParser(new Lexer(infile));
191 parser.filename = state.infile + ".struct";
192 CUP$TDLParser$actions.debug = state.verbose > 1 ;
193 state.ptStructures = (ParseNode) parser.parse().value;
194 } catch (FileNotFoundException fnfe) {
195 System.err.println("Unable to open file: " + state.infile + ".struct");
197 } catch (Exception e) {
198 // System.out.println(e);
199 // e.printStackTrace();
203 /* parse model file */
205 debugMessage(1, "Parsing model file");
207 FileInputStream infile = new FileInputStream(state.infile + ".model");
208 MDLParser parser = new MDLParser(new Lexer(infile));
209 parser.filename = state.infile + ".model";
210 CUP$MDLParser$actions.debug = state.verbose > 1 ;
211 state.ptModel = (ParseNode) parser.parse().value;
212 } catch (FileNotFoundException fnfe) {
213 System.err.println("Unable to open file: " + state.infile + ".model");
215 } catch (Exception e) {
216 // System.out.println(e);
217 // e.printStackTrace();
221 /* parse space file */
223 debugMessage(1, "Parsing space file");
225 FileInputStream infile = new FileInputStream(state.infile + ".space");
226 SDLParser parser = new SDLParser(new Lexer(infile));
227 parser.filename = state.infile + ".space";
228 CUP$SDLParser$actions.debug = state.verbose > 1 ;
229 state.ptSpace = (ParseNode) parser.parse().value;
230 } catch (FileNotFoundException fnfe) {
231 System.err.println("Unable to open file: " + state.infile + ".space");
233 } catch (Exception e) {
234 System.out.println(e);
239 /* parse constraints file */
241 debugMessage(1, "Parsing constraints file");
243 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
244 CDLParser parser = new CDLParser(new Lexer(infile));
245 parser.filename = state.infile + ".constraints";
246 CUP$CDLParser$actions.debug = state.verbose > 1 ;
247 state.ptConstraints = (ParseNode) parser.parse().value;
248 } catch (FileNotFoundException fnfe) {
249 System.err.println("Unable to open file: " + state.infile + ".constraints");
251 } catch (Exception e) {
252 // System.out.println(e);
253 // e.printStackTrace();
258 !CUP$TDLParser$actions.errors &&
259 !CUP$SDLParser$actions.errors &&
260 !CUP$CDLParser$actions.errors &&
261 !CUP$MDLParser$actions.errors;
264 // if verbosity is on, then output parse trees as .dot files
265 if (success && state.verbose > 0) {
267 FileOutputStream dotfile;
269 dotfile = new FileOutputStream(state.infile + ".struct.dot");
270 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
273 dotfile = new FileOutputStream(state.infile + ".model.dot");
274 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
277 dotfile = new FileOutputStream(state.infile + ".space.dot");
278 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
281 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
282 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
284 } catch (Exception e) {
294 public static boolean scan(State state) {
295 FileInputStream infile = null;
297 boolean errors = false;
298 String files[] = { new String(state.infile + ".struct"),
299 new String(state.infile + ".model"),
300 new String(state.infile + ".constraints"),
301 new String(state.infile + ".space") };
305 for (int i = 0; i < files.length; i++) {
307 String filename = files[i];
310 infile = new FileInputStream(filename);
311 } catch (FileNotFoundException fnfe) {
312 System.err.println("Unable to open file: " + filename);
316 lexer = new Lexer(infile);
321 java_cup.runtime.Symbol symbol;
323 symbol = lexer.next_token();
325 if (symbol.sym == Sym.EOF) {
327 } else if (symbol.sym == Sym.BAD) {
331 if (State.verbose > 2) {
332 System.out.println("Got token: " + symbol.value);
335 } catch (Exception e) {