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