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