Bug fixes and add some code for easy debugging
[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.BuildCodeMGC;
19 import IR.Flat.BuildFlat;
20 import IR.Flat.BuildCode;
21 import IR.Flat.BuildCodeTran;
22 import IR.Flat.BuildOoOJavaCode;
23 import IR.Flat.Inliner;
24 import IR.ClassDescriptor;
25 import IR.State;
26 import IR.TaskDescriptor;
27 import IR.TypeUtil;
28 import Analysis.SSJava.SSJavaAnalysis;
29 import Analysis.Scheduling.MCImplSynthesis;
30 import Analysis.Scheduling.Schedule;
31 import Analysis.Scheduling.ScheduleAnalysis;
32 import Analysis.Scheduling.ScheduleSimulator;
33 import Analysis.TaskStateAnalysis.TaskAnalysis;
34 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
35 import Analysis.TaskStateAnalysis.TaskGraph;
36 import Analysis.CallGraph.CallGraph;
37 import Analysis.TaskStateAnalysis.FEdge;
38 import Analysis.TaskStateAnalysis.FlagState;
39 import Analysis.TaskStateAnalysis.TagAnalysis;
40 import Analysis.TaskStateAnalysis.GarbageAnalysis;
41 import Analysis.TaskStateAnalysis.ExecutionGraph;
42 import Analysis.TaskStateAnalysis.SafetyAnalysis;
43 import Analysis.Locality.LocalityAnalysis;
44 import Analysis.Locality.GenerateConversions;
45 import Analysis.Prefetch.PrefetchAnalysis;
46 import Analysis.FlatIRGraph.FlatIRGraph;
47 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
48 import Analysis.Disjoint.DisjointAnalysis;
49 import Analysis.OoOJava.OoOJavaAnalysis;
50 import Analysis.Loops.*;
51 import Analysis.Liveness;
52 import Analysis.ArrayReferencees;
53 import Analysis.Pointer.Pointer;
54 import IR.MethodDescriptor;
55 import IR.Flat.FlatMethod;
56 import Interface.*;
57 import Util.GraphNode;
58 import Util.GraphNode.DFS;
59 import Util.GraphNode.SCC;
60
61 public class Main {
62
63   /** Main method for the compiler.  */
64
65   public static void main(String args[]) throws Exception {
66     String ClassLibraryPrefix="./ClassLibrary/";
67     State state=new State();
68     Vector sourcefiles=new Vector();
69     State.initTimer();
70     state.classpath.add(".");
71
72     String outputdir = null;
73     boolean isDistributeInfo = false;
74     boolean isDisAll = false;
75     int startnum = 0;
76     
77
78     for(int i=0; i<args.length; i++) {
79       String option=args[i];
80       if (option.equals("-precise"))
81         IR.Flat.BuildCode.GENERATEPRECISEGC=true;
82       else if (option.equals("-prefetch"))
83         state.PREFETCH=true;
84       else if (option.equals("-dir"))
85         IR.Flat.BuildCode.PREFIX=args[++i]+"/";
86       else if (option.equals("-fastcheck"))
87         state.FASTCHECK=true;
88       else if (option.equals("-selfloop"))
89         state.selfloops.add(args[++i]);
90           else if (option.equals("-outputdir"))
91         state.outputdir = args[++i];
92       else if (option.equals("-excprefetch"))
93         state.excprefetch.add(args[++i]);
94       else if (option.equals("-classlibrary"))
95         state.classpath.add(args[++i]);
96       else if (option.equals("-inlineatomic")) {
97         state.INLINEATOMIC=true;
98         state.inlineatomicdepth=Integer.parseInt(args[++i]);
99       } else if(option.equals("-numcore")) {
100         ++i;
101         state.CORENUM = Integer.parseInt(args[i]);
102       } else if(option.equals("-numcore4gc")) {
103         ++i;
104         state.CORENUM4GC = Integer.parseInt(args[i]);
105       } else if (option.equals("-mainclass"))
106         state.main=args[++i];
107       else if (option.equals("-trueprob")) {
108         state.TRUEPROB=Double.parseDouble(args[++i]);
109       } else if (option.equals("-printflat"))
110         State.PRINTFLAT=true;
111       else if (option.equals("-printscheduling"))
112         State.PRINTSCHEDULING=true;
113       else if (option.equals("-minimize"))
114         state.MINIMIZE=true;
115       else if (option.equals("-printschedulesim"))
116         State.PRINTSCHEDULESIM=true;
117       else if (option.equals("-printcriticalpath"))
118           State.PRINTCRITICALPATH=true;
119       else if (option.equals("-struct"))
120         state.structfile=args[++i];
121       else if (option.equals("-conscheck"))
122         state.CONSCHECK=true;
123       else if (option.equals("-task"))
124         state.TASK=true;
125       else if (option.equals("-abortreaders"))
126         state.ABORTREADERS=true;
127       else if (option.equals("-sandbox"))
128         state.SANDBOX=true;
129       else if (option.equals("-taskstate"))
130         state.TASKSTATE=true;
131       else if (option.equals("-tagstate"))
132         state.TAGSTATE=true;
133       else if (option.equals("-stmarray"))
134         state.STMARRAY=true;
135       else if (option.equals("-eventmonitor"))
136         state.EVENTMONITOR=true;
137       else if (option.equals("-dualview"))
138         state.DUALVIEW=true;
139       else if (option.equals("-hybrid"))
140         state.HYBRID=true;
141       else if (option.equals("-flatirtasks")) {
142         state.FLATIRGRAPH=true;
143         state.FLATIRGRAPHTASKS=true;
144       } else if (option.equals("-flatirusermethods")) {
145         state.FLATIRGRAPH=true;
146         state.FLATIRGRAPHUSERMETHODS=true;
147       } else if (option.equals("-flatirlibmethods")) {
148         state.FLATIRGRAPH=true;
149         state.FLATIRGRAPHLIBMETHODS=true;
150       } else if (option.equals("-bamboocompiletime")) {
151         state.BAMBOOCOMPILETIME = true;
152       } else if (option.equals("-multicore"))
153         state.MULTICORE=true;
154       else if (option.equals("-multicoregc"))
155         state.MULTICOREGC=true;
156       else if (option.equals("-mgc")) {
157         state.MGC = true;
158       } else if (option.equals("-objectlockdebug")) {
159         state.OBJECTLOCKDEBUG = true;
160       } else if (option.equals("-ownership"))
161         state.OWNERSHIP=true;
162       else if (option.equals("-ownallocdepth")) {
163         state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
164       } else if (option.equals("-ownwritedots")) {
165         state.OWNERSHIPWRITEDOTS=true;
166         if (args[++i].equals("all")) {
167           state.OWNERSHIPWRITEALL=true;
168         }
169       } else if (option.equals("-ownaliasfile")) {
170         state.OWNERSHIPALIASFILE=args[++i];
171       } else if (option.equals("-ownaliasfiletab")) {
172         state.OWNERSHIPALIASFILE=args[++i];
173         state.OWNERSHIPALIASTAB=true;      
174       } else if (option.equals("-owndebugcallee")) {
175         state.OWNERSHIPDEBUGCALLEE=args[++i];
176       } else if (option.equals("-owndebugcaller")) {
177         state.OWNERSHIPDEBUGCALLER=args[++i];
178       } else if (option.equals("-owndebugcallcount")) {
179         state.OWNERSHIPDEBUGCALLCOUNT=Integer.parseInt(args[++i]);
180       } else if (option.equals("-pointer")) {
181         state.POINTER=true;
182       } else if (option.equals("-disjoint"))
183         state.DISJOINT=true;
184       else if (option.equals("-disjoint-k")) {
185         state.DISJOINTALLOCDEPTH=Integer.parseInt(args[++i]);
186
187       } else if (option.equals("-disjoint-write-dots")) {
188         state.DISJOINTWRITEDOTS = true;
189         String arg = args[++i];
190         if( arg.equals("all") ) {
191           state.DISJOINTWRITEALL = true;
192         } else if( arg.equals("final") ) {
193           state.DISJOINTWRITEALL = false;
194         } else {
195           throw new Error("disjoint-write-dots requires argument <all/final>");
196         }
197
198       } else if (option.equals("-disjoint-write-initial-contexts")) {
199         state.DISJOINTWRITEINITCONTEXTS = true;
200
201       } else if (option.equals("-disjoint-write-ihms")) {
202         state.DISJOINTWRITEIHMS = true;
203
204       } else if (option.equals("-disjoint-alias-file")) {
205         state.DISJOINTALIASFILE = args[++i];
206         String arg = args[++i];
207         if( arg.equals("normal") ) {
208           state.DISJOINTALIASTAB = false;
209         } else if( arg.equals("tabbed") ) {
210           state.DISJOINTALIASTAB = true;
211         } else {
212           throw new Error("disjoint-alias-file requires arguments: <filename> <normal/tabbed>");
213         }
214
215       } else if (option.equals("-disjoint-debug-callsite")) {
216         state.DISJOINTDEBUGCALLEE=args[++i];
217         state.DISJOINTDEBUGCALLER=args[++i];
218         state.DISJOINTDEBUGCALLVISITTOSTART=Integer.parseInt(args[++i]);
219         state.DISJOINTDEBUGCALLNUMVISITS=Integer.parseInt(args[++i]);
220         String arg = args[++i];
221         if( arg.equals("true") ) {
222           state.DISJOINTDEBUGCALLSTOPAFTER = true;
223         } else if( arg.equals("false") ) {
224           state.DISJOINTDEBUGCALLSTOPAFTER = false;
225         } else {
226           throw new Error("disjoint-debug-callsite requires arguments:\n"+
227                           "  <callee symbol> <caller symbol> <# visit to start> <# visits to capture> <T/F stop after>");
228         }
229       
230       } else if (option.equals("-disjoint-debug-snap-method")) {
231         state.DISJOINTSNAPSYMBOL=args[++i];
232         state.DISJOINTSNAPVISITTOSTART=Integer.parseInt(args[++i]);
233         state.DISJOINTSNAPNUMVISITS=Integer.parseInt(args[++i]);
234         String arg = args[++i];
235         if( arg.equals("true") ) {
236           state.DISJOINTSNAPSTOPAFTER = true;
237         } else if( arg.equals("false") ) {
238           state.DISJOINTSNAPSTOPAFTER = false;
239         } else {
240           throw new Error("disjoint-debug-snap-method requires arguments:\n"+
241                           "  <method symbol> <# visit to start> <# visits to snap> <T/F stop after>");
242         }
243
244       } else if( option.equals( "-disjoint-release-mode" ) ) {
245         state.DISJOINTRELEASEMODE = true;        
246
247       } else if( option.equals( "-disjoint-dvisit-stack" ) ) {
248         state.DISJOINTDVISITSTACK         = true;      
249         state.DISJOINTDVISITPQUE          = false;
250         state.DISJOINTDVISITSTACKEESONTOP = false;
251
252       } else if( option.equals( "-disjoint-dvisit-pqueue" ) ) {
253         state.DISJOINTDVISITPQUE          = true;
254         state.DISJOINTDVISITSTACK         = false;
255         state.DISJOINTDVISITSTACKEESONTOP = false;
256
257       } else if( option.equals( "-disjoint-dvisit-stack-callees-on-top" ) ) {
258         state.DISJOINTDVISITSTACKEESONTOP = true;
259         state.DISJOINTDVISITPQUE          = false;
260         state.DISJOINTDVISITSTACK         = false;      
261
262       } else if( option.equals( "-disjoint-desire-determinism" ) ) {
263         state.DISJOINTDETERMINISM = true;
264
265         // when asking analysis for a deterministic result, force
266         // a stack-based visiting scheme, because the priority queue
267         // requires a non-deterministic topological sort
268         state.DISJOINTDVISITSTACKEESONTOP = true;
269         state.DISJOINTDVISITPQUE          = false;
270         state.DISJOINTDVISITSTACK         = false;
271
272
273       } else if( option.equals( "-disjoint-debug-scheduling" ) ) {
274         state.DISJOINTDEBUGSCHEDULING = true;
275       }
276       
277
278       else if (option.equals("-optional"))
279         state.OPTIONAL=true;
280       else if (option.equals("-optimize"))
281         state.OPTIMIZE=true;
282       else if (option.equals("-noloop"))
283         state.NOLOOP=true;
284       else if (option.equals("-dcopts"))
285         state.DCOPTS=true;
286       else if (option.equals("-arraypad"))
287         state.ARRAYPAD=true;
288       else if (option.equals("-delaycomp"))
289         state.DELAYCOMP=true;
290       else if (option.equals("-raw"))
291         state.RAW=true;
292       else if (option.equals("-scheduling"))
293         state.SCHEDULING=true;
294       else if (option.equals("-distributioninfo"))
295         isDistributeInfo=true;
296       else if (option.equals("-disall"))
297         isDisAll=true;
298       else if (option.equals("-disstart"))
299         startnum = Integer.parseInt(args[++i]);
300       else if (option.equals("-useprofile")) {
301         state.USEPROFILE=true;
302     state.profilename = args[++i];
303       }
304       else if (option.equals("-thread"))
305         state.THREAD=true;
306       else if (option.equals("-dsm"))
307         state.DSM=true;
308       else if (option.equals("-recoverystats"))
309   state.DSMRECOVERYSTATS=true;
310       else if (option.equals("-dsmtask"))
311   state.DSMTASK=true;
312       else if (option.equals("-singleTM"))
313         state.SINGLETM=true;
314       else if (option.equals("-readset"))
315         state.READSET=true;
316       else if (option.equals("-webinterface"))
317         state.WEBINTERFACE=true;
318       else if (option.equals("-instructionfailures"))
319         state.INSTRUCTIONFAILURE=true;
320       else if (option.equals("-abcclose"))
321         state.ARRAYBOUNDARYCHECK=false;
322
323       else if (option.equals("-methodeffects")) {
324         state.METHODEFFECTS=true;
325         
326       } else if (option.equals("-coreprof")) {
327         state.COREPROF=true;
328
329       } else if (option.equals("-ooojava")) {
330         state.OOOJAVA  = true;
331         state.DISJOINT = true;
332         state.OOO_NUMCORES   = Integer.parseInt( args[++i] );
333         state.OOO_MAXSESEAGE = Integer.parseInt( args[++i] );
334
335       } else if (option.equals("-ooodebug") ){ 
336         state.OOODEBUG  = true;
337       } else if (option.equals("-rcr")){      
338         state.RCR = true;
339         state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
340       } else if (option.equals("-rcr_debug")){
341         state.RCR_DEBUG = true;
342         state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
343       } else if (option.equals("-rcr_debug_verbose")){
344   state.RCR_DEBUG_VERBOSE = true;
345   state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
346       } else if (option.equals("-nostalltr")){
347         state.NOSTALLTR = true;     
348       } else if (option.equals("-ssjava")){
349   state.SSJAVA = true;     
350       } else if (option.equals("-printlinenum")){
351   state.LINENUM=true;
352       }else if (option.equals("-help")) {      
353         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
354         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
355         System.out.println("-dir outputdirectory -- output code in outputdirectory");
356         System.out.println("-struct structfile -- output structure declarations for repair tool");
357         System.out.println("-mainclass -- main function to call");
358         System.out.println("-dsm -- distributed shared memory support");
359         System.out.println("-singleTM -- single machine committing transactions");
360         System.out.println("-abortreaders -- abort readers");
361         System.out.println("-precise -- use precise garbage collection");
362         System.out.println("-conscheck -- turn on consistency checking");
363         System.out.println("-task -- compiler for tasks");
364         System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
365         System.out.println("-thread -- threads");
366         System.out.println("-trueprob <d> -- probability of true branch");
367         System.out.println("-printflat -- print out flat representation");
368         System.out.println("-instructionfailures -- insert code for instruction level failures");
369         System.out.println("-taskstate -- do task state analysis");
370         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
371         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
372         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
373         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
374         System.out.println("-ownership -- do ownership analysis");
375         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
376         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
377         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
378         System.out.println("-optimize -- enable optimizations");
379         System.out.println("-noloop -- disable loop optimizations");
380         System.out.println("-optional -- enable optional arguments");
381         System.out.println("-abcclose close the array boundary check");
382         System.out.println("-scheduling do task scheduling");
383         System.out.println("-mlp <num cores> <max sese age> build mlp code");
384         System.out.println("-mlpdebug if mlp, report progress and interim results");
385         System.out.println("-multicore generate multi-core version binary");
386         System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
387         System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
388         System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
389         System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
390         System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
391         System.out.println("-threadsimulate generate multi-thread simulate version binary");
392         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");
393         System.out.println("-printscheduling -- print out scheduling graphs");
394         System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
395         System.out.println("-webinterface -- enable web interface");
396         System.out.println("-linenum print out line numbers in generated C codes");
397         System.out.println("-help -- print out help");
398         System.exit(0);
399       } else {
400         sourcefiles.add(args[i]);
401       }
402     }
403     
404     //add default classpath
405     if (state.classpath.size()==1)
406       state.classpath.add(ClassLibraryPrefix);
407
408     State.logEvent("Done Parsing Commands");
409
410     SSJavaAnalysis ssjava=new SSJavaAnalysis(state);
411     BuildIR bir=new BuildIR(state);
412     TypeUtil tu=new TypeUtil(state, bir);
413
414     SemanticCheck sc=new SemanticCheck(state,tu);
415
416     for(int i=0;i<sourcefiles.size();i++)
417       loadClass(state, bir,(String)sourcefiles.get(i));
418
419     //Stuff the runtime wants to see
420     sc.getClass("String");
421     sc.getClass("Math");
422     sc.getClass("File");
423     sc.getClass("Socket");
424     sc.getClass("ServerSocket");
425     sc.getClass("FileInputStream");
426     sc.getClass("FileOutputStream");
427     if (state.TASK) {
428       sc.getClass("TagDescriptor");
429     }
430     if (state.THREAD||state.DSM||state.SINGLETM||state.MGC) {
431       sc.getClass("Thread");
432     }
433
434
435     sc.semanticCheck();
436     State.logEvent("Done Semantic Checking");
437
438     tu.createFullTable();
439     State.logEvent("Done Creating TypeUtil");
440
441     // SSJava
442     if(state.SSJAVA){
443       ssjava.doCheck();
444     }
445     
446
447     BuildFlat bf=new BuildFlat(state,tu);
448     bf.buildFlat();
449     State.logEvent("Done Building Flat");
450
451     SafetyAnalysis sa=null;
452     PrefetchAnalysis pa=null;
453     OoOJavaAnalysis  oooa=null;
454     if (state.INLINEATOMIC) {
455       Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
456       while(classit.hasNext()) {
457         ClassDescriptor cn=(ClassDescriptor)classit.next();
458         Iterator methodit=cn.getMethods();
459         while(methodit.hasNext()) {
460           // do inlining
461           MethodDescriptor md=(MethodDescriptor)methodit.next();
462           FlatMethod fm=state.getMethodFlat(md);
463           Inliner.inlineAtomic(state, tu, fm, state.inlineatomicdepth);
464         }
465       }
466     }
467
468     if (state.OPTIMIZE) {
469       CallGraph callgraph=new CallGraph(state);
470       CopyPropagation cp=new CopyPropagation();
471       DeadCode dc=new DeadCode();
472       GlobalFieldType gft=new GlobalFieldType(callgraph, state, tu.getMain());
473       CSE cse=new CSE(gft, tu);
474       localCSE lcse=new localCSE(gft, tu);
475       LoopOptimize lo=null;
476       if (!state.NOLOOP)
477           lo=new LoopOptimize(gft, tu);
478       Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
479       while(classit.hasNext()) {
480         ClassDescriptor cn=(ClassDescriptor)classit.next();
481         Iterator methodit=cn.getMethods();
482         while(methodit.hasNext()) {
483           /* Classify parameters */
484           MethodDescriptor md=(MethodDescriptor)methodit.next();
485           FlatMethod fm=state.getMethodFlat(md);
486           cp.optimize(fm);
487           dc.optimize(fm);
488           if (!state.NOLOOP)
489               lo.optimize(fm);
490           cp.optimize(fm);
491           dc.optimize(fm);
492           lcse.doAnalysis(fm);
493           cse.doAnalysis(fm);
494           cp.optimize(fm);
495           dc.optimize(fm);
496           cp.optimize(fm);
497           dc.optimize(fm);
498         }
499       }
500       State.logEvent("Done Optimizing");
501     }
502
503     if (state.FLATIRGRAPH) {
504       FlatIRGraph firg = new FlatIRGraph(state,
505                                          state.FLATIRGRAPHTASKS,
506                                          state.FLATIRGRAPHUSERMETHODS,
507                                          state.FLATIRGRAPHLIBMETHODS);
508     }
509     
510     if (state.OWNERSHIP) {
511       CallGraph callGraph = new CallGraph(state);
512       Liveness liveness = new Liveness();
513       ArrayReferencees ar = new ArrayReferencees(state);
514       OwnershipAnalysis oa = new OwnershipAnalysis(state,
515                                                    tu,
516                                                    callGraph,
517                                                    liveness,
518                                                    ar,
519                                                    state.OWNERSHIPALLOCDEPTH,
520                                                    state.OWNERSHIPWRITEDOTS,
521                                                    state.OWNERSHIPWRITEALL,
522                                                    state.OWNERSHIPALIASFILE,
523                                                    state.METHODEFFECTS);
524     }
525
526     if (state.DISJOINT && !state.OOOJAVA) {
527       CallGraph        cg = new CallGraph(state);
528       Liveness         l  = new Liveness();
529       ArrayReferencees ar = new ArrayReferencees(state);
530       DisjointAnalysis da = new DisjointAnalysis(state, tu, cg, l, ar, null, null);
531     }
532
533     if (state.OOOJAVA) {
534       CallGraph        cg  = new CallGraph(state);
535       Liveness         l   = new Liveness();
536       ArrayReferencees ar  = new ArrayReferencees(state);
537       oooa = new OoOJavaAnalysis(state, tu, cg, l, ar);
538     }
539
540
541     if (state.TAGSTATE) {
542       CallGraph callgraph=new CallGraph(state);
543       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
544       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
545     }
546
547     if (state.TASKSTATE) {
548       CallGraph callgraph=new CallGraph(state);
549       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
550       TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
551       ta.taskAnalysis();
552       TaskGraph tg=new TaskGraph(state, ta);
553       tg.createDOTfiles();
554
555       if (state.OPTIONAL) {
556         ExecutionGraph et=new ExecutionGraph(state, ta);
557         et.createExecutionGraph();
558         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
559         sa.doAnalysis();
560         state.storeAnalysisResult(sa.getResult());
561         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
562       }
563
564       if (state.WEBINTERFACE) {
565         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
566         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
567         JhttpServer serve=new JhttpServer(8000,wi);
568         serve.run();
569       }
570
571       if (state.SCHEDULING) {
572         // Use ownership analysis to get alias information
573         CallGraph callGraph = new CallGraph(state);
574         Liveness liveness = new Liveness();
575         ArrayReferencees ar = new ArrayReferencees(state);
576         OwnershipAnalysis oa = null;/*new OwnershipAnalysis(state,
577                                                      tu,
578                                                      callGraph,
579                                                  liveness,
580                                                  ar,
581                                                      state.OWNERSHIPALLOCDEPTH,
582                                                      state.OWNERSHIPWRITEDOTS,
583                                                      state.OWNERSHIPWRITEALL,
584                                                      state.OWNERSHIPALIASFILE);*/
585         
586         // synthesis a layout according to target multicore processor
587         MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
588                                                               ta,
589                                                               oa);
590         if(isDistributeInfo) {
591             mcImplSynthesis.distribution(isDisAll, startnum);
592         } else {
593             double timeStartAnalysis = (double) System.nanoTime();
594             mcImplSynthesis.setScheduleThreshold(20);
595             mcImplSynthesis.setProbThreshold(0);
596             mcImplSynthesis.setGenerateThreshold(30);
597             Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
598             
599             double timeEndAnalysis = (double) System.nanoTime();
600         if(state.BAMBOOCOMPILETIME) {
601           double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow( 10.0, 9.0 ) );
602           System.err.println("The analysis took" + dt +  "sec.");
603           System.exit(0);
604         }
605
606             // generate multicore codes
607             if(state.MULTICORE) {
608                 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
609                                                               bf.getMap(),
610                                                               tu,
611                                                               sa,
612                                                               scheduling,
613                                                               mcImplSynthesis.getCoreNum(),
614                                                               state.CORENUM4GC);
615                 bcm.setOwnershipAnalysis(oa);
616                 bcm.buildCode();
617             }
618             scheduling.clear();
619             scheduling = null;
620         }
621       }
622     }
623     
624     if (state.MGC) {
625       // generate multicore codes
626       if(state.MULTICORE) {
627         BuildCodeMGC bcmgc=new BuildCodeMGC(state,
628                                             bf.getMap(),
629                                             tu,
630                                             sa,
631                                             state.CORENUM,
632                                             state.CORENUM,
633                                             state.CORENUM4GC);
634         bcmgc.buildCode();
635       }
636     }
637   
638     if(!state.MULTICORE) {
639       BuildCode bc;
640
641       if (state.DSM||state.SINGLETM) {
642         CallGraph callgraph=new CallGraph(state);
643         if (state.PREFETCH) {
644           //speed up prefetch generation using locality analysis results
645           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
646           pa=new PrefetchAnalysis(state, callgraph, tu, la);
647         }
648         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
649         GenerateConversions gc=new GenerateConversions(la, state);
650         bc=new BuildCodeTran(state, bf.getMap(), tu, la, pa);
651       } else {
652         if( state.OOOJAVA ) {
653           bc=new BuildOoOJavaCode(state, bf.getMap(), tu, sa, oooa);
654         } else {
655           bc=new BuildCode(state, bf.getMap(), tu, sa);
656         }
657       }
658
659       bc.buildCode();
660       State.logEvent("Done With BuildCode");
661         
662     }
663
664     System.out.println("Lines="+state.lines);
665     System.exit(0);
666   }
667
668   public static void loadClass(State state, BuildIR bir, String sourcefile) {
669     try {
670       ParseNode pn=readSourceFile(state, sourcefile);
671       bir.buildtree(pn, null,sourcefile);
672     } catch (Exception e) {
673       System.out.println("Error in sourcefile:"+sourcefile);
674       e.printStackTrace();
675       System.exit(-1);
676     } catch (Error e) {
677       System.out.println("Error in sourcefile:"+sourcefile);
678       e.printStackTrace();
679       System.exit(-1);
680     }
681   }
682
683   /** Reads in a source file and adds the parse tree to the state object. */
684
685   public static ParseNode readSourceFile(State state, String sourcefile) {
686     try {
687       Reader fr= new BufferedReader(new FileReader(sourcefile));
688       Lex.Lexer l = new Lex.Lexer(fr);
689       java_cup.runtime.lr_parser g;
690       g = new Parse.Parser(l);
691       ParseNode p=null;
692       try {
693         p=(ParseNode) g./*debug_*/parse().value;
694       } catch (Exception e) {
695         System.err.println("Error parsing file:"+sourcefile);
696         e.printStackTrace();
697         System.exit(-1);
698       }
699       state.addParseNode(p);
700       if (l.numErrors()!=0) {
701         System.out.println("Error parsing "+sourcefile);
702         System.exit(l.numErrors());
703       }
704       state.lines+=l.line_num;
705       return p;
706
707     } catch (Exception e) {
708       throw new Error(e);
709     }
710   }
711 }