bug fixes
[IRC.git] / Robust / src / Main / Main.java
1 package Main;
2
3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
5 import java.io.Reader;
6 import java.io.BufferedReader;
7 import java.io.FileReader;
8 import java.io.FileInputStream;
9 import java.util.Hashtable;
10 import java.util.Iterator;
11 import java.util.Set;
12 import java.util.Vector;
13
14 import IR.Tree.ParseNode;
15 import IR.Tree.BuildIR;
16 import IR.Tree.SemanticCheck;
17 import IR.Flat.BuildCodeMultiCore;
18 import IR.Flat.BuildFlat;
19 import IR.Flat.BuildCode;
20 import IR.ClassDescriptor;
21 import IR.State;
22 import IR.TaskDescriptor;
23 import IR.TypeUtil;
24 import Analysis.Scheduling.Schedule;
25 import Analysis.Scheduling.ScheduleAnalysis;
26 import Analysis.Scheduling.ScheduleSimulator;
27 import Analysis.TaskStateAnalysis.TaskAnalysis;
28 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
29 import Analysis.TaskStateAnalysis.TaskGraph;
30 import Analysis.CallGraph.CallGraph;
31 import Analysis.TaskStateAnalysis.FEdge;
32 import Analysis.TaskStateAnalysis.FlagState;
33 import Analysis.TaskStateAnalysis.TagAnalysis;
34 import Analysis.TaskStateAnalysis.GarbageAnalysis;
35 import Analysis.TaskStateAnalysis.ExecutionGraph;
36 import Analysis.TaskStateAnalysis.SafetyAnalysis;
37 import Analysis.Locality.LocalityAnalysis;
38 import Analysis.Locality.GenerateConversions;
39 import Analysis.Prefetch.PrefetchAnalysis;
40 import Analysis.FlatIRGraph.FlatIRGraph;
41 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
42 import Interface.*;
43 import Util.GraphNode;
44 import Util.GraphNode.DFS;
45 import Util.GraphNode.SCC;
46
47 public class Main {
48
49   /** Main method for the compiler.  */
50
51   public static void main(String args[]) throws Exception {
52     String ClassLibraryPrefix="./ClassLibrary/";
53     State state=new State();
54     Vector sourcefiles=new Vector();
55     state.classpath.add(".");
56
57     for(int i=0; i<args.length; i++) {
58       String option=args[i];
59       if (option.equals("-precise"))
60         IR.Flat.BuildCode.GENERATEPRECISEGC=true;
61       else if (option.equals("-prefetch"))
62         state.PREFETCH=true;
63       else if (option.equals("-dir"))
64         IR.Flat.BuildCode.PREFIX=args[++i]+"/";
65       else if (option.equals("-fastcheck"))
66         state.FASTCHECK=true;
67       else if (option.equals("-selfloop"))
68         state.selfloops.add(args[++i]);
69       else if (option.equals("-excprefetch"))
70         state.excprefetch.add(args[++i]);
71       else if (option.equals("-classlibrary"))
72         state.classpath.add(args[++i]);
73       else if(option.equals("-numcore")) {
74         ++i;
75         state.CORENUM = Integer.parseInt(args[i]);
76       } else if (option.equals("-mainclass"))
77         state.main=args[++i];
78       else if (option.equals("-trueprob")) {
79         state.TRUEPROB=Double.parseDouble(args[++i]);
80       } else if (option.equals("-printflat"))
81         State.PRINTFLAT=true;
82       else if (option.equals("-printscheduling"))
83         State.PRINTSCHEDULING=true;
84       else if (option.equals("-printschedulesim"))
85         State.PRINTSCHEDULESIM=true;
86       else if (option.equals("-struct"))
87         state.structfile=args[++i];
88       else if (option.equals("-conscheck"))
89         state.CONSCHECK=true;
90       else if (option.equals("-task"))
91         state.TASK=true;
92       else if (option.equals("-abortreaders"))
93         state.ABORTREADERS=true;
94       else if (option.equals("-taskstate"))
95         state.TASKSTATE=true;
96       else if (option.equals("-tagstate"))
97         state.TAGSTATE=true;
98       else if (option.equals("-flatirtasks")) {
99         state.FLATIRGRAPH=true;
100         state.FLATIRGRAPHTASKS=true;
101       } else if (option.equals("-flatirusermethods")) {
102         state.FLATIRGRAPH=true;
103         state.FLATIRGRAPHUSERMETHODS=true;
104       } else if (option.equals("-flatirlibmethods")) {
105         state.FLATIRGRAPH=true;
106         state.FLATIRGRAPHLIBMETHODS=true;
107       } else if (option.equals("-multicore"))
108         state.MULTICORE=true;
109       else if (option.equals("-ownership"))
110         state.OWNERSHIP=true;
111       else if (option.equals("-ownallocdepth")) {
112         state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
113       } else if (option.equals("-ownwritedots")) {
114         state.OWNERSHIPWRITEDOTS=true;
115         if (args[++i].equals("all")) {
116           state.OWNERSHIPWRITEALL=true;
117         }
118       } else if (option.equals("-ownaliasfile"))
119         state.OWNERSHIPALIASFILE=args[++i];
120       else if (option.equals("-optional"))
121         state.OPTIONAL=true;
122       else if (option.equals("-raw"))
123         state.RAW=true;
124       else if (option.equals("-scheduling"))
125         state.SCHEDULING=true;
126       else if (option.equals("-useprofile"))
127         state.USEPROFILE=true;
128       else if (option.equals("-thread"))
129         state.THREAD=true;
130       else if (option.equals("-dsm"))
131         state.DSM=true;
132       else if (option.equals("-webinterface"))
133         state.WEBINTERFACE=true;
134       else if (option.equals("-instructionfailures"))
135         state.INSTRUCTIONFAILURE=true;
136       else if (option.equals("-abcclose"))
137         state.ARRAYBOUNDARYCHECK=false;
138       else if (option.equals("-help")) {
139         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
140         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
141         System.out.println("-dir outputdirectory -- output code in outputdirectory");
142         System.out.println("-struct structfile -- output structure declarations for repair tool");
143         System.out.println("-mainclass -- main function to call");
144         System.out.println("-dsm -- distributed shared memory support");
145         System.out.println("-abortreaders -- abort readers");
146         System.out.println("-precise -- use precise garbage collection");
147         System.out.println("-conscheck -- turn on consistency checking");
148         System.out.println("-task -- compiler for tasks");
149         System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
150         System.out.println("-thread -- threads");
151         System.out.println("-trueprob <d> -- probability of true branch");
152         System.out.println("-printflat -- print out flat representation");
153         System.out.println("-instructionfailures -- insert code for instruction level failures");
154         System.out.println("-taskstate -- do task state analysis");
155         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
156         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
157         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
158         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
159         System.out.println("-ownership -- do ownership analysis");
160         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
161         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
162         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
163         System.out.println("-optional -- enable optional arguments");
164         System.out.println("-abcclose close the array boundary check");
165         System.out.println("-scheduling do task scheduling");
166         System.out.println("-multicore generate multi-core version binary");
167         System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
168         System.out.println("-raw generate raw version binary (should be used together with -multicore)");
169         System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
170         System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
171         System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
172         System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
173         System.out.println("-threadsimulate generate multi-thread simulate version binary");
174         System.out.println("-rawuseio use standard io to output profiling data (should be used together with -raw and -profile), it only works with single core version");
175         System.out.println("-printscheduling -- print out scheduling graphs");
176         System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
177         System.out.println("-webinterface -- enable web interface");
178         System.out.println("-help -- print out help");
179         System.exit(0);
180       } else {
181         if (args[i].indexOf(".java")!=-1)
182           sourcefiles.add(args[i].substring(0,args[i].indexOf(".java")));
183         else
184           sourcefiles.add(args[i]);
185       }
186     }
187
188     //add default classpath
189     if (state.classpath.size()==1)
190       state.classpath.add(ClassLibraryPrefix);
191
192     BuildIR bir=new BuildIR(state);
193     TypeUtil tu=new TypeUtil(state, bir);
194     
195
196     SemanticCheck sc=new SemanticCheck(state,tu);
197
198     for(int i=0;i<sourcefiles.size();i++)
199       sc.getClass((String)sourcefiles.get(i));
200
201     //Stuff the runtime wants to see
202     sc.getClass("String");
203     sc.getClass("Math");
204     sc.getClass("File");
205     sc.getClass("Socket");
206     sc.getClass("ServerSocket");
207     sc.getClass("FileInputStream");
208     sc.getClass("FileOutputStream");
209     if (state.TASK) {
210       sc.getClass("TagDescriptor");
211     }
212
213     sc.semanticCheck();
214
215     tu.createFullTable();
216
217     BuildFlat bf=new BuildFlat(state,tu);
218     bf.buildFlat();
219     SafetyAnalysis sa=null;
220     PrefetchAnalysis pa=null;
221
222     if (state.TAGSTATE) {
223       CallGraph callgraph=new CallGraph(state);
224       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
225       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
226     }
227
228     if (state.TASKSTATE) {
229       CallGraph callgraph=new CallGraph(state);
230       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
231       TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
232       ta.taskAnalysis();
233       TaskGraph tg=new TaskGraph(state, ta);
234       tg.createDOTfiles();
235
236       if (state.OPTIONAL) {
237         ExecutionGraph et=new ExecutionGraph(state, ta);
238         et.createExecutionGraph();
239         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
240         sa.doAnalysis();
241         state.storeAnalysisResult(sa.getResult());
242         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
243       }
244
245       if (state.WEBINTERFACE) {
246         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
247         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
248         JhttpServer serve=new JhttpServer(8000,wi);
249         serve.run();
250       }
251
252       if (state.SCHEDULING) {
253         // Use ownership analysis to get alias information
254         CallGraph callGraph = new CallGraph(state);
255         OwnershipAnalysis oa = new OwnershipAnalysis(state,
256                                                      tu,
257                                                      callGraph,
258                                                      state.OWNERSHIPALLOCDEPTH,
259                                                      state.OWNERSHIPWRITEDOTS,
260                                                      state.OWNERSHIPWRITEALL,
261                                                      state.OWNERSHIPALIASFILE);
262
263         // generate multiple schedulings
264         ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state,
265                                                                  ta);
266         // necessary preparation such as read profile info etc.
267         scheduleAnalysis.preparation();
268         scheduleAnalysis.preSchedule();
269         scheduleAnalysis.scheduleAnalysis();
270         //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
271         scheduleAnalysis.setCoreNum(state.CORENUM);
272         scheduleAnalysis.schedule();
273
274         //simulate these schedulings
275         ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(),
276                                                                     state,
277                                                                     ta);
278         Vector<Vector<Schedule>> schedulings = scheduleAnalysis.getSchedulings();
279         Vector<Integer> selectedScheduling = scheduleSimulator.simulate(schedulings);
280
281         if(state.MULTICORE) {
282           Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.firstElement());
283           BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
284                                                         bf.getMap(),
285                                                         tu,
286                                                         sa,
287                                                         scheduling,
288                                                         scheduleAnalysis.getCoreNum(),
289                                                         pa);
290           bcm.setOwnershipAnalysis(oa);
291           bcm.buildCode();
292           scheduling = null;
293         }
294         schedulings = null;
295         selectedScheduling = null;
296       }
297
298     }
299
300     if(!state.MULTICORE) {
301       if (state.DSM) {
302         CallGraph callgraph=new CallGraph(state);
303         if (state.PREFETCH) {
304           //speed up prefetch generation using locality analysis results
305           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
306           pa=new PrefetchAnalysis(state, callgraph, tu, la);
307         }
308
309         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
310         GenerateConversions gc=new GenerateConversions(la, state);
311         BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
312         bc.buildCode();
313       } else {
314         BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
315         bc.buildCode();
316       }
317     }
318
319     if (state.FLATIRGRAPH) {
320       FlatIRGraph firg = new FlatIRGraph(state,
321                                          state.FLATIRGRAPHTASKS,
322                                          state.FLATIRGRAPHUSERMETHODS,
323                                          state.FLATIRGRAPHLIBMETHODS);
324     }
325
326     if (state.OWNERSHIP) {
327       CallGraph callGraph = new CallGraph(state);
328       OwnershipAnalysis oa = new OwnershipAnalysis(state,
329                                                    tu,
330                                                    callGraph,
331                                                    state.OWNERSHIPALLOCDEPTH,
332                                                    state.OWNERSHIPWRITEDOTS,
333                                                    state.OWNERSHIPWRITEALL,
334                                                    state.OWNERSHIPALIASFILE);
335     }
336
337     System.exit(0);
338   }
339
340   /** Reads in a source file and adds the parse tree to the state object. */
341
342   public static ParseNode readSourceFile(State state, String sourcefile) {
343     try {
344       Reader fr= new BufferedReader(new FileReader(sourcefile));
345       Lex.Lexer l = new Lex.Lexer(fr);
346       java_cup.runtime.lr_parser g;
347       g = new Parse.Parser(l);
348       ParseNode p=null;
349       try {
350         p=(ParseNode) g./*debug_*/ parse().value;
351       } catch (Exception e) {
352         System.err.println("Error parsing file:"+sourcefile);
353         e.printStackTrace();
354         System.exit(-1);
355       }
356       state.addParseNode(p);
357       if (l.numErrors()!=0) {
358         System.out.println("Error parsing "+sourcefile);
359         System.exit(l.numErrors());
360       }
361       return p;
362
363     } catch (Exception e) {
364       throw new Error(e);
365     }
366   }
367 }