ff46a2380a656846faf91b0e5c94d323bb1398bb
[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     for(int i=0;i<sourcefiles.size();i++)
198       sc.getClass((String)sourcefiles.get(i));
199
200     //Stuff the runtime wants to see
201     sc.getClass("String");
202     sc.getClass("Math");
203     sc.getClass("File");
204     sc.getClass("Socket");
205     sc.getClass("ServerSocket");
206     sc.getClass("FileInputStream");
207     sc.getClass("FileOutputStream");
208
209     sc.semanticCheck();
210
211     tu.createFullTable();
212
213     BuildFlat bf=new BuildFlat(state,tu);
214     bf.buildFlat();
215     SafetyAnalysis sa=null;
216     PrefetchAnalysis pa=null;
217
218     if (state.TAGSTATE) {
219       CallGraph callgraph=new CallGraph(state);
220       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
221       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
222     }
223
224     if (state.TASKSTATE) {
225       CallGraph callgraph=new CallGraph(state);
226       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
227       TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
228       ta.taskAnalysis();
229       TaskGraph tg=new TaskGraph(state, ta);
230       tg.createDOTfiles();
231
232       if (state.OPTIONAL) {
233         ExecutionGraph et=new ExecutionGraph(state, ta);
234         et.createExecutionGraph();
235         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
236         sa.doAnalysis();
237         state.storeAnalysisResult(sa.getResult());
238         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
239       }
240
241       if (state.WEBINTERFACE) {
242         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
243         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
244         JhttpServer serve=new JhttpServer(8000,wi);
245         serve.run();
246       }
247
248       if (state.SCHEDULING) {
249         // Use ownership analysis to get alias information
250         CallGraph callGraph = new CallGraph(state);
251         OwnershipAnalysis oa = new OwnershipAnalysis(state,
252                                                      tu,
253                                                      callGraph,
254                                                      state.OWNERSHIPALLOCDEPTH,
255                                                      state.OWNERSHIPWRITEDOTS,
256                                                      state.OWNERSHIPWRITEALL,
257                                                      state.OWNERSHIPALIASFILE);
258
259         // generate multiple schedulings
260         ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state,
261                                                                  ta);
262         // necessary preparation such as read profile info etc.
263         scheduleAnalysis.preparation();
264         scheduleAnalysis.preSchedule();
265         scheduleAnalysis.scheduleAnalysis();
266         //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
267         scheduleAnalysis.setCoreNum(state.CORENUM);
268         scheduleAnalysis.schedule();
269
270         //simulate these schedulings
271         ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(),
272                                                                     state,
273                                                                     ta);
274         Vector<Vector<Schedule>> schedulings = scheduleAnalysis.getSchedulings();
275         Vector<Integer> selectedScheduling = scheduleSimulator.simulate(schedulings);
276
277         if(state.MULTICORE) {
278           Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.firstElement());
279           BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
280                                                         bf.getMap(),
281                                                         tu,
282                                                         sa,
283                                                         scheduling,
284                                                         scheduleAnalysis.getCoreNum(),
285                                                         pa);
286           bcm.setOwnershipAnalysis(oa);
287           bcm.buildCode();
288           scheduling = null;
289         }
290         schedulings = null;
291         selectedScheduling = null;
292       }
293
294     }
295
296     if(!state.MULTICORE) {
297       if (state.DSM) {
298         CallGraph callgraph=new CallGraph(state);
299         if (state.PREFETCH) {
300           //speed up prefetch generation using locality analysis results
301           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
302           pa=new PrefetchAnalysis(state, callgraph, tu, la);
303         }
304
305         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
306         GenerateConversions gc=new GenerateConversions(la, state);
307         BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
308         bc.buildCode();
309       } else {
310         BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
311         bc.buildCode();
312       }
313     }
314
315     if (state.FLATIRGRAPH) {
316       FlatIRGraph firg = new FlatIRGraph(state,
317                                          state.FLATIRGRAPHTASKS,
318                                          state.FLATIRGRAPHUSERMETHODS,
319                                          state.FLATIRGRAPHLIBMETHODS);
320     }
321
322     if (state.OWNERSHIP) {
323       CallGraph callGraph = new CallGraph(state);
324       OwnershipAnalysis oa = new OwnershipAnalysis(state,
325                                                    tu,
326                                                    callGraph,
327                                                    state.OWNERSHIPALLOCDEPTH,
328                                                    state.OWNERSHIPWRITEDOTS,
329                                                    state.OWNERSHIPWRITEALL,
330                                                    state.OWNERSHIPALIASFILE);
331     }
332
333     System.exit(0);
334   }
335
336   /** Reads in a source file and adds the parse tree to the state object. */
337
338   public static ParseNode readSourceFile(State state, String sourcefile) {
339     try {
340       Reader fr= new BufferedReader(new FileReader(sourcefile));
341       Lex.Lexer l = new Lex.Lexer(fr);
342       java_cup.runtime.lr_parser g;
343       g = new Parse.Parser(l);
344       ParseNode p=null;
345       try {
346         p=(ParseNode) g./*debug_*/ parse().value;
347       } catch (Exception e) {
348         System.err.println("Error parsing file:"+sourcefile);
349         e.printStackTrace();
350         System.exit(-1);
351       }
352       state.addParseNode(p);
353       if (l.numErrors()!=0) {
354         System.out.println("Error parsing "+sourcefile);
355         System.exit(l.numErrors());
356       }
357       return p;
358
359     } catch (Exception e) {
360       throw new Error(e);
361     }
362   }
363 }