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