1c0d7e1a3174b56e39aa45cfe19dce680b4443b5
[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     public static boolean REPAIR=true;
21     
22     public static void main(String[] args) {
23         State state = null;
24         boolean success = true;
25         CLI cli = new CLI();
26         cli.parse(args);
27         printArgInfo(cli); // prints debugging information and warning
28
29         state = new State();
30         State.currentState = state;
31         State.debug = cli.debug;
32         State.verbose = cli.verbose;
33         State.infile = cli.infile;
34         State.outfile = cli.outfile;
35
36         /*
37          * added: terminates with an error message if no input file
38          * specified at command line
39          */
40
41         System.out.println("\nMCC v0.0.1 - MIT LCS (Author: Daniel Roy, Brian Demsky)");
42
43         if (cli.infile == null) {
44             System.err.println("\nError: no input file specified");
45             System.exit(-1);
46         }
47         
48         if (cli.target == CLI.ASSEMBLY || cli.target == CLI.DEFAULT) {
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             if (REPAIR) {
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                 if(REPAIR) {
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                     ng.generate(gcode);
104                 }
105                 gcode.close();
106             } catch (Exception e) {
107                 e.printStackTrace();
108                 System.exit(-1);
109             }
110             
111             if (state.debug) {
112                 System.out.println("Compilation of " + state.infile + " successful.");
113                 System.out.println("#SUCCESS#");
114             }
115         } else if (cli.target == CLI.INTER) {
116             if (state.debug) {
117                 System.out.println("Semantic analysis for " + cli.infile + ".");
118             }
119
120             success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
121             success = parse(state) || error(state, "Parsing failed, not attempting semantic analysis.");
122             success = semantics(state) || error(state, "Semantic analysis failed.");
123
124             if (state.debug) {
125                 System.out.println("Semantic analysis of " + state.infile + " successful.");
126                 System.out.println("#SUCCESS#");
127             }
128         } else if (cli.target == CLI.PARSE) {
129             if (state.debug) {
130                 System.out.println("Parsing " + cli.infile + ".");
131             }
132
133             success = scan(state) || error(state, "Scanning failed, not attempting to parse.");
134             success = parse(state) || error(state, "Parsing failed.");
135
136             if (state.debug) {
137                 System.out.println("Parsing of " + state.infile + " successful.");
138                 System.out.println("#SUCCESS#");
139             }
140         } else if (cli.target == CLI.SCAN) {
141             if (state.debug) {
142                 System.out.println("Scanning " + cli.infile + ".");
143             }
144
145             success = scan(state) || error(state, "Scanning failed.");
146
147             if (state.debug) {
148                 System.out.println("Scanning of " + state.infile + " successful.");
149                 System.out.println("#SUCCESS#");
150             }
151         }
152     }
153
154     private static void printArgInfo(CLI cli) {
155         if (cli.debug) {
156             System.out.println("Printing debugging information...");
157             System.out.println("Input filename: " + cli.infile);
158             System.out.println("Output filename: " + cli.outfile);
159             System.out.print("Target: ");
160
161             switch(cli.target) {
162             case CLI.ASSEMBLY:
163                 System.out.println("ASSEMBLY");
164                 break;
165             case CLI.DEFAULT:
166                 System.out.println("DEFAULT");
167                 break;
168             case CLI.INTER:
169                 System.out.println("INTER");
170                 break;
171             case CLI.LOWIR:
172                 System.out.println("LOWIR");
173                 break;
174             case CLI.PARSE:
175                 System.out.println("PARSE");
176                 break;
177             case CLI.SCAN:
178                 System.out.println("SCAN");
179                 break;
180             default:
181                 System.out.println("not recognized");
182                 break;
183             }
184
185             for (int i = 0; i < cli.opts.length; i++) {
186                 if (cli.opts[i]) {
187                     System.out.println("Optimization");
188                 }
189             }
190         }
191
192         for (int i = 0; i < cli.extraopts.size(); i++) {
193             System.err.println("Warning: optimization \"" +
194                                cli.extraopts.elementAt(i) +
195                                "\" not recognized");
196         }
197
198         for (int i = 0; i < cli.extras.size(); i++) {
199             System.err.println("Warning: option \"" +
200                                cli.extras.elementAt(i) +
201                                "\" not recognized");
202         }
203     }
204
205     private static boolean error(State state, String error) {
206         System.err.println(error);
207         if (state.debug) {
208             System.out.println("#ERROR#");
209         }
210         System.exit(-1);
211         return false;
212     }
213
214     public static boolean semantics(State state) {
215         SimpleIRErrorReporter er = new SimpleIRErrorReporter();
216         SemanticChecker checker = new SemanticChecker();
217         boolean ok = true;
218
219         try {
220             ok = checker.check(state, er);
221         } catch (Exception e) {
222             er.report(null, e.toString());
223             e.printStackTrace();
224             er.error = true;
225         }
226
227         if (!ok) {
228             er.report(null, "Semantic check failed.");
229         }
230
231         System.out.print(er.toString());
232
233         return !er.error;
234     }
235
236     public static void debugMessage(int level, String s) {
237         if (State.currentState.verbose >= level) {
238             System.err.println(s);
239         }
240     }
241
242     public static boolean parse(State state) {
243         
244         /* parse structure file */
245         try {
246             debugMessage(1, "Parsing structure file");
247             LineCount.reset();
248             FileInputStream infile = new FileInputStream(state.infile + ".struct");
249             TDLParser parser = new TDLParser(new Lexer(infile));
250             CUP$TDLParser$actions.debug = state.verbose > 1 ;
251             state.ptStructures = (ParseNode) parser.parse().value;
252         } catch (FileNotFoundException fnfe) {
253             System.err.println("Unable to open file: " + state.infile + ".struct");
254             System.exit(-1);
255         } catch (Exception e) {
256             //      System.out.println(e);
257             //      e.printStackTrace();
258             return false;
259         }
260
261         /* parse model file */
262         try {
263             debugMessage(1, "Parsing model file");
264             LineCount.reset();
265             FileInputStream infile = new FileInputStream(state.infile + ".model");
266             MDLParser parser = new MDLParser(new Lexer(infile));
267             CUP$MDLParser$actions.debug = state.verbose > 1 ;
268             state.ptModel = (ParseNode) parser.parse().value;
269         } catch (FileNotFoundException fnfe) {
270             System.err.println("Unable to open file: " + state.infile + ".model");
271             System.exit(-1);
272         } catch (Exception e) {
273             //      System.out.println(e);
274             //      e.printStackTrace();
275             return false;
276         }
277
278         /* parse space file */
279         try {
280             debugMessage(1, "Parsing space file");
281             LineCount.reset();
282             FileInputStream infile = new FileInputStream(state.infile + ".space");
283             SDLParser parser = new SDLParser(new Lexer(infile));
284             CUP$SDLParser$actions.debug = state.verbose > 1 ;
285             state.ptSpace = (ParseNode) parser.parse().value;
286         } catch (FileNotFoundException fnfe) {
287             System.err.println("Unable to open file: " + state.infile + ".space");
288             System.exit(-1);
289         } catch (Exception e) {
290             //      System.out.println(e);
291             //      e.printStackTrace();
292             return false;
293         }
294
295         /* parse constraints file */
296         try {
297             debugMessage(1, "Parsing constraints file");
298             LineCount.reset();
299             FileInputStream infile = new FileInputStream(state.infile + ".constraints");
300             CDLParser parser = new CDLParser(new Lexer(infile));
301             CUP$CDLParser$actions.debug = state.verbose > 1 ;
302             state.ptConstraints = (ParseNode) parser.parse().value;
303         } catch (FileNotFoundException fnfe) {
304             System.err.println("Unable to open file: " + state.infile + ".constraints");
305             System.exit(-1);
306         } catch (Exception e) {
307             //      System.out.println(e);
308             //      e.printStackTrace();
309             return false;
310         }
311
312         boolean success = 
313             !CUP$TDLParser$actions.errors && 
314             !CUP$SDLParser$actions.errors && 
315             !CUP$CDLParser$actions.errors && 
316             !CUP$MDLParser$actions.errors;
317
318                 
319         // if verbosity is on, then output parse trees as .dot files
320         if (success && state.verbose > 0) {
321             try {
322                 FileOutputStream dotfile;
323
324                 dotfile = new FileOutputStream(state.infile + ".struct.dot");
325                 ParseNodeDOTVisitor.visit(dotfile, state.ptStructures);                
326                 dotfile.close();
327
328                 dotfile = new FileOutputStream(state.infile + ".model.dot");
329                 ParseNodeDOTVisitor.visit(dotfile, state.ptModel);                
330                 dotfile.close();
331
332                 dotfile = new FileOutputStream(state.infile + ".space.dot");
333                 ParseNodeDOTVisitor.visit(dotfile, state.ptSpace);                
334                 dotfile.close();
335
336                 dotfile = new FileOutputStream(state.infile + ".constraints.dot");
337                 ParseNodeDOTVisitor.visit(dotfile, state.ptConstraints);                
338                 dotfile.close();
339             } catch (Exception e) {
340                 e.printStackTrace();
341                 return false;
342             }
343         }
344             
345         return success;
346     }
347
348
349     public static boolean scan(State state) {
350         FileInputStream infile = null;
351         Lexer lexer;
352         boolean errors = false;
353         String files[] = { new String(state.infile + ".struct"),
354                            new String(state.infile + ".model"),
355                            new String(state.infile + ".constraints"),
356                            new String(state.infile + ".space") };
357
358
359
360         for (int i = 0; i < files.length; i++) {
361
362             String filename = files[i];
363
364             try {
365                 infile = new FileInputStream(filename);
366             } catch (FileNotFoundException fnfe) {
367                 System.err.println("Unable to open file: " + filename);
368                 System.exit(-1);
369             }
370             
371             lexer = new Lexer(infile);
372
373             
374             try {
375                 while (true) {
376                     java_cup.runtime.Symbol symbol;
377                     
378                     symbol = lexer.next_token();
379                     
380                     if (symbol.sym == Sym.EOF) {
381                         break;
382                     } else if (symbol.sym == Sym.BAD) {
383                         errors = true;
384                     }
385                     
386                     if (State.verbose > 2) {
387                         System.out.println("Got token: " + symbol.value);
388                     }
389                 }
390             } catch (Exception e) {
391                 return false;
392             }
393         }
394
395         return !errors;
396     }
397
398
399 }
400