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