3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
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;
12 import java.util.Vector;
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;
23 import IR.TaskDescriptor;
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;
52 import Util.GraphNode;
53 import Util.GraphNode.DFS;
54 import Util.GraphNode.SCC;
58 /** Main method for the compiler. */
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(".");
66 String outputdir = null;
67 boolean isDistributeInfo = false;
68 boolean isDisAll = false;
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"))
77 else if (option.equals("-dir"))
78 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
79 else if (option.equals("-fastcheck"))
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")) {
94 state.CORENUM = Integer.parseInt(args[i]);
95 } else if(option.equals("-numcore4gc")) {
97 state.CORENUM4GC = Integer.parseInt(args[i]);
98 } else if (option.equals("-mainclass"))
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"))
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"))
118 else if (option.equals("-abortreaders"))
119 state.ABORTREADERS=true;
120 else if (option.equals("-sandbox"))
122 else if (option.equals("-taskstate"))
123 state.TASKSTATE=true;
124 else if (option.equals("-tagstate"))
126 else if (option.equals("-stmarray"))
128 else if (option.equals("-eventmonitor"))
129 state.EVENTMONITOR=true;
130 else if (option.equals("-dualview"))
132 else if (option.equals("-hybrid"))
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;
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]);
171 else if (option.equals("-disjoint"))
174 else if (option.equals("-disjoint-k")) {
175 state.DISJOINTALLOCDEPTH=Integer.parseInt(args[++i]);
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;
185 throw new Error("disjoint-write-dots requires argument <all/final>");
188 } else if (option.equals("-disjoint-write-ihms")) {
189 state.DISJOINTWRITEIHMS = true;
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;
199 throw new Error("disjoint-alias-file requires arguments <filename> <normal/tabbed>");
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]);
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;
217 throw new Error("disjoint-debug-snap-method requires arguments <method symbol> <# visit to start> <# visits to snap> <T/F stop after>");
220 } else if( option.equals( "-disjoint-release-mode" ) ) {
221 state.DISJOINTRELEASEMODE = true;
223 } else if( option.equals( "-disjoint-dvisit-stack" ) ) {
224 state.DISJOINTDVISITSTACK = true;
226 } else if( option.equals( "-disjoint-dvisit-pqueue" ) ) {
227 state.DISJOINTDVISITPQUE = true;
231 else if (option.equals("-optional"))
233 else if (option.equals("-optimize"))
235 else if (option.equals("-dcopts"))
237 else if (option.equals("-arraypad"))
239 else if (option.equals("-delaycomp"))
240 state.DELAYCOMP=true;
241 else if (option.equals("-raw"))
243 else if (option.equals("-scheduling"))
244 state.SCHEDULING=true;
245 else if (option.equals("-distributioninfo"))
246 isDistributeInfo=true;
247 else if (option.equals("-disall"))
249 else if (option.equals("-disstart"))
250 startnum = Integer.parseInt(args[++i]);
251 else if (option.equals("-useprofile")) {
252 state.USEPROFILE=true;
253 state.profilename = args[++i];
255 else if (option.equals("-thread"))
257 else if (option.equals("-dsm"))
259 else if (option.equals("-recoverystats"))
260 state.DSMRECOVERYSTATS=true;
261 else if (option.equals("-dsmtask"))
263 else if (option.equals("-singleTM"))
265 else if (option.equals("-readset"))
267 else if (option.equals("-webinterface"))
268 state.WEBINTERFACE=true;
269 else if (option.equals("-instructionfailures"))
270 state.INSTRUCTIONFAILURE=true;
271 else if (option.equals("-abcclose"))
272 state.ARRAYBOUNDARYCHECK=false;
274 else if (option.equals("-mlp")) {
276 state.OWNERSHIP = true;
277 state.MLP_NUMCORES = Integer.parseInt( args[++i] );
278 state.MLP_MAXSESEAGE = Integer.parseInt( args[++i] );
280 } else if (option.equals("-mlpdebug")) {
283 } else if (option.equals("-methodeffects")) {
284 state.METHODEFFECTS=true;
285 }else if (option.equals("-help")) {
286 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
287 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
288 System.out.println("-dir outputdirectory -- output code in outputdirectory");
289 System.out.println("-struct structfile -- output structure declarations for repair tool");
290 System.out.println("-mainclass -- main function to call");
291 System.out.println("-dsm -- distributed shared memory support");
292 System.out.println("-singleTM -- single machine committing transactions");
293 System.out.println("-abortreaders -- abort readers");
294 System.out.println("-precise -- use precise garbage collection");
295 System.out.println("-conscheck -- turn on consistency checking");
296 System.out.println("-task -- compiler for tasks");
297 System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
298 System.out.println("-thread -- threads");
299 System.out.println("-trueprob <d> -- probability of true branch");
300 System.out.println("-printflat -- print out flat representation");
301 System.out.println("-instructionfailures -- insert code for instruction level failures");
302 System.out.println("-taskstate -- do task state analysis");
303 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
304 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
305 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
306 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
307 System.out.println("-ownership -- do ownership analysis");
308 System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
309 System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
310 System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
311 System.out.println("-optimize -- enable optimizations");
312 System.out.println("-optional -- enable optional arguments");
313 System.out.println("-abcclose close the array boundary check");
314 System.out.println("-scheduling do task scheduling");
315 System.out.println("-mlp <num cores> <max sese age> build mlp code");
316 System.out.println("-mlpdebug if mlp, report progress and interim results");
317 System.out.println("-multicore generate multi-core version binary");
318 System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
319 System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
320 System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
321 System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
322 System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
323 System.out.println("-threadsimulate generate multi-thread simulate version binary");
324 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");
325 System.out.println("-printscheduling -- print out scheduling graphs");
326 System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
327 System.out.println("-webinterface -- enable web interface");
328 System.out.println("-help -- print out help");
331 sourcefiles.add(args[i]);
335 //add default classpath
336 if (state.classpath.size()==1)
337 state.classpath.add(ClassLibraryPrefix);
339 BuildIR bir=new BuildIR(state);
340 TypeUtil tu=new TypeUtil(state, bir);
343 SemanticCheck sc=new SemanticCheck(state,tu);
345 for(int i=0;i<sourcefiles.size();i++)
346 loadClass(state, bir,(String)sourcefiles.get(i));
348 //Stuff the runtime wants to see
349 sc.getClass("String");
352 sc.getClass("Socket");
353 sc.getClass("ServerSocket");
354 sc.getClass("FileInputStream");
355 sc.getClass("FileOutputStream");
357 sc.getClass("TagDescriptor");
359 if (state.THREAD||state.DSM||state.SINGLETM) {
360 sc.getClass("Thread");
365 tu.createFullTable();
367 BuildFlat bf=new BuildFlat(state,tu);
369 SafetyAnalysis sa=null;
370 PrefetchAnalysis pa=null;
371 MLPAnalysis mlpa=null;
372 if (state.INLINEATOMIC) {
373 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
374 while(classit.hasNext()) {
375 ClassDescriptor cn=(ClassDescriptor)classit.next();
376 Iterator methodit=cn.getMethods();
377 while(methodit.hasNext()) {
379 MethodDescriptor md=(MethodDescriptor)methodit.next();
380 FlatMethod fm=state.getMethodFlat(md);
381 Inliner.inlineAtomic(state, tu, fm, state.inlineatomicdepth);
387 if (state.OPTIMIZE) {
388 CallGraph callgraph=new CallGraph(state);
389 CopyPropagation cp=new CopyPropagation();
390 DeadCode dc=new DeadCode();
391 GlobalFieldType gft=new GlobalFieldType(callgraph, state, tu.getMain());
392 CSE cse=new CSE(gft, tu);
393 localCSE lcse=new localCSE(gft, tu);
394 LoopOptimize lo=new LoopOptimize(gft, tu);
395 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
396 while(classit.hasNext()) {
397 ClassDescriptor cn=(ClassDescriptor)classit.next();
398 Iterator methodit=cn.getMethods();
399 while(methodit.hasNext()) {
400 /* Classify parameters */
401 MethodDescriptor md=(MethodDescriptor)methodit.next();
402 FlatMethod fm=state.getMethodFlat(md);
418 if (state.FLATIRGRAPH) {
419 FlatIRGraph firg = new FlatIRGraph(state,
420 state.FLATIRGRAPHTASKS,
421 state.FLATIRGRAPHUSERMETHODS,
422 state.FLATIRGRAPHLIBMETHODS);
425 if (state.OWNERSHIP && !state.MLP) {
426 CallGraph callGraph = new CallGraph(state);
427 Liveness liveness = new Liveness();
428 ArrayReferencees ar = new ArrayReferencees(state);
429 OwnershipAnalysis oa = new OwnershipAnalysis(state,
434 state.OWNERSHIPALLOCDEPTH,
435 state.OWNERSHIPWRITEDOTS,
436 state.OWNERSHIPWRITEALL,
437 state.OWNERSHIPALIASFILE,
438 state.METHODEFFECTS);
442 CallGraph callGraph = new CallGraph(state);
443 Liveness liveness = new Liveness();
444 ArrayReferencees ar = new ArrayReferencees(state);
445 OwnershipAnalysis oa = new OwnershipAnalysis(state,
450 state.OWNERSHIPALLOCDEPTH,
451 state.OWNERSHIPWRITEDOTS,
452 state.OWNERSHIPWRITEALL,
453 state.OWNERSHIPALIASFILE,
454 state.METHODEFFECTS);
455 mlpa = new MLPAnalysis(state,
461 if (state.DISJOINT) {
462 CallGraph cg = new CallGraph(state);
463 Liveness l = new Liveness();
464 ArrayReferencees ar = new ArrayReferencees(state);
465 DisjointAnalysis oa = new DisjointAnalysis(state, tu, cg, l, ar);
468 if (state.TAGSTATE) {
469 CallGraph callgraph=new CallGraph(state);
470 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
471 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
474 if (state.TASKSTATE) {
475 CallGraph callgraph=new CallGraph(state);
476 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
477 TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
479 TaskGraph tg=new TaskGraph(state, ta);
482 if (state.OPTIONAL) {
483 ExecutionGraph et=new ExecutionGraph(state, ta);
484 et.createExecutionGraph();
485 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
487 state.storeAnalysisResult(sa.getResult());
488 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
491 if (state.WEBINTERFACE) {
492 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
493 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
494 JhttpServer serve=new JhttpServer(8000,wi);
498 if (state.SCHEDULING) {
499 // Use ownership analysis to get alias information
500 CallGraph callGraph = new CallGraph(state);
501 Liveness liveness = new Liveness();
502 ArrayReferencees ar = new ArrayReferencees(state);
503 OwnershipAnalysis oa = new OwnershipAnalysis(state,
508 state.OWNERSHIPALLOCDEPTH,
509 state.OWNERSHIPWRITEDOTS,
510 state.OWNERSHIPWRITEALL,
511 state.OWNERSHIPALIASFILE);
513 // synthesis a layout according to target multicore processor
514 MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
517 if(isDistributeInfo) {
518 mcImplSynthesis.distribution(isDisAll, startnum);
520 double timeStartAnalysis = (double) System.nanoTime();
521 mcImplSynthesis.setScheduleThreshold(20);
522 mcImplSynthesis.setProbThreshold(0);
523 mcImplSynthesis.setGenerateThreshold(30);
524 Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
526 double timeEndAnalysis = (double) System.nanoTime();
527 if(state.BAMBOOCOMPILETIME) {
528 double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow( 10.0, 9.0 ) );
529 System.err.println("The analysis took" + dt + "sec.");
533 // generate multicore codes
534 if(state.MULTICORE) {
535 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
540 mcImplSynthesis.getCoreNum(),
543 bcm.setOwnershipAnalysis(oa);
551 if(!state.MULTICORE) {
552 if (state.DSM||state.SINGLETM) {
553 CallGraph callgraph=new CallGraph(state);
554 if (state.PREFETCH) {
555 //speed up prefetch generation using locality analysis results
556 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
557 pa=new PrefetchAnalysis(state, callgraph, tu, la);
559 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
560 GenerateConversions gc=new GenerateConversions(la, state);
561 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa, mlpa);
564 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa, mlpa);
569 System.out.println("Lines="+state.lines);
573 public static void loadClass(State state, BuildIR bir, String sourcefile) {
575 ParseNode pn=readSourceFile(state, sourcefile);
576 bir.buildtree(pn, null);
577 } catch (Exception e) {
578 System.out.println("Error in sourcefile:"+sourcefile);
582 System.out.println("Error in sourcefile:"+sourcefile);
588 /** Reads in a source file and adds the parse tree to the state object. */
590 public static ParseNode readSourceFile(State state, String sourcefile) {
592 Reader fr= new BufferedReader(new FileReader(sourcefile));
593 Lex.Lexer l = new Lex.Lexer(fr);
594 java_cup.runtime.lr_parser g;
595 g = new Parse.Parser(l);
598 p=(ParseNode) g./*debug_*/parse().value;
599 } catch (Exception e) {
600 System.err.println("Error parsing file:"+sourcefile);
604 state.addParseNode(p);
605 if (l.numErrors()!=0) {
606 System.out.println("Error parsing "+sourcefile);
607 System.exit(l.numErrors());
609 state.lines+=l.line_num;
612 } catch (Exception e) {