3 import java.io.FileOutputStream;
4 import java.io.InputStream;
5 import java.io.PrintStream;
7 import java.io.BufferedReader;
8 import java.io.FileReader;
9 import java.io.FileInputStream;
10 import java.util.Iterator;
11 import java.util.Vector;
13 import IR.Tree.ParseNode;
14 import IR.Tree.BuildIR;
15 import IR.Tree.SemanticCheck;
16 import IR.Flat.BuildCodeMultiCore;
17 import IR.Flat.BuildFlat;
18 import IR.Flat.BuildCode;
19 import IR.ClassDescriptor;
21 import IR.TaskDescriptor;
23 import Analysis.Scheduling.Schedule;
24 import Analysis.Scheduling.ScheduleAnalysis;
25 import Analysis.Scheduling.ScheduleEdge;
26 import Analysis.Scheduling.ScheduleNode;
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;
47 /** Main method for the compiler. */
49 public static void main(String args[]) throws Exception {
50 String ClassLibraryPrefix="./ClassLibrary/";
51 State state=new State();
53 for(int i=0;i<args.length;i++) {
54 String option=args[i];
55 if (option.equals("-precise"))
56 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
57 else if (option.equals("-prefetch"))
59 else if (option.equals("-dir"))
60 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
61 else if (option.equals("-selfloop"))
62 state.selfloops.add(args[++i]);
63 else if (option.equals("-excprefetch"))
64 state.excprefetch.add(args[++i]);
65 else if (option.equals("-classlibrary"))
66 ClassLibraryPrefix=args[++i]+"/";
67 else if(option.equals("-numcore")) {
69 state.CORENUM = Integer.parseInt(args[i]);
71 else if (option.equals("-mainclass"))
73 else if (option.equals("-trueprob")) {
74 state.TRUEPROB=Double.parseDouble(args[++i]);
75 } else if (option.equals("-printflat"))
77 else if (option.equals("-struct"))
78 state.structfile=args[++i];
79 else if (option.equals("-conscheck"))
81 else if (option.equals("-task"))
83 else if (option.equals("-taskstate"))
85 else if (option.equals("-tagstate"))
87 else if (option.equals("-flatirtasks")) {
88 state.FLATIRGRAPH=true;
89 state.FLATIRGRAPHTASKS=true;
91 else if (option.equals("-flatirusermethods")) {
92 state.FLATIRGRAPH=true;
93 state.FLATIRGRAPHUSERMETHODS=true;
95 else if (option.equals("-flatirlibmethods")) {
96 state.FLATIRGRAPH=true;
97 state.FLATIRGRAPHLIBMETHODS=true;
99 else if (option.equals("-multicore"))
100 state.MULTICORE=true;
101 else if (option.equals("-ownership"))
102 state.OWNERSHIP=true;
103 else if (option.equals("-optional"))
105 else if (option.equals("-raw"))
107 else if (option.equals("-scheduling"))
108 state.SCHEDULING=true;
109 else if (option.equals("-thread"))
111 else if (option.equals("-dsm"))
113 else if (option.equals("-webinterface"))
114 state.WEBINTERFACE=true;
115 else if (option.equals("-instructionfailures"))
116 state.INSTRUCTIONFAILURE=true;
117 else if (option.equals("-help")) {
118 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
119 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
120 System.out.println("-dir outputdirectory -- output code in outputdirectory");
121 System.out.println("-struct structfile -- output structure declarations for repair tool");
122 System.out.println("-mainclass -- main function to call");
123 System.out.println("-dsm -- distributed shared memory support");
124 System.out.println("-precise -- use precise garbage collection");
125 System.out.println("-conscheck -- turn on consistency checking");
126 System.out.println("-task -- compiler for tasks");
127 System.out.println("-thread -- threads");
128 System.out.println("-trueprob <d> -- probability of true branch");
129 System.out.println("-printflat -- print out flat representation");
130 System.out.println("-instructionfailures -- insert code for instruction level failures");
131 System.out.println("-taskstate -- do task state analysis");
132 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
133 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
134 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
135 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
136 System.out.println("-ownership -- do ownership analysis");
137 System.out.println("-optional -- enable optional arguments");
138 System.out.println("-webinterface -- enable web interface");
139 System.out.println("-help -- print out help");
142 readSourceFile(state, args[i]);
147 readSourceFile(state, ClassLibraryPrefix+"System.java");
148 readSourceFile(state, ClassLibraryPrefix+"String.java");
149 readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
150 readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
151 readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
152 readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
153 readSourceFile(state, ClassLibraryPrefix+"Integer.java");
154 readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
156 readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
157 readSourceFile(state, ClassLibraryPrefix+"InputStream.java");
158 readSourceFile(state, ClassLibraryPrefix+"OutputStream.java");
159 readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
160 readSourceFile(state, ClassLibraryPrefix+"File.java");
161 readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
162 readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java");
163 readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java");
165 readSourceFile(state, ClassLibraryPrefix+"Math.java");
166 readSourceFile(state, ClassLibraryPrefix+"gnu/Random.java");
167 readSourceFile(state, ClassLibraryPrefix+"Vector.java");
168 readSourceFile(state, ClassLibraryPrefix+"Enumeration.java");
171 readSourceFile(state, ClassLibraryPrefix+"Object.java");
172 readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
173 } else if (state.DSM) {
174 readSourceFile(state, ClassLibraryPrefix+"ThreadDSM.java");
175 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaDSM.java");
176 readSourceFile(state, ClassLibraryPrefix+"Barrier.java");
179 readSourceFile(state, ClassLibraryPrefix+"Thread.java");
180 readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
182 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaNT.java");
186 readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
187 readSourceFile(state, ClassLibraryPrefix+"Socket.java");
188 readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
190 readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
191 readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
194 BuildIR bir=new BuildIR(state);
197 TypeUtil tu=new TypeUtil(state);
199 SemanticCheck sc=new SemanticCheck(state,tu);
201 tu.createFullTable();
203 BuildFlat bf=new BuildFlat(state,tu);
205 SafetyAnalysis sa=null;
206 PrefetchAnalysis pa=null;
208 if (state.TAGSTATE) {
209 CallGraph callgraph=new CallGraph(state);
210 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
211 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis);
214 if (state.TASKSTATE) {
215 CallGraph callgraph=new CallGraph(state);
216 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
217 TaskAnalysis ta=new TaskAnalysis(state, taganalysis);
219 TaskGraph tg=new TaskGraph(state, ta);
222 if (state.OPTIONAL) {
223 ExecutionGraph et=new ExecutionGraph(state, ta);
224 et.createExecutionGraph();
225 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
227 state.storeAnalysisResult(sa.getResult());
228 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
231 if (state.WEBINTERFACE) {
232 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
233 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
234 JhttpServer serve=new JhttpServer(8000,wi);
238 if (state.SCHEDULING) {
239 // Save the current standard input, output, and error streams
240 // for later restoration.
241 PrintStream origOut = System.out;
243 // Create a new output stream for the standard output.
244 PrintStream stdout = null;
246 stdout = new PrintStream (new FileOutputStream("SimulatorResult.out"));
247 } catch (Exception e) {
248 // Sigh. Couldn't open the file.
249 System.out.println ("Redirect: Unable to open output file!");
253 // Print stuff to the original output and error streams.
254 // On most systems all of this will end up on your console when you
255 // run this application.
256 //origOut.println ("\nRedirect: Round #1");
257 //System.out.println ("Test output via 'System.out'.");
258 //origOut.println ("Test output via 'origOut' reference.");
260 // Set the System out and err streams to use our replacements.
261 System.setOut(stdout);
263 // Print stuff to the original output and error streams.
264 // The stuff printed through the 'origOut' and 'origErr' references
265 // should go to the console on most systems while the messages
266 // printed through the 'System.out' and 'System.err' will end up in
267 // the files we created for them.
268 //origOut.println ("\nRedirect: Round #2");
269 //System.out.println ("Test output via 'SimulatorResult.out'.");
270 //origOut.println ("Test output via 'origOut' reference.");
273 // Randomly set the newRate and probability of FEdges
274 java.util.Random r=new java.util.Random();
276 for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
277 ClassDescriptor cd=(ClassDescriptor) it_classes.next();
279 Vector rootnodes=ta.getRootNodes(cd);
281 for(Iterator it_rootnodes=rootnodes.iterator();it_rootnodes.hasNext();){
282 FlagState root=(FlagState)it_rootnodes.next();
283 Vector allocatingTasks = root.getAllocatingTasks();
284 if(allocatingTasks != null) {
285 for(int k = 0; k < allocatingTasks.size(); k++) {
286 TaskDescriptor td = (TaskDescriptor)allocatingTasks.elementAt(k);
287 Vector<FEdge> fev = (Vector<FEdge>)ta.getFEdgesFromTD(td);
288 int numEdges = fev.size();
290 for(int j = 0; j < numEdges; j++) {
291 FEdge pfe = fev.elementAt(j);
292 if(numEdges - j == 1) {
293 pfe.setProbability(total);
295 if((total != 0) && (total != 1)){
297 tint = r.nextInt()%total;
300 pfe.setProbability(tint);
304 tint = r.nextInt()%10;
305 } while(tint <= 0);*/
306 //int newRate = tint;
307 //int newRate = (j+1)%2+1;
309 String cdname = cd.getSymbol();
310 if((cdname.equals("SeriesRunner")) ||
311 (cdname.equals("MDRunner")) ||
312 (cdname.equals("Stage")) ||
313 (cdname.equals("AppDemoRunner")) ||
314 (cdname.equals("FilterBankAtom"))) {
316 } else if(cdname.equals("SentenceParser")) {
320 tint = r.nextInt()%100;
322 int probability = tint;*/
323 int probability = 100;
324 pfe.addNewObjInfo(cd, newRate, probability);
330 Iterator it_flags = ta.getFlagStates(cd).iterator();
331 while(it_flags.hasNext()) {
332 FlagState fs = (FlagState)it_flags.next();
333 Iterator it_edges = fs.edges();
334 while(it_edges.hasNext()) {
336 tint = r.nextInt()%10;
337 } while(tint <= 0);*/
339 ((FEdge)it_edges.next()).setExeTime(tint);
345 // generate multiple schedulings
346 ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state, ta);
347 scheduleAnalysis.preSchedule();
348 scheduleAnalysis.scheduleAnalysis();
349 //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
350 scheduleAnalysis.setCoreNum(state.CORENUM);
351 scheduleAnalysis.schedule();
353 //simulate these schedulings
354 ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(), state, ta);
355 Iterator it_scheduling = scheduleAnalysis.getSchedulingsIter();
357 Vector<Integer> selectedScheduling = new Vector<Integer>();
358 int processTime = Integer.MAX_VALUE;
359 while(it_scheduling.hasNext()) {
360 Vector<Schedule> scheduling = (Vector<Schedule>)it_scheduling.next();
361 scheduleSimulator.setScheduling(scheduling);
362 int tmpTime = scheduleSimulator.process();
363 if(tmpTime < processTime) {
364 selectedScheduling.clear();
365 selectedScheduling.add(index);
366 processTime = tmpTime;
367 } else if(tmpTime == processTime) {
368 selectedScheduling.add(index);
372 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
373 for(int i = 0; i < selectedScheduling.size(); i++) {
374 System.out.print((selectedScheduling.elementAt(i) + 1) + ", ");
376 System.out.println();
378 /*ScheduleSimulator scheduleSimulator = new ScheduleSimulator(4, state, ta);
379 Vector<Schedule> scheduling = new Vector<Schedule>();
380 for(int i = 0; i < 4; i++) {
381 Schedule schedule = new Schedule(i);
382 scheduling.add(schedule);
384 Iterator it_tasks = state.getTaskSymbolTable().getAllDescriptorsIterator();
385 while(it_tasks.hasNext()) {
386 TaskDescriptor td = (TaskDescriptor)it_tasks.next();
387 if(td.getSymbol().equals("t10")) {
388 scheduling.elementAt(1).addTask(td);
390 scheduling.elementAt(0).addTask(td);
393 ClassDescriptor cd = (ClassDescriptor)state.getClassSymbolTable().get("E");
394 scheduling.elementAt(0).addTargetCore(cd, 1);
395 scheduleSimulator.setScheduling(scheduling);
396 scheduleSimulator.process();
398 Vector<Schedule> scheduling1 = new Vector<Schedule>();
399 for(int i = 0; i < 4; i++) {
400 Schedule schedule = new Schedule(i);
401 scheduling1.add(schedule);
403 Iterator it_tasks1 = state.getTaskSymbolTable().getAllDescriptorsIterator();
404 while(it_tasks1.hasNext()) {
405 TaskDescriptor td = (TaskDescriptor)it_tasks1.next();
406 scheduling1.elementAt(0).addTask(td);
408 scheduleSimulator.setScheduling(scheduling1);
409 scheduleSimulator.process();*/
411 // Close the streams.
414 System.setOut(origOut);
415 } catch (Exception e) {
416 origOut.println ("Redirect: Unable to close files!");
419 if(state.MULTICORE) {
420 //it_scheduling = scheduleAnalysis.getSchedulingsIter();
421 //Vector<Schedule> scheduling = (Vector<Schedule>)it_scheduling.next();
422 Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.lastElement());
423 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state, bf.getMap(), tu, sa, scheduling, scheduleAnalysis.getCoreNum(), pa);
430 if(!state.MULTICORE) {
432 CallGraph callgraph=new CallGraph(state);
433 if (state.PREFETCH) {
434 //speed up prefetch generation using locality analysis results
435 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
436 pa=new PrefetchAnalysis(state, callgraph, tu, la);
439 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
440 GenerateConversions gc=new GenerateConversions(la, state);
441 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
444 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
449 if (state.FLATIRGRAPH) {
450 FlatIRGraph firg = new FlatIRGraph(state,
451 state.FLATIRGRAPHTASKS,
452 state.FLATIRGRAPHUSERMETHODS,
453 state.FLATIRGRAPHLIBMETHODS);
456 if (state.OWNERSHIP) {
457 CallGraph callGraph = new CallGraph( state );
458 int allocationDepth = 3;
459 OwnershipAnalysis oa =
460 new OwnershipAnalysis( state, callGraph, allocationDepth );
461 //This was breaking the compile
462 // oa.writeAllAliases( "identifiedAliases.txt" );
468 /** Reads in a source file and adds the parse tree to the state object. */
470 private static void readSourceFile(State state, String sourcefile) throws Exception {
471 Reader fr = new BufferedReader(new FileReader(sourcefile));
472 Lex.Lexer l = new Lex.Lexer(fr);
473 java_cup.runtime.lr_parser g;
474 g = new Parse.Parser(l);
477 p=(ParseNode) g./*debug_*/parse().value;
478 } catch (Exception e) {
479 System.err.println("Error parsing file:"+sourcefile);
483 state.addParseNode(p);
484 if (l.numErrors()!=0) {
485 System.out.println("Error parsing "+sourcefile);
486 System.exit(l.numErrors());