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