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