add new option -singleTM for committing transactions on a single machine
[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.Hashtable;
10 import java.util.Iterator;
11 import java.util.Set;
12 import java.util.Vector;
13
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;
21 import IR.State;
22 import IR.TaskDescriptor;
23 import IR.TypeUtil;
24 import Analysis.Scheduling.MCImplSynthesis;
25 import Analysis.Scheduling.Schedule;
26 import Analysis.Scheduling.ScheduleAnalysis;
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;
43 import Interface.*;
44 import Util.GraphNode;
45 import Util.GraphNode.DFS;
46 import Util.GraphNode.SCC;
47
48 public class Main {
49
50   /** Main method for the compiler.  */
51
52   public static void main(String args[]) throws Exception {
53     String ClassLibraryPrefix="./ClassLibrary/";
54     State state=new State();
55     Vector sourcefiles=new Vector();
56     state.classpath.add(".");
57
58     String outputdir = null;
59     boolean isDistributeInfo = false;
60
61     for(int i=0; i<args.length; i++) {
62       String option=args[i];
63       if (option.equals("-precise"))
64         IR.Flat.BuildCode.GENERATEPRECISEGC=true;
65       else if (option.equals("-prefetch"))
66         state.PREFETCH=true;
67       else if (option.equals("-dir"))
68         IR.Flat.BuildCode.PREFIX=args[++i]+"/";
69       else if (option.equals("-fastcheck"))
70         state.FASTCHECK=true;
71       else if (option.equals("-selfloop"))
72         state.selfloops.add(args[++i]);
73           else if (option.equals("-outputdir"))
74         state.outputdir = args[++i];
75       else if (option.equals("-excprefetch"))
76         state.excprefetch.add(args[++i]);
77       else if (option.equals("-classlibrary"))
78         state.classpath.add(args[++i]);
79       else if(option.equals("-numcore")) {
80         ++i;
81         state.CORENUM = Integer.parseInt(args[i]);
82       } else if (option.equals("-mainclass"))
83         state.main=args[++i];
84       else if (option.equals("-trueprob")) {
85         state.TRUEPROB=Double.parseDouble(args[++i]);
86       } else if (option.equals("-printflat"))
87         State.PRINTFLAT=true;
88       else if (option.equals("-printscheduling"))
89         State.PRINTSCHEDULING=true;
90       else if (option.equals("-printschedulesim"))
91         State.PRINTSCHEDULESIM=true;
92       else if (option.equals("-printcriticalpath"))
93           State.PRINTCRITICALPATH=true;
94       else if (option.equals("-struct"))
95         state.structfile=args[++i];
96       else if (option.equals("-conscheck"))
97         state.CONSCHECK=true;
98       else if (option.equals("-task"))
99         state.TASK=true;
100       else if (option.equals("-abortreaders"))
101         state.ABORTREADERS=true;
102       else if (option.equals("-taskstate"))
103         state.TASKSTATE=true;
104       else if (option.equals("-tagstate"))
105         state.TAGSTATE=true;
106       else if (option.equals("-flatirtasks")) {
107         state.FLATIRGRAPH=true;
108         state.FLATIRGRAPHTASKS=true;
109       } else if (option.equals("-flatirusermethods")) {
110         state.FLATIRGRAPH=true;
111         state.FLATIRGRAPHUSERMETHODS=true;
112       } else if (option.equals("-flatirlibmethods")) {
113         state.FLATIRGRAPH=true;
114         state.FLATIRGRAPHLIBMETHODS=true;
115       } else if (option.equals("-multicore"))
116         state.MULTICORE=true;
117       else if (option.equals("-ownership"))
118         state.OWNERSHIP=true;
119       else if (option.equals("-ownallocdepth")) {
120         state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
121       } else if (option.equals("-ownwritedots")) {
122         state.OWNERSHIPWRITEDOTS=true;
123         if (args[++i].equals("all")) {
124           state.OWNERSHIPWRITEALL=true;
125         }
126       } else if (option.equals("-ownaliasfile"))
127         state.OWNERSHIPALIASFILE=args[++i];
128       else if (option.equals("-optional"))
129         state.OPTIONAL=true;
130       else if (option.equals("-raw"))
131         state.RAW=true;
132       else if (option.equals("-scheduling"))
133         state.SCHEDULING=true;
134       else if (option.equals("-distributioninfo"))
135         isDistributeInfo=true;
136       else if (option.equals("-useprofile"))
137         state.USEPROFILE=true;
138       else if (option.equals("-thread"))
139         state.THREAD=true;
140       else if (option.equals("-dsm"))
141         state.DSM=true;
142       else if (option.equals("-singleTM"))
143         state.SINGLETM=true;
144       else if (option.equals("-webinterface"))
145         state.WEBINTERFACE=true;
146       else if (option.equals("-instructionfailures"))
147         state.INSTRUCTIONFAILURE=true;
148       else if (option.equals("-abcclose"))
149         state.ARRAYBOUNDARYCHECK=false;
150       else if (option.equals("-mlp"))
151         state.MLP=true;
152       else if (option.equals("-help")) {
153         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
154         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
155         System.out.println("-dir outputdirectory -- output code in outputdirectory");
156         System.out.println("-struct structfile -- output structure declarations for repair tool");
157         System.out.println("-mainclass -- main function to call");
158         System.out.println("-dsm -- distributed shared memory support");
159         System.out.println("-singleTM -- single machine committing transactions");
160         System.out.println("-abortreaders -- abort readers");
161         System.out.println("-precise -- use precise garbage collection");
162         System.out.println("-conscheck -- turn on consistency checking");
163         System.out.println("-task -- compiler for tasks");
164         System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
165         System.out.println("-thread -- threads");
166         System.out.println("-trueprob <d> -- probability of true branch");
167         System.out.println("-printflat -- print out flat representation");
168         System.out.println("-instructionfailures -- insert code for instruction level failures");
169         System.out.println("-taskstate -- do task state analysis");
170         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
171         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
172         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
173         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
174         System.out.println("-ownership -- do ownership analysis");
175         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
176         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
177         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
178         System.out.println("-optional -- enable optional arguments");
179         System.out.println("-abcclose close the array boundary check");
180         System.out.println("-scheduling do task scheduling");
181         System.out.println("-mlp build mlp code");
182         System.out.println("-multicore generate multi-core version binary");
183         System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
184         System.out.println("-raw generate raw version binary (should be used together with -multicore)");
185         System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
186         System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
187         System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
188         System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
189         System.out.println("-threadsimulate generate multi-thread simulate version binary");
190         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");
191         System.out.println("-printscheduling -- print out scheduling graphs");
192         System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
193         System.out.println("-webinterface -- enable web interface");
194         System.out.println("-help -- print out help");
195         System.exit(0);
196       } else {
197         sourcefiles.add(args[i]);
198       }
199     }
200
201     //add default classpath
202     if (state.classpath.size()==1)
203       state.classpath.add(ClassLibraryPrefix);
204
205     BuildIR bir=new BuildIR(state);
206     TypeUtil tu=new TypeUtil(state, bir);
207     
208
209     SemanticCheck sc=new SemanticCheck(state,tu);
210
211     for(int i=0;i<sourcefiles.size();i++)
212       loadClass(state, bir,(String)sourcefiles.get(i));
213
214     //Stuff the runtime wants to see
215     sc.getClass("String");
216     sc.getClass("Math");
217     sc.getClass("File");
218     sc.getClass("Socket");
219     sc.getClass("ServerSocket");
220     sc.getClass("FileInputStream");
221     sc.getClass("FileOutputStream");
222     if (state.TASK) {
223       sc.getClass("TagDescriptor");
224     }
225     if (state.THREAD||state.DSM) {
226         sc.getClass("Thread");
227     }
228
229     sc.semanticCheck();
230
231     tu.createFullTable();
232
233     BuildFlat bf=new BuildFlat(state,tu);
234     bf.buildFlat();
235     SafetyAnalysis sa=null;
236     PrefetchAnalysis pa=null;
237
238     if (state.TAGSTATE) {
239       CallGraph callgraph=new CallGraph(state);
240       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
241       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis, tu);
242     }
243
244     if (state.TASKSTATE) {
245       CallGraph callgraph=new CallGraph(state);
246       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
247       TaskAnalysis ta=new TaskAnalysis(state, taganalysis, tu);
248       ta.taskAnalysis();
249       TaskGraph tg=new TaskGraph(state, ta);
250       tg.createDOTfiles();
251
252       if (state.OPTIONAL) {
253         ExecutionGraph et=new ExecutionGraph(state, ta);
254         et.createExecutionGraph();
255         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
256         sa.doAnalysis();
257         state.storeAnalysisResult(sa.getResult());
258         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
259       }
260
261       if (state.WEBINTERFACE) {
262         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
263         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
264         JhttpServer serve=new JhttpServer(8000,wi);
265         serve.run();
266       }
267
268       if (state.SCHEDULING) {
269         // Use ownership analysis to get alias information
270         CallGraph callGraph = new CallGraph(state);
271         OwnershipAnalysis oa = null;/*new OwnershipAnalysis(state,
272                                                      tu,
273                                                      callGraph,
274                                                      state.OWNERSHIPALLOCDEPTH,
275                                                      state.OWNERSHIPWRITEDOTS,
276                                                      state.OWNERSHIPWRITEALL,
277                                                      state.OWNERSHIPALIASFILE);*/
278         
279         // synthesis a layout according to target multicore processor
280         MCImplSynthesis mcImplSynthesis = new MCImplSynthesis(state,
281                                                               ta,
282                                                               oa);
283         if(isDistributeInfo) {
284             mcImplSynthesis.distribution();
285         } else {
286             mcImplSynthesis.setScheduleThreshold(20);
287             mcImplSynthesis.setProbThreshold(0);
288             mcImplSynthesis.setGenerateThreshold(30);
289             Vector<Schedule> scheduling = mcImplSynthesis.synthesis();
290
291             // generate multicore codes
292             if(state.MULTICORE) {
293                 BuildCodeMultiCore bcm=new BuildCodeMultiCore(state,
294                                                               bf.getMap(),
295                                                               tu,
296                                                               sa,
297                                                               scheduling,
298                                                               mcImplSynthesis.getCoreNum(),
299                                                               pa);
300                 bcm.setOwnershipAnalysis(oa);
301                 bcm.buildCode();
302             }
303             scheduling.clear();
304             scheduling = null;
305         }
306       }
307     }
308
309     if(!state.MULTICORE) {
310       if (state.DSM) {
311         CallGraph callgraph=new CallGraph(state);
312         if (state.PREFETCH) {
313           //speed up prefetch generation using locality analysis results
314           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
315           pa=new PrefetchAnalysis(state, callgraph, tu, la);
316         }
317
318         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
319         GenerateConversions gc=new GenerateConversions(la, state);
320         BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
321         bc.buildCode();
322       } else {
323         BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
324         bc.buildCode();
325       }
326     }
327
328     if (state.FLATIRGRAPH) {
329       FlatIRGraph firg = new FlatIRGraph(state,
330                                          state.FLATIRGRAPHTASKS,
331                                          state.FLATIRGRAPHUSERMETHODS,
332                                          state.FLATIRGRAPHLIBMETHODS);
333     }
334
335     if (state.OWNERSHIP) {
336       CallGraph callGraph = new CallGraph(state);
337       OwnershipAnalysis oa = new OwnershipAnalysis(state,
338                                                    tu,
339                                                    callGraph,
340                                                    state.OWNERSHIPALLOCDEPTH,
341                                                    state.OWNERSHIPWRITEDOTS,
342                                                    state.OWNERSHIPWRITEALL,
343                                                    state.OWNERSHIPALIASFILE);
344     }
345
346
347     System.out.println("Lines="+state.lines);
348     System.exit(0);
349   }
350
351   public static void loadClass(State state, BuildIR bir, String sourcefile) {
352     ParseNode pn=readSourceFile(state, sourcefile);
353     bir.buildtree(pn, null);
354   }
355
356   /** Reads in a source file and adds the parse tree to the state object. */
357
358   public static ParseNode readSourceFile(State state, String sourcefile) {
359     try {
360       Reader fr= new BufferedReader(new FileReader(sourcefile));
361       Lex.Lexer l = new Lex.Lexer(fr);
362       java_cup.runtime.lr_parser g;
363       g = new Parse.Parser(l);
364       ParseNode p=null;
365       try {
366         p=(ParseNode) g./*debug_*/ parse().value;
367       } catch (Exception e) {
368         System.err.println("Error parsing file:"+sourcefile);
369         e.printStackTrace();
370         System.exit(-1);
371       }
372       state.addParseNode(p);
373       if (l.numErrors()!=0) {
374         System.out.println("Error parsing "+sourcefile);
375         System.exit(l.numErrors());
376       }
377       state.lines+=l.line_num;
378       return p;
379
380     } catch (Exception e) {
381       throw new Error(e);
382     }
383   }
384 }