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.ClassDescriptor;
22 import IR.TaskDescriptor;
24 import Analysis.Scheduling.MCImplSynthesis;
25 import Analysis.Scheduling.Schedule;
26 import Analysis.Scheduling.ScheduleAnalysis;
27 import Analysis.Scheduling.ScheduleSimulator;
28 import Analysis.TaskStateAnalysis.TaskAnalysis;
29 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
30 import Analysis.TaskStateAnalysis.TaskGraph;
31 import Analysis.CallGraph.CallGraph;
32 import Analysis.TaskStateAnalysis.FEdge;
33 import Analysis.TaskStateAnalysis.FlagState;
34 import Analysis.TaskStateAnalysis.TagAnalysis;
35 import Analysis.TaskStateAnalysis.GarbageAnalysis;
36 import Analysis.TaskStateAnalysis.ExecutionGraph;
37 import Analysis.TaskStateAnalysis.SafetyAnalysis;
38 import Analysis.Locality.LocalityAnalysis;
39 import Analysis.Locality.GenerateConversions;
40 import Analysis.Prefetch.PrefetchAnalysis;
41 import Analysis.FlatIRGraph.FlatIRGraph;
42 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
44 import Util.GraphNode;
45 import Util.GraphNode.DFS;
46 import Util.GraphNode.SCC;
50 /** Main method for the compiler. */
52 public static void main(String args[]) throws Exception {
53 String ClassLibraryPrefix="./ClassLibrary/";
54 State state=new State();
55 Vector sourcefiles=new Vector();
56 state.classpath.add(".");
58 String outputdir = null;
59 boolean isDistributeInfo = false;
61 for(int i=0; i<args.length; i++) {
62 String option=args[i];
63 if (option.equals("-precise"))
64 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
65 else if (option.equals("-prefetch"))
67 else if (option.equals("-dir"))
68 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
69 else if (option.equals("-fastcheck"))
71 else if (option.equals("-selfloop"))
72 state.selfloops.add(args[++i]);
73 else if (option.equals("-outputdir"))
74 state.outputdir = args[++i];
75 else if (option.equals("-excprefetch"))
76 state.excprefetch.add(args[++i]);
77 else if (option.equals("-classlibrary"))
78 state.classpath.add(args[++i]);
79 else if(option.equals("-numcore")) {
81 state.CORENUM = Integer.parseInt(args[i]);
82 } else if (option.equals("-mainclass"))
84 else if (option.equals("-trueprob")) {
85 state.TRUEPROB=Double.parseDouble(args[++i]);
86 } else if (option.equals("-printflat"))
88 else if (option.equals("-printscheduling"))
89 State.PRINTSCHEDULING=true;
90 else if (option.equals("-printschedulesim"))
91 State.PRINTSCHEDULESIM=true;
92 else if (option.equals("-printcriticalpath"))
93 State.PRINTCRITICALPATH=true;
94 else if (option.equals("-struct"))
95 state.structfile=args[++i];
96 else if (option.equals("-conscheck"))
98 else if (option.equals("-task"))
100 else if (option.equals("-abortreaders"))
101 state.ABORTREADERS=true;
102 else if (option.equals("-taskstate"))
103 state.TASKSTATE=true;
104 else if (option.equals("-tagstate"))
106 else if (option.equals("-flatirtasks")) {
107 state.FLATIRGRAPH=true;
108 state.FLATIRGRAPHTASKS=true;
109 } else if (option.equals("-flatirusermethods")) {
110 state.FLATIRGRAPH=true;
111 state.FLATIRGRAPHUSERMETHODS=true;
112 } else if (option.equals("-flatirlibmethods")) {
113 state.FLATIRGRAPH=true;
114 state.FLATIRGRAPHLIBMETHODS=true;
115 } else if (option.equals("-multicore"))
116 state.MULTICORE=true;
117 else if (option.equals("-ownership"))
118 state.OWNERSHIP=true;
119 else if (option.equals("-ownallocdepth")) {
120 state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
121 } else if (option.equals("-ownwritedots")) {
122 state.OWNERSHIPWRITEDOTS=true;
123 if (args[++i].equals("all")) {
124 state.OWNERSHIPWRITEALL=true;
126 } else if (option.equals("-ownaliasfile"))
127 state.OWNERSHIPALIASFILE=args[++i];
128 else if (option.equals("-optional"))
130 else if (option.equals("-raw"))
132 else if (option.equals("-scheduling"))
133 state.SCHEDULING=true;
134 else if (option.equals("-distributioninfo"))
135 isDistributeInfo=true;
136 else if (option.equals("-useprofile"))
137 state.USEPROFILE=true;
138 else if (option.equals("-thread"))
140 else if (option.equals("-dsm"))
142 else if (option.equals("-singleTM"))
144 else if (option.equals("-webinterface"))
145 state.WEBINTERFACE=true;
146 else if (option.equals("-instructionfailures"))
147 state.INSTRUCTIONFAILURE=true;
148 else if (option.equals("-abcclose"))
149 state.ARRAYBOUNDARYCHECK=false;
150 else if (option.equals("-mlp"))
152 else if (option.equals("-help")) {
153 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
154 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
155 System.out.println("-dir outputdirectory -- output code in outputdirectory");
156 System.out.println("-struct structfile -- output structure declarations for repair tool");
157 System.out.println("-mainclass -- main function to call");
158 System.out.println("-dsm -- distributed shared memory support");
159 System.out.println("-singleTM -- single machine committing transactions");
160 System.out.println("-abortreaders -- abort readers");
161 System.out.println("-precise -- use precise garbage collection");
162 System.out.println("-conscheck -- turn on consistency checking");
163 System.out.println("-task -- compiler for tasks");
164 System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
165 System.out.println("-thread -- threads");
166 System.out.println("-trueprob <d> -- probability of true branch");
167 System.out.println("-printflat -- print out flat representation");
168 System.out.println("-instructionfailures -- insert code for instruction level failures");
169 System.out.println("-taskstate -- do task state analysis");
170 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
171 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
172 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
173 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
174 System.out.println("-ownership -- do ownership analysis");
175 System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
176 System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
177 System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
178 System.out.println("-optional -- enable optional arguments");
179 System.out.println("-abcclose close the array boundary check");
180 System.out.println("-scheduling do task scheduling");
181 System.out.println("-mlp build mlp code");
182 System.out.println("-multicore generate multi-core version binary");
183 System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
184 System.out.println("-raw generate raw version binary (should be used together with -multicore)");
185 System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
186 System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
187 System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
188 System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
189 System.out.println("-threadsimulate generate multi-thread simulate version binary");
190 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");
191 System.out.println("-printscheduling -- print out scheduling graphs");
192 System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
193 System.out.println("-webinterface -- enable web interface");
194 System.out.println("-help -- print out help");
197 sourcefiles.add(args[i]);
201 //add default classpath
202 if (state.classpath.size()==1)
203 state.classpath.add(ClassLibraryPrefix);
205 BuildIR bir=new BuildIR(state);
206 TypeUtil tu=new TypeUtil(state, bir);
209 SemanticCheck sc=new SemanticCheck(state,tu);
211 for(int i=0;i<sourcefiles.size();i++)
212 loadClass(state, bir,(String)sourcefiles.get(i));
214 //Stuff the runtime wants to see
215 sc.getClass("String");
218 sc.getClass("Socket");
219 sc.getClass("ServerSocket");
220 sc.getClass("FileInputStream");
221 sc.getClass("FileOutputStream");
223 sc.getClass("TagDescriptor");
225 if (state.THREAD||state.DSM) {
226 sc.getClass("Thread");
231 tu.createFullTable();
233 BuildFlat bf=new BuildFlat(state,tu);
235 SafetyAnalysis sa=null;
236 PrefetchAnalysis pa=null;
238 if (state.TAGSTATE) {
239 CallGraph callgraph=new CallGraph(state);
240 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
241 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
244 if (state.TASKSTATE) {
245 CallGraph callgraph=new CallGraph(state);
246 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
247 TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
249 TaskGraph tg=new TaskGraph(state, ta);
252 if (state.OPTIONAL) {
253 ExecutionGraph et=new ExecutionGraph(state, ta);
254 et.createExecutionGraph();
255 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
257 state.storeAnalysisResult(sa.getResult());
258 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
261 if (state.WEBINTERFACE) {
262 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
263 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
264 JhttpServer serve=new JhttpServer(8000,wi);
268 if (state.SCHEDULING) {
269 // Use ownership analysis to get alias information
270 CallGraph callGraph = new CallGraph(state);
271 OwnershipAnalysis oa = null;/*new OwnershipAnalysis(state,
274 state.OWNERSHIPALLOCDEPTH,
275 state.OWNERSHIPWRITEDOTS,
276 state.OWNERSHIPWRITEALL,
277 state.OWNERSHIPALIASFILE);*/
279 // synthesis a layout according to target multicore processor
280 MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
283 if(isDistributeInfo) {
284 mcImplSynthesis.distribution();
286 mcImplSynthesis.setScheduleThreshold(20);
287 mcImplSynthesis.setProbThreshold(0);
288 mcImplSynthesis.setGenerateThreshold(30);
289 Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
291 // generate multicore codes
292 if(state.MULTICORE) {
293 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
298 mcImplSynthesis.getCoreNum(),
300 bcm.setOwnershipAnalysis(oa);
309 if(!state.MULTICORE) {
311 CallGraph callgraph=new CallGraph(state);
312 if (state.PREFETCH) {
313 //speed up prefetch generation using locality analysis results
314 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
315 pa=new PrefetchAnalysis(state, callgraph, tu, la);
318 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
319 GenerateConversions gc=new GenerateConversions(la, state);
320 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
323 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
328 if (state.FLATIRGRAPH) {
329 FlatIRGraph firg = new FlatIRGraph(state,
330 state.FLATIRGRAPHTASKS,
331 state.FLATIRGRAPHUSERMETHODS,
332 state.FLATIRGRAPHLIBMETHODS);
335 if (state.OWNERSHIP) {
336 CallGraph callGraph = new CallGraph(state);
337 OwnershipAnalysis oa = new OwnershipAnalysis(state,
340 state.OWNERSHIPALLOCDEPTH,
341 state.OWNERSHIPWRITEDOTS,
342 state.OWNERSHIPWRITEALL,
343 state.OWNERSHIPALIASFILE);
347 System.out.println("Lines="+state.lines);
351 public static void loadClass(State state, BuildIR bir, String sourcefile) {
352 ParseNode pn=readSourceFile(state, sourcefile);
353 bir.buildtree(pn, null);
356 /** Reads in a source file and adds the parse tree to the state object. */
358 public static ParseNode readSourceFile(State state, String sourcefile) {
360 Reader fr= new BufferedReader(new FileReader(sourcefile));
361 Lex.Lexer l = new Lex.Lexer(fr);
362 java_cup.runtime.lr_parser g;
363 g = new Parse.Parser(l);
366 p=(ParseNode) g./*debug_*/ parse().value;
367 } catch (Exception e) {
368 System.err.println("Error parsing file:"+sourcefile);
372 state.addParseNode(p);
373 if (l.numErrors()!=0) {
374 System.out.println("Error parsing "+sourcefile);
375 System.exit(l.numErrors());
377 state.lines+=l.line_num;
380 } catch (Exception e) {