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;
16 /** Main method for the compiler. */
18 public static void main(String args[]) throws Exception {
19 String ClassLibraryPrefix="./ClassLibrary/";
20 State state=new State();
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"))
32 else if (option.equals("-struct"))
33 state.structfile=args[++i];
34 else if (option.equals("-conscheck"))
36 else if (option.equals("-task"))
38 else if (option.equals("-thread"))
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");
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");
56 readSourceFile(state, args[i]);
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");
73 readSourceFile(state, ClassLibraryPrefix+"Object.java");
74 readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
75 readSourceFile(state, ClassLibraryPrefix+"Socket.java");
76 readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
78 readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
79 readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
80 readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
84 readSourceFile(state, ClassLibraryPrefix+"Thread.java");
87 BuildIR bir=new BuildIR(state);
90 TypeUtil tu=new TypeUtil(state);
92 SemanticCheck sc=new SemanticCheck(state,tu);
96 BuildFlat bf=new BuildFlat(state,tu);
99 BuildCode bc=new BuildCode(state, bf.getMap(), tu);
104 /** Reads in a source file and adds the parse tree to the state object. */
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());