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