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.Schedule;
25 import Analysis.Scheduling.ScheduleAnalysis;
26 import Analysis.Scheduling.ScheduleSimulator;
27 import Analysis.TaskStateAnalysis.TaskAnalysis;
28 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
29 import Analysis.TaskStateAnalysis.TaskGraph;
30 import Analysis.CallGraph.CallGraph;
31 import Analysis.TaskStateAnalysis.FEdge;
32 import Analysis.TaskStateAnalysis.FlagState;
33 import Analysis.TaskStateAnalysis.TagAnalysis;
34 import Analysis.TaskStateAnalysis.GarbageAnalysis;
35 import Analysis.TaskStateAnalysis.ExecutionGraph;
36 import Analysis.TaskStateAnalysis.SafetyAnalysis;
37 import Analysis.Locality.LocalityAnalysis;
38 import Analysis.Locality.GenerateConversions;
39 import Analysis.Prefetch.PrefetchAnalysis;
40 import Analysis.FlatIRGraph.FlatIRGraph;
41 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
43 import Util.GraphNode;
44 import Util.GraphNode.DFS;
45 import Util.GraphNode.SCC;
49 /** Main method for the compiler. */
51 public static void main(String args[]) throws Exception {
52 String ClassLibraryPrefix="./ClassLibrary/";
53 State state=new State();
55 for(int i=0; i<args.length; i++) {
56 String option=args[i];
57 if (option.equals("-precise"))
58 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
59 else if (option.equals("-prefetch"))
61 else if (option.equals("-dir"))
62 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
63 else if (option.equals("-fastcheck"))
65 else if (option.equals("-selfloop"))
66 state.selfloops.add(args[++i]);
67 else if (option.equals("-excprefetch"))
68 state.excprefetch.add(args[++i]);
69 else if (option.equals("-classlibrary"))
70 ClassLibraryPrefix=args[++i]+"/";
71 else if(option.equals("-numcore")) {
73 state.CORENUM = Integer.parseInt(args[i]);
74 } else if (option.equals("-mainclass"))
76 else if (option.equals("-trueprob")) {
77 state.TRUEPROB=Double.parseDouble(args[++i]);
78 } else if (option.equals("-printflat"))
80 else if (option.equals("-printscheduling"))
81 State.PRINTSCHEDULING=true;
82 else if (option.equals("-printschedulesim"))
83 State.PRINTSCHEDULESIM=true;
84 else if (option.equals("-struct"))
85 state.structfile=args[++i];
86 else if (option.equals("-conscheck"))
88 else if (option.equals("-task"))
90 else if (option.equals("-abortreaders"))
91 state.ABORTREADERS=true;
92 else if (option.equals("-taskstate"))
94 else if (option.equals("-tagstate"))
96 else if (option.equals("-flatirtasks")) {
97 state.FLATIRGRAPH=true;
98 state.FLATIRGRAPHTASKS=true;
99 } else if (option.equals("-flatirusermethods")) {
100 state.FLATIRGRAPH=true;
101 state.FLATIRGRAPHUSERMETHODS=true;
102 } else if (option.equals("-flatirlibmethods")) {
103 state.FLATIRGRAPH=true;
104 state.FLATIRGRAPHLIBMETHODS=true;
105 } else if (option.equals("-multicore"))
106 state.MULTICORE=true;
107 else if (option.equals("-ownership"))
108 state.OWNERSHIP=true;
109 else if (option.equals("-ownallocdepth")) {
110 state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
111 } else if (option.equals("-ownwritedots")) {
112 state.OWNERSHIPWRITEDOTS=true;
113 if (args[++i].equals("all")) {
114 state.OWNERSHIPWRITEALL=true;
116 } else if (option.equals("-ownaliasfile"))
117 state.OWNERSHIPALIASFILE=args[++i];
118 else if (option.equals("-optional"))
120 else if (option.equals("-raw"))
122 else if (option.equals("-scheduling"))
123 state.SCHEDULING=true;
124 else if (option.equals("-useprofile"))
125 state.USEPROFILE=true;
126 else if (option.equals("-thread"))
128 else if (option.equals("-dsm"))
130 else if (option.equals("-webinterface"))
131 state.WEBINTERFACE=true;
132 else if (option.equals("-instructionfailures"))
133 state.INSTRUCTIONFAILURE=true;
134 else if (option.equals("-abcclose"))
135 state.ARRAYBOUNDARYCHECK=false;
136 else if (option.equals("-help")) {
137 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
138 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
139 System.out.println("-dir outputdirectory -- output code in outputdirectory");
140 System.out.println("-struct structfile -- output structure declarations for repair tool");
141 System.out.println("-mainclass -- main function to call");
142 System.out.println("-dsm -- distributed shared memory support");
143 System.out.println("-abortreaders -- abort readers");
144 System.out.println("-precise -- use precise garbage collection");
145 System.out.println("-conscheck -- turn on consistency checking");
146 System.out.println("-task -- compiler for tasks");
147 System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
148 System.out.println("-thread -- threads");
149 System.out.println("-trueprob <d> -- probability of true branch");
150 System.out.println("-printflat -- print out flat representation");
151 System.out.println("-instructionfailures -- insert code for instruction level failures");
152 System.out.println("-taskstate -- do task state analysis");
153 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
154 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
155 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
156 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
157 System.out.println("-ownership -- do ownership analysis");
158 System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
159 System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
160 System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
161 System.out.println("-optional -- enable optional arguments");
162 System.out.println("-abcclose close the array boundary check");
163 System.out.println("-scheduling do task scheduling");
164 System.out.println("-multicore generate multi-core version binary");
165 System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
166 System.out.println("-raw generate raw version binary (should be used together with -multicore)");
167 System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
168 System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
169 System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
170 System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
171 System.out.println("-threadsimulate generate multi-thread simulate version binary");
172 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");
173 System.out.println("-printscheduling -- print out scheduling graphs");
174 System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
175 System.out.println("-webinterface -- enable web interface");
176 System.out.println("-help -- print out help");
179 readSourceFile(state, args[i]);
184 readSourceFile(state, ClassLibraryPrefix+"System.java");
185 readSourceFile(state, ClassLibraryPrefix+"String.java");
186 readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
187 readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
188 readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
189 readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
190 readSourceFile(state, ClassLibraryPrefix+"Integer.java");
191 readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
193 readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
194 readSourceFile(state, ClassLibraryPrefix+"PushbackInputStream.java");
195 readSourceFile(state, ClassLibraryPrefix+"InputStream.java");
196 readSourceFile(state, ClassLibraryPrefix+"OutputStream.java");
197 readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
198 readSourceFile(state, ClassLibraryPrefix+"File.java");
199 readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
200 readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java");
201 readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java");
202 readSourceFile(state, ClassLibraryPrefix+"gnu/StringTokenizer.java");
204 readSourceFile(state, ClassLibraryPrefix+"Math.java");
205 readSourceFile(state, ClassLibraryPrefix+"gnu/Random.java");
206 readSourceFile(state, ClassLibraryPrefix+"Vector.java");
207 readSourceFile(state, ClassLibraryPrefix+"Enumeration.java");
208 readSourceFile(state, ClassLibraryPrefix+"Dictionary.java");
209 readSourceFile(state, ClassLibraryPrefix+"Writer.java");
210 readSourceFile(state, ClassLibraryPrefix+"BufferedWriter.java");
211 readSourceFile(state, ClassLibraryPrefix+"OutputStreamWriter.java");
212 readSourceFile(state, ClassLibraryPrefix+"FileWriter.java");
213 readSourceFile(state, ClassLibraryPrefix+"Date.java");
217 readSourceFile(state, ClassLibraryPrefix+"ObjectFC.java");
219 readSourceFile(state, ClassLibraryPrefix+"Object.java");
220 readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
221 } else if (state.DSM) {
222 readSourceFile(state, ClassLibraryPrefix+"ThreadDSM.java");
223 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaDSM.java");
224 readSourceFile(state, ClassLibraryPrefix+"Barrier.java");
227 readSourceFile(state, ClassLibraryPrefix+"Thread.java");
228 readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
230 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaNT.java");
234 readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
235 readSourceFile(state, ClassLibraryPrefix+"Socket.java");
236 readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
238 readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
239 readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
242 BuildIR bir=new BuildIR(state);
245 TypeUtil tu=new TypeUtil(state);
247 SemanticCheck sc=new SemanticCheck(state,tu);
249 tu.createFullTable();
251 BuildFlat bf=new BuildFlat(state,tu);
253 SafetyAnalysis sa=null;
254 PrefetchAnalysis pa=null;
256 if (state.TAGSTATE) {
257 CallGraph callgraph=new CallGraph(state);
258 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
259 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis);
262 if (state.TASKSTATE) {
263 CallGraph callgraph=new CallGraph(state);
264 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
265 TaskAnalysis ta=new TaskAnalysis(state, taganalysis);
267 TaskGraph tg=new TaskGraph(state, ta);
270 if (state.OPTIONAL) {
271 ExecutionGraph et=new ExecutionGraph(state, ta);
272 et.createExecutionGraph();
273 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
275 state.storeAnalysisResult(sa.getResult());
276 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
279 if (state.WEBINTERFACE) {
280 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
281 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
282 JhttpServer serve=new JhttpServer(8000,wi);
286 if (state.SCHEDULING) {
287 // Use ownership analysis to get alias information
288 CallGraph callGraph = new CallGraph(state);
289 OwnershipAnalysis oa = new OwnershipAnalysis(state,
292 state.OWNERSHIPALLOCDEPTH,
293 state.OWNERSHIPWRITEDOTS,
294 state.OWNERSHIPWRITEALL,
295 state.OWNERSHIPALIASFILE);
297 // generate multiple schedulings
298 ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state,
300 // necessary preparation such as read profile info etc.
301 scheduleAnalysis.preparation();
302 scheduleAnalysis.preSchedule();
303 scheduleAnalysis.scheduleAnalysis();
304 //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
305 scheduleAnalysis.setCoreNum(state.CORENUM);
306 scheduleAnalysis.schedule();
308 //simulate these schedulings
309 ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(),
312 Vector<Vector<Schedule>> schedulings = scheduleAnalysis.getSchedulings();
313 Vector<Integer> selectedScheduling = scheduleSimulator.simulate(schedulings);
315 if(state.MULTICORE) {
316 Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.firstElement());
317 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
322 scheduleAnalysis.getCoreNum(),
324 bcm.setOwnershipAnalysis(oa);
329 selectedScheduling = null;
334 if(!state.MULTICORE) {
336 CallGraph callgraph=new CallGraph(state);
337 if (state.PREFETCH) {
338 //speed up prefetch generation using locality analysis results
339 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
340 pa=new PrefetchAnalysis(state, callgraph, tu, la);
343 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
344 GenerateConversions gc=new GenerateConversions(la, state);
345 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
348 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
353 if (state.FLATIRGRAPH) {
354 FlatIRGraph firg = new FlatIRGraph(state,
355 state.FLATIRGRAPHTASKS,
356 state.FLATIRGRAPHUSERMETHODS,
357 state.FLATIRGRAPHLIBMETHODS);
360 if (state.OWNERSHIP) {
361 CallGraph callGraph = new CallGraph(state);
362 OwnershipAnalysis oa = new OwnershipAnalysis(state,
365 state.OWNERSHIPALLOCDEPTH,
366 state.OWNERSHIPWRITEDOTS,
367 state.OWNERSHIPWRITEALL,
368 state.OWNERSHIPALIASFILE);
374 /** Reads in a source file and adds the parse tree to the state object. */
376 private static void readSourceFile(State state, String sourcefile) throws Exception {
377 Reader fr = new BufferedReader(new FileReader(sourcefile));
378 Lex.Lexer l = new Lex.Lexer(fr);
379 java_cup.runtime.lr_parser g;
380 g = new Parse.Parser(l);
383 p=(ParseNode) g./*debug_*/ parse().value;
384 } catch (Exception e) {
385 System.err.println("Error parsing file:"+sourcefile);
389 state.addParseNode(p);
390 if (l.numErrors()!=0) {
391 System.out.println("Error parsing "+sourcefile);
392 System.exit(l.numErrors());