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