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.BuildFlat;
17 import IR.Flat.BuildCode;
18 import IR.ClassDescriptor;
20 import IR.TaskDescriptor;
22 import Analysis.Scheduling.Schedule;
23 import Analysis.Scheduling.ScheduleAnalysis;
24 import Analysis.Scheduling.ScheduleEdge;
25 import Analysis.Scheduling.ScheduleNode;
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;
46 /** Main method for the compiler. */
48 public static void main(String args[]) throws Exception {
49 String ClassLibraryPrefix="./ClassLibrary/";
50 State state=new State();
52 for(int i=0;i<args.length;i++) {
53 String option=args[i];
54 if (option.equals("-precise"))
55 IR.Flat.BuildCode.GENERATEPRECISEGC=true;
56 else if (option.equals("-prefetch"))
58 else if (option.equals("-dir"))
59 IR.Flat.BuildCode.PREFIX=args[++i]+"/";
60 else if (option.equals("-selfloop"))
61 state.selfloops.add(args[++i]);
62 else if (option.equals("-classlibrary"))
63 ClassLibraryPrefix=args[++i]+"/";
64 else if (option.equals("-mainclass"))
66 else if (option.equals("-struct"))
67 state.structfile=args[++i];
68 else if (option.equals("-conscheck"))
70 else if (option.equals("-task"))
72 else if (option.equals("-taskstate"))
74 else if (option.equals("-tagstate"))
76 else if (option.equals("-flatirtasks")) {
77 state.FLATIRGRAPH=true;
78 state.FLATIRGRAPHTASKS=true;
80 else if (option.equals("-flatirusermethods")) {
81 state.FLATIRGRAPH=true;
82 state.FLATIRGRAPHUSERMETHODS=true;
84 else if (option.equals("-flatirlibmethods")) {
85 state.FLATIRGRAPH=true;
86 state.FLATIRGRAPHLIBMETHODS=true;
88 else if (option.equals("-ownership"))
90 else if (option.equals("-optional"))
92 else if (option.equals("-scheduling"))
93 state.SCHEDULING=true;
94 else if (option.equals("-thread"))
96 else if (option.equals("-dsm"))
98 else if (option.equals("-webinterface"))
99 state.WEBINTERFACE=true;
100 else if (option.equals("-instructionfailures"))
101 state.INSTRUCTIONFAILURE=true;
102 else if (option.equals("-help")) {
103 System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
104 System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
105 System.out.println("-dir outputdirectory -- output code in outputdirectory");
106 System.out.println("-struct structfile -- output structure declarations for repair tool");
107 System.out.println("-mainclass -- main function to call");
108 System.out.println("-dsm -- distributed shared memory support");
109 System.out.println("-precise -- use precise garbage collection");
110 System.out.println("-conscheck -- turn on consistency checking");
111 System.out.println("-task -- compiler for tasks");
112 System.out.println("-thread -- threads");
113 System.out.println("-instructionfailures -- insert code for instruction level failures");
114 System.out.println("-taskstate -- do task state analysis");
115 System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
116 System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
117 System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
118 System.out.println(" note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
119 System.out.println("-ownership -- do ownership analysis");
120 System.out.println("-optional -- enable optional arguments");
121 System.out.println("-webinterface -- enable web interface");
122 System.out.println("-help -- print out help");
125 readSourceFile(state, args[i]);
130 readSourceFile(state, ClassLibraryPrefix+"System.java");
131 readSourceFile(state, ClassLibraryPrefix+"String.java");
132 readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
133 readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
134 readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
135 readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
136 readSourceFile(state, ClassLibraryPrefix+"Integer.java");
137 readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
138 readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
139 readSourceFile(state, ClassLibraryPrefix+"InputStream.java");
140 readSourceFile(state, ClassLibraryPrefix+"OutputStream.java");
141 readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
142 readSourceFile(state, ClassLibraryPrefix+"File.java");
143 readSourceFile(state, ClassLibraryPrefix+"Math.java");
144 readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
145 readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java");
146 readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java");
147 readSourceFile(state, ClassLibraryPrefix+"gnu/Random.java");
148 readSourceFile(state, ClassLibraryPrefix+"Vector.java");
149 readSourceFile(state, ClassLibraryPrefix+"Enumeration.java");
153 readSourceFile(state, ClassLibraryPrefix+"Object.java");
154 readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
155 } else if (state.DSM) {
156 readSourceFile(state, ClassLibraryPrefix+"ThreadDSM.java");
157 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaDSM.java");
160 readSourceFile(state, ClassLibraryPrefix+"Thread.java");
161 readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
163 readSourceFile(state, ClassLibraryPrefix+"ObjectJavaNT.java");
167 readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
168 readSourceFile(state, ClassLibraryPrefix+"Socket.java");
169 readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
171 readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
172 readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
175 BuildIR bir=new BuildIR(state);
178 TypeUtil tu=new TypeUtil(state);
180 SemanticCheck sc=new SemanticCheck(state,tu);
182 tu.createFullTable();
184 BuildFlat bf=new BuildFlat(state,tu);
186 SafetyAnalysis sa=null;
188 if (state.TAGSTATE) {
189 CallGraph callgraph=new CallGraph(state);
190 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
191 TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis);
194 if (state.TASKSTATE) {
195 CallGraph callgraph=new CallGraph(state);
196 TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
197 TaskAnalysis ta=new TaskAnalysis(state, taganalysis);
199 TaskGraph tg=new TaskGraph(state, ta);
202 if (state.OPTIONAL) {
203 ExecutionGraph et=new ExecutionGraph(state, ta);
204 et.createExecutionGraph();
205 sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
207 state.storeAnalysisResult(sa.getResult());
208 state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
211 if (state.WEBINTERFACE) {
212 GarbageAnalysis ga=new GarbageAnalysis(state, ta);
213 WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
214 JhttpServer serve=new JhttpServer(8000,wi);
218 if (state.SCHEDULING) {
219 // Save the current standard input, output, and error streams
220 // for later restoration.
221 PrintStream origOut = System.out;
223 // Create a new output stream for the standard output.
224 PrintStream stdout = null;
226 stdout = new PrintStream (new FileOutputStream("SimulatorResult.out"));
227 } catch (Exception e) {
228 // Sigh. Couldn't open the file.
229 System.out.println ("Redirect: Unable to open output file!");
233 // Print stuff to the original output and error streams.
234 // On most systems all of this will end up on your console when you
235 // run this application.
236 origOut.println ("\nRedirect: Round #1");
237 System.out.println ("Test output via 'System.out'.");
238 origOut.println ("Test output via 'origOut' reference.");
240 // Set the System out and err streams to use our replacements.
241 System.setOut(stdout);
243 // Print stuff to the original output and error streams.
244 // The stuff printed through the 'origOut' and 'origErr' references
245 // should go to the console on most systems while the messages
246 // printed through the 'System.out' and 'System.err' will end up in
247 // the files we created for them.
248 origOut.println ("\nRedirect: Round #2");
249 System.out.println ("Test output via 'SimulatorResult.out'.");
250 origOut.println ("Test output via 'origOut' reference.");
253 // Randomly set the newRate and probability of FEdges
254 java.util.Random r=new java.util.Random();
256 for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
257 ClassDescriptor cd=(ClassDescriptor) it_classes.next();
259 Vector rootnodes=ta.getRootNodes(cd);
261 for(Iterator it_rootnodes=rootnodes.iterator();it_rootnodes.hasNext();){
262 FlagState root=(FlagState)it_rootnodes.next();
263 Vector allocatingTasks = root.getAllocatingTasks();
264 if(allocatingTasks != null) {
265 for(int k = 0; k < allocatingTasks.size(); k++) {
266 TaskDescriptor td = (TaskDescriptor)allocatingTasks.elementAt(k);
267 Vector<FEdge> fev = (Vector<FEdge>)ta.getFEdgesFromTD(td);
268 int numEdges = fev.size();
270 for(int j = 0; j < numEdges; j++) {
271 FEdge pfe = fev.elementAt(j);
272 if(numEdges - j == 1) {
273 pfe.setProbability(total);
277 tint = r.nextInt()%total;
280 pfe.setProbability(tint);
284 tint = r.nextInt()%10;
286 //int newRate = tint;
287 int newRate = (j+1)%2+1;
289 tint = r.nextInt()%100;
291 int probability = tint;*/
292 int probability = 100;
293 pfe.addNewObjInfo(cd, newRate, probability);
299 Iterator it_flags = ta.getFlagStates(cd).iterator();
300 while(it_flags.hasNext()) {
301 FlagState fs = (FlagState)it_flags.next();
302 Iterator it_edges = fs.edges();
303 while(it_edges.hasNext()) {
305 tint = r.nextInt()%10;
307 ((FEdge)it_edges.next()).setExeTime(tint);
313 // generate multiple schedulings
314 ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state, ta);
315 scheduleAnalysis.preSchedule();
316 scheduleAnalysis.scheduleAnalysis();
317 scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
318 scheduleAnalysis.schedule();
320 //simulate these schedulings
321 ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(), state, ta);
322 Iterator it_scheduling = scheduleAnalysis.getSchedulingsIter();
324 Vector<Integer> selectedScheduling = new Vector<Integer>();
325 int processTime = Integer.MAX_VALUE;
326 while(it_scheduling.hasNext()) {
327 Vector<Schedule> scheduling = (Vector<Schedule>)it_scheduling.next();
328 scheduleSimulator.setScheduling(scheduling);
329 int tmpTime = scheduleSimulator.process();
330 if(tmpTime < processTime) {
331 selectedScheduling.clear();
332 selectedScheduling.add(index);
333 processTime = tmpTime;
334 } else if(tmpTime == processTime) {
335 selectedScheduling.add(index);
339 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
340 for(int i = 0; i < selectedScheduling.size(); i++) {
341 System.out.print(selectedScheduling.elementAt(i) + ", ");
343 System.out.println();
345 /*ScheduleSimulator scheduleSimulator = new ScheduleSimulator(4, state, ta);
346 Vector<Schedule> scheduling = new Vector<Schedule>();
347 for(int i = 0; i < 4; i++) {
348 Schedule schedule = new Schedule(i);
349 scheduling.add(schedule);
351 Iterator it_tasks = state.getTaskSymbolTable().getAllDescriptorsIterator();
352 while(it_tasks.hasNext()) {
353 TaskDescriptor td = (TaskDescriptor)it_tasks.next();
354 if(td.getSymbol().equals("t10")) {
355 scheduling.elementAt(1).addTask(td);
357 scheduling.elementAt(0).addTask(td);
360 ClassDescriptor cd = (ClassDescriptor)state.getClassSymbolTable().get("E");
361 scheduling.elementAt(0).addTargetCore(cd, 1);
362 scheduleSimulator.setScheduling(scheduling);
363 scheduleSimulator.process();
365 Vector<Schedule> scheduling1 = new Vector<Schedule>();
366 for(int i = 0; i < 4; i++) {
367 Schedule schedule = new Schedule(i);
368 scheduling1.add(schedule);
370 Iterator it_tasks1 = state.getTaskSymbolTable().getAllDescriptorsIterator();
371 while(it_tasks1.hasNext()) {
372 TaskDescriptor td = (TaskDescriptor)it_tasks1.next();
373 scheduling1.elementAt(0).addTask(td);
375 scheduleSimulator.setScheduling(scheduling1);
376 scheduleSimulator.process();*/
378 // Close the streams.
381 System.setOut(origOut);
382 } catch (Exception e) {
383 origOut.println ("Redirect: Unable to close files!");
390 CallGraph callgraph=new CallGraph(state);
391 if (state.PREFETCH) {
392 PrefetchAnalysis pa=new PrefetchAnalysis(state, callgraph, tu);
394 LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
395 GenerateConversions gc=new GenerateConversions(la, state);
396 BuildCode bc=new BuildCode(state, bf.getMap(), tu, la);
399 BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa);
403 if (state.FLATIRGRAPH) {
404 FlatIRGraph firg = new FlatIRGraph(state,
405 state.FLATIRGRAPHTASKS,
406 state.FLATIRGRAPHUSERMETHODS,
407 state.FLATIRGRAPHLIBMETHODS);
410 if (state.OWNERSHIP) {
411 CallGraph callGraph = new CallGraph( state );
412 int allocationDepth = 3;
413 OwnershipAnalysis oa =
414 new OwnershipAnalysis( state, callGraph, allocationDepth );
420 /** Reads in a source file and adds the parse tree to the state object. */
422 private static void readSourceFile(State state, String sourcefile) throws Exception {
423 Reader fr = new BufferedReader(new FileReader(sourcefile));
424 Lex.Lexer l = new Lex.Lexer(fr);
425 java_cup.runtime.lr_parser g;
426 g = new Parse.Parser(l);
429 p=(ParseNode) g./*debug_*/parse().value;
430 } catch (Exception e) {
431 System.err.println("Error parsing file:"+sourcefile);
435 state.addParseNode(p);
436 if (l.numErrors()!=0) {
437 System.out.println("Error parsing "+sourcefile);
438 System.exit(l.numErrors());