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