1
3 package jminusminus;
4
5 import java.io.FileNotFoundException;
6
7 import static jminusminus.TokenKind.EOF;
8
9
34 public class Main {
35 private static boolean errorHasOccurred;
37
38
43 public static void main(String args[]) {
44 String caller = "java jminusminus.Main";
45 String sourceFile = "";
46 String debugOption = "";
47 String outputDir = ".";
48 boolean spimOutput = false;
49 String registerAllocation = "";
50 errorHasOccurred = false;
51 for (int i = 0; i < args.length; i++) {
52 if (args[i].equals("j--")) {
53 caller = "j--";
54 } else if (args[i].endsWith(".java")) {
55 sourceFile = args[i];
56 } else if (args[i].equals("-t") || args[i].equals("-p") || args[i].equals("-pa") ||
57 args[i].equals("-a")) {
58 debugOption = args[i];
59 } else if (args[i].endsWith("-d") && (i + 1) < args.length) {
60 outputDir = args[++i];
61 } else if (args[i].endsWith("-s") && (i + 1) < args.length) {
62 spimOutput = true;
63 registerAllocation = args[++i];
64 if (!registerAllocation.equals("naive") && !registerAllocation.equals("linear") &&
65 !registerAllocation.equals("graph") || registerAllocation.equals("")) {
66 printUsage(caller);
67 return;
68 }
69 } else if (args[i].endsWith("-r") && (i + 1) < args.length) {
70 NPhysicalRegister.MAX_COUNT = Math.min(18, Integer.parseInt(args[++i]));
71 NPhysicalRegister.MAX_COUNT = Math.max(1, NPhysicalRegister.MAX_COUNT);
72 } else {
73 printUsage(caller);
74 return;
75 }
76 }
77 if (sourceFile.equals("")) {
78 printUsage(caller);
79 return;
80 }
81
82 LookaheadScanner scanner = null;
83 try {
84 scanner = new LookaheadScanner(sourceFile);
85 } catch (FileNotFoundException e) {
86 System.err.println("Error: file " + sourceFile + " not found.");
87 return;
88 }
89
90 if (debugOption.equals("-t")) {
91 TokenInfo token;
93 do {
94 scanner.next();
95 token = scanner.token();
96 System.out.printf("%d\t : %s = %s\n", token.line(), token.tokenRep(),
97 token.image());
98 } while (token.kind() != EOF);
99 errorHasOccurred |= scanner.errorHasOccured();
100 return;
101 }
102
103 Parser parser = new Parser(scanner);
105 JCompilationUnit ast = parser.compilationUnit();
106 errorHasOccurred |= parser.errorHasOccurred();
107 if (debugOption.equals("-p")) {
108 JSONElement json = new JSONElement();
109 ast.toJSON(json);
110 System.out.println(json.toString());
111 return;
112 }
113 if (errorHasOccurred) {
114 return;
115 }
116
117 ast.preAnalyze();
119 errorHasOccurred |= JAST.compilationUnit.errorHasOccurred();
120 if (debugOption.equals("-pa")) {
121 JSONElement json = new JSONElement();
122 ast.toJSON(json);
123 System.out.println(json.toString());
124 return;
125 }
126 if (errorHasOccurred) {
127 return;
128 }
129
130 ast.analyze(null);
132 errorHasOccurred |= JAST.compilationUnit.errorHasOccurred();
133 if (debugOption.equals("-a")) {
134 JSONElement json = new JSONElement();
135 ast.toJSON(json);
136 System.out.println(json.toString());
137 return;
138 }
139 if (errorHasOccurred) {
140 return;
141 }
142
143 CLEmitter clEmitter = new CLEmitter(!spimOutput);
145 clEmitter.destinationDir(outputDir);
146 ast.codegen(clEmitter);
147 errorHasOccurred |= clEmitter.errorHasOccurred();
148 if (errorHasOccurred) {
149 return;
150 }
151
152 if (spimOutput) {
155 NEmitter nEmitter = new NEmitter(sourceFile, ast.clFiles(), registerAllocation);
156 nEmitter.destinationDir(outputDir);
157 nEmitter.write();
158 errorHasOccurred |= nEmitter.errorHasOccurred();
159 }
160 }
161
162 private static void printUsage(String caller) {
164 String usage = "Usage: " + caller
165 + " <options> <source file>\n"
166 + "Where possible options include:\n"
167 + " -t Only tokenize input and print tokens to STDOUT\n"
168 + " -p Only parse input and print AST to STDOUT\n"
169 + " -pa Only parse and pre-analyze input and print AST to STDOUT\n"
170 + " -a Only parse, pre-analyze, and analyze input and print AST to STDOUT\n"
171 + " -s <naive|linear|graph> Generate SPIM code\n"
172 + " -r <num> Physical registers (1-18) available for allocation; default = 8\n"
173 + " -d <dir> Specify where to place output files; default = .";
174 System.out.println(usage);
175 }
176 }
177