b0ff6efdd4dc2df6b57f753daac8950ffb3c85b3
[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
82       if (state.TASK) {
83           readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
84           readSourceFile(state, ClassLibraryPrefix+"Socket.java");
85           readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
86       } else {
87           readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
88           readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
89       }
90
91       BuildIR bir=new BuildIR(state);
92       bir.buildtree();
93       
94 //      PrintTree ptree=new PrintTree(state);
95  //     ptree.buildtree();
96
97       TypeUtil tu=new TypeUtil(state);
98       
99       SemanticCheck sc=new SemanticCheck(state,tu);
100       sc.semanticCheck();
101       tu.createFullTable();
102
103       BuildFlat bf=new BuildFlat(state,tu);
104       bf.buildFlat();
105
106 //      System.out.println("Flat");
107 //    PrintTree ptree1=new PrintTree(state);
108 //  ptree1.buildtree();
109
110         TaskAnalysis ta=new TaskAnalysis(state,bf.getMap());
111         ta.taskAnalysis();
112 //      ta.printAdjList();
113
114
115
116       BuildCode bc=new BuildCode(state, bf.getMap(), tu);
117       bc.buildCode();
118       System.exit(0);
119   }
120
121     /** Reads in a source file and adds the parse tree to the state object. */
122     
123     private static void readSourceFile(State state, String sourcefile) throws Exception {
124         Reader fr = new BufferedReader(new FileReader(sourcefile));
125         Lex.Lexer l = new Lex.Lexer(fr);
126         java_cup.runtime.lr_parser g;
127         g = new Parse.Parser(l);
128         ParseNode p=null;
129         try {
130             p=(ParseNode) g./*debug_*/parse().value;
131         } catch (Exception e) {
132             System.err.println("Error parsing file:"+sourcefile);
133             e.printStackTrace();
134             System.exit(-1);
135         }
136         state.addParseNode(p);
137         if (l.numErrors()!=0) {
138             System.out.println("Error parsing "+sourcefile);
139             System.exit(l.numErrors());
140         }
141     }
142 }