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();
54 Vector sourcefiles=new Vector();
55 state.classpath.add(".");
57 String outputdir = null;
59 for(int i=0; i<args.length; i++) {
60 String option=args[i];
61 if (option.equals("-precise"))
62 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
63 else if (option.equals("-prefetch"))
65 else if (option.equals("-dir"))
66 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
67 else if (option.equals("-fastcheck"))
69 else if (option.equals("-selfloop"))
70 state.selfloops.add(args[++i]);
71 else if (option.equals("-outputdir"))
72 outputdir = args[++i];
73 else if (option.equals("-excprefetch"))
74 state.excprefetch.add(args[++i]);
75 else if (option.equals("-classlibrary"))
76 state.classpath.add(args[++i]);
77 else if(option.equals("-numcore")) {
79 state.CORENUM = Integer.parseInt(args[i]);
80 } else if (option.equals("-mainclass"))
82 else if (option.equals("-trueprob")) {
83 state.TRUEPROB=Double.parseDouble(args[++i]);
84 } else if (option.equals("-printflat"))
86 else if (option.equals("-printscheduling"))
87 State.PRINTSCHEDULING=true;
88 else if (option.equals("-printschedulesim"))
89 State.PRINTSCHEDULESIM=true;
90 else if (option.equals("-struct"))
91 state.structfile=args[++i];
92 else if (option.equals("-conscheck"))
94 else if (option.equals("-task"))
96 else if (option.equals("-abortreaders"))
97 state.ABORTREADERS=true;
98 else if (option.equals("-taskstate"))
100 else if (option.equals("-tagstate"))
102 else if (option.equals("-flatirtasks")) {
103 state.FLATIRGRAPH=true;
104 state.FLATIRGRAPHTASKS=true;
105 } else if (option.equals("-flatirusermethods")) {
106 state.FLATIRGRAPH=true;
107 state.FLATIRGRAPHUSERMETHODS=true;
108 } else if (option.equals("-flatirlibmethods")) {
109 state.FLATIRGRAPH=true;
110 state.FLATIRGRAPHLIBMETHODS=true;
111 } else if (option.equals("-multicore"))
112 state.MULTICORE=true;
113 else if (option.equals("-ownership"))
114 state.OWNERSHIP=true;
115 else if (option.equals("-ownallocdepth")) {
116 state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
117 } else if (option.equals("-ownwritedots")) {
118 state.OWNERSHIPWRITEDOTS=true;
119 if (args[++i].equals("all")) {
120 state.OWNERSHIPWRITEALL=true;
122 } else if (option.equals("-ownaliasfile"))
123 state.OWNERSHIPALIASFILE=args[++i];
124 else if (option.equals("-optional"))
126 else if (option.equals("-raw"))
128 else if (option.equals("-scheduling"))
129 state.SCHEDULING=true;
130 else if (option.equals("-useprofile"))
131 state.USEPROFILE=true;
132 else if (option.equals("-thread"))
134 else if (option.equals("-dsm"))
136 else if (option.equals("-webinterface"))
137 state.WEBINTERFACE=true;
138 else if (option.equals("-instructionfailures"))
139 state.INSTRUCTIONFAILURE=true;
140 else if (option.equals("-abcclose"))
141 state.ARRAYBOUNDARYCHECK=false;
142 else if (option.equals("-help")) {
143 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
144 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
145 System.out.println("-dir outputdirectory -- output code in outputdirectory");
146 System.out.println("-struct structfile -- output structure declarations for repair tool");
147 System.out.println("-mainclass -- main function to call");
148 System.out.println("-dsm -- distributed shared memory support");
149 System.out.println("-abortreaders -- abort readers");
150 System.out.println("-precise -- use precise garbage collection");
151 System.out.println("-conscheck -- turn on consistency checking");
152 System.out.println("-task -- compiler for tasks");
153 System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
154 System.out.println("-thread -- threads");
155 System.out.println("-trueprob <d> -- probability of true branch");
156 System.out.println("-printflat -- print out flat representation");
157 System.out.println("-instructionfailures -- insert code for instruction level failures");
158 System.out.println("-taskstate -- do task state analysis");
159 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
160 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
161 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
162 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
163 System.out.println("-ownership -- do ownership analysis");
164 System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
165 System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
166 System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
167 System.out.println("-optional -- enable optional arguments");
168 System.out.println("-abcclose close the array boundary check");
169 System.out.println("-scheduling do task scheduling");
170 System.out.println("-multicore generate multi-core version binary");
171 System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
172 System.out.println("-raw generate raw version binary (should be used together with -multicore)");
173 System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
174 System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
175 System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
176 System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
177 System.out.println("-threadsimulate generate multi-thread simulate version binary");
178 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");
179 System.out.println("-printscheduling -- print out scheduling graphs");
180 System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
181 System.out.println("-webinterface -- enable web interface");
182 System.out.println("-help -- print out help");
185 sourcefiles.add(args[i]);
189 //add default classpath
190 if (state.classpath.size()==1)
191 state.classpath.add(ClassLibraryPrefix);
193 BuildIR bir=new BuildIR(state);
194 TypeUtil tu=new TypeUtil(state, bir);
197 SemanticCheck sc=new SemanticCheck(state,tu);
199 for(int i=0;i<sourcefiles.size();i++)
200 loadClass(state, bir,(String)sourcefiles.get(i));
202 //Stuff the runtime wants to see
203 sc.getClass("String");
206 sc.getClass("Socket");
207 sc.getClass("ServerSocket");
208 sc.getClass("FileInputStream");
209 sc.getClass("FileOutputStream");
211 sc.getClass("TagDescriptor");
214 sc.getClass("Thread");
219 tu.createFullTable();
221 BuildFlat bf=new BuildFlat(state,tu);
223 SafetyAnalysis sa=null;
224 PrefetchAnalysis pa=null;
226 if (state.TAGSTATE) {
227 CallGraph callgraph=new CallGraph(state);
228 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
229 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
232 if (state.TASKSTATE) {
233 CallGraph callgraph=new CallGraph(state);
234 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
235 TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
237 TaskGraph tg=new TaskGraph(state, ta);
240 if (state.OPTIONAL) {
241 ExecutionGraph et=new ExecutionGraph(state, ta);
242 et.createExecutionGraph();
243 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
245 state.storeAnalysisResult(sa.getResult());
246 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
249 if (state.WEBINTERFACE) {
250 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
251 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
252 JhttpServer serve=new JhttpServer(8000,wi);
256 if (state.SCHEDULING) {
257 // Use ownership analysis to get alias information
258 CallGraph callGraph = new CallGraph(state);
259 OwnershipAnalysis oa = new OwnershipAnalysis(state,
262 state.OWNERSHIPALLOCDEPTH,
263 state.OWNERSHIPWRITEDOTS,
264 state.OWNERSHIPWRITEALL,
265 state.OWNERSHIPALIASFILE);
267 // generate multiple schedulings
268 ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state,
270 // necessary preparation such as read profile info etc.
271 scheduleAnalysis.preparation();
272 scheduleAnalysis.preSchedule();
273 scheduleAnalysis.scheduleAnalysis();
274 //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
275 scheduleAnalysis.setCoreNum(state.CORENUM);
276 scheduleAnalysis.schedule();
278 //simulate these schedulings
279 ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(),
282 Vector<Vector<Schedule>> schedulings = scheduleAnalysis.getSchedulings();
283 Vector<Integer> selectedScheduling = scheduleSimulator.simulate(schedulings);
285 if(state.MULTICORE) {
286 Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.firstElement());
287 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
292 scheduleAnalysis.getCoreNum(),
294 bcm.setOwnershipAnalysis(oa);
299 selectedScheduling = null;
304 if(!state.MULTICORE) {
306 CallGraph callgraph=new CallGraph(state);
307 if (state.PREFETCH) {
308 //speed up prefetch generation using locality analysis results
309 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
310 pa=new PrefetchAnalysis(state, callgraph, tu, la);
313 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
314 GenerateConversions gc=new GenerateConversions(la, state);
315 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
318 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
323 if (state.FLATIRGRAPH) {
324 FlatIRGraph firg = new FlatIRGraph(state,
325 state.FLATIRGRAPHTASKS,
326 state.FLATIRGRAPHUSERMETHODS,
327 state.FLATIRGRAPHLIBMETHODS);
330 if (state.OWNERSHIP) {
331 CallGraph callGraph = new CallGraph(state);
332 OwnershipAnalysis oa = new OwnershipAnalysis(state,
335 state.OWNERSHIPALLOCDEPTH,
336 state.OWNERSHIPWRITEDOTS,
337 state.OWNERSHIPWRITEALL,
338 state.OWNERSHIPALIASFILE);
344 public static void loadClass(State state, BuildIR bir, String sourcefile) {
345 ParseNode pn=readSourceFile(state, sourcefile);
346 bir.buildtree(pn, null);
349 /** Reads in a source file and adds the parse tree to the state object. */
351 public static ParseNode readSourceFile(State state, String sourcefile) {
353 Reader fr= new BufferedReader(new FileReader(sourcefile));
354 Lex.Lexer l = new Lex.Lexer(fr);
355 java_cup.runtime.lr_parser g;
356 g = new Parse.Parser(l);
359 p=(ParseNode) g./*debug_*/ parse().value;
360 } catch (Exception e) {
361 System.err.println("Error parsing file:"+sourcefile);
365 state.addParseNode(p);
366 if (l.numErrors()!=0) {
367 System.out.println("Error parsing "+sourcefile);
368 System.exit(l.numErrors());
372 } catch (Exception e) {