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