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;
31 public static boolean DEBUGGRAPH=false;
33 public static Vector debuggraphs=new Vector();
35 public static void main(String[] args) {
37 boolean success = true;
40 printArgInfo(cli); // prints debugging information and warning
43 State.currentState = state;
44 State.debug = cli.debug;
45 State.verbose = cli.verbose;
46 State.infile = cli.infile;
47 State.outfile = cli.outfile;
50 * added: terminates with an error message if no input file
51 * specified at command line
54 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
56 if (cli.infile == null) {
57 System.err.println("\nError: no input file specified");
62 System.out.println("Compiling " + cli.infile + ".");
65 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
66 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
67 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
70 state.setanalysis=new SetAnalysis(state);
71 Termination termination=null;
72 /* Check partition constraints */
73 (new ImplicitSchema(state)).update();
74 termination=new Termination(state);
77 (new DependencyBuilder(state)).calculate();
80 Vector nodes = new Vector(state.constraintnodes.values());
81 nodes.addAll(state.rulenodes.values());
83 FileOutputStream dotfile;
85 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
86 GraphNode.DOTVisitor.visit(dotfile, nodes);
88 } catch (Exception e) {
94 FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
97 // do model optimizations
98 //(new Optimizer(state)).optimize();
100 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.c");
101 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
102 RepairGenerator wg = new RepairGenerator(state,termination);
103 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
107 WorklistGenerator ng = new WorklistGenerator(state);
108 SetInclusion.worklist=true;
109 RelationInclusion.worklist=true;
113 } catch (Exception e) {
119 System.out.println("Compilation of " + state.infile + " successful.");
120 System.out.println("#SUCCESS#");
124 private static void printArgInfo(CLI cli) {
126 System.out.println("Printing debugging information...");
127 System.out.println("Input filename: " + cli.infile);
128 System.out.println("Output filename: " + cli.outfile);
130 for (int i = 0; i < cli.opts.length; i++) {
132 System.out.println("Optimization");
137 for (int i = 0; i < cli.extraopts.size(); i++) {
138 System.err.println("Warning: optimization \"" +
139 cli.extraopts.elementAt(i) +
140 "\" not recognized");
143 for (int i = 0; i < cli.extras.size(); i++) {
144 System.err.println("Warning: option \"" +
145 cli.extras.elementAt(i) +
146 "\" not recognized");
150 private static boolean error(State state, String error) {
151 System.err.println(error);
153 System.out.println("#ERROR#");
159 public static boolean semantics(State state) {
160 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
161 SemanticChecker checker = new SemanticChecker();
165 ok = checker.check(state, er);
166 } catch (Exception e) {
167 er.report(null, e.toString());
173 er.report(null, "Semantic check failed.");
176 System.out.print(er.toString());
181 public static void debugMessage(int level, String s) {
182 if (State.currentState.verbose >= level) {
183 System.err.println(s);
187 public static boolean parse(State state) {
189 /* parse structure file */
191 debugMessage(1, "Parsing structure file");
193 FileInputStream infile = new FileInputStream(state.infile + ".struct");
194 TDLParser parser = new TDLParser(new Lexer(infile));
195 parser.filename = state.infile + ".struct";
196 CUP$TDLParser$actions.debug = state.verbose > 1 ;
197 state.ptStructures = (ParseNode) parser.parse().value;
198 } catch (FileNotFoundException fnfe) {
199 System.err.println("Unable to open file: " + state.infile + ".struct");
201 } catch (Exception e) {
202 System.out.println(e);
207 /* parse model file */
209 debugMessage(1, "Parsing model file");
211 FileInputStream infile = new FileInputStream(state.infile + ".model");
212 MDLParser parser = new MDLParser(new Lexer(infile));
213 parser.filename = state.infile + ".model";
214 CUP$MDLParser$actions.debug = state.verbose > 1 ;
215 state.ptModel = (ParseNode) parser.parse().value;
216 } catch (FileNotFoundException fnfe) {
217 System.err.println("Unable to open file: " + state.infile + ".model");
219 } catch (Exception e) {
220 System.out.println(e);
225 /* parse space file */
227 debugMessage(1, "Parsing space file");
229 FileInputStream infile = new FileInputStream(state.infile + ".space");
230 SDLParser parser = new SDLParser(new Lexer(infile));
231 parser.filename = state.infile + ".space";
232 CUP$SDLParser$actions.debug = state.verbose > 1 ;
233 state.ptSpace = (ParseNode) parser.parse().value;
234 } catch (FileNotFoundException fnfe) {
235 System.err.println("Unable to open file: " + state.infile + ".space");
237 } catch (Exception e) {
238 System.out.println(e);
243 /* parse constraints file */
245 debugMessage(1, "Parsing constraints file");
247 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
248 CDLParser parser = new CDLParser(new Lexer(infile));
249 parser.filename = state.infile + ".constraints";
250 CUP$CDLParser$actions.debug = state.verbose > 1 ;
251 state.ptConstraints = (ParseNode) parser.parse().value;
252 } catch (FileNotFoundException fnfe) {
253 System.err.println("Unable to open file: " + state.infile + ".constraints");
255 } catch (Exception e) {
256 System.out.println(e);
262 !CUP$TDLParser$actions.errors &&
263 !CUP$SDLParser$actions.errors &&
264 !CUP$CDLParser$actions.errors &&
265 !CUP$MDLParser$actions.errors;
268 // if verbosity is on, then output parse trees as .dot files
269 if (success && state.verbose > 0) {
271 FileOutputStream dotfile;
273 dotfile = new FileOutputStream(state.infile + ".struct.dot");
274 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
277 dotfile = new FileOutputStream(state.infile + ".model.dot");
278 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
281 dotfile = new FileOutputStream(state.infile + ".space.dot");
282 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
285 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
286 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
288 } catch (Exception e) {
298 public static boolean scan(State state) {
299 FileInputStream infile = null;
301 boolean errors = false;
302 String files[] = { new String(state.infile + ".struct"),
303 new String(state.infile + ".model"),
304 new String(state.infile + ".constraints"),
305 new String(state.infile + ".space") };
309 for (int i = 0; i < files.length; i++) {
311 String filename = files[i];
314 infile = new FileInputStream(filename);
315 } catch (FileNotFoundException fnfe) {
316 System.err.println("Unable to open file: " + filename);
320 lexer = new Lexer(infile);
325 java_cup.runtime.Symbol symbol;
327 symbol = lexer.next_token();
329 if (symbol.sym == Sym.EOF) {
331 } else if (symbol.sym == Sym.BAD) {
335 if (State.verbose > 2) {
336 System.out.println("Got token: " + symbol.value);
339 } catch (Exception e) {