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