Changes:
[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     
23     public static void main(String[] args) {
24         State state = null;
25         boolean success = true;
26         CLI cli = new CLI();
27         cli.parse(args);
28         printArgInfo(cli); // prints debugging information and warning
29
30         state = new State();
31         State.currentState = state;
32         State.debug = cli.debug;
33         State.verbose = cli.verbose;
34         State.infile = cli.infile;
35         State.outfile = cli.outfile;
36
37         /*
38          * added: terminates with an error message if no input file
39          * specified at command line
40          */
41
42         System.out.println("MCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)\n");
43
44         if (cli.infile == null) {
45             System.err.println("\nError: no input file specified");
46             System.exit(-1);
47         }
48         
49         if (state.debug) {
50             System.out.println("Compiling " + cli.infile + ".");
51         }
52         
53         success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
54         success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
55         success = semantics(state) || error(state, "Semantic analysis failed, not attempting variable initialization.");
56         
57         
58         Termination termination=null;
59         /* Check partition constraints */
60         (new ImplicitSchema(state)).update();
61         termination=new Termination(state);
62         
63         state.printall();
64         (new DependencyBuilder(state)).calculate();
65         
66         try {
67             Vector nodes = new Vector(state.constraintnodes.values());
68             nodes.addAll(state.rulenodes.values());
69             
70             FileOutputStream dotfile;
71             dotfile = new FileOutputStream(cli.infile + ".dependencies.edgelabels.dot");
72             GraphNode.useEdgeLabels = true;
73             GraphNode.DOTVisitor.visit(dotfile, nodes);
74             dotfile.close();
75             
76             dotfile = new FileOutputStream(cli.infile + ".dependencies.dot");
77             GraphNode.useEdgeLabels = false;
78             GraphNode.DOTVisitor.visit(dotfile, nodes);                
79             dotfile.close();
80         } catch (Exception e) {
81             e.printStackTrace();
82             System.exit(-1);
83         }
84         
85         try {
86             FileOutputStream gcode = new FileOutputStream(cli.infile + ".cc");
87             
88             
89             // do model optimizations
90             //(new Optimizer(state)).optimize();
91             
92             FileOutputStream gcode2 = new FileOutputStream(cli.infile + "_aux.cc");
93             FileOutputStream gcode3 = new FileOutputStream(cli.infile + "_aux.h");
94             RepairGenerator wg = new RepairGenerator(state,termination);
95             wg.generate(gcode,gcode2,gcode3, cli.infile + "_aux.h");
96             gcode2.close();
97             gcode3.close();
98             /*          } else {
99                         WorklistGenerator ng = new WorklistGenerator(state);
100                     SetInclusion.worklist=true;
101                     RelationInclusion.worklist=true;
102                     ng.generate(gcode);
103                     }*/
104             gcode.close();
105         } catch (Exception e) {
106             e.printStackTrace();
107             System.exit(-1);
108         }
109         
110         if (state.debug) {
111             System.out.println("Compilation of " + state.infile + " successful.");
112             System.out.println("#SUCCESS#");
113         }
114     }
115
116     private static void printArgInfo(CLI cli) {
117         if (cli.debug) {
118             System.out.println("Printing debugging information...");
119             System.out.println("Input filename: " + cli.infile);
120             System.out.println("Output filename: " + cli.outfile);
121
122             for (int i = 0; i < cli.opts.length; i++) {
123                 if (cli.opts[i]) {
124                     System.out.println("Optimization");
125                 }
126             }
127         }
128
129         for (int i = 0; i < cli.extraopts.size(); i++) {
130             System.err.println("Warning: optimization \"" +
131                                cli.extraopts.elementAt(i) +
132                                "\" not recognized");
133         }
134
135         for (int i = 0; i < cli.extras.size(); i++) {
136             System.err.println("Warning: option \"" +
137                                cli.extras.elementAt(i) +
138                                "\" not recognized");
139         }
140     }
141
142     private static boolean error(State state, String error) {
143         System.err.println(error);
144         if (state.debug) {
145             System.out.println("#ERROR#");
146         }
147         System.exit(-1);
148         return false;
149     }
150
151     public static boolean semantics(State state) {
152         SimpleIRErrorReporter er = new SimpleIRErrorReporter();
153         SemanticChecker checker = new SemanticChecker();
154         boolean ok = true;
155
156         try {
157             ok = checker.check(state, er);
158         } catch (Exception e) {
159             er.report(null, e.toString());
160             e.printStackTrace();
161             er.error = true;
162         }
163
164         if (!ok) {
165             er.report(null, "Semantic check failed.");
166         }
167
168         System.out.print(er.toString());
169
170         return !er.error;
171     }
172
173     public static void debugMessage(int level, String s) {
174         if (State.currentState.verbose >= level) {
175             System.err.println(s);
176         }
177     }
178
179     public static boolean parse(State state) {
180         
181         /* parse structure file */
182         try {
183             debugMessage(1, "Parsing structure file");
184             LineCount.reset();
185             FileInputStream infile = new FileInputStream(state.infile + ".struct");
186             TDLParser parser = new TDLParser(new Lexer(infile));
187             parser.filename = state.infile + ".struct";
188             CUP$TDLParser$actions.debug = state.verbose > 1 ;
189             state.ptStructures = (ParseNode) parser.parse().value;
190         } catch (FileNotFoundException fnfe) {
191             System.err.println("Unable to open file: " + state.infile + ".struct");
192             System.exit(-1);
193         } catch (Exception e) {
194             //      System.out.println(e);
195             //      e.printStackTrace();
196             return false;
197         }
198
199         /* parse model file */
200         try {
201             debugMessage(1, "Parsing model file");
202             LineCount.reset();
203             FileInputStream infile = new FileInputStream(state.infile + ".model");
204             MDLParser parser = new MDLParser(new Lexer(infile));
205             parser.filename = state.infile + ".model";
206             CUP$MDLParser$actions.debug = state.verbose > 1 ;
207             state.ptModel = (ParseNode) parser.parse().value;
208         } catch (FileNotFoundException fnfe) {
209             System.err.println("Unable to open file: " + state.infile + ".model");
210             System.exit(-1);
211         } catch (Exception e) {
212             //      System.out.println(e);
213             //      e.printStackTrace();
214             return false;
215         }
216
217         /* parse space file */
218         try {
219             debugMessage(1, "Parsing space file");
220             LineCount.reset();
221             FileInputStream infile = new FileInputStream(state.infile + ".space");
222             SDLParser parser = new SDLParser(new Lexer(infile));
223             parser.filename = state.infile + ".space";
224             CUP$SDLParser$actions.debug = state.verbose > 1 ;
225             state.ptSpace = (ParseNode) parser.parse().value;
226         } catch (FileNotFoundException fnfe) {
227             System.err.println("Unable to open file: " + state.infile + ".space");
228             System.exit(-1);
229         } catch (Exception e) {
230             System.out.println(e);
231             e.printStackTrace();
232             return false;
233         }
234
235         /* parse constraints file */
236         try {
237             debugMessage(1, "Parsing constraints file");
238             LineCount.reset();
239             FileInputStream infile = new FileInputStream(state.infile + ".constraints");
240             CDLParser parser = new CDLParser(new Lexer(infile));
241             parser.filename = state.infile + ".constraints";
242             CUP$CDLParser$actions.debug = state.verbose > 1 ;
243             state.ptConstraints = (ParseNode) parser.parse().value;
244         } catch (FileNotFoundException fnfe) {
245             System.err.println("Unable to open file: " + state.infile + ".constraints");
246             System.exit(-1);
247         } catch (Exception e) {
248             //      System.out.println(e);
249             //      e.printStackTrace();
250             return false;
251         }
252
253         boolean success = 
254             !CUP$TDLParser$actions.errors && 
255             !CUP$SDLParser$actions.errors && 
256             !CUP$CDLParser$actions.errors && 
257             !CUP$MDLParser$actions.errors;
258
259                 
260         // if verbosity is on, then output parse trees as .dot files
261         if (success && state.verbose > 0) {
262             try {
263                 FileOutputStream dotfile;
264
265                 dotfile = new FileOutputStream(state.infile + ".struct.dot");
266                 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);                
267                 dotfile.close();
268
269                 dotfile = new FileOutputStream(state.infile + ".model.dot");
270                 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);                
271                 dotfile.close();
272
273                 dotfile = new FileOutputStream(state.infile + ".space.dot");
274                 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);                
275                 dotfile.close();
276
277                 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
278                 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);                
279                 dotfile.close();
280             } catch (Exception e) {
281                 e.printStackTrace();
282                 return false;
283             }
284         }
285             
286         return success;
287     }
288
289
290     public static boolean scan(State state) {
291         FileInputStream infile = null;
292         Lexer lexer;
293         boolean errors = false;
294         String files[] = { new String(state.infile + ".struct"),
295                            new String(state.infile + ".model"),
296                            new String(state.infile + ".constraints"),
297                            new String(state.infile + ".space") };
298
299
300
301         for (int i = 0; i < files.length; i++) {
302
303             String filename = files[i];
304
305             try {
306                 infile = new FileInputStream(filename);
307             } catch (FileNotFoundException fnfe) {
308                 System.err.println("Unable to open file: " + filename);
309                 System.exit(-1);
310             }
311             
312             lexer = new Lexer(infile);
313
314             
315             try {
316                 while (true) {
317                     java_cup.runtime.Symbol symbol;
318                     
319                     symbol = lexer.next_token();
320                     
321                     if (symbol.sym == Sym.EOF) {
322                         break;
323                     } else if (symbol.sym == Sym.BAD) {
324                         errors = true;
325                     }
326                     
327                     if (State.verbose > 2) {
328                         System.out.println("Got token: " + symbol.value);
329                     }
330                 }
331             } catch (Exception e) {
332                 return false;
333             }
334         }
335
336         return !errors;
337     }
338
339
340 }
341