fix changes checked in earlier
[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 //import IR.PrintTree;
14 import Analysis.TaskStateAnalysis.TaskAnalysis;
15
16 public class Main {
17
18     /** Main method for the compiler.  */
19
20   public static void main(String args[]) throws Exception {
21       String ClassLibraryPrefix="./ClassLibrary/";
22       State state=new State();
23       
24       for(int i=0;i<args.length;i++) {
25           String option=args[i];
26           if (option.equals("-precise"))
27               IR.Flat.BuildCode.GENERATEPRECISEGC=true;
28           else if (option.equals("-dir"))
29               IR.Flat.BuildCode.PREFIX=args[++i]+"/";
30           else if (option.equals("-classlibrary"))
31               ClassLibraryPrefix=args[++i]+"/";
32           else if (option.equals("-mainclass"))
33               state.main=args[++i];
34           else if (option.equals("-struct"))
35               state.structfile=args[++i];
36           else if (option.equals("-conscheck"))
37               state.CONSCHECK=true;
38           else if (option.equals("-task"))
39               state.TASK=true;
40           else if (option.equals("-thread"))
41               state.THREAD=true;
42           else if (option.equals("-instructionfailures"))
43               state.INSTRUCTIONFAILURE=true;
44           else if (option.equals("-help")) {
45               System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
46               System.out.println("-dir outputdirectory -- output code in outputdirectory");
47               System.out.println("-struct structfile -- output structure declarations for repair tool");
48               System.out.println("-mainclass -- main function to call");
49               System.out.println("-precise -- use precise garbage collection");
50
51               System.out.println("-conscheck -- turn on consistency checking");
52               System.out.println("-task -- compiler for tasks");
53               System.out.println("-thread -- threads");
54               System.out.println("-instructionfailures -- insert code for instruction level failures");
55               System.out.println("-help -- print out help");
56               System.exit(0);
57           } else {
58               readSourceFile(state, args[i]);
59           }
60       }
61       
62
63       readSourceFile(state, ClassLibraryPrefix+"System.java");
64       readSourceFile(state, ClassLibraryPrefix+"String.java");
65       readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
66       readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
67       readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
68       readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
69       readSourceFile(state, ClassLibraryPrefix+"Integer.java");
70       readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
71       readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
72       readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
73       readSourceFile(state, ClassLibraryPrefix+"File.java");
74       readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
75
76       if (state.THREAD) {
77           readSourceFile(state, ClassLibraryPrefix+"Thread.java");
78           readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
79       } else {
80           readSourceFile(state, ClassLibraryPrefix+"Object.java");
81           //      readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
82       }
83
84       if (state.TASK) {
85           readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
86           readSourceFile(state, ClassLibraryPrefix+"Socket.java");
87           readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
88       } else {
89           readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
90           readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
91       }
92
93       BuildIR bir=new BuildIR(state);
94       bir.buildtree();
95       
96 //      PrintTree ptree=new PrintTree(state);
97  //     ptree.buildtree();
98
99       TypeUtil tu=new TypeUtil(state);
100       
101       SemanticCheck sc=new SemanticCheck(state,tu);
102       sc.semanticCheck();
103       tu.createFullTable();
104
105       BuildFlat bf=new BuildFlat(state,tu);
106       bf.buildFlat();
107
108 //      System.out.println("Flat");
109 //    PrintTree ptree1=new PrintTree(state);
110 //  ptree1.buildtree();
111
112 //      TaskAnalysis ta=new TaskAnalysis(state,bf.getMap());
113 //      ta.taskAnalysis();
114 //      ta.printAdjList();
115
116
117
118       BuildCode bc=new BuildCode(state, bf.getMap(), tu);
119       bc.buildCode();
120       System.exit(0);
121   }
122
123     /** Reads in a source file and adds the parse tree to the state object. */
124     
125     private static void readSourceFile(State state, String sourcefile) throws Exception {
126         Reader fr = new BufferedReader(new FileReader(sourcefile));
127         Lex.Lexer l = new Lex.Lexer(fr);
128         java_cup.runtime.lr_parser g;
129         g = new Parse.Parser(l);
130         ParseNode p=null;
131         try {
132             p=(ParseNode) g./*debug_*/parse().value;
133         } catch (Exception e) {
134             System.err.println("Error parsing file:"+sourcefile);
135             e.printStackTrace();
136             System.exit(-1);
137         }
138         state.addParseNode(p);
139         if (l.numErrors()!=0) {
140             System.out.println("Error parsing "+sourcefile);
141             System.exit(l.numErrors());
142         }
143     }
144 }