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