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