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