e9b82b964763480cc99e87e55f83288f0918c7c6
[IRC.git] / Robust / src / Main / Main.java
1 package Main;
2
3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
5 import java.io.Reader;
6 import java.io.BufferedReader;
7 import java.io.FileReader;
8 import java.io.FileInputStream;
9 import java.util.Iterator;
10 import java.util.Set;
11 import java.util.Vector;
12
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;
20 import IR.State;
21 import IR.TaskDescriptor;
22 import IR.TypeUtil;
23 import Analysis.Scheduling.Schedule;
24 import Analysis.Scheduling.ScheduleAnalysis;
25 import Analysis.Scheduling.ScheduleSimulator;
26 import Analysis.TaskStateAnalysis.TaskAnalysis;
27 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
28 import Analysis.TaskStateAnalysis.TaskGraph;
29 import Analysis.CallGraph.CallGraph;
30 import Analysis.TaskStateAnalysis.FEdge;
31 import Analysis.TaskStateAnalysis.FlagState;
32 import Analysis.TaskStateAnalysis.TagAnalysis;
33 import Analysis.TaskStateAnalysis.GarbageAnalysis;
34 import Analysis.TaskStateAnalysis.ExecutionGraph;
35 import Analysis.TaskStateAnalysis.SafetyAnalysis;
36 import Analysis.Locality.LocalityAnalysis;
37 import Analysis.Locality.GenerateConversions;
38 import Analysis.Prefetch.PrefetchAnalysis;
39 import Analysis.FlatIRGraph.FlatIRGraph;
40 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
41 import Interface.*;
42 import Util.GraphNode;
43 import Util.GraphNode.DFS;
44 import Util.GraphNode.SCC;
45
46 public class Main {
47
48   /** Main method for the compiler.  */
49
50   public static void main(String args[]) throws Exception {
51     String ClassLibraryPrefix="./ClassLibrary/";
52     State state=new State();
53
54     for(int i=0; i<args.length; i++) {
55       String option=args[i];
56       if (option.equals("-precise"))
57         IR.Flat.BuildCode.GENERATEPRECISEGC=true;
58       else if (option.equals("-prefetch"))
59         state.PREFETCH=true;
60       else if (option.equals("-dir"))
61         IR.Flat.BuildCode.PREFIX=args[++i]+"/";
62       else if (option.equals("-selfloop"))
63         state.selfloops.add(args[++i]);
64       else if (option.equals("-excprefetch"))
65         state.excprefetch.add(args[++i]);
66       else if (option.equals("-classlibrary"))
67         ClassLibraryPrefix=args[++i]+"/";
68       else if(option.equals("-numcore")) {
69         ++i;
70         state.CORENUM = Integer.parseInt(args[i]);
71       } else if (option.equals("-mainclass"))
72         state.main=args[++i];
73       else if (option.equals("-trueprob")) {
74         state.TRUEPROB=Double.parseDouble(args[++i]);
75       } else if (option.equals("-printflat"))
76         State.PRINTFLAT=true;
77       else if (option.equals("-struct"))
78         state.structfile=args[++i];
79       else if (option.equals("-conscheck"))
80         state.CONSCHECK=true;
81       else if (option.equals("-task"))
82         state.TASK=true;
83       else if (option.equals("-taskstate"))
84         state.TASKSTATE=true;
85       else if (option.equals("-tagstate"))
86         state.TAGSTATE=true;
87       else if (option.equals("-flatirtasks")) {
88         state.FLATIRGRAPH=true;
89         state.FLATIRGRAPHTASKS=true;
90       } else if (option.equals("-flatirusermethods")) {
91         state.FLATIRGRAPH=true;
92         state.FLATIRGRAPHUSERMETHODS=true;
93       } else if (option.equals("-flatirlibmethods")) {
94         state.FLATIRGRAPH=true;
95         state.FLATIRGRAPHLIBMETHODS=true;
96       } else if (option.equals("-multicore"))
97         state.MULTICORE=true;
98       else if (option.equals("-ownership"))
99         state.OWNERSHIP=true;
100       else if (option.equals("-ownallocdepth")) {
101         state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
102       } else if (option.equals("-ownwritedots")) {
103         state.OWNERSHIPWRITEDOTS=true;
104         if (args[++i].equals("all")) {
105           state.OWNERSHIPWRITEALL=true;
106         }
107       } else if (option.equals("-ownaliasfile"))
108         state.OWNERSHIPALIASFILE=args[++i];
109       else if (option.equals("-optional"))
110         state.OPTIONAL=true;
111       else if (option.equals("-raw"))
112         state.RAW=true;
113       else if (option.equals("-scheduling"))
114         state.SCHEDULING=true;
115       else if (option.equals("-useprofile"))
116         state.USEPROFILE=true;
117       else if (option.equals("-thread"))
118         state.THREAD=true;
119       else if (option.equals("-dsm"))
120         state.DSM=true;
121       else if (option.equals("-webinterface"))
122         state.WEBINTERFACE=true;
123       else if (option.equals("-instructionfailures"))
124         state.INSTRUCTIONFAILURE=true;
125       else if (option.equals("-help")) {
126         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
127         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
128         System.out.println("-dir outputdirectory -- output code in outputdirectory");
129         System.out.println("-struct structfile -- output structure declarations for repair tool");
130         System.out.println("-mainclass -- main function to call");
131         System.out.println("-dsm -- distributed shared memory support");
132         System.out.println("-precise -- use precise garbage collection");
133         System.out.println("-conscheck -- turn on consistency checking");
134         System.out.println("-task -- compiler for tasks");
135         System.out.println("-thread -- threads");
136         System.out.println("-trueprob <d> -- probability of true branch");
137         System.out.println("-printflat -- print out flat representation");
138         System.out.println("-instructionfailures -- insert code for instruction level failures");
139         System.out.println("-taskstate -- do task state analysis");
140         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
141         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
142         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
143         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
144         System.out.println("-ownership -- do ownership analysis");
145         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
146         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
147         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
148         System.out.println("-optional -- enable optional arguments");
149         System.out.println("-webinterface -- enable web interface");
150         System.out.println("-help -- print out help");
151         System.exit(0);
152       } else {
153         readSourceFile(state, args[i]);
154       }
155     }
156
157
158     readSourceFile(state, ClassLibraryPrefix+"System.java");
159     readSourceFile(state, ClassLibraryPrefix+"String.java");
160     readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
161     readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
162     readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
163     readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
164     readSourceFile(state, ClassLibraryPrefix+"Integer.java");
165     readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
166     //if(!state.RAW) {
167     readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
168     readSourceFile(state, ClassLibraryPrefix+"InputStream.java");
169     readSourceFile(state, ClassLibraryPrefix+"OutputStream.java");
170     readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
171     readSourceFile(state, ClassLibraryPrefix+"File.java");
172     readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
173     readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java");
174     readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java");
175     //}
176     readSourceFile(state, ClassLibraryPrefix+"Math.java");
177     readSourceFile(state, ClassLibraryPrefix+"gnu/Random.java");
178     readSourceFile(state, ClassLibraryPrefix+"Vector.java");
179     readSourceFile(state, ClassLibraryPrefix+"Enumeration.java");
180     readSourceFile(state, ClassLibraryPrefix+"Dictionary.java");
181     readSourceFile(state, ClassLibraryPrefix+"Writer.java");
182     readSourceFile(state, ClassLibraryPrefix+"BufferedWriter.java");
183     readSourceFile(state, ClassLibraryPrefix+"OutputStreamWriter.java");
184     readSourceFile(state, ClassLibraryPrefix+"FileWriter.java");
185     readSourceFile(state, ClassLibraryPrefix+"Date.java");
186
187     if (state.TASK) {
188       readSourceFile(state, ClassLibraryPrefix+"Object.java");
189       readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
190     } else if (state.DSM) {
191       readSourceFile(state, ClassLibraryPrefix+"ThreadDSM.java");
192       readSourceFile(state, ClassLibraryPrefix+"ObjectJavaDSM.java");
193       readSourceFile(state, ClassLibraryPrefix+"Barrier.java");
194     } else {
195       if (state.THREAD) {
196         readSourceFile(state, ClassLibraryPrefix+"Thread.java");
197         readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
198       } else
199         readSourceFile(state, ClassLibraryPrefix+"ObjectJavaNT.java");
200     }
201
202     if (state.TASK) {
203       readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
204       readSourceFile(state, ClassLibraryPrefix+"Socket.java");
205       readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
206     } else {
207       readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
208       readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
209     }
210
211     BuildIR bir=new BuildIR(state);
212     bir.buildtree();
213
214     TypeUtil tu=new TypeUtil(state);
215
216     SemanticCheck sc=new SemanticCheck(state,tu);
217     sc.semanticCheck();
218     tu.createFullTable();
219
220     BuildFlat bf=new BuildFlat(state,tu);
221     bf.buildFlat();
222     SafetyAnalysis sa=null;
223     PrefetchAnalysis pa=null;
224
225     if (state.TAGSTATE) {
226       CallGraph callgraph=new CallGraph(state);
227       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
228       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis);
229     }
230
231     if (state.TASKSTATE) {
232       CallGraph callgraph=new CallGraph(state);
233       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
234       TaskAnalysis ta=new TaskAnalysis(state, taganalysis);
235       ta.taskAnalysis();
236       TaskGraph tg=new TaskGraph(state, ta);
237       tg.createDOTfiles();
238
239       if (state.OPTIONAL) {
240         ExecutionGraph et=new ExecutionGraph(state, ta);
241         et.createExecutionGraph();
242         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
243         sa.doAnalysis();
244         state.storeAnalysisResult(sa.getResult());
245         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
246       }
247
248       if (state.WEBINTERFACE) {
249         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
250         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
251         JhttpServer serve=new JhttpServer(8000,wi);
252         serve.run();
253       }
254
255       if (state.SCHEDULING) {
256         // Indentify backedges
257         for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
258           ClassDescriptor cd=(ClassDescriptor) it_classes.next();
259           if(cd.hasFlags()) {
260             Set<FlagState> fss = ta.getFlagStates(cd);
261             SCC scc=GraphNode.DFS.computeSCC(fss);
262             if (scc.hasCycles()) {
263               for(int i=0; i<scc.numSCC(); i++) {
264                 if (scc.hasCycle(i)) {
265                   Set cycleset = scc.getSCC(i);
266                   Iterator it_fs = cycleset.iterator();
267                   while(it_fs.hasNext()) {
268                     FlagState fs = (FlagState)it_fs.next();
269                     Iterator it_edges = fs.edges();
270                     while(it_edges.hasNext()) {
271                       FEdge edge = (FEdge)it_edges.next();
272                       if(cycleset.contains(edge.getTarget())) {
273                         // a backedge
274                         edge.setisbackedge(true);
275                       }
276                     }
277                   }
278                 }
279               }
280             }
281           }
282         }
283
284         // set up profiling data
285         if(state.USEPROFILE) {
286           // read in profile data and set
287           FileInputStream inStream = new FileInputStream("/scratch/profile.rst");
288           byte[] b = new byte[1024 * 100];
289           int length = inStream.read(b);
290           if(length < 0) {
291             System.out.print("No content in input file: /scratch/profile.rst\n");
292             System.exit(-1);
293           }
294           String profiledata = new String(b, 0, length);
295           java.util.Hashtable<String, Integer> taskinfo = new java.util.Hashtable<String, Integer>();
296
297           int inindex = profiledata.indexOf('\n');
298           while((inindex != -1) ) {
299             String inline = profiledata.substring(0, inindex);
300             profiledata = profiledata.substring(inindex + 1);
301             //System.printString(inline + "\n");
302             int tmpinindex = inline.indexOf(',');
303             if(tmpinindex == -1) {
304               break;
305             }
306             String inname = inline.substring(0, tmpinindex);
307             String inint = inline.substring(tmpinindex + 1);
308             while(inint.startsWith(" ")) {
309               inint = inint.substring(1);
310             }
311             int duration = Integer.parseInt(inint);
312             taskinfo.put(inname, duration);
313             inindex = profiledata.indexOf('\n');
314           }
315
316           java.util.Random r=new java.util.Random();
317           int tint = 0;
318           for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
319             ClassDescriptor cd=(ClassDescriptor) it_classes.next();
320             if(cd.hasFlags()) {
321               Vector rootnodes=ta.getRootNodes(cd);
322               if(rootnodes!=null) {
323                 for(Iterator it_rootnodes=rootnodes.iterator(); it_rootnodes.hasNext();) {
324                   FlagState root=(FlagState)it_rootnodes.next();
325                   Vector allocatingTasks = root.getAllocatingTasks();
326                   if(allocatingTasks != null) {
327                     for(int k = 0; k < allocatingTasks.size(); k++) {
328                       TaskDescriptor td = (TaskDescriptor)allocatingTasks.elementAt(k);
329                       Vector<FEdge> fev = (Vector<FEdge>)ta.getFEdgesFromTD(td);
330                       int numEdges = fev.size();
331                       int total = 100;
332                       for(int j = 0; j < numEdges; j++) {
333                         FEdge pfe = fev.elementAt(j);
334                         if(numEdges - j == 1) {
335                           pfe.setProbability(total);
336                         } else {
337                           if((total != 0) && (total != 1)) {
338                             do {
339                               tint = r.nextInt()%total;
340                             } while(tint <= 0);
341                           }
342                           pfe.setProbability(tint);
343                           total -= tint;
344                         }
345                         //do {
346                         //   tint = r.nextInt()%10;
347                         //  } while(tint <= 0);
348                         //int newRate = tint;
349                         //int newRate = (j+1)%2+1;
350                         int newRate = 1;
351                         String cdname = cd.getSymbol();
352                         if((cdname.equals("SeriesRunner")) ||
353                            (cdname.equals("MDRunner")) ||
354                            (cdname.equals("Stage")) ||
355                            (cdname.equals("AppDemoRunner")) ||
356                            (cdname.equals("FilterBankAtom"))) {
357                           newRate = 16;
358                         } else if(cdname.equals("SentenceParser")) {
359                           newRate = 4;
360                         }
361                         //do {
362                         //    tint = r.nextInt()%100;
363                         //   } while(tint <= 0);
364                         //   int probability = tint;
365                         int probability = 100;
366                         pfe.addNewObjInfo(cd, newRate, probability);
367                       }
368                     }
369                   }
370                 }
371               }
372               Iterator it_flags = ta.getFlagStates(cd).iterator();
373               while(it_flags.hasNext()) {
374                 FlagState fs = (FlagState)it_flags.next();
375                 Iterator it_edges = fs.edges();
376                 int total = 100;
377                 while(it_edges.hasNext()) {
378                   //do {
379                   //    tint = r.nextInt()%10;
380                   //   } while(tint <= 0);
381                   FEdge edge = (FEdge)it_edges.next();
382                   tint = taskinfo.get(edge.getTask().getSymbol()).intValue();
383                   edge.setExeTime(tint);
384                   if((fs.getClassDescriptor().getSymbol().equals("MD")) && (edge.getTask().getSymbol().equals("t6"))) {
385                     if(edge.isbackedge()) {
386                       if(edge.getTarget().equals(edge.getSource())) {
387                         edge.setProbability(93.75);
388                       } else {
389                         edge.setProbability(3.125);
390                       }
391                     } else {
392                       edge.setProbability(3.125);
393                     }
394                     continue;
395                   }
396                   if(!it_edges.hasNext()) {
397                     edge.setProbability(total);
398                   } else {
399                     if((total != 0) && (total != 1)) {
400                       do {
401                         tint = r.nextInt()%total;
402                       } while(tint <= 0);
403                     }
404                     edge.setProbability(tint);
405                     total -= tint;
406                   }
407                 }
408               }
409             }
410           }
411         } else {
412           // for test
413           // Randomly set the newRate and probability of FEdges
414           java.util.Random r=new java.util.Random();
415           int tint = 0;
416           for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
417             ClassDescriptor cd=(ClassDescriptor) it_classes.next();
418             if(cd.hasFlags()) {
419               Vector rootnodes=ta.getRootNodes(cd);
420               if(rootnodes!=null) {
421                 for(Iterator it_rootnodes=rootnodes.iterator(); it_rootnodes.hasNext();) {
422                   FlagState root=(FlagState)it_rootnodes.next();
423                   Vector allocatingTasks = root.getAllocatingTasks();
424                   if(allocatingTasks != null) {
425                     for(int k = 0; k < allocatingTasks.size(); k++) {
426                       TaskDescriptor td = (TaskDescriptor)allocatingTasks.elementAt(k);
427                       Vector<FEdge> fev = (Vector<FEdge>)ta.getFEdgesFromTD(td);
428                       int numEdges = fev.size();
429                       int total = 100;
430                       for(int j = 0; j < numEdges; j++) {
431                         FEdge pfe = fev.elementAt(j);
432                         if(numEdges - j == 1) {
433                           pfe.setProbability(total);
434                         } else {
435                           if((total != 0) && (total != 1)) {
436                             do {
437                               tint = r.nextInt()%total;
438                             } while(tint <= 0);
439                           }
440                           pfe.setProbability(tint);
441                           total -= tint;
442                         }
443                         //do {
444                         //   tint = r.nextInt()%10;
445                         //  } while(tint <= 0);
446                         //int newRate = tint;
447                         //int newRate = (j+1)%2+1;
448                         int newRate = 1;
449                         String cdname = cd.getSymbol();
450                         if((cdname.equals("SeriesRunner")) ||
451                            (cdname.equals("MDRunner")) ||
452                            (cdname.equals("Stage")) ||
453                            (cdname.equals("AppDemoRunner")) ||
454                            (cdname.equals("FilterBankAtom"))) {
455                           newRate = 16;
456                         } else if(cdname.equals("SentenceParser")) {
457                           newRate = 4;
458                         }
459                         //do {
460                         //    tint = r.nextInt()%100;
461                         //   } while(tint <= 0);
462                         //   int probability = tint;
463                         int probability = 100;
464                         pfe.addNewObjInfo(cd, newRate, probability);
465                       }
466                     }
467                   }
468                 }
469               }
470
471               Iterator it_flags = ta.getFlagStates(cd).iterator();
472               while(it_flags.hasNext()) {
473                 FlagState fs = (FlagState)it_flags.next();
474                 Iterator it_edges = fs.edges();
475                 int total = 100;
476                 while(it_edges.hasNext()) {
477                   //do {
478                   //    tint = r.nextInt()%10;
479                   //   } while(tint <= 0);
480                   tint = 3;
481                   FEdge edge = (FEdge)it_edges.next();
482                   edge.setExeTime(tint);
483                   if((fs.getClassDescriptor().getSymbol().equals("MD")) && (edge.getTask().getSymbol().equals("t6"))) {
484                     if(edge.isbackedge()) {
485                       if(edge.getTarget().equals(edge.getSource())) {
486                         edge.setProbability(93.75);
487                       } else {
488                         edge.setProbability(3.125);
489                       }
490                     } else {
491                       edge.setProbability(3.125);
492                     }
493                     continue;
494                   }
495                   if(!it_edges.hasNext()) {
496                     edge.setProbability(total);
497                   } else {
498                     if((total != 0) && (total != 1)) {
499                       do {
500                         tint = r.nextInt()%total;
501                       } while(tint <= 0);
502                     }
503                     edge.setProbability(tint);
504                     total -= tint;
505                   }
506                 }
507               }
508             }
509           }
510         }
511
512         // Use ownership analysis to get alias information
513         CallGraph callGraph = new CallGraph(state);
514         OwnershipAnalysis oa = new OwnershipAnalysis(state,
515                                                      tu,
516                                                      callGraph,
517                                                      state.OWNERSHIPALLOCDEPTH,
518                                                      state.OWNERSHIPWRITEDOTS,
519                                                      state.OWNERSHIPWRITEALL,
520                                                      state.OWNERSHIPALIASFILE);
521
522         // Save the current standard input, output, and error streams
523         // for later restoration.
524         PrintStream origOut = System.out;
525
526         // Create a new output stream for the standard output.
527         PrintStream stdout  = null;
528         try {
529           stdout = new PrintStream(new FileOutputStream("SimulatorResult.out"));
530         } catch (Exception e) {
531           // Sigh.  Couldn't open the file.
532           System.out.println("Redirect:  Unable to open output file!");
533           System.exit(1);
534         }
535
536         // Print stuff to the original output and error streams.
537         // On most systems all of this will end up on your console when you
538         // run this application.
539         //origOut.println ("\nRedirect:  Round #1");
540         //System.out.println ("Test output via 'System.out'.");
541         //origOut.println ("Test output via 'origOut' reference.");
542
543         // Set the System out and err streams to use our replacements.
544         System.setOut(stdout);
545
546         // Print stuff to the original output and error streams.
547         // The stuff printed through the 'origOut' and 'origErr' references
548         // should go to the console on most systems while the messages
549         // printed through the 'System.out' and 'System.err' will end up in
550         // the files we created for them.
551         //origOut.println ("\nRedirect:  Round #2");
552         //System.out.println ("Test output via 'SimulatorResult.out'.");
553         //origOut.println ("Test output via 'origOut' reference.");
554
555         // generate multiple schedulings
556         ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state, ta);
557         scheduleAnalysis.preSchedule();
558         scheduleAnalysis.scheduleAnalysis();
559         //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
560         scheduleAnalysis.setCoreNum(state.CORENUM);
561         scheduleAnalysis.schedule();
562
563         //simulate these schedulings
564         ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(), state, ta);
565         Iterator it_scheduling = scheduleAnalysis.getSchedulingsIter();
566         int index = 0;
567         Vector<Integer> selectedScheduling = new Vector<Integer>();
568         int processTime = Integer.MAX_VALUE;
569         while(it_scheduling.hasNext()) {
570           Vector<Schedule> scheduling = (Vector<Schedule>)it_scheduling.next();
571           scheduleSimulator.setScheduling(scheduling);
572           int tmpTime = scheduleSimulator.process();
573           if(tmpTime < processTime) {
574             selectedScheduling.clear();
575             selectedScheduling.add(index);
576             processTime = tmpTime;
577           } else if(tmpTime == processTime) {
578             selectedScheduling.add(index);
579           }
580           index++;
581         }
582         System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
583         for(int i = 0; i < selectedScheduling.size(); i++) {
584           System.out.print((selectedScheduling.elementAt(i) + 1) + ", ");
585         }
586         System.out.println();
587
588         /*ScheduleSimulator scheduleSimulator = new ScheduleSimulator(4, state, ta);
589            Vector<Schedule> scheduling = new Vector<Schedule>();
590            for(int i = 0; i < 4; i++) {
591             Schedule schedule = new Schedule(i);
592             scheduling.add(schedule);
593            }
594            Iterator it_tasks = state.getTaskSymbolTable().getAllDescriptorsIterator();
595            while(it_tasks.hasNext()) {
596             TaskDescriptor td = (TaskDescriptor)it_tasks.next();
597             if(td.getSymbol().equals("t10")) {
598                 scheduling.elementAt(1).addTask(td);
599             } else {
600                 scheduling.elementAt(0).addTask(td);
601             }
602            }
603            ClassDescriptor cd = (ClassDescriptor)state.getClassSymbolTable().get("E");
604            scheduling.elementAt(0).addTargetCore(cd, 1);
605            scheduleSimulator.setScheduling(scheduling);
606            scheduleSimulator.process();
607
608            Vector<Schedule> scheduling1 = new Vector<Schedule>();
609            for(int i = 0; i < 4; i++) {
610             Schedule schedule = new Schedule(i);
611             scheduling1.add(schedule);
612            }
613            Iterator it_tasks1 = state.getTaskSymbolTable().getAllDescriptorsIterator();
614            while(it_tasks1.hasNext()) {
615             TaskDescriptor td = (TaskDescriptor)it_tasks1.next();
616             scheduling1.elementAt(0).addTask(td);
617            }
618            scheduleSimulator.setScheduling(scheduling1);
619            scheduleSimulator.process();*/
620
621         // Close the streams.
622         try {
623           stdout.close();
624           System.setOut(origOut);
625         } catch (Exception e) {
626           origOut.println("Redirect:  Unable to close files!");
627         }
628
629         if(state.MULTICORE) {
630           //it_scheduling = scheduleAnalysis.getSchedulingsIter();
631           //Vector<Schedule> scheduling = (Vector<Schedule>)it_scheduling.next();
632           Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.lastElement());
633           BuildCodeMultiCore bcm=new BuildCodeMultiCore(state, bf.getMap(), tu, sa, scheduling, scheduleAnalysis.getCoreNum(), pa);
634           bcm.setOwnershipAnalysis(oa);
635           bcm.buildCode();
636         }
637       }
638
639     }
640
641     if(!state.MULTICORE) {
642       if (state.DSM) {
643         CallGraph callgraph=new CallGraph(state);
644         if (state.PREFETCH) {
645           //speed up prefetch generation using locality analysis results
646           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
647           pa=new PrefetchAnalysis(state, callgraph, tu, la);
648         }
649
650         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
651         GenerateConversions gc=new GenerateConversions(la, state);
652         BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
653         bc.buildCode();
654       } else {
655         BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
656         bc.buildCode();
657       }
658     }
659
660     if (state.FLATIRGRAPH) {
661       FlatIRGraph firg = new FlatIRGraph(state,
662                                          state.FLATIRGRAPHTASKS,
663                                          state.FLATIRGRAPHUSERMETHODS,
664                                          state.FLATIRGRAPHLIBMETHODS);
665     }
666
667     if (state.OWNERSHIP) {
668       CallGraph callGraph = new CallGraph(state);
669       OwnershipAnalysis oa = new OwnershipAnalysis(state,
670                                                    tu,
671                                                    callGraph,
672                                                    state.OWNERSHIPALLOCDEPTH,
673                                                    state.OWNERSHIPWRITEDOTS,
674                                                    state.OWNERSHIPWRITEALL,
675                                                    state.OWNERSHIPALIASFILE);
676     }
677
678     System.exit(0);
679   }
680
681   /** Reads in a source file and adds the parse tree to the state object. */
682
683   private static void readSourceFile(State state, String sourcefile) throws Exception {
684     Reader fr = new BufferedReader(new FileReader(sourcefile));
685     Lex.Lexer l = new Lex.Lexer(fr);
686     java_cup.runtime.lr_parser g;
687     g = new Parse.Parser(l);
688     ParseNode p=null;
689     try {
690       p=(ParseNode) g./*debug_*/ parse().value;
691     } catch (Exception e) {
692       System.err.println("Error parsing file:"+sourcefile);
693       e.printStackTrace();
694       System.exit(-1);
695     }
696     state.addParseNode(p);
697     if (l.numErrors()!=0) {
698       System.out.println("Error parsing "+sourcefile);
699       System.exit(l.numErrors());
700     }
701   }
702 }