b6036fc962aa66d8cd667014f395ec2873828d14
[IRC.git] / Robust / src / Main / Main.java
1 package Main;
2
3 import java.io.Reader;
4 import java.io.BufferedReader;
5 import java.io.FileReader;
6 import IR.Tree.ParseNode;
7 import IR.Tree.BuildIR;
8 import IR.Tree.SemanticCheck;
9 import IR.Flat.BuildFlat;
10 import IR.Flat.BuildCode;
11 import IR.State;
12 import IR.TypeUtil;
13
14 public class Main {
15
16     /** Main method for the compiler.  */
17
18   public static void main(String args[]) throws Exception {
19       String ClassLibraryPrefix="./ClassLibrary/";
20       State state=new State();
21       
22       for(int i=0;i<args.length;i++) {
23           String option=args[i];
24           if (option.equals("-precise"))
25               IR.Flat.BuildCode.GENERATEPRECISEGC=true;
26           else if (option.equals("-dir"))
27               IR.Flat.BuildCode.PREFIX=args[++i]+"/";
28           else if (option.equals("-classlibrary"))
29               ClassLibraryPrefix=args[++i]+"/";
30           else if (option.equals("-mainclass"))
31               state.main=args[++i];
32           else if (option.equals("-struct"))
33               state.structfile=args[++i];
34           else if (option.equals("-conscheck"))
35               state.CONSCHECK=true;
36           else if (option.equals("-task"))
37               state.TASK=true;
38           else if (option.equals("-thread"))
39               state.THREAD=true;
40           else if (option.equals("-instructionfailures"))
41               state.INSTRUCTIONFAILURE=true;
42           else if (option.equals("-help")) {
43               System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
44               System.out.println("-dir outputdirectory -- output code in outputdirectory");
45               System.out.println("-struct structfile -- output structure declarations for repair tool");
46               System.out.println("-mainclass -- main function to call");
47               System.out.println("-precise -- use precise garbage collection");
48
49               System.out.println("-conscheck -- turn on consistency checking");
50               System.out.println("-task -- compiler for tasks");
51               System.out.println("-thread -- threads");
52               System.out.println("-instructionfailures -- insert code for instruction level failures");
53               System.out.println("-help -- print out help");
54               System.exit(0);
55           } else {
56               readSourceFile(state, args[i]);
57           }
58       }
59       
60
61       readSourceFile(state, ClassLibraryPrefix+"System.java");
62       readSourceFile(state, ClassLibraryPrefix+"String.java");
63       readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
64       readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
65       readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
66       readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
67       readSourceFile(state, ClassLibraryPrefix+"Integer.java");
68       readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
69       readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
70       readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
71       readSourceFile(state, ClassLibraryPrefix+"File.java");
72       if (state.TASK) {
73           readSourceFile(state, ClassLibraryPrefix+"Object.java");
74           readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
75           readSourceFile(state, ClassLibraryPrefix+"Socket.java");
76           readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
77       } else {
78           readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
79           readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
80           readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
81       }
82
83       if (state.THREAD) {
84           readSourceFile(state, ClassLibraryPrefix+"Thread.java");
85       }
86
87       BuildIR bir=new BuildIR(state);
88       bir.buildtree();
89       
90       TypeUtil tu=new TypeUtil(state);
91       
92       SemanticCheck sc=new SemanticCheck(state,tu);
93       sc.semanticCheck();
94       tu.createFullTable();
95
96       BuildFlat bf=new BuildFlat(state,tu);
97       bf.buildFlat();
98
99       BuildCode bc=new BuildCode(state, bf.getMap(), tu);
100       bc.buildCode();
101       System.exit(0);
102   }
103
104     /** Reads in a source file and adds the parse tree to the state object. */
105     
106     private static void readSourceFile(State state, String sourcefile) throws Exception {
107         Reader fr = new BufferedReader(new FileReader(sourcefile));
108         Lex.Lexer l = new Lex.Lexer(fr);
109         java_cup.runtime.lr_parser g;
110         g = new Parse.Parser(l);
111         ParseNode p=(ParseNode) g./*debug_*/parse().value;
112         state.addParseNode(p);
113         if (l.numErrors()!=0) {
114             System.out.println("Error parsing "+sourcefile);
115             System.exit(l.numErrors());
116         }
117     }
118 }