2ec01a630eb17ea5cd3a3775a1e171616182a618
[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("-disjoint-disable-global-sweep") ) {
299         state.DISJOINT_USE_GLOBAL_SWEEP = false;
300
301       } else if( option.equals("-disjoint-disable-strong-update") ) {
302         state.DISJOINT_USE_STRONG_UPDATE = false;
303
304       } else if( option.equals("-disjoint-disable-predicates") ) {
305         state.DISJOINT_USE_PREDICATES = false;
306
307       } else if( option.equals("-disjoint-summarize-per-class") ) {
308         state.DISJOINT_SUMMARIZE_PER_CLASS = true;
309
310
311       } else if( option.equals("-disjoint-count-visits") ) {
312         state.DISJOINT_COUNT_VISITS = true;
313
314       } else if( option.equals("-disjoint-count-graph-elements") ) {
315         state.DISJOINT_COUNT_GRAPH_ELEMENTS = true;
316         state.DISJOINT_COUNT_GRAPH_ELEMENTS_FILE = args[++i];
317
318       } else if (option.equals("-optional"))
319         state.OPTIONAL=true;
320       else if (option.equals("-optimize"))
321         state.OPTIMIZE=true;
322       else if (option.equals("-noloop"))
323         state.NOLOOP=true;
324       else if (option.equals("-dcopts"))
325         state.DCOPTS=true;
326       else if (option.equals("-arraypad"))
327         state.ARRAYPAD=true;
328       else if (option.equals("-delaycomp"))
329         state.DELAYCOMP=true;
330       else if (option.equals("-raw"))
331         state.RAW=true;
332       else if (option.equals("-scheduling"))
333         state.SCHEDULING=true;
334       else if (option.equals("-distributioninfo"))
335         isDistributeInfo=true;
336       else if (option.equals("-disall"))
337         isDisAll=true;
338       else if (option.equals("-disstart"))
339         startnum = Integer.parseInt(args[++i]);
340       else if (option.equals("-useprofile")) {
341         state.USEPROFILE=true;
342         state.profilename = args[++i];
343       } else if (option.equals("-thread"))
344         state.THREAD=true;
345       else if (option.equals("-dsm"))
346         state.DSM=true;
347       else if (option.equals("-recoverystats"))
348         state.DSMRECOVERYSTATS=true;
349       else if (option.equals("-dsmtask"))
350         state.DSMTASK=true;
351       else if (option.equals("-singleTM"))
352         state.SINGLETM=true;
353       else if (option.equals("-readset"))
354         state.READSET=true;
355       else if (option.equals("-webinterface"))
356         state.WEBINTERFACE=true;
357       else if (option.equals("-instructionfailures"))
358         state.INSTRUCTIONFAILURE=true;
359       else if (option.equals("-abcclose"))
360         state.ARRAYBOUNDARYCHECK=false;
361
362       else if (option.equals("-methodeffects")) {
363         state.METHODEFFECTS=true;
364
365       } else if (option.equals("-coreprof")) {
366         state.COREPROF=true;
367
368       } else if (option.equals("-ooojava")) {
369         state.OOOJAVA  = true;
370         state.DISJOINT = true;
371         state.OOO_NUMCORES   = Integer.parseInt(args[++i]);
372         state.OOO_MAXSESEAGE = Integer.parseInt(args[++i]);
373
374       } else if (option.equals("-ooodebug") ) {
375         state.OOODEBUG  = true;
376       } else if (option.equals("-rcr")) {
377         state.RCR = true;
378         state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
379       } else if (option.equals("-rcr_debug")) {
380         state.RCR_DEBUG = true;
381         state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
382       } else if (option.equals("-rcr_debug_verbose")) {
383         state.RCR_DEBUG_VERBOSE = true;
384         state.KEEP_RG_FOR_ALL_PROGRAM_POINTS=true;
385       } else if (option.equals("-nostalltr")) {
386         state.NOSTALLTR = true;
387
388       } else if (option.equals("-ssjava")) {
389         state.SSJAVA = true;
390         state.SSJAVA_GENCODE_PREVENT_CRASHES = true;
391
392       } else if (option.equals("-ssjavadebug")) {
393         state.SSJAVADEBUG = true;
394       } else if (option.equals("-ssjavainfer")) {
395         state.SSJAVAINFER= true;
396       } else if( option.equals( "-ssjava-inject-error" ) ) {
397         state.SSJAVA_GENCODE_PREVENT_CRASHES = true;
398         state.SSJAVA_INJECT_ERROR   = true;
399         state.SSJAVA_INV_ERROR_PROB = Integer.parseInt( args[++i] );
400         state.SSJAVA_ERROR_SEED     = Integer.parseInt( args[++i] );
401
402         // special case, if the inverse prob is 0, turn off errors
403         if( state.SSJAVA_INV_ERROR_PROB == 0 ) {
404           state.SSJAVA_INJECT_ERROR = false;
405         }
406
407
408       }else if (option.equals("-printlinenum")) {
409         state.LINENUM=true;
410       } else if (option.equals("-help")) {
411         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
412         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
413         System.out.println("-dir outputdirectory -- output code in outputdirectory");
414         System.out.println("-struct structfile -- output structure declarations for repair tool");
415         System.out.println("-mainclass -- main function to call");
416         System.out.println("-dsm -- distributed shared memory support");
417         System.out.println("-singleTM -- single machine committing transactions");
418         System.out.println("-abortreaders -- abort readers");
419         System.out.println("-precise -- use precise garbage collection");
420         System.out.println("-conscheck -- turn on consistency checking");
421         System.out.println("-task -- compiler for tasks");
422         System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
423         System.out.println("-thread -- threads");
424         System.out.println("-trueprob <d> -- probability of true branch");
425         System.out.println("-printflat -- print out flat representation");
426         System.out.println("-instructionfailures -- insert code for instruction level failures");
427         System.out.println("-taskstate -- do task state analysis");
428         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
429         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
430         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
431         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
432         System.out.println("-ownership -- do ownership analysis");
433         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
434         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
435         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
436         System.out.println("-optimize -- enable optimizations");
437         System.out.println("-noloop -- disable loop optimizations");
438         System.out.println("-optional -- enable optional arguments");
439         System.out.println("-abcclose close the array boundary check");
440         System.out.println("-scheduling do task scheduling");
441         System.out.println("-mlp <num cores> <max sese age> build mlp code");
442         System.out.println("-mlpdebug if mlp, report progress and interim results");
443         System.out.println("-multicore generate multi-core version binary");
444         System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
445         System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
446         System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
447         System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
448         System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
449         System.out.println("-threadsimulate generate multi-thread simulate version binary");
450         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");
451         System.out.println("-printscheduling -- print out scheduling graphs");
452         System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
453         System.out.println("-webinterface -- enable web interface");
454         System.out.println("-linenum print out line numbers in generated C codes");
455         System.out.println("-help -- print out help");
456         System.exit(0);
457       } else {
458         sourcefiles.add(args[i]);
459       }
460     }
461
462     //add default classpath
463     if (state.classpath.size()==1)
464       state.classpath.add(ClassLibraryPrefix);
465
466     State.logEvent("Done Parsing Commands");
467     System.out.println("Classpath: "+state.classpath);
468
469     
470
471     TypeUtil tu;
472     BuildFlat bf;
473     JavaBuilder jb=null;
474
475     if (!state.JNI) {
476       BuildIR bir=new BuildIR(state);
477       tu=new TypeUtil(state, bir);
478       SemanticCheck sc=new SemanticCheck(state,tu);
479
480       for(int i=0; i<sourcefiles.size(); i++)
481         loadClass(state, bir, sourcefiles.get(i));
482
483       //Stuff the runtime wants to see
484
485       if (state.TASK) {
486         sc.getClass(null, "TagDescriptor");
487       }
488
489       sc.semanticCheck();
490       State.logEvent("Done Semantic Checking");
491
492       tu.createFullTable();
493       State.logEvent("Done Creating TypeUtil");
494
495       bf=new BuildFlat(state,tu);
496       bf.buildFlat();
497       State.logEvent("Done Building Flat");
498     } else {
499       jb=new JavaBuilder(state);
500       jb.build();
501       tu=jb.getTypeUtil();
502       bf=jb.getBuildFlat();
503     }
504
505     SafetyAnalysis sa=null;
506     PrefetchAnalysis pa=null;
507     OoOJavaAnalysis oooa=null;
508     HeapAnalysis heapAnalysis=null;
509
510     if (state.INLINEATOMIC) {
511       Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
512       while(classit.hasNext()) {
513         ClassDescriptor cn=(ClassDescriptor)classit.next();
514         Iterator methodit=cn.getMethods();
515         while(methodit.hasNext()) {
516           // do inlining
517           MethodDescriptor md=(MethodDescriptor)methodit.next();
518           FlatMethod fm=state.getMethodFlat(md);
519           Inliner.inlineAtomic(state, tu, fm, state.inlineatomicdepth);
520         }
521       }
522     }
523
524     CallGraph callgraph=jb!=null?jb:(state.TASK?new BaseCallGraph(state, tu):new JavaCallGraph(state, tu));
525
526     // SSJava
527     SSJavaAnalysis ssjava=new SSJavaAnalysis(state,tu,bf,callgraph);
528     if(state.SSJAVA) {
529       ssjava.doCheck();
530       State.logEvent("Done SSJava Checking");
531     }
532
533     if (state.OPTIMIZE) {
534       CopyPropagation cp=new CopyPropagation();
535       DeadCode dc=new DeadCode();
536       GlobalFieldType gft=new GlobalFieldType(callgraph, state, tu.getMain());
537       CSE cse=new CSE(gft, tu);
538       localCSE lcse=new localCSE(gft, tu);
539       LoopOptimize lo=null;
540       if (!state.NOLOOP)
541         lo=new LoopOptimize(gft, tu);
542       Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
543       while(classit.hasNext()) {
544         ClassDescriptor cn=(ClassDescriptor)classit.next();
545         Iterator methodit=cn.getMethods();
546         while(methodit.hasNext()) {
547           /* Classify parameters */
548           MethodDescriptor md=(MethodDescriptor)methodit.next();
549           FlatMethod fm=state.getMethodFlat(md);
550           if (fm==null)
551             continue;
552           cp.optimize(fm);
553           dc.optimize(fm);
554           if (!state.NOLOOP){            
555             lo.analyze(fm);
556             lo.optimize(fm);
557           }
558             
559           
560           cp.optimize(fm);
561           dc.optimize(fm);
562           lcse.doAnalysis(fm);
563           cse.doAnalysis(fm);
564           cp.optimize(fm);
565           dc.optimize(fm);
566           cp.optimize(fm);
567           dc.optimize(fm);
568         }
569       }
570       State.logEvent("Done Optimizing");
571       
572     }
573
574     if (state.FLATIRGRAPH) {
575       FlatIRGraph firg = new FlatIRGraph(state,
576                                          state.FLATIRGRAPHTASKS,
577                                          state.FLATIRGRAPHUSERMETHODS,
578                                          state.FLATIRGRAPHLIBMETHODS);
579     }
580
581     if (state.OWNERSHIP) {
582       Liveness liveness = new Liveness();
583       ArrayReferencees ar = new ArrayReferencees(state, tu, callgraph);
584       OwnershipAnalysis oa = new OwnershipAnalysis(state,
585                                                    tu,
586                                                    callgraph,
587                                                    liveness,
588                                                    ar,
589                                                    state.OWNERSHIPALLOCDEPTH,
590                                                    state.OWNERSHIPWRITEDOTS,
591                                                    state.OWNERSHIPWRITEALL,
592                                                    state.OWNERSHIPALIASFILE,
593                                                    state.METHODEFFECTS);
594     }
595
596     if (state.DISJOINT && !state.OOOJAVA) {
597       Liveness l  = new Liveness();
598       ArrayReferencees ar = new ArrayReferencees(state, tu, callgraph);
599       DisjointAnalysis da = new DisjointAnalysis(state, tu, callgraph, l, ar, null, null);
600       heapAnalysis = da;
601     }
602
603     if (state.OOOJAVA) {
604       Liveness l   = new Liveness();
605       ArrayReferencees ar  = new ArrayReferencees(state, tu, callgraph);
606       oooa = new OoOJavaAnalysis(state, tu, callgraph, l, ar);
607       heapAnalysis = oooa.getHeapAnalysis();
608     }
609
610
611     if (state.TAGSTATE) {
612       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
613       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
614     }
615
616     if (state.TASKSTATE) {
617       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
618       TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
619       ta.taskAnalysis();
620       TaskGraph tg=new TaskGraph(state, ta);
621       tg.createDOTfiles();
622
623       if (state.OPTIONAL) {
624         ExecutionGraph et=new ExecutionGraph(state, ta);
625         et.createExecutionGraph();
626         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
627         sa.doAnalysis();
628         state.storeAnalysisResult(sa.getResult());
629         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
630       }
631
632       if (state.WEBINTERFACE) {
633         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
634         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
635         JhttpServer serve=new JhttpServer(8000,wi);
636         serve.run();
637       }
638
639       if (state.SCHEDULING) {
640         // Use ownership analysis to get alias information
641         Liveness liveness = new Liveness();
642         ArrayReferencees ar = new ArrayReferencees(state, tu, callgraph);
643         OwnershipAnalysis oa = null; /*new OwnershipAnalysis(state,
644                                                      tu,
645                                                      callGraph,
646                                                  liveness,
647                                                  ar,
648                                                      state.OWNERSHIPALLOCDEPTH,
649                                                      state.OWNERSHIPWRITEDOTS,
650                                                      state.OWNERSHIPWRITEALL,
651                                                      state.OWNERSHIPALIASFILE);*/
652
653         // synthesis a layout according to target multicore processor
654         MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
655                                                               ta,
656                                                               oa);
657         if(isDistributeInfo) {
658           mcImplSynthesis.distribution(isDisAll, startnum);
659         } else {
660           double timeStartAnalysis = (double) System.nanoTime();
661           mcImplSynthesis.setScheduleThreshold(20);
662           mcImplSynthesis.setProbThreshold(0);
663           mcImplSynthesis.setGenerateThreshold(30);
664           Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
665
666           double timeEndAnalysis = (double) System.nanoTime();
667           if(state.BAMBOOCOMPILETIME) {
668             double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow(10.0, 9.0) );
669             System.err.println("The analysis took" + dt +  "sec.");
670             System.exit(0);
671           }
672
673           // generate multicore codes
674           if(state.MULTICORE) {
675             BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
676                                                           bf.getMap(),
677                                                           tu,
678                                                           sa,
679                                                           scheduling,
680                                                           mcImplSynthesis.getCoreNum(),
681                                                           state.CORENUM4GC, callgraph);
682             bcm.setOwnershipAnalysis(oa);
683             bcm.buildCode();
684           }
685           scheduling.clear();
686           scheduling = null;
687         }
688       }
689     }
690
691     if (state.MGC) {
692       // generate multicore codes
693       if(state.MULTICORE) {
694         BuildCodeMGC bcmgc=new BuildCodeMGC(state,
695                                             bf.getMap(),
696                                             tu,
697                                             sa,
698                                             state.CORENUM,
699                                             state.CORENUM,
700                                             state.CORENUM4GC, callgraph);
701         bcmgc.buildCode();
702       }
703     }
704
705     if(!state.MULTICORE) {
706       BuildCode bc;
707
708       if (state.DSM||state.SINGLETM) {
709         if (state.PREFETCH) {
710           //speed up prefetch generation using locality analysis results
711           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
712           pa=new PrefetchAnalysis(state, callgraph, tu, la);
713         }
714         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
715         GenerateConversions gc=new GenerateConversions(la, state);
716         bc=new BuildCodeTran(state, bf.getMap(), tu, la, pa, callgraph);
717       } else {
718         if( state.OOOJAVA ) {
719           bc=new BuildOoOJavaCode(state, bf.getMap(), tu, sa, oooa, callgraph);
720         } else {
721           bc=new BuildCode(state, bf.getMap(), tu, sa, callgraph, jb);
722         }
723       }
724
725       if( (state.OOOJAVA || state.POINTSTO_CHECK_V_RUNTIME) &&
726           heapAnalysis != null ) {
727         // use this extension to generate the allocsite field of Object and
728         // ArrayObject for whatever other extensions and systems need it
729         BCXallocsiteObjectField bcx = new BCXallocsiteObjectField( bc, tu, heapAnalysis );
730         bc.registerExtension( bcx );
731       }
732
733       if( state.POINTSTO_CHECK_V_RUNTIME &&
734           heapAnalysis != null ) {
735         BCXPointsToCheckVRuntime bcx = new BCXPointsToCheckVRuntime( state, bc, tu, heapAnalysis );
736         bc.registerExtension( bcx );
737       }
738
739       if( state.SSJAVA_INJECT_ERROR ) {
740         BCXSSJavaInjectError bcx = new BCXSSJavaInjectError( state, bc );
741         bc.registerExtension( bcx );
742       }
743
744       bc.buildCode();
745       State.logEvent("Done With BuildCode");
746
747     }
748
749     System.out.println("Lines="+state.lines);
750     System.exit(0);
751   }
752
753   public static void loadClass(State state, BuildIR bir, String sourcefile) {
754     try {
755       ParseNode pn=readSourceFile(state, sourcefile);
756       bir.buildtree(pn, null,sourcefile);
757     } catch (Exception e) {
758       System.out.println("Error in sourcefile:"+sourcefile);
759       e.printStackTrace();
760       System.exit(-1);
761     } catch (Error e) {
762       System.out.println("Error in sourcefile:"+sourcefile);
763       e.printStackTrace();
764       System.exit(-1);
765     }
766   }
767
768   /** Reads in a source file and adds the parse tree to the state object. */
769
770   public static ParseNode readSourceFile(State state, String sourcefile) {
771     try {
772       Reader fr= new BufferedReader(new FileReader(sourcefile));
773       Lex.Lexer l = new Lex.Lexer(fr, state.TASK, state.DSM);
774       java_cup.runtime.lr_parser g;
775       g = new Parse.Parser(l);
776       ParseNode p=null;
777       try {
778         p=(ParseNode) g./*debug_*/ parse().value;
779       } catch (Exception e) {
780         System.err.println("Error parsing file:"+sourcefile);
781         e.printStackTrace();
782         System.exit(-1);
783       }
784       state.addParseNode(p);
785       if (l.numErrors()!=0) {
786         System.out.println("Error parsing "+sourcefile);
787         System.exit(l.numErrors());
788       }
789       state.lines+=l.line_num;
790       return p;
791
792     } catch (Exception e) {
793       throw new Error(e);
794     }
795   }
796 }