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