1 | |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
4 | // |
---|
5 | // The contents of this file are covered under the licence agreement in the |
---|
6 | // file "LICENCE" distributed with Cforall. |
---|
7 | // |
---|
8 | // main.cc -- |
---|
9 | // |
---|
10 | // Author : Richard C. Bilson |
---|
11 | // Created On : Fri May 15 23:12:02 2015 |
---|
12 | // Last Modified By : Andrew Beach |
---|
13 | // Last Modified On : Wed Jul 26 14:38:00 2017 |
---|
14 | // Update Count : 443 |
---|
15 | // |
---|
16 | |
---|
17 | #include <cxxabi.h> // for __cxa_demangle |
---|
18 | #include <execinfo.h> // for backtrace, backtrace_symbols |
---|
19 | #include <getopt.h> // for no_argument, optind, geto... |
---|
20 | #include <signal.h> // for signal, SIGABRT, SIGSEGV |
---|
21 | #include <cassert> // for assertf |
---|
22 | #include <cstdio> // for fopen, FILE, fclose, stdin |
---|
23 | #include <cstdlib> // for exit, free, abort, EXIT_F... |
---|
24 | #include <cstring> // for index |
---|
25 | #include <fstream> // for ofstream |
---|
26 | #include <iostream> // for operator<<, basic_ostream |
---|
27 | #include <iterator> // for back_inserter |
---|
28 | #include <list> // for list |
---|
29 | #include <string> // for char_traits, operator<< |
---|
30 | |
---|
31 | #include "../config.h" // for CFA_LIBDIR |
---|
32 | #include "CodeGen/FixMain.h" // for FixMain |
---|
33 | #include "CodeGen/FixNames.h" // for fixNames |
---|
34 | #include "CodeGen/Generate.h" // for generate |
---|
35 | #include "CodeTools/DeclStats.h" // for printDeclStats |
---|
36 | #include "CodeTools/TrackLoc.h" // for fillLocations |
---|
37 | #include "Common/PassVisitor.h" |
---|
38 | #include "Common/CompilerError.h" // for CompilerError |
---|
39 | #include "Common/SemanticError.h" // for SemanticError |
---|
40 | #include "Common/UnimplementedError.h" // for UnimplementedError |
---|
41 | #include "Common/utility.h" // for deleteAll, filter, printAll |
---|
42 | #include "Concurrency/Waitfor.h" // for generateWaitfor |
---|
43 | #include "ControlStruct/ExceptTranslate.h" // for translateEHM |
---|
44 | #include "ControlStruct/Mutate.h" // for mutate |
---|
45 | #include "GenPoly/Box.h" // for box |
---|
46 | #include "GenPoly/CopyParams.h" // for copyParams |
---|
47 | #include "GenPoly/InstantiateGeneric.h" // for instantiateGeneric |
---|
48 | #include "GenPoly/Lvalue.h" // for convertLvalue |
---|
49 | #include "GenPoly/Specialize.h" // for convertSpecializations |
---|
50 | #include "InitTweak/FixInit.h" // for fix |
---|
51 | #include "InitTweak/GenInit.h" // for genInit |
---|
52 | #include "MakeLibCfa.h" // for makeLibCfa |
---|
53 | #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic |
---|
54 | #include "Parser/ParseNode.h" // for DeclarationNode, buildList |
---|
55 | #include "Parser/TypedefTable.h" // for TypedefTable |
---|
56 | #include "ResolvExpr/AlternativePrinter.h" // for AlternativePrinter |
---|
57 | #include "ResolvExpr/Resolver.h" // for resolve |
---|
58 | #include "SymTab/Validate.h" // for validate |
---|
59 | #include "SynTree/Declaration.h" // for Declaration |
---|
60 | #include "SynTree/Visitor.h" // for acceptAll |
---|
61 | #include "Tuples/Tuples.h" // for expandMemberTuples, expan... |
---|
62 | #include "Virtual/ExpandCasts.h" // for expandCasts |
---|
63 | |
---|
64 | using namespace std; |
---|
65 | |
---|
66 | #define OPTPRINT(x) if ( errorp ) cerr << x << endl; |
---|
67 | |
---|
68 | |
---|
69 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; |
---|
70 | TypedefTable typedefTable; |
---|
71 | DeclarationNode * parseTree = nullptr; // program parse tree |
---|
72 | |
---|
73 | extern int yydebug; // set for -g flag (Grammar) |
---|
74 | bool |
---|
75 | astp = false, |
---|
76 | bresolvep = false, |
---|
77 | bboxp = false, |
---|
78 | bcodegenp = false, |
---|
79 | ctorinitp = false, |
---|
80 | declstatsp = false, |
---|
81 | exprp = false, |
---|
82 | expraltp = false, |
---|
83 | libcfap = false, |
---|
84 | nopreludep = false, |
---|
85 | noprotop = false, |
---|
86 | nomainp = false, |
---|
87 | parsep = false, |
---|
88 | resolvep = false, // used in AlternativeFinder |
---|
89 | symtabp = false, |
---|
90 | treep = false, |
---|
91 | tuplep = false, |
---|
92 | validp = false, |
---|
93 | errorp = false, |
---|
94 | codegenp = false, |
---|
95 | prettycodegenp = false, |
---|
96 | nolinemarks = false; |
---|
97 | |
---|
98 | static void parse_cmdline( int argc, char *argv[], const char *& filename ); |
---|
99 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); |
---|
100 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); |
---|
101 | |
---|
102 | static void backtrace( int start ) { // skip first N stack frames |
---|
103 | enum { Frames = 50 }; |
---|
104 | void * array[Frames]; |
---|
105 | int size = ::backtrace( array, Frames ); |
---|
106 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names |
---|
107 | |
---|
108 | *index( messages[0], '(' ) = '\0'; // find executable name |
---|
109 | cerr << "Stack back trace for: " << messages[0] << endl; |
---|
110 | |
---|
111 | // skip last 2 stack frames after main |
---|
112 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { |
---|
113 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; |
---|
114 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset |
---|
115 | if ( *p == '(' ) { |
---|
116 | mangled_name = p; |
---|
117 | } else if ( *p == '+' ) { |
---|
118 | offset_begin = p; |
---|
119 | } else if ( *p == ')' ) { |
---|
120 | offset_end = p; |
---|
121 | break; |
---|
122 | } // if |
---|
123 | } // for |
---|
124 | |
---|
125 | // if line contains symbol, attempt to demangle |
---|
126 | int frameNo = i - start; |
---|
127 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { |
---|
128 | *mangled_name++ = '\0'; // delimit strings |
---|
129 | *offset_begin++ = '\0'; |
---|
130 | *offset_end++ = '\0'; |
---|
131 | |
---|
132 | int status; |
---|
133 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); |
---|
134 | // bug in __cxa_demangle for single-character lower-case non-mangled names |
---|
135 | if ( status == 0 ) { // demangling successful ? |
---|
136 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
137 | << real_name << "+" << offset_begin << offset_end << endl; |
---|
138 | } else { // otherwise, output mangled name |
---|
139 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
140 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; |
---|
141 | } // if |
---|
142 | |
---|
143 | free( real_name ); |
---|
144 | } else { // otherwise, print the whole line |
---|
145 | cerr << "(" << frameNo << ") " << messages[i] << endl; |
---|
146 | } // if |
---|
147 | } // for |
---|
148 | |
---|
149 | free( messages ); |
---|
150 | } // backtrace |
---|
151 | |
---|
152 | void sigSegvBusHandler( int sig_num ) { |
---|
153 | cerr << "*CFA runtime error* program cfa-cpp terminated with " |
---|
154 | << (sig_num == SIGSEGV ? "segment fault" : "bus error") |
---|
155 | << "." << endl; |
---|
156 | backtrace( 2 ); // skip first 2 stack frames |
---|
157 | exit( EXIT_FAILURE ); |
---|
158 | } // sigSegvBusHandler |
---|
159 | |
---|
160 | void sigAbortHandler( __attribute__((unused)) int sig_num ) { |
---|
161 | backtrace( 6 ); // skip first 6 stack frames |
---|
162 | signal( SIGABRT, SIG_DFL); // reset default signal handler |
---|
163 | raise( SIGABRT ); // reraise SIGABRT |
---|
164 | } // sigAbortHandler |
---|
165 | |
---|
166 | |
---|
167 | int main( int argc, char * argv[] ) { |
---|
168 | FILE * input; // use FILE rather than istream because yyin is FILE |
---|
169 | ostream *output = & cout; |
---|
170 | const char *filename = nullptr; |
---|
171 | list< Declaration * > translationUnit; |
---|
172 | |
---|
173 | signal( SIGSEGV, sigSegvBusHandler ); |
---|
174 | signal( SIGBUS, sigSegvBusHandler ); |
---|
175 | signal( SIGABRT, sigAbortHandler ); |
---|
176 | |
---|
177 | parse_cmdline( argc, argv, filename ); // process command-line arguments |
---|
178 | CodeGen::FixMain::setReplaceMain( !nomainp ); |
---|
179 | |
---|
180 | try { |
---|
181 | // choose to read the program from a file or stdin |
---|
182 | if ( optind < argc ) { // any commands after the flags ? => input file name |
---|
183 | input = fopen( argv[ optind ], "r" ); |
---|
184 | assertf( input, "cannot open %s\n", argv[ optind ] ); |
---|
185 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to) |
---|
186 | if ( filename == nullptr ) filename = argv[ optind ]; |
---|
187 | // prelude filename comes in differently |
---|
188 | if ( libcfap ) filename = "prelude.cf"; |
---|
189 | optind += 1; |
---|
190 | } else { // no input file name |
---|
191 | input = stdin; |
---|
192 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass |
---|
193 | // a fake name along |
---|
194 | if ( filename == nullptr ) filename = "stdin"; |
---|
195 | } // if |
---|
196 | |
---|
197 | // read in the builtins, extras, and the prelude |
---|
198 | if ( ! nopreludep ) { // include gcc builtins |
---|
199 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. |
---|
200 | |
---|
201 | // Read to gcc builtins, if not generating the cfa library |
---|
202 | FILE * gcc_builtins = fopen( libcfap | treep ? "../prelude/gcc-builtins.cf" : CFA_LIBDIR "/gcc-builtins.cf", "r" ); |
---|
203 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); |
---|
204 | parse( gcc_builtins, LinkageSpec::Compiler ); |
---|
205 | |
---|
206 | // read the extra prelude in, if not generating the cfa library |
---|
207 | FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" ); |
---|
208 | assertf( extras, "cannot open extras.cf\n" ); |
---|
209 | parse( extras, LinkageSpec::C ); |
---|
210 | |
---|
211 | if ( ! libcfap ) { |
---|
212 | // read the prelude in, if not generating the cfa library |
---|
213 | FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" ); |
---|
214 | assertf( prelude, "cannot open prelude.cf\n" ); |
---|
215 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
216 | |
---|
217 | // Read to cfa builtins, if not generating the cfa library |
---|
218 | FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" ); |
---|
219 | assertf( builtins, "cannot open builtins.cf\n" ); |
---|
220 | parse( builtins, LinkageSpec::BuiltinCFA ); |
---|
221 | } // if |
---|
222 | } // if |
---|
223 | |
---|
224 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); |
---|
225 | |
---|
226 | if ( parsep ) { |
---|
227 | parseTree->printList( cout ); |
---|
228 | delete parseTree; |
---|
229 | return 0; |
---|
230 | } // if |
---|
231 | |
---|
232 | buildList( parseTree, translationUnit ); |
---|
233 | delete parseTree; |
---|
234 | parseTree = nullptr; |
---|
235 | |
---|
236 | if ( astp ) { |
---|
237 | dump( translationUnit ); |
---|
238 | return 0; |
---|
239 | } // if |
---|
240 | |
---|
241 | // OPTPRINT( "Concurrency" ) |
---|
242 | // Concurrency::applyKeywords( translationUnit ); |
---|
243 | |
---|
244 | // add the assignment statement after the initialization of a type parameter |
---|
245 | OPTPRINT( "validate" ) |
---|
246 | SymTab::validate( translationUnit, symtabp ); |
---|
247 | if ( symtabp ) { |
---|
248 | deleteAll( translationUnit ); |
---|
249 | return 0; |
---|
250 | } // if |
---|
251 | |
---|
252 | if ( expraltp ) { |
---|
253 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); |
---|
254 | acceptAll( translationUnit, printer ); |
---|
255 | return 0; |
---|
256 | } // if |
---|
257 | |
---|
258 | if ( validp ) { |
---|
259 | dump( translationUnit ); |
---|
260 | return 0; |
---|
261 | } // if |
---|
262 | |
---|
263 | OPTPRINT( "mutate" ) |
---|
264 | ControlStruct::mutate( translationUnit ); |
---|
265 | OPTPRINT( "fixNames" ) |
---|
266 | CodeGen::fixNames( translationUnit ); |
---|
267 | OPTPRINT( "genInit" ) |
---|
268 | InitTweak::genInit( translationUnit ); |
---|
269 | OPTPRINT( "expandMemberTuples" ); |
---|
270 | Tuples::expandMemberTuples( translationUnit ); |
---|
271 | if ( libcfap ) { |
---|
272 | // generate the bodies of cfa library functions |
---|
273 | LibCfa::makeLibCfa( translationUnit ); |
---|
274 | } // if |
---|
275 | |
---|
276 | if ( declstatsp ) { |
---|
277 | CodeTools::printDeclStats( translationUnit ); |
---|
278 | deleteAll( translationUnit ); |
---|
279 | return 0; |
---|
280 | } |
---|
281 | |
---|
282 | if ( bresolvep ) { |
---|
283 | dump( translationUnit ); |
---|
284 | return 0; |
---|
285 | } // if |
---|
286 | |
---|
287 | OPTPRINT( "resolve" ) |
---|
288 | ResolvExpr::resolve( translationUnit ); |
---|
289 | if ( exprp ) { |
---|
290 | dump( translationUnit ); |
---|
291 | return 0; |
---|
292 | } // if |
---|
293 | |
---|
294 | // fix ObjectDecl - replaces ConstructorInit nodes |
---|
295 | OPTPRINT( "fixInit" ) |
---|
296 | InitTweak::fix( translationUnit, filename, libcfap || treep ); |
---|
297 | if ( ctorinitp ) { |
---|
298 | dump ( translationUnit ); |
---|
299 | return 0; |
---|
300 | } // if |
---|
301 | |
---|
302 | OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused |
---|
303 | Tuples::expandUniqueExpr( translationUnit ); |
---|
304 | |
---|
305 | OPTPRINT( "translateEHM" ); |
---|
306 | ControlStruct::translateEHM( translationUnit ); |
---|
307 | |
---|
308 | OPTPRINT( "generateWaitfor" ); |
---|
309 | Concurrency::generateWaitFor( translationUnit ); |
---|
310 | |
---|
311 | OPTPRINT( "convertSpecializations" ) // needs to happen before tuple types are expanded |
---|
312 | GenPoly::convertSpecializations( translationUnit ); |
---|
313 | |
---|
314 | OPTPRINT( "expandTuples" ); // xxx - is this the right place for this? |
---|
315 | Tuples::expandTuples( translationUnit ); |
---|
316 | if ( tuplep ) { |
---|
317 | dump( translationUnit ); |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | OPTPRINT( "virtual expandCasts" ) // Must come after translateEHM |
---|
322 | Virtual::expandCasts( translationUnit ); |
---|
323 | |
---|
324 | OPTPRINT("instantiateGenerics") |
---|
325 | GenPoly::instantiateGeneric( translationUnit ); |
---|
326 | OPTPRINT( "copyParams" ); |
---|
327 | GenPoly::copyParams( translationUnit ); |
---|
328 | OPTPRINT( "convertLvalue" ) |
---|
329 | GenPoly::convertLvalue( translationUnit ); |
---|
330 | |
---|
331 | if ( bboxp ) { |
---|
332 | dump( translationUnit ); |
---|
333 | return 0; |
---|
334 | } // if |
---|
335 | OPTPRINT( "box" ) |
---|
336 | GenPoly::box( translationUnit ); |
---|
337 | |
---|
338 | if ( bcodegenp ) { |
---|
339 | dump( translationUnit ); |
---|
340 | return 0; |
---|
341 | } |
---|
342 | |
---|
343 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name |
---|
344 | output = new ofstream( argv[ optind ] ); |
---|
345 | } // if |
---|
346 | |
---|
347 | CodeTools::fillLocations( translationUnit ); |
---|
348 | CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, ! nolinemarks ); |
---|
349 | |
---|
350 | CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" ); |
---|
351 | |
---|
352 | if ( output != &cout ) { |
---|
353 | delete output; |
---|
354 | } // if |
---|
355 | } catch ( SemanticError &e ) { |
---|
356 | if ( errorp ) { |
---|
357 | cerr << "---AST at error:---" << endl; |
---|
358 | dump( translationUnit, cerr ); |
---|
359 | cerr << endl << "---End of AST, begin error message:---\n" << endl; |
---|
360 | } // if |
---|
361 | e.print( cerr ); |
---|
362 | if ( output != &cout ) { |
---|
363 | delete output; |
---|
364 | } // if |
---|
365 | return 1; |
---|
366 | } catch ( UnimplementedError &e ) { |
---|
367 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; |
---|
368 | if ( output != &cout ) { |
---|
369 | delete output; |
---|
370 | } // if |
---|
371 | return 1; |
---|
372 | } catch ( CompilerError &e ) { |
---|
373 | cerr << "Compiler Error: " << e.get_what() << endl; |
---|
374 | cerr << "(please report bugs to [REDACTED])" << endl; |
---|
375 | if ( output != &cout ) { |
---|
376 | delete output; |
---|
377 | } // if |
---|
378 | return 1; |
---|
379 | } // try |
---|
380 | |
---|
381 | deleteAll( translationUnit ); |
---|
382 | return 0; |
---|
383 | } // main |
---|
384 | |
---|
385 | void parse_cmdline( int argc, char * argv[], const char *& filename ) { |
---|
386 | enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, }; |
---|
387 | |
---|
388 | static struct option long_opts[] = { |
---|
389 | { "ast", no_argument, 0, Ast }, |
---|
390 | { "before-box", no_argument, 0, Bbox }, |
---|
391 | { "before-resolver", no_argument, 0, Bresolver }, |
---|
392 | { "ctorinitfix", no_argument, 0, CtorInitFix }, |
---|
393 | { "decl-stats", no_argument, 0, DeclStats }, |
---|
394 | { "expr", no_argument, 0, Expr }, |
---|
395 | { "expralt", no_argument, 0, ExprAlt }, |
---|
396 | { "grammar", no_argument, 0, Grammar }, |
---|
397 | { "libcfa", no_argument, 0, LibCFA }, |
---|
398 | { "no-preamble", no_argument, 0, Nopreamble }, |
---|
399 | { "parse", no_argument, 0, Parse }, |
---|
400 | { "no-prototypes", no_argument, 0, Prototypes }, |
---|
401 | { "resolver", no_argument, 0, Resolver }, |
---|
402 | { "symbol", no_argument, 0, Symbol }, |
---|
403 | { "tree", no_argument, 0, Tree }, |
---|
404 | { "tuple-expansion", no_argument, 0, TupleExpansion }, |
---|
405 | { "validate", no_argument, 0, Validate }, |
---|
406 | { 0, 0, 0, 0 } |
---|
407 | }; // long_opts |
---|
408 | int long_index; |
---|
409 | |
---|
410 | opterr = 0; // (global) prevent getopt from printing error messages |
---|
411 | |
---|
412 | int c; |
---|
413 | while ( (c = getopt_long( argc, argv, "abBcCdefglLmnpqrstTvyzZD:F:", long_opts, &long_index )) != -1 ) { |
---|
414 | switch ( c ) { |
---|
415 | case Ast: |
---|
416 | case 'a': // dump AST |
---|
417 | astp = true; |
---|
418 | break; |
---|
419 | case Bresolver: |
---|
420 | case 'b': // print before resolver steps |
---|
421 | bresolvep = true; |
---|
422 | break; |
---|
423 | case 'B': // print before box steps |
---|
424 | bboxp = true; |
---|
425 | break; |
---|
426 | case CtorInitFix: |
---|
427 | case 'c': // print after constructors and destructors are replaced |
---|
428 | ctorinitp = true; |
---|
429 | break; |
---|
430 | case 'C': // print before code generation |
---|
431 | bcodegenp = true; |
---|
432 | break; |
---|
433 | case DeclStats: |
---|
434 | case 'd': |
---|
435 | declstatsp = true; |
---|
436 | break; |
---|
437 | case Expr: |
---|
438 | case 'e': // dump AST after expression analysis |
---|
439 | exprp = true; |
---|
440 | break; |
---|
441 | case ExprAlt: |
---|
442 | case 'f': // print alternatives for expressions |
---|
443 | expraltp = true; |
---|
444 | break; |
---|
445 | case Grammar: |
---|
446 | case 'g': // bison debugging info (grammar rules) |
---|
447 | yydebug = true; |
---|
448 | break; |
---|
449 | case LibCFA: |
---|
450 | case 'l': // generate libcfa.c |
---|
451 | libcfap = true; |
---|
452 | break; |
---|
453 | case 'L': // surpress lines marks |
---|
454 | nolinemarks = true; |
---|
455 | break; |
---|
456 | case Nopreamble: |
---|
457 | case 'n': // do not read preamble |
---|
458 | nopreludep = true; |
---|
459 | break; |
---|
460 | case Prototypes: |
---|
461 | case 'p': // generate prototypes for preamble functions |
---|
462 | noprotop = true; |
---|
463 | break; |
---|
464 | case 'm': // don't replace the main |
---|
465 | nomainp = true; |
---|
466 | break; |
---|
467 | case Parse: |
---|
468 | case 'q': // dump parse tree |
---|
469 | parsep = true; |
---|
470 | break; |
---|
471 | case Resolver: |
---|
472 | case 'r': // print resolver steps |
---|
473 | resolvep = true; |
---|
474 | break; |
---|
475 | case Symbol: |
---|
476 | case 's': // print symbol table events |
---|
477 | symtabp = true; |
---|
478 | break; |
---|
479 | case Tree: |
---|
480 | case 't': // build in tree |
---|
481 | treep = true; |
---|
482 | break; |
---|
483 | case TupleExpansion: |
---|
484 | case 'T': // print after tuple expansion |
---|
485 | tuplep = true; |
---|
486 | break; |
---|
487 | case 'v': // dump AST after decl validation pass |
---|
488 | validp = true; |
---|
489 | break; |
---|
490 | case 'y': // dump AST on error |
---|
491 | errorp = true; |
---|
492 | break; |
---|
493 | case 'z': // dump as codegen rather than AST |
---|
494 | codegenp = true; |
---|
495 | break; |
---|
496 | case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.) |
---|
497 | prettycodegenp = true; |
---|
498 | break; |
---|
499 | case 'D': // ignore -Dxxx |
---|
500 | break; |
---|
501 | case 'F': // source file-name without suffix |
---|
502 | filename = optarg; |
---|
503 | break; |
---|
504 | case '?': |
---|
505 | if ( optopt ) { // short option ? |
---|
506 | assertf( false, "Unknown option: -%c\n", (char)optopt ); |
---|
507 | } else { |
---|
508 | assertf( false, "Unknown option: %s\n", argv[optind - 1] ); |
---|
509 | } // if |
---|
510 | default: |
---|
511 | abort(); |
---|
512 | } // switch |
---|
513 | } // while |
---|
514 | } // parse_cmdline |
---|
515 | |
---|
516 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { |
---|
517 | extern int yyparse( void ); |
---|
518 | extern FILE * yyin; |
---|
519 | extern int yylineno; |
---|
520 | |
---|
521 | ::linkage = linkage; // set globals |
---|
522 | yyin = input; |
---|
523 | yylineno = 1; |
---|
524 | typedefTable.enterScope(); |
---|
525 | int parseStatus = yyparse(); |
---|
526 | |
---|
527 | fclose( input ); |
---|
528 | if ( shouldExit || parseStatus != 0 ) { |
---|
529 | exit( parseStatus ); |
---|
530 | } // if |
---|
531 | } // parse |
---|
532 | |
---|
533 | static bool notPrelude( Declaration * decl ) { |
---|
534 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
535 | } // notPrelude |
---|
536 | |
---|
537 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { |
---|
538 | list< Declaration * > decls; |
---|
539 | |
---|
540 | if ( noprotop ) { |
---|
541 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); |
---|
542 | } else { |
---|
543 | decls = translationUnit; |
---|
544 | } // if |
---|
545 | |
---|
546 | // depending on commandline options, either generate code or dump the AST |
---|
547 | if ( codegenp ) { |
---|
548 | CodeGen::generate( decls, out, ! noprotop, prettycodegenp ); |
---|
549 | } else { |
---|
550 | printAll( decls, out ); |
---|
551 | } |
---|
552 | deleteAll( translationUnit ); |
---|
553 | } // dump |
---|
554 | |
---|
555 | // Local Variables: // |
---|
556 | // tab-width: 4 // |
---|
557 | // mode: c++ // |
---|
558 | // compile-command: "make install" // |
---|
559 | // End: // |
---|