6fad5086b8eb5ab02dc3d5fa200eac4905444c23
[repair.git] / Repair / RepairCompiler / MCC / Compiler.java
1 package MCC;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.IR.*;
6
7 /**
8  * The main compiler module, which does the following:
9  * <ul>
10  *  <li>
11  *   nothing.
12  *  </li>
13  * <ul>
14  *
15  * @author <b>Daniel Roy</b> droy (at) mit (dot) edu
16  * @version %I, %G
17  */
18
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
32     public static Vector debuggraphs=new Vector();
33
34     public static void main(String[] args) {
35         State state = null;
36         boolean success = true;
37         CLI cli = new CLI();
38         cli.parse(args);
39         printArgInfo(cli); // prints debugging information and warning
40
41         state = new State();
42         State.currentState = state;
43         State.debug = cli.debug;
44         State.verbose = cli.verbose;
45         State.infile = cli.infile;
46         State.outfile = cli.outfile;
47
48         /*
49          * added: terminates with an error message if no input file
50          * specified at command line
51          */
52
53         System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
54
55         if (cli.infile == null) {
56             System.err.println("\nError: no input file specified");
57             System.exit(-1);
58         }
59
60         if (state.debug) {
61             System.out.println("Compiling " + cli.infile + ".");
62         }
63
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.");
67
68
69         state.setanalysis=new SetAnalysis(state);
70         Termination termination=null;
71         /* Check partition constraints */
72         (new ImplicitSchema(state)).update();
73         termination=new Termination(state);
74
75         state.printall();
76         (new DependencyBuilder(state)).calculate();
77
78         try {
79             Vector nodes = new Vector(state.constraintnodes.values());
80             nodes.addAll(state.rulenodes.values());
81
82             FileOutputStream dotfile;
83             dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
84             GraphNode.useEdgeLabels = true;
85             GraphNode.DOTVisitor.visit(dotfile, nodes);
86             dotfile.close();
87
88             dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
89             GraphNode.useEdgeLabels = false;
90             GraphNode.DOTVisitor.visit(dotfile, nodes);
91             dotfile.close();
92         } catch (Exception e) {
93             e.printStackTrace();
94             System.exit(-1);
95         }
96
97         try {
98             FileOutputStream gcode = new FileOutputStream(cli.infile + ".c");
99
100
101             // do model optimizations
102             //(new Optimizer(state)).optimize();
103
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");
108             gcode2.close();
109             gcode3.close();
110             /*          } else {
111                         WorklistGenerator ng = new WorklistGenerator(state);
112                     SetInclusion.worklist=true;
113                     RelationInclusion.worklist=true;
114                     ng.generate(gcode);
115                     }*/
116             gcode.close();
117         } catch (Exception e) {
118             e.printStackTrace();
119             System.exit(-1);
120         }
121
122         if (state.debug) {
123             System.out.println("Compilation of " + state.infile + " successful.");
124             System.out.println("#SUCCESS#");
125         }
126     }
127
128     private static void printArgInfo(CLI cli) {
129         if (cli.debug) {
130             System.out.println("Printing debugging information...");
131             System.out.println("Input filename: " + cli.infile);
132             System.out.println("Output filename: " + cli.outfile);
133
134             for (int i = 0; i < cli.opts.length; i++) {
135                 if (cli.opts[i]) {
136                     System.out.println("Optimization");
137                 }
138             }
139         }
140
141         for (int i = 0; i < cli.extraopts.size(); i++) {
142             System.err.println("Warning: optimization \"" +
143                                cli.extraopts.elementAt(i) +
144                                "\" not recognized");
145         }
146
147         for (int i = 0; i < cli.extras.size(); i++) {
148             System.err.println("Warning: option \"" +
149                                cli.extras.elementAt(i) +
150                                "\" not recognized");
151         }
152     }
153
154     private static boolean error(State state, String error) {
155         System.err.println(error);
156         if (state.debug) {
157             System.out.println("#ERROR#");
158         }
159         System.exit(-1);
160         return false;
161     }
162
163     public static boolean semantics(State state) {
164         SimpleIRErrorReporter er = new SimpleIRErrorReporter();
165         SemanticChecker checker = new SemanticChecker();
166         boolean ok = true;
167
168         try {
169             ok = checker.check(state, er);
170         } catch (Exception e) {
171             er.report(null, e.toString());
172             e.printStackTrace();
173             er.error = true;
174         }
175
176         if (!ok) {
177             er.report(null, "Semantic check failed.");
178         }
179
180         System.out.print(er.toString());
181
182         return !er.error;
183     }
184
185     public static void debugMessage(int level, String s) {
186         if (State.currentState.verbose >= level) {
187             System.err.println(s);
188         }
189     }
190
191     public static boolean parse(State state) {
192
193         /* parse structure file */
194         try {
195             debugMessage(1, "Parsing structure file");
196             LineCount.reset();
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");
204             System.exit(-1);
205         } catch (Exception e) {
206             System.out.println(e);
207             e.printStackTrace();
208             return false;
209         }
210
211         /* parse model file */
212         try {
213             debugMessage(1, "Parsing model file");
214             LineCount.reset();
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");
222             System.exit(-1);
223         } catch (Exception e) {
224             System.out.println(e);
225             e.printStackTrace();
226             return false;
227         }
228
229         /* parse space file */
230         try {
231             debugMessage(1, "Parsing space file");
232             LineCount.reset();
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");
240             System.exit(-1);
241         } catch (Exception e) {
242             System.out.println(e);
243             e.printStackTrace();
244             return false;
245         }
246
247         /* parse constraints file */
248         try {
249             debugMessage(1, "Parsing constraints file");
250             LineCount.reset();
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");
258             System.exit(-1);
259         } catch (Exception e) {
260             System.out.println(e);
261             e.printStackTrace();
262             return false;
263         }
264
265         boolean success =
266             !CUP$TDLParser$actions.errors &&
267             !CUP$SDLParser$actions.errors &&
268             !CUP$CDLParser$actions.errors &&
269             !CUP$MDLParser$actions.errors;
270
271
272         // if verbosity is on, then output parse trees as .dot files
273         if (success && state.verbose > 0) {
274             try {
275                 FileOutputStream dotfile;
276
277                 dotfile = new FileOutputStream(state.infile + ".struct.dot");
278                 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);
279                 dotfile.close();
280
281                 dotfile = new FileOutputStream(state.infile + ".model.dot");
282                 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);
283                 dotfile.close();
284
285                 dotfile = new FileOutputStream(state.infile + ".space.dot");
286                 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);
287                 dotfile.close();
288
289                 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
290                 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);
291                 dotfile.close();
292             } catch (Exception e) {
293                 e.printStackTrace();
294                 return false;
295             }
296         }
297
298         return success;
299     }
300
301
302     public static boolean scan(State state) {
303         FileInputStream infile = null;
304         Lexer lexer;
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") };
310
311
312
313         for (int i = 0; i < files.length; i++) {
314
315             String filename = files[i];
316
317             try {
318                 infile = new FileInputStream(filename);
319             } catch (FileNotFoundException fnfe) {
320                 System.err.println("Unable to open file: " + filename);
321                 System.exit(-1);
322             }
323
324             lexer = new Lexer(infile);
325
326
327             try {
328                 while (true) {
329                     java_cup.runtime.Symbol symbol;
330
331                     symbol = lexer.next_token();
332
333                     if (symbol.sym == Sym.EOF) {
334                         break;
335                     } else if (symbol.sym == Sym.BAD) {
336                         errors = true;
337                     }
338
339                     if (State.verbose > 2) {
340                         System.out.println("Got token: " + symbol.value);
341                     }
342                 }
343             } catch (Exception e) {
344                 return false;
345             }
346         }
347
348         return !errors;
349     }
350
351
352 }