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