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 TIME=false;
30 public static Vector debuggraphs=new Vector();
32 public static void main(String[] args) {
34 boolean success = true;
37 printArgInfo(cli); // prints debugging information and warning
40 State.currentState = state;
41 State.debug = cli.debug;
42 State.verbose = cli.verbose;
43 State.infile = cli.infile;
44 State.outfile = cli.outfile;
47 * added: terminates with an error message if no input file
48 * specified at command line
51 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
53 if (cli.infile == null) {
54 System.err.println("\nError: no input file specified");
59 System.out.println("Compiling " + cli.infile + ".");
62 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
63 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
64 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
67 state.setanalysis=new SetAnalysis(state);
68 Termination termination=null;
69 /* Check partition constraints */
70 (new ImplicitSchema(state)).update();
71 termination=new Termination(state);
74 (new DependencyBuilder(state)).calculate();
77 Vector nodes = new Vector(state.constraintnodes.values());
78 nodes.addAll(state.rulenodes.values());
80 FileOutputStream dotfile;
81 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
82 GraphNode.useEdgeLabels = true;
83 GraphNode.DOTVisitor.visit(dotfile, nodes);
86 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
87 GraphNode.useEdgeLabels = false;
88 GraphNode.DOTVisitor.visit(dotfile, nodes);
90 } catch (Exception e) {
96 FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
99 // do model optimizations
100 //(new Optimizer(state)).optimize();
102 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.c");
103 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
104 RepairGenerator wg = new RepairGenerator(state,termination);
105 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
109 WorklistGenerator ng = new WorklistGenerator(state);
110 SetInclusion.worklist=true;
111 RelationInclusion.worklist=true;
115 } catch (Exception e) {
121 System.out.println("Compilation of " + state.infile + " successful.");
122 System.out.println("#SUCCESS#");
126 private static void printArgInfo(CLI cli) {
128 System.out.println("Printing debugging information...");
129 System.out.println("Input filename: " + cli.infile);
130 System.out.println("Output filename: " + cli.outfile);
132 for (int i = 0; i < cli.opts.length; i++) {
134 System.out.println("Optimization");
139 for (int i = 0; i < cli.extraopts.size(); i++) {
140 System.err.println("Warning: optimization \"" +
141 cli.extraopts.elementAt(i) +
142 "\" not recognized");
145 for (int i = 0; i < cli.extras.size(); i++) {
146 System.err.println("Warning: option \"" +
147 cli.extras.elementAt(i) +
148 "\" not recognized");
152 private static boolean error(State state, String error) {
153 System.err.println(error);
155 System.out.println("#ERROR#");
161 public static boolean semantics(State state) {
162 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
163 SemanticChecker checker = new SemanticChecker();
167 ok = checker.check(state, er);
168 } catch (Exception e) {
169 er.report(null, e.toString());
175 er.report(null, "Semantic check failed.");
178 System.out.print(er.toString());
183 public static void debugMessage(int level, String s) {
184 if (State.currentState.verbose >= level) {
185 System.err.println(s);
189 public static boolean parse(State state) {
191 /* parse structure file */
193 debugMessage(1, "Parsing structure file");
195 FileInputStream infile = new FileInputStream(state.infile + ".struct");
196 TDLParser parser = new TDLParser(new Lexer(infile));
197 parser.filename = state.infile + ".struct";
198 CUP$TDLParser$actions.debug = state.verbose > 1 ;
199 state.ptStructures = (ParseNode) parser.parse().value;
200 } catch (FileNotFoundException fnfe) {
201 System.err.println("Unable to open file: " + state.infile + ".struct");
203 } catch (Exception e) {
204 System.out.println(e);
209 /* parse model file */
211 debugMessage(1, "Parsing model file");
213 FileInputStream infile = new FileInputStream(state.infile + ".model");
214 MDLParser parser = new MDLParser(new Lexer(infile));
215 parser.filename = state.infile + ".model";
216 CUP$MDLParser$actions.debug = state.verbose > 1 ;
217 state.ptModel = (ParseNode) parser.parse().value;
218 } catch (FileNotFoundException fnfe) {
219 System.err.println("Unable to open file: " + state.infile + ".model");
221 } catch (Exception e) {
222 System.out.println(e);
227 /* parse space file */
229 debugMessage(1, "Parsing space file");
231 FileInputStream infile = new FileInputStream(state.infile + ".space");
232 SDLParser parser = new SDLParser(new Lexer(infile));
233 parser.filename = state.infile + ".space";
234 CUP$SDLParser$actions.debug = state.verbose > 1 ;
235 state.ptSpace = (ParseNode) parser.parse().value;
236 } catch (FileNotFoundException fnfe) {
237 System.err.println("Unable to open file: " + state.infile + ".space");
239 } catch (Exception e) {
240 System.out.println(e);
245 /* parse constraints file */
247 debugMessage(1, "Parsing constraints file");
249 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
250 CDLParser parser = new CDLParser(new Lexer(infile));
251 parser.filename = state.infile + ".constraints";
252 CUP$CDLParser$actions.debug = state.verbose > 1 ;
253 state.ptConstraints = (ParseNode) parser.parse().value;
254 } catch (FileNotFoundException fnfe) {
255 System.err.println("Unable to open file: " + state.infile + ".constraints");
257 } catch (Exception e) {
258 System.out.println(e);
264 !CUP$TDLParser$actions.errors &&
265 !CUP$SDLParser$actions.errors &&
266 !CUP$CDLParser$actions.errors &&
267 !CUP$MDLParser$actions.errors;
270 // if verbosity is on, then output parse trees as .dot files
271 if (success && state.verbose > 0) {
273 FileOutputStream dotfile;
275 dotfile = new FileOutputStream(state.infile + ".struct.dot");
276 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
279 dotfile = new FileOutputStream(state.infile + ".model.dot");
280 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
283 dotfile = new FileOutputStream(state.infile + ".space.dot");
284 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
287 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
288 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
290 } catch (Exception e) {
300 public static boolean scan(State state) {
301 FileInputStream infile = null;
303 boolean errors = false;
304 String files[] = { new String(state.infile + ".struct"),
305 new String(state.infile + ".model"),
306 new String(state.infile + ".constraints"),
307 new String(state.infile + ".space") };
311 for (int i = 0; i < files.length; i++) {
313 String filename = files[i];
316 infile = new FileInputStream(filename);
317 } catch (FileNotFoundException fnfe) {
318 System.err.println("Unable to open file: " + filename);
322 lexer = new Lexer(infile);
327 java_cup.runtime.Symbol symbol;
329 symbol = lexer.next_token();
331 if (symbol.sym == Sym.EOF) {
333 } else if (symbol.sym == Sym.BAD) {
337 if (State.verbose > 2) {
338 System.out.println("Got token: " + symbol.value);
341 } catch (Exception e) {