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 Vector debuggraphs=new Vector();
34 public static void main(String[] args) {
36 boolean success = true;
39 printArgInfo(cli); // prints debugging information and warning
42 State.currentState = state;
43 State.debug = cli.debug;
44 State.verbose = cli.verbose;
45 State.infile = cli.infile;
46 State.outfile = cli.outfile;
49 * added: terminates with an error message if no input file
50 * specified at command line
53 System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
55 if (cli.infile == null) {
56 System.err.println("\nError: no input file specified");
61 System.out.println("Compiling " + cli.infile + ".");
64 success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
65 success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
66 success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
69 state.setanalysis=new SetAnalysis(state);
70 Termination termination=null;
71 /* Check partition constraints */
72 (new ImplicitSchema(state)).update();
73 termination=new Termination(state);
76 (new DependencyBuilder(state)).calculate();
79 Vector nodes = new Vector(state.constraintnodes.values());
80 nodes.addAll(state.rulenodes.values());
82 FileOutputStream dotfile;
83 dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
84 GraphNode.useEdgeLabels = true;
85 GraphNode.DOTVisitor.visit(dotfile, nodes);
88 dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
89 GraphNode.useEdgeLabels = false;
90 GraphNode.DOTVisitor.visit(dotfile, nodes);
92 } catch (Exception e) {
98 FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
101 // do model optimizations
102 //(new Optimizer(state)).optimize();
104 FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.c");
105 FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
106 RepairGenerator wg = new RepairGenerator(state,termination);
107 wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
111 WorklistGenerator ng = new WorklistGenerator(state);
112 SetInclusion.worklist=true;
113 RelationInclusion.worklist=true;
117 } catch (Exception e) {
123 System.out.println("Compilation of " + state.infile + " successful.");
124 System.out.println("#SUCCESS#");
128 private static void printArgInfo(CLI cli) {
130 System.out.println("Printing debugging information...");
131 System.out.println("Input filename: " + cli.infile);
132 System.out.println("Output filename: " + cli.outfile);
134 for (int i = 0; i < cli.opts.length; i++) {
136 System.out.println("Optimization");
141 for (int i = 0; i < cli.extraopts.size(); i++) {
142 System.err.println("Warning: optimization \"" +
143 cli.extraopts.elementAt(i) +
144 "\" not recognized");
147 for (int i = 0; i < cli.extras.size(); i++) {
148 System.err.println("Warning: option \"" +
149 cli.extras.elementAt(i) +
150 "\" not recognized");
154 private static boolean error(State state, String error) {
155 System.err.println(error);
157 System.out.println("#ERROR#");
163 public static boolean semantics(State state) {
164 SimpleIRErrorReporter er = new SimpleIRErrorReporter();
165 SemanticChecker checker = new SemanticChecker();
169 ok = checker.check(state, er);
170 } catch (Exception e) {
171 er.report(null, e.toString());
177 er.report(null, "Semantic check failed.");
180 System.out.print(er.toString());
185 public static void debugMessage(int level, String s) {
186 if (State.currentState.verbose >= level) {
187 System.err.println(s);
191 public static boolean parse(State state) {
193 /* parse structure file */
195 debugMessage(1, "Parsing structure file");
197 FileInputStream infile = new FileInputStream(state.infile + ".struct");
198 TDLParser parser = new TDLParser(new Lexer(infile));
199 parser.filename = state.infile + ".struct";
200 CUP$TDLParser$actions.debug = state.verbose > 1 ;
201 state.ptStructures = (ParseNode) parser.parse().value;
202 } catch (FileNotFoundException fnfe) {
203 System.err.println("Unable to open file: " + state.infile + ".struct");
205 } catch (Exception e) {
206 System.out.println(e);
211 /* parse model file */
213 debugMessage(1, "Parsing model file");
215 FileInputStream infile = new FileInputStream(state.infile + ".model");
216 MDLParser parser = new MDLParser(new Lexer(infile));
217 parser.filename = state.infile + ".model";
218 CUP$MDLParser$actions.debug = state.verbose > 1 ;
219 state.ptModel = (ParseNode) parser.parse().value;
220 } catch (FileNotFoundException fnfe) {
221 System.err.println("Unable to open file: " + state.infile + ".model");
223 } catch (Exception e) {
224 System.out.println(e);
229 /* parse space file */
231 debugMessage(1, "Parsing space file");
233 FileInputStream infile = new FileInputStream(state.infile + ".space");
234 SDLParser parser = new SDLParser(new Lexer(infile));
235 parser.filename = state.infile + ".space";
236 CUP$SDLParser$actions.debug = state.verbose > 1 ;
237 state.ptSpace = (ParseNode) parser.parse().value;
238 } catch (FileNotFoundException fnfe) {
239 System.err.println("Unable to open file: " + state.infile + ".space");
241 } catch (Exception e) {
242 System.out.println(e);
247 /* parse constraints file */
249 debugMessage(1, "Parsing constraints file");
251 FileInputStream infile = new FileInputStream(state.infile + ".constraints");
252 CDLParser parser = new CDLParser(new Lexer(infile));
253 parser.filename = state.infile + ".constraints";
254 CUP$CDLParser$actions.debug = state.verbose > 1 ;
255 state.ptConstraints = (ParseNode) parser.parse().value;
256 } catch (FileNotFoundException fnfe) {
257 System.err.println("Unable to open file: " + state.infile + ".constraints");
259 } catch (Exception e) {
260 System.out.println(e);
266 !CUP$TDLParser$actions.errors &&
267 !CUP$SDLParser$actions.errors &&
268 !CUP$CDLParser$actions.errors &&
269 !CUP$MDLParser$actions.errors;
272 // if verbosity is on, then output parse trees as .dot files
273 if (success && state.verbose > 0) {
275 FileOutputStream dotfile;
277 dotfile = new FileOutputStream(state.infile + ".struct.dot");
278 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
281 dotfile = new FileOutputStream(state.infile + ".model.dot");
282 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
285 dotfile = new FileOutputStream(state.infile + ".space.dot");
286 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
289 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
290 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
292 } catch (Exception e) {
302 public static boolean scan(State state) {
303 FileInputStream infile = null;
305 boolean errors = false;
306 String files[] = { new String(state.infile + ".struct"),
307 new String(state.infile + ".model"),
308 new String(state.infile + ".constraints"),
309 new String(state.infile + ".space") };
313 for (int i = 0; i < files.length; i++) {
315 String filename = files[i];
318 infile = new FileInputStream(filename);
319 } catch (FileNotFoundException fnfe) {
320 System.err.println("Unable to open file: " + filename);
324 lexer = new Lexer(infile);
329 java_cup.runtime.Symbol symbol;
331 symbol = lexer.next_token();
333 if (symbol.sym == Sym.EOF) {
335 } else if (symbol.sym == Sym.BAD) {
339 if (State.verbose > 2) {
340 System.out.println("Got token: " + symbol.value);
343 } catch (Exception e) {