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