1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | #include <iterator> |
---|
4 | #include <algorithm> |
---|
5 | #include <cstdio> |
---|
6 | #include "Parser/Parser.h" |
---|
7 | #include "Parser/ParseNode.h" |
---|
8 | #include "Parser/LinkageSpec.h" |
---|
9 | #include "SynTree/Declaration.h" |
---|
10 | #include "SynTree/Visitor.h" |
---|
11 | #include "GenPoly/Lvalue.h" |
---|
12 | #include "GenPoly/Specialize.h" |
---|
13 | #include "GenPoly/Box.h" |
---|
14 | #include "GenPoly/CopyParams.h" |
---|
15 | #include "CodeGen/Generate.h" |
---|
16 | #include "CodeGen/FixNames.h" |
---|
17 | #include "ControlStruct/Mutate.h" |
---|
18 | #include "Tuples/Mutate.h" |
---|
19 | #include "Tuples/FunctionChecker.h" |
---|
20 | #include "SymTab/Mangler.h" |
---|
21 | #include "SymTab/Indexer.h" |
---|
22 | #include "SymTab/Validate.h" |
---|
23 | #include "ResolvExpr/AlternativePrinter.h" |
---|
24 | #include "ResolvExpr/Resolver.h" |
---|
25 | #include "MakeLibCfa.h" |
---|
26 | #include "InitTweak/Mutate.h" |
---|
27 | //#include "Explain/GenProlog.h" |
---|
28 | //#include "Try/Visit.h" |
---|
29 | |
---|
30 | #include "SemanticError.h" |
---|
31 | #include "UnimplementedError.h" |
---|
32 | #include "utility.h" |
---|
33 | |
---|
34 | #include "../config.h" |
---|
35 | |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | extern "C"{ |
---|
39 | #include <unistd.h> |
---|
40 | } // extern |
---|
41 | |
---|
42 | FILE *open_prelude(); |
---|
43 | FILE *open_builtins(); |
---|
44 | bool beVerbose = false; |
---|
45 | bool resolveVerbose = false; |
---|
46 | |
---|
47 | int main( int argc, char *argv[] ) { |
---|
48 | bool debugp = false, treep = false, astp = false, manglep = false, symtabp = false, validp = false; |
---|
49 | bool preludep = true, protop = false, libp = false; |
---|
50 | bool exprp = false, codegenp = false; |
---|
51 | int c; |
---|
52 | FILE *input, *prelude, *builtins; |
---|
53 | std::ostream *output = &std::cout; |
---|
54 | |
---|
55 | opterr = 0; |
---|
56 | |
---|
57 | while ( (c = getopt( argc, argv, "dtsgmvxcenprlDz:" )) != -1 ) { |
---|
58 | switch (c) { |
---|
59 | case 'd': |
---|
60 | /* bison debugging info */ |
---|
61 | debugp = true; |
---|
62 | break; |
---|
63 | case 't': |
---|
64 | /* dump parse tree */ |
---|
65 | treep = true; |
---|
66 | break; |
---|
67 | case 's': |
---|
68 | /* dump AST */ |
---|
69 | astp = true; |
---|
70 | break; |
---|
71 | case 'g': |
---|
72 | /* print alternatives for expressions */ |
---|
73 | manglep = true; |
---|
74 | break; |
---|
75 | case 'm': |
---|
76 | /* print symbol table events */ |
---|
77 | symtabp = true; |
---|
78 | break; |
---|
79 | case 'r': |
---|
80 | /* print resolver steps */ |
---|
81 | resolveVerbose = true; |
---|
82 | break; |
---|
83 | case 'x': |
---|
84 | /* dump AST after decl validation pass */ |
---|
85 | validp = true; |
---|
86 | break; |
---|
87 | case 'e': |
---|
88 | /* dump AST after expression analysis */ |
---|
89 | exprp = true; |
---|
90 | break; |
---|
91 | case 'z': |
---|
92 | codegenp = true; |
---|
93 | break; |
---|
94 | case 'n': |
---|
95 | /* don't read preamble */ |
---|
96 | preludep = false; |
---|
97 | break; |
---|
98 | case 'p': |
---|
99 | /* generate prototypes for preamble functions */ |
---|
100 | protop = true; |
---|
101 | break; |
---|
102 | case 'l': |
---|
103 | /* generate libcfa.c */ |
---|
104 | libp = true; |
---|
105 | break; |
---|
106 | case 'v': |
---|
107 | /* verbose */ |
---|
108 | beVerbose = true; |
---|
109 | break; |
---|
110 | case 'D': |
---|
111 | /* ignore -Dxxx */ |
---|
112 | break; |
---|
113 | case '?': |
---|
114 | cout << "Unknown option: '" << (char)optopt << "'" << endl; |
---|
115 | exit(1); |
---|
116 | default: |
---|
117 | abort(); |
---|
118 | } // switch |
---|
119 | } // while |
---|
120 | |
---|
121 | try { |
---|
122 | if ( optind < argc ) { |
---|
123 | input = fopen( argv[ optind ], "r" ); |
---|
124 | if ( ! input ) { |
---|
125 | std::cout << "Error: can't open " << argv[optind] << std::endl; |
---|
126 | exit( 1 ); |
---|
127 | } // if |
---|
128 | optind++; |
---|
129 | } else { |
---|
130 | input = stdin; |
---|
131 | } // if |
---|
132 | |
---|
133 | if ( optind < argc ) { |
---|
134 | output = new ofstream( argv[ optind ] ); |
---|
135 | } // if |
---|
136 | |
---|
137 | Parser::get_parser().set_debug( debugp ); |
---|
138 | |
---|
139 | if ( preludep ) { |
---|
140 | // include gcc builtins |
---|
141 | builtins = open_builtins(); |
---|
142 | if ( !builtins ) { |
---|
143 | std::cout << "Error: can't open builtins" << std::endl; |
---|
144 | exit( 1 ); |
---|
145 | } // if |
---|
146 | |
---|
147 | Parser::get_parser().set_linkage( LinkageSpec::Compiler ); |
---|
148 | Parser::get_parser().parse( builtins ); |
---|
149 | |
---|
150 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
151 | return Parser::get_parser().get_parseStatus(); |
---|
152 | } // if |
---|
153 | fclose( builtins ); |
---|
154 | |
---|
155 | // include cfa prelude |
---|
156 | if ( libp ) { |
---|
157 | prelude = input; |
---|
158 | } else { |
---|
159 | prelude = open_prelude(); |
---|
160 | } // if |
---|
161 | if ( !prelude ) { |
---|
162 | std::cout << "Error: can't open prelude" << std::endl; |
---|
163 | exit( 1 ); |
---|
164 | } // if |
---|
165 | |
---|
166 | Parser::get_parser().set_linkage( LinkageSpec::Intrinsic ); |
---|
167 | Parser::get_parser().parse( prelude ); |
---|
168 | |
---|
169 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
170 | return Parser::get_parser().get_parseStatus(); |
---|
171 | } // if |
---|
172 | fclose( prelude ); |
---|
173 | } // if |
---|
174 | |
---|
175 | if ( libp ) { |
---|
176 | std::list< Declaration* > translationUnit; |
---|
177 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
178 | Parser::get_parser().freeTree(); |
---|
179 | SymTab::validate( translationUnit, false ); |
---|
180 | CodeGen::fixNames( translationUnit ); |
---|
181 | LibCfa::makeLibCfa( translationUnit ); |
---|
182 | ResolvExpr::resolve( translationUnit ); |
---|
183 | GenPoly::convertLvalue( translationUnit ); |
---|
184 | GenPoly::box( translationUnit ); |
---|
185 | CodeGen::generate( translationUnit, *output, true ); |
---|
186 | if ( output != &std::cout ) { |
---|
187 | delete output; |
---|
188 | } // if |
---|
189 | return 0; |
---|
190 | } // if |
---|
191 | |
---|
192 | Parser::get_parser().set_linkage( LinkageSpec::Cforall ); |
---|
193 | |
---|
194 | Parser::get_parser().parse( input ); |
---|
195 | if ( debugp || Parser::get_parser().get_parseStatus() != 0 ) { |
---|
196 | return Parser::get_parser().get_parseStatus(); |
---|
197 | } // if |
---|
198 | fclose( input ); |
---|
199 | |
---|
200 | if ( treep ) { |
---|
201 | Parser::get_parser().get_parseTree()->printList( std::cout ); |
---|
202 | Parser::get_parser().freeTree(); |
---|
203 | return 0; |
---|
204 | } // if |
---|
205 | |
---|
206 | std::list< Declaration* > translationUnit; |
---|
207 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
208 | |
---|
209 | Parser::get_parser().freeTree(); |
---|
210 | if ( astp ) { |
---|
211 | printAll( translationUnit, std::cout ); |
---|
212 | return 0; |
---|
213 | } // if |
---|
214 | |
---|
215 | if ( manglep ) { |
---|
216 | SymTab::validate( translationUnit, false ); |
---|
217 | ResolvExpr::AlternativePrinter printer( std::cout ); |
---|
218 | acceptAll( translationUnit, printer ); |
---|
219 | return 0; |
---|
220 | } // if |
---|
221 | |
---|
222 | if ( symtabp ) { |
---|
223 | SymTab::validate( translationUnit, true ); |
---|
224 | return 0; |
---|
225 | } // if |
---|
226 | |
---|
227 | if ( validp ) { |
---|
228 | SymTab::validate( translationUnit, false ); |
---|
229 | printAll( translationUnit, std::cout ); |
---|
230 | return 0; |
---|
231 | } // if |
---|
232 | |
---|
233 | if ( exprp ) { |
---|
234 | SymTab::validate( translationUnit, false ); |
---|
235 | ResolvExpr::resolve( translationUnit ); |
---|
236 | printAll( translationUnit, std::cout ); |
---|
237 | return 0; |
---|
238 | } // if |
---|
239 | |
---|
240 | if ( codegenp ) { |
---|
241 | // print the tree right before code generation... |
---|
242 | // InitTweak::mutate( translationUnit ); |
---|
243 | // InitTweak::tweak( translationUnit ); |
---|
244 | //printAll( translationUnit, std::cout ); |
---|
245 | |
---|
246 | // std::cerr << "finished tweaking" << std::endl; |
---|
247 | SymTab::validate( translationUnit, false ); |
---|
248 | ControlStruct::mutate( translationUnit ); |
---|
249 | CodeGen::fixNames( translationUnit ); |
---|
250 | ResolvExpr::resolve( translationUnit ); |
---|
251 | GenPoly::copyParams( translationUnit ); |
---|
252 | GenPoly::convertSpecializations( translationUnit ); |
---|
253 | GenPoly::convertLvalue( translationUnit ); |
---|
254 | GenPoly::box( translationUnit ); |
---|
255 | printAll( translationUnit, std::cout ); |
---|
256 | return 0; |
---|
257 | } // if |
---|
258 | |
---|
259 | //std::cerr << "before validate" << std::endl; |
---|
260 | SymTab::validate( translationUnit, false ); |
---|
261 | //Try::visit( translationUnit ); |
---|
262 | //Tuples::mutate( translationUnit ); |
---|
263 | //InitTweak::mutate( translationUnit ); |
---|
264 | //std::cerr << "before mutate" << std::endl; |
---|
265 | ControlStruct::mutate( translationUnit ); |
---|
266 | //std::cerr << "before fixNames" << std::endl; |
---|
267 | CodeGen::fixNames( translationUnit ); |
---|
268 | //std::cerr << "before resolve" << std::endl; |
---|
269 | ResolvExpr::resolve( translationUnit ); |
---|
270 | //Tuples::checkFunctions( translationUnit ); |
---|
271 | // std::cerr << "Finished tuple checkfunctions" << std::endl; |
---|
272 | //printAll( translationUnit, std::cerr ); |
---|
273 | //std::cerr << "before copyParams" << std::endl; |
---|
274 | GenPoly::copyParams( translationUnit ); |
---|
275 | //std::cerr << "before convertSpecializations" << std::endl; |
---|
276 | GenPoly::convertSpecializations( translationUnit ); |
---|
277 | //std::cerr << "before convertLvalue" << std::endl; |
---|
278 | GenPoly::convertLvalue( translationUnit ); |
---|
279 | //std::cerr << "before box" << std::endl; |
---|
280 | GenPoly::box( translationUnit ); |
---|
281 | //Tuples::mutate( translationUnit ); |
---|
282 | |
---|
283 | CodeGen::generate( translationUnit, *output, protop ); |
---|
284 | |
---|
285 | if ( output != &std::cout ) { |
---|
286 | delete output; |
---|
287 | } // if |
---|
288 | |
---|
289 | } catch ( SemanticError &e ) { |
---|
290 | e.print( cout ); |
---|
291 | if ( output != &std::cout ) { |
---|
292 | delete output; |
---|
293 | } // if |
---|
294 | return 1; |
---|
295 | } catch ( UnimplementedError &e ) { |
---|
296 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl; |
---|
297 | if ( output != &std::cout ) { |
---|
298 | delete output; |
---|
299 | } // if |
---|
300 | return 1; |
---|
301 | } catch ( CompilerError &e ) { |
---|
302 | std::cerr << "Compiler Error: " << e.get_what() << std::endl; |
---|
303 | std::cerr << "(please report bugs to " << std::endl; |
---|
304 | if ( output != &std::cout ) { |
---|
305 | delete output; |
---|
306 | } // if |
---|
307 | return 1; |
---|
308 | } // try |
---|
309 | |
---|
310 | return 0; |
---|
311 | } // main |
---|
312 | |
---|
313 | FILE *open_prelude() { |
---|
314 | FILE *ret; |
---|
315 | |
---|
316 | const string name("prelude.cf"), |
---|
317 | full_name = string(CFA_LIBDIR) + "/" + name; |
---|
318 | |
---|
319 | if ( beVerbose ) { |
---|
320 | cout << "Reading from " << full_name << endl; |
---|
321 | } // if |
---|
322 | |
---|
323 | if ( ! (ret = fopen(full_name.c_str(), "r" ) ) ) |
---|
324 | return fopen(name.c_str(), "r" ); // trying current directory |
---|
325 | else |
---|
326 | return ret; |
---|
327 | } // open_prelude |
---|
328 | |
---|
329 | FILE *open_builtins() { |
---|
330 | FILE *ret; |
---|
331 | |
---|
332 | const char *name = "builtins.cf"; |
---|
333 | const char *full_name = CFA_LIBDIR "/builtins.cf"; |
---|
334 | |
---|
335 | if ( beVerbose ) { |
---|
336 | cout << "Reading from " << full_name << endl; |
---|
337 | } // if |
---|
338 | |
---|
339 | if ( ! (ret = fopen(full_name, "r" ) ) ) |
---|
340 | return fopen(name, "r" ); // trying current directory |
---|
341 | else |
---|
342 | return ret; |
---|
343 | } // open_builtins |
---|
344 | |
---|
345 | // Local Variables: // |
---|
346 | // compile-command: "make" // |
---|
347 | // End: // |
---|