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;
27 public static boolean ALLOCATECPLUSPLUS=false;
28 public static boolean OMITCOMP=false;
29 public static boolean MERGENODES=false;
30 public static boolean TIME=false;
32 public static HashSet removeconj=new HashSet();
33 public static boolean DEBUGGRAPH=false;
34 public static boolean REJECTLENGTH=false;
35 public static boolean PRINTREPAIRS=false;
36 public static boolean EXACTALLOCATION=false;
39 public static Vector debuggraphs=new Vector();
41 public static void main(String[] args) {
43 boolean success = true;
46 printArgInfo(cli); // prints debugging information and warning
49 State.currentState = state;
50 State.debug = cli.debug;
51 State.verbose = cli.verbose;
52 State.infile = cli.infile;
53 State.outfile = cli.outfile;
56 * added: terminates with an error message if no input file
57 * specified at command line
60 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
62 if (cli.infile == null) {
63 System.err.println("\nError: no input file specified");
68 System.out.println("Compiling " + cli.infile + ".");
71 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
72 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
73 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
76 state.setanalysis=new SetAnalysis(state);
77 Termination termination=null;
78 /* Check partition constraints */
79 (new ImplicitSchema(state)).update();
80 termination=new Termination(state);
83 (new DependencyBuilder(state)).calculate();
86 Vector nodes = new Vector(state.constraintnodes.values());
87 nodes.addAll(state.rulenodes.values());
89 FileOutputStream dotfile;
91 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
92 GraphNode.DOTVisitor.visit(dotfile, nodes);
94 } catch (Exception e) {
100 FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
103 // do model optimizations
104 //(new Optimizer(state)).optimize();
106 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.c");
107 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
108 RepairGenerator wg = new RepairGenerator(state,termination);
109 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
113 WorklistGenerator ng = new WorklistGenerator(state);
114 SetInclusion.worklist=true;
115 RelationInclusion.worklist=true;
119 } catch (Exception e) {
125 System.out.println("Compilation of " + state.infile + " successful.");
126 System.out.println("#SUCCESS#");
130 private static void printArgInfo(CLI cli) {
132 System.out.println("Printing debugging information...");
133 System.out.println("Input filename: " + cli.infile);
134 System.out.println("Output filename: " + cli.outfile);
136 for (int i = 0; i < cli.opts.length; i++) {
138 System.out.println("Optimization");
143 for (int i = 0; i < cli.extraopts.size(); i++) {
144 System.err.println("Warning: optimization \"" +
145 cli.extraopts.elementAt(i) +
146 "\" not recognized");
149 for (int i = 0; i < cli.extras.size(); i++) {
150 System.err.println("Warning: option \"" +
151 cli.extras.elementAt(i) +
152 "\" not recognized");
156 private static boolean error(State state, String error) {
157 System.err.println(error);
159 System.out.println("#ERROR#");
165 public static boolean semantics(State state) {
166 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
167 SemanticChecker checker = new SemanticChecker();
171 ok = checker.check(state, er);
172 } catch (Exception e) {
173 er.report(null, e.toString());
179 er.report(null, "Semantic check failed.");
182 System.out.print(er.toString());
187 public static void debugMessage(int level, String s) {
188 if (State.currentState.verbose >= level) {
189 System.err.println(s);
193 public static boolean parse(State state) {
195 /* parse structure file */
197 debugMessage(1, "Parsing structure file");
199 FileInputStream infile = new FileInputStream(state.infile + ".struct");
200 TDLParser parser = new TDLParser(new Lexer(infile));
201 parser.filename = state.infile + ".struct";
202 CUP$TDLParser$actions.debug = state.verbose > 1 ;
203 state.ptStructures = (ParseNode) parser.parse().value;
204 } catch (FileNotFoundException fnfe) {
205 System.err.println("Unable to open file: " + state.infile + ".struct");
207 } catch (Exception e) {
208 System.out.println(e);
213 /* parse model file */
215 debugMessage(1, "Parsing model file");
217 FileInputStream infile = new FileInputStream(state.infile + ".model");
218 MDLParser parser = new MDLParser(new Lexer(infile));
219 parser.filename = state.infile + ".model";
220 CUP$MDLParser$actions.debug = state.verbose > 1 ;
221 state.ptModel = (ParseNode) parser.parse().value;
222 } catch (FileNotFoundException fnfe) {
223 System.err.println("Unable to open file: " + state.infile + ".model");
225 } catch (Exception e) {
226 System.out.println(e);
231 /* parse space file */
233 debugMessage(1, "Parsing space file");
235 FileInputStream infile = new FileInputStream(state.infile + ".space");
236 SDLParser parser = new SDLParser(new Lexer(infile));
237 parser.filename = state.infile + ".space";
238 CUP$SDLParser$actions.debug = state.verbose > 1 ;
239 state.ptSpace = (ParseNode) parser.parse().value;
240 } catch (FileNotFoundException fnfe) {
241 System.err.println("Unable to open file: " + state.infile + ".space");
243 } catch (Exception e) {
244 System.out.println(e);
249 /* parse constraints file */
251 debugMessage(1, "Parsing constraints file");
253 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
254 CDLParser parser = new CDLParser(new Lexer(infile));
255 parser.filename = state.infile + ".constraints";
256 CUP$CDLParser$actions.debug = state.verbose > 1 ;
257 state.ptConstraints = (ParseNode) parser.parse().value;
258 } catch (FileNotFoundException fnfe) {
259 System.err.println("Unable to open file: " + state.infile + ".constraints");
261 } catch (Exception e) {
262 System.out.println(e);
268 !CUP$TDLParser$actions.errors &&
269 !CUP$SDLParser$actions.errors &&
270 !CUP$CDLParser$actions.errors &&
271 !CUP$MDLParser$actions.errors;
274 // if verbosity is on, then output parse trees as .dot files
275 if (success && state.verbose > 0) {
277 FileOutputStream dotfile;
279 dotfile = new FileOutputStream(state.infile + ".struct.dot");
280 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
283 dotfile = new FileOutputStream(state.infile + ".model.dot");
284 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
287 dotfile = new FileOutputStream(state.infile + ".space.dot");
288 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
291 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
292 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
294 } catch (Exception e) {
304 public static boolean scan(State state) {
305 FileInputStream infile = null;
307 boolean errors = false;
308 String files[] = { new String(state.infile + ".struct"),
309 new String(state.infile + ".model"),
310 new String(state.infile + ".constraints"),
311 new String(state.infile + ".space") };
315 for (int i = 0; i < files.length; i++) {
317 String filename = files[i];
320 infile = new FileInputStream(filename);
321 } catch (FileNotFoundException fnfe) {
322 System.err.println("Unable to open file: " + filename);
326 lexer = new Lexer(infile);
331 java_cup.runtime.Symbol symbol;
333 symbol = lexer.next_token();
335 if (symbol.sym == Sym.EOF) {
337 } else if (symbol.sym == Sym.BAD) {
341 if (State.verbose > 2) {
342 System.out.println("Got token: " + symbol.value);
345 } catch (Exception e) {