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.MLP.MLPAnalysis;
45 import Analysis.Loops.*;
46 import IR.MethodDescriptor;
47 import IR.Flat.FlatMethod;
49 import Util.GraphNode;
50 import Util.GraphNode.DFS;
51 import Util.GraphNode.SCC;
55 /** Main method for the compiler. */
57 public static void main(String args[]) throws Exception {
58 String ClassLibraryPrefix="./ClassLibrary/";
59 State state=new State();
60 Vector sourcefiles=new Vector();
61 state.classpath.add(".");
63 String outputdir = null;
64 boolean isDistributeInfo = false;
66 for(int i=0; i<args.length; i++) {
67 String option=args[i];
68 if (option.equals("-precise"))
69 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
70 else if (option.equals("-prefetch"))
72 else if (option.equals("-dir"))
73 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
74 else if (option.equals("-fastcheck"))
76 else if (option.equals("-selfloop"))
77 state.selfloops.add(args[++i]);
78 else if (option.equals("-outputdir"))
79 state.outputdir = args[++i];
80 else if (option.equals("-excprefetch"))
81 state.excprefetch.add(args[++i]);
82 else if (option.equals("-classlibrary"))
83 state.classpath.add(args[++i]);
84 else if (option.equals("-inlineatomic")) {
85 state.INLINEATOMIC=true;
86 state.inlineatomicdepth=Integer.parseInt(args[++i]);
87 } else if(option.equals("-numcore")) {
89 state.CORENUM = Integer.parseInt(args[i]);
90 } else if (option.equals("-mainclass"))
92 else if (option.equals("-trueprob")) {
93 state.TRUEPROB=Double.parseDouble(args[++i]);
94 } else if (option.equals("-printflat"))
96 else if (option.equals("-printscheduling"))
97 State.PRINTSCHEDULING=true;
98 else if (option.equals("-minimize"))
100 else if (option.equals("-printschedulesim"))
101 State.PRINTSCHEDULESIM=true;
102 else if (option.equals("-printcriticalpath"))
103 State.PRINTCRITICALPATH=true;
104 else if (option.equals("-struct"))
105 state.structfile=args[++i];
106 else if (option.equals("-conscheck"))
107 state.CONSCHECK=true;
108 else if (option.equals("-task"))
110 else if (option.equals("-abortreaders"))
111 state.ABORTREADERS=true;
112 else if (option.equals("-taskstate"))
113 state.TASKSTATE=true;
114 else if (option.equals("-tagstate"))
116 else if (option.equals("-flatirtasks")) {
117 state.FLATIRGRAPH=true;
118 state.FLATIRGRAPHTASKS=true;
119 } else if (option.equals("-flatirusermethods")) {
120 state.FLATIRGRAPH=true;
121 state.FLATIRGRAPHUSERMETHODS=true;
122 } else if (option.equals("-flatirlibmethods")) {
123 state.FLATIRGRAPH=true;
124 state.FLATIRGRAPHLIBMETHODS=true;
125 } else if (option.equals("-multicore"))
126 state.MULTICORE=true;
127 else if (option.equals("-ownership"))
128 state.OWNERSHIP=true;
129 else if (option.equals("-ownallocdepth")) {
130 state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
131 } else if (option.equals("-ownwritedots")) {
132 state.OWNERSHIPWRITEDOTS=true;
133 if (args[++i].equals("all")) {
134 state.OWNERSHIPWRITEALL=true;
136 } else if (option.equals("-ownaliasfile"))
137 state.OWNERSHIPALIASFILE=args[++i];
138 else if (option.equals("-optional"))
140 else if (option.equals("-optimize"))
142 else if (option.equals("-dcopts"))
144 else if (option.equals("-delaycomp"))
145 state.DELAYCOMP=true;
146 else if (option.equals("-raw"))
148 else if (option.equals("-scheduling"))
149 state.SCHEDULING=true;
150 else if (option.equals("-distributioninfo"))
151 isDistributeInfo=true;
152 else if (option.equals("-useprofile"))
153 state.USEPROFILE=true;
154 else if (option.equals("-thread"))
156 else if (option.equals("-dsm"))
158 else if (option.equals("-singleTM"))
160 else if (option.equals("-webinterface"))
161 state.WEBINTERFACE=true;
162 else if (option.equals("-instructionfailures"))
163 state.INSTRUCTIONFAILURE=true;
164 else if (option.equals("-abcclose"))
165 state.ARRAYBOUNDARYCHECK=false;
166 else if (option.equals("-mlp")) {
168 state.OWNERSHIP=true;
169 } else if (option.equals("-mlpdebug")) {
172 state.OWNERSHIP=true;
173 } else if (option.equals("-help")) {
174 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
175 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
176 System.out.println("-dir outputdirectory -- output code in outputdirectory");
177 System.out.println("-struct structfile -- output structure declarations for repair tool");
178 System.out.println("-mainclass -- main function to call");
179 System.out.println("-dsm -- distributed shared memory support");
180 System.out.println("-singleTM -- single machine committing transactions");
181 System.out.println("-abortreaders -- abort readers");
182 System.out.println("-precise -- use precise garbage collection");
183 System.out.println("-conscheck -- turn on consistency checking");
184 System.out.println("-task -- compiler for tasks");
185 System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
186 System.out.println("-thread -- threads");
187 System.out.println("-trueprob <d> -- probability of true branch");
188 System.out.println("-printflat -- print out flat representation");
189 System.out.println("-instructionfailures -- insert code for instruction level failures");
190 System.out.println("-taskstate -- do task state analysis");
191 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
192 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
193 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
194 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
195 System.out.println("-ownership -- do ownership analysis");
196 System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
197 System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
198 System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
199 System.out.println("-optimize -- enable optimizations");
200 System.out.println("-optional -- enable optional arguments");
201 System.out.println("-abcclose close the array boundary check");
202 System.out.println("-scheduling do task scheduling");
203 System.out.println("-mlp build mlp code");
204 System.out.println("-mlp build mlp code, report progress and interim results");
205 System.out.println("-multicore generate multi-core version binary");
206 System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
207 System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
208 System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
209 System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
210 System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
211 System.out.println("-threadsimulate generate multi-thread simulate version binary");
212 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");
213 System.out.println("-printscheduling -- print out scheduling graphs");
214 System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
215 System.out.println("-webinterface -- enable web interface");
216 System.out.println("-help -- print out help");
219 sourcefiles.add(args[i]);
223 //add default classpath
224 if (state.classpath.size()==1)
225 state.classpath.add(ClassLibraryPrefix);
227 BuildIR bir=new BuildIR(state);
228 TypeUtil tu=new TypeUtil(state, bir);
231 SemanticCheck sc=new SemanticCheck(state,tu);
233 for(int i=0;i<sourcefiles.size();i++)
234 loadClass(state, bir,(String)sourcefiles.get(i));
236 //Stuff the runtime wants to see
237 sc.getClass("String");
240 sc.getClass("Socket");
241 sc.getClass("ServerSocket");
242 sc.getClass("FileInputStream");
243 sc.getClass("FileOutputStream");
245 sc.getClass("TagDescriptor");
247 if (state.THREAD||state.DSM||state.SINGLETM) {
248 sc.getClass("Thread");
253 tu.createFullTable();
255 BuildFlat bf=new BuildFlat(state,tu);
257 SafetyAnalysis sa=null;
258 PrefetchAnalysis pa=null;
259 MLPAnalysis mlpa=null;
260 if (state.INLINEATOMIC) {
261 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
262 while(classit.hasNext()) {
263 ClassDescriptor cn=(ClassDescriptor)classit.next();
264 Iterator methodit=cn.getMethods();
265 while(methodit.hasNext()) {
267 MethodDescriptor md=(MethodDescriptor)methodit.next();
268 FlatMethod fm=state.getMethodFlat(md);
269 Inliner.inlineAtomic(state, tu, fm, state.inlineatomicdepth);
275 if (state.OPTIMIZE) {
276 CallGraph callgraph=new CallGraph(state);
277 CopyPropagation cp=new CopyPropagation();
278 DeadCode dc=new DeadCode();
279 GlobalFieldType gft=new GlobalFieldType(callgraph, state, tu.getMain());
280 CSE cse=new CSE(gft, tu);
281 localCSE lcse=new localCSE(gft, tu);
282 LoopOptimize lo=new LoopOptimize(gft, tu);
283 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
284 while(classit.hasNext()) {
285 ClassDescriptor cn=(ClassDescriptor)classit.next();
286 Iterator methodit=cn.getMethods();
287 while(methodit.hasNext()) {
288 /* Classify parameters */
289 MethodDescriptor md=(MethodDescriptor)methodit.next();
290 FlatMethod fm=state.getMethodFlat(md);
306 if (state.FLATIRGRAPH) {
307 FlatIRGraph firg = new FlatIRGraph(state,
308 state.FLATIRGRAPHTASKS,
309 state.FLATIRGRAPHUSERMETHODS,
310 state.FLATIRGRAPHLIBMETHODS);
313 if (state.OWNERSHIP && !state.MLP) {
314 CallGraph callGraph = new CallGraph(state);
315 OwnershipAnalysis oa = new OwnershipAnalysis(state,
318 state.OWNERSHIPALLOCDEPTH,
319 state.OWNERSHIPWRITEDOTS,
320 state.OWNERSHIPWRITEALL,
321 state.OWNERSHIPALIASFILE);
325 CallGraph callGraph = new CallGraph(state);
326 OwnershipAnalysis oa = new OwnershipAnalysis(state,
329 state.OWNERSHIPALLOCDEPTH,
330 state.OWNERSHIPWRITEDOTS,
331 state.OWNERSHIPWRITEALL,
332 state.OWNERSHIPALIASFILE);
333 mlpa = new MLPAnalysis(state,
339 if (state.TAGSTATE) {
340 CallGraph callgraph=new CallGraph(state);
341 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
342 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
345 if (state.TASKSTATE) {
346 CallGraph callgraph=new CallGraph(state);
347 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
348 TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
350 TaskGraph tg=new TaskGraph(state, ta);
353 if (state.OPTIONAL) {
354 ExecutionGraph et=new ExecutionGraph(state, ta);
355 et.createExecutionGraph();
356 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
358 state.storeAnalysisResult(sa.getResult());
359 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
362 if (state.WEBINTERFACE) {
363 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
364 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
365 JhttpServer serve=new JhttpServer(8000,wi);
369 if (state.SCHEDULING) {
370 // Use ownership analysis to get alias information
371 CallGraph callGraph = new CallGraph(state);
372 OwnershipAnalysis oa = new OwnershipAnalysis(state,
375 state.OWNERSHIPALLOCDEPTH,
376 state.OWNERSHIPWRITEDOTS,
377 state.OWNERSHIPWRITEALL,
378 state.OWNERSHIPALIASFILE);
380 // synthesis a layout according to target multicore processor
381 MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
384 if(isDistributeInfo) {
385 mcImplSynthesis.distribution();
387 //double timeStartAnalysis = (double) System.nanoTime();
388 mcImplSynthesis.setScheduleThreshold(20);
389 mcImplSynthesis.setProbThreshold(0);
390 mcImplSynthesis.setGenerateThreshold(30);
391 Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
393 //double timeEndAnalysis = (double) System.nanoTime();
394 //double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow( 10.0, 9.0 ) );
395 //System.err.println("The analysis took" + dt + "sec.");
397 // generate multicore codes
398 if(state.MULTICORE) {
399 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
404 mcImplSynthesis.getCoreNum(),
406 bcm.setOwnershipAnalysis(oa);
414 if(!state.MULTICORE) {
415 if (state.DSM||state.SINGLETM) {
416 CallGraph callgraph=new CallGraph(state);
417 if (state.PREFETCH) {
418 //speed up prefetch generation using locality analysis results
419 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
420 pa=new PrefetchAnalysis(state, callgraph, tu, la);
422 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
423 GenerateConversions gc=new GenerateConversions(la, state);
424 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa, mlpa);
427 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa, mlpa);
432 System.out.println("Lines="+state.lines);
436 public static void loadClass(State state, BuildIR bir, String sourcefile) {
437 ParseNode pn=readSourceFile(state, sourcefile);
438 bir.buildtree(pn, null);
441 /** Reads in a source file and adds the parse tree to the state object. */
443 public static ParseNode readSourceFile(State state, String sourcefile) {
445 Reader fr= new BufferedReader(new FileReader(sourcefile));
446 Lex.Lexer l = new Lex.Lexer(fr);
447 java_cup.runtime.lr_parser g;
448 g = new Parse.Parser(l);
451 p=(ParseNode) g./*debug_*/parse().value;
452 } catch (Exception e) {
453 System.err.println("Error parsing file:"+sourcefile);
457 state.addParseNode(p);
458 if (l.numErrors()!=0) {
459 System.out.println("Error parsing "+sourcefile);
460 System.exit(l.numErrors());
462 state.lines+=l.line_num;
465 } catch (Exception e) {