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