Some modifications to allow to print the name of the file when a syntax error is...
[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=false;
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             CUP$TDLParser$actions.debug = state.verbose > 1 ;
253             state.ptStructures = (ParseNode) parser.parse().value;
254         } catch (FileNotFoundException fnfe) {
255             System.err.println("Unable to open file: " + state.infile + ".struct");
256             System.exit(-1);
257         } catch (Exception e) {
258             //      System.out.println(e);
259             //      e.printStackTrace();
260             return false;
261         }
262
263         /* parse model file */
264         try {
265             debugMessage(1, "Parsing model file");
266             LineCount.reset();
267             FileInputStream infile = new FileInputStream(state.infile + ".model");
268             MDLParser parser = new MDLParser(new Lexer(infile));
269             parser.filename = state.infile + ".model";
270             CUP$MDLParser$actions.debug = state.verbose > 1 ;
271             state.ptModel = (ParseNode) parser.parse().value;
272         } catch (FileNotFoundException fnfe) {
273             System.err.println("Unable to open file: " + state.infile + ".model");
274             System.exit(-1);
275         } catch (Exception e) {
276             //      System.out.println(e);
277             //      e.printStackTrace();
278             return false;
279         }
280
281         /* parse space file */
282         try {
283             debugMessage(1, "Parsing space file");
284             LineCount.reset();
285             FileInputStream infile = new FileInputStream(state.infile + ".space");
286             SDLParser parser = new SDLParser(new Lexer(infile));
287             CUP$SDLParser$actions.debug = state.verbose > 1 ;
288             state.ptSpace = (ParseNode) parser.parse().value;
289         } catch (FileNotFoundException fnfe) {
290             System.err.println("Unable to open file: " + state.infile + ".space");
291             System.exit(-1);
292         } catch (Exception e) {
293             System.out.println(e);
294             e.printStackTrace();
295             return false;
296         }
297
298         /* parse constraints file */
299         try {
300             debugMessage(1, "Parsing constraints file");
301             LineCount.reset();
302             FileInputStream infile = new FileInputStream(state.infile + ".constraints");
303             CDLParser parser = new CDLParser(new Lexer(infile));
304             CUP$CDLParser$actions.debug = state.verbose > 1 ;
305             state.ptConstraints = (ParseNode) parser.parse().value;
306         } catch (FileNotFoundException fnfe) {
307             System.err.println("Unable to open file: " + state.infile + ".constraints");
308             System.exit(-1);
309         } catch (Exception e) {
310             //      System.out.println(e);
311             //      e.printStackTrace();
312             return false;
313         }
314
315         boolean success = 
316             !CUP$TDLParser$actions.errors && 
317             !CUP$SDLParser$actions.errors && 
318             !CUP$CDLParser$actions.errors && 
319             !CUP$MDLParser$actions.errors;
320
321                 
322         // if verbosity is on, then output parse trees as .dot files
323         if (success && state.verbose > 0) {
324             try {
325                 FileOutputStream dotfile;
326
327                 dotfile = new FileOutputStream(state.infile + ".struct.dot");
328                 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);                
329                 dotfile.close();
330
331                 dotfile = new FileOutputStream(state.infile + ".model.dot");
332                 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);                
333                 dotfile.close();
334
335                 dotfile = new FileOutputStream(state.infile + ".space.dot");
336                 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);                
337                 dotfile.close();
338
339                 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
340                 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);                
341                 dotfile.close();
342             } catch (Exception e) {
343                 e.printStackTrace();
344                 return false;
345             }
346         }
347             
348         return success;
349     }
350
351
352     public static boolean scan(State state) {
353         FileInputStream infile = null;
354         Lexer lexer;
355         boolean errors = false;
356         String files[] = { new String(state.infile + ".struct"),
357                            new String(state.infile + ".model"),
358                            new String(state.infile + ".constraints"),
359                            new String(state.infile + ".space") };
360
361
362
363         for (int i = 0; i < files.length; i++) {
364
365             String filename = files[i];
366
367             try {
368                 infile = new FileInputStream(filename);
369             } catch (FileNotFoundException fnfe) {
370                 System.err.println("Unable to open file: " + filename);
371                 System.exit(-1);
372             }
373             
374             lexer = new Lexer(infile);
375
376             
377             try {
378                 while (true) {
379                     java_cup.runtime.Symbol symbol;
380                     
381                     symbol = lexer.next_token();
382                     
383                     if (symbol.sym == Sym.EOF) {
384                         break;
385                     } else if (symbol.sym == Sym.BAD) {
386                         errors = true;
387                     }
388                     
389                     if (State.verbose > 2) {
390                         System.out.println("Got token: " + symbol.value);
391                     }
392                 }
393             } catch (Exception e) {
394                 return false;
395             }
396         }
397
398         return !errors;
399     }
400
401
402 }
403