1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // main.cc -- |
---|
8 | // |
---|
9 | // Author : Peter Buhr and Rob Schluntz |
---|
10 | // Created On : Fri May 15 23:12:02 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Aug 23 06:50:08 2019 |
---|
13 | // Update Count : 607 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cxxabi.h> // for __cxa_demangle |
---|
17 | #include <execinfo.h> // for backtrace, backtrace_symbols |
---|
18 | #include <getopt.h> // for no_argument, optind, geto... |
---|
19 | #include <cassert> // for assertf |
---|
20 | #include <cstdio> // for fopen, FILE, fclose, stdin |
---|
21 | #include <cstdlib> // for exit, free, abort, EXIT_F... |
---|
22 | #include <csignal> // for signal, SIGABRT, SIGSEGV |
---|
23 | #include <cstring> // for index |
---|
24 | #include <fstream> // for ofstream |
---|
25 | #include <iostream> // for operator<<, basic_ostream |
---|
26 | #include <iomanip> |
---|
27 | #include <iterator> // for back_inserter |
---|
28 | #include <list> // for list |
---|
29 | #include <string> // for char_traits, operator<< |
---|
30 | |
---|
31 | #include "CompilationState.h" |
---|
32 | #include "../config.h" // for CFA_LIBDIR |
---|
33 | #include "CodeGen/FixMain.h" // for FixMain |
---|
34 | #include "CodeGen/FixNames.h" // for fixNames |
---|
35 | #include "CodeGen/Generate.h" // for generate |
---|
36 | #include "CodeTools/DeclStats.h" // for printDeclStats |
---|
37 | #include "CodeTools/ResolvProtoDump.h" // for dumpAsResolvProto |
---|
38 | #include "CodeTools/TrackLoc.h" // for fillLocations |
---|
39 | #include "Common/CompilerError.h" // for CompilerError |
---|
40 | #include "Common/Stats.h" |
---|
41 | #include "Common/PassVisitor.h" |
---|
42 | #include "Common/SemanticError.h" // for SemanticError |
---|
43 | #include "Common/UnimplementedError.h" // for UnimplementedError |
---|
44 | #include "Common/utility.h" // for deleteAll, filter, printAll |
---|
45 | #include "Concurrency/Waitfor.h" // for generateWaitfor |
---|
46 | #include "ControlStruct/ExceptTranslate.h" // for translateEHM |
---|
47 | #include "ControlStruct/Mutate.h" // for mutate |
---|
48 | #include "GenPoly/Box.h" // for box |
---|
49 | #include "GenPoly/InstantiateGeneric.h" // for instantiateGeneric |
---|
50 | #include "GenPoly/Lvalue.h" // for convertLvalue |
---|
51 | #include "GenPoly/Specialize.h" // for convertSpecializations |
---|
52 | #include "InitTweak/FixInit.h" // for fix |
---|
53 | #include "InitTweak/GenInit.h" // for genInit |
---|
54 | #include "MakeLibCfa.h" // for makeLibCfa |
---|
55 | #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic |
---|
56 | #include "Parser/ParseNode.h" // for DeclarationNode, buildList |
---|
57 | #include "Parser/TypedefTable.h" // for TypedefTable |
---|
58 | #include "ResolvExpr/AlternativePrinter.h" // for AlternativePrinter |
---|
59 | #include "ResolvExpr/Resolver.h" // for resolve |
---|
60 | #include "SymTab/Validate.h" // for validate |
---|
61 | #include "SynTree/Declaration.h" // for Declaration |
---|
62 | #include "SynTree/Visitor.h" // for acceptAll |
---|
63 | #include "Tuples/Tuples.h" // for expandMemberTuples, expan... |
---|
64 | #include "Virtual/ExpandCasts.h" // for expandCasts |
---|
65 | |
---|
66 | |
---|
67 | using namespace std; |
---|
68 | |
---|
69 | static void NewPass( const char * const name ) { |
---|
70 | Stats::Heap::newPass( name ); |
---|
71 | using namespace Stats::Counters; |
---|
72 | { |
---|
73 | static auto group = build<CounterGroup>( "Pass Visitor" ); |
---|
74 | auto pass = build<CounterGroup>( name, group ); |
---|
75 | pass_visitor_stats.depth = 0; |
---|
76 | pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass ); |
---|
77 | pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass ); |
---|
78 | } |
---|
79 | { |
---|
80 | static auto group = build<CounterGroup>( "Syntax Node" ); |
---|
81 | auto pass = build<CounterGroup>( name, group ); |
---|
82 | BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass ); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | #define PASS( name, pass ) \ |
---|
87 | if ( errorp ) { cerr << name << endl; } \ |
---|
88 | NewPass(name); \ |
---|
89 | Stats::Time::StartBlock(name); \ |
---|
90 | pass; \ |
---|
91 | Stats::Time::StopBlock(); |
---|
92 | |
---|
93 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; |
---|
94 | TypedefTable typedefTable; |
---|
95 | DeclarationNode * parseTree = nullptr; // program parse tree |
---|
96 | |
---|
97 | static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start |
---|
98 | |
---|
99 | static std::string PreludeDirector = ""; |
---|
100 | |
---|
101 | static void parse_cmdline( int argc, char *argv[] ); |
---|
102 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); |
---|
103 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); |
---|
104 | |
---|
105 | static void backtrace( int start ) { // skip first N stack frames |
---|
106 | enum { Frames = 50 }; |
---|
107 | void * array[Frames]; |
---|
108 | int size = ::backtrace( array, Frames ); |
---|
109 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names |
---|
110 | |
---|
111 | *index( messages[0], '(' ) = '\0'; // find executable name |
---|
112 | cerr << "Stack back trace for: " << messages[0] << endl; |
---|
113 | |
---|
114 | // skip last 2 stack frames after main |
---|
115 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { |
---|
116 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; |
---|
117 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset |
---|
118 | if ( *p == '(' ) { |
---|
119 | mangled_name = p; |
---|
120 | } else if ( *p == '+' ) { |
---|
121 | offset_begin = p; |
---|
122 | } else if ( *p == ')' ) { |
---|
123 | offset_end = p; |
---|
124 | break; |
---|
125 | } // if |
---|
126 | } // for |
---|
127 | |
---|
128 | // if line contains symbol, attempt to demangle |
---|
129 | int frameNo = i - start; |
---|
130 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { |
---|
131 | *mangled_name++ = '\0'; // delimit strings |
---|
132 | *offset_begin++ = '\0'; |
---|
133 | *offset_end++ = '\0'; |
---|
134 | |
---|
135 | int status; |
---|
136 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); |
---|
137 | // bug in __cxa_demangle for single-character lower-case non-mangled names |
---|
138 | if ( status == 0 ) { // demangling successful ? |
---|
139 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
140 | << real_name << "+" << offset_begin << offset_end << endl; |
---|
141 | } else { // otherwise, output mangled name |
---|
142 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
143 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; |
---|
144 | } // if |
---|
145 | |
---|
146 | free( real_name ); |
---|
147 | } else { // otherwise, print the whole line |
---|
148 | cerr << "(" << frameNo << ") " << messages[i] << endl; |
---|
149 | } // if |
---|
150 | } // for |
---|
151 | |
---|
152 | free( messages ); |
---|
153 | } // backtrace |
---|
154 | |
---|
155 | static void sigSegvBusHandler( int sig_num ) { |
---|
156 | cerr << "*CFA runtime error* program cfa-cpp terminated with " |
---|
157 | << (sig_num == SIGSEGV ? "segment fault" : "bus error") |
---|
158 | << "." << endl; |
---|
159 | backtrace( 2 ); // skip first 2 stack frames |
---|
160 | //_exit( EXIT_FAILURE ); |
---|
161 | abort(); // cause core dump for debugging |
---|
162 | } // sigSegvBusHandler |
---|
163 | |
---|
164 | static void sigAbortHandler( __attribute__((unused)) int sig_num ) { |
---|
165 | backtrace( 6 ); // skip first 6 stack frames |
---|
166 | signal( SIGABRT, SIG_DFL); // reset default signal handler |
---|
167 | raise( SIGABRT ); // reraise SIGABRT |
---|
168 | } // sigAbortHandler |
---|
169 | |
---|
170 | int main( int argc, char * argv[] ) { |
---|
171 | FILE * input; // use FILE rather than istream because yyin is FILE |
---|
172 | ostream * output = & cout; |
---|
173 | list< Declaration * > translationUnit; |
---|
174 | |
---|
175 | signal( SIGSEGV, sigSegvBusHandler ); |
---|
176 | signal( SIGBUS, sigSegvBusHandler ); |
---|
177 | signal( SIGABRT, sigAbortHandler ); |
---|
178 | |
---|
179 | // std::cout << "main" << std::endl; |
---|
180 | // for ( int i = 0; i < argc; i += 1 ) { |
---|
181 | // std::cout << '\t' << argv[i] << std::endl; |
---|
182 | // } // for |
---|
183 | |
---|
184 | parse_cmdline( argc, argv ); // process command-line arguments |
---|
185 | CodeGen::FixMain::setReplaceMain( !nomainp ); |
---|
186 | |
---|
187 | if ( waiting_for_gdb ) { |
---|
188 | std::cerr << "Waiting for gdb" << std::endl; |
---|
189 | std::cerr << "run :" << std::endl; |
---|
190 | std::cerr << " gdb attach " << getpid() << std::endl; |
---|
191 | raise(SIGSTOP); |
---|
192 | } // if |
---|
193 | |
---|
194 | try { |
---|
195 | // choose to read the program from a file or stdin |
---|
196 | if ( optind < argc ) { // any commands after the flags ? => input file name |
---|
197 | input = fopen( argv[ optind ], "r" ); |
---|
198 | assertf( input, "cannot open %s because %s\n", argv[ optind ], strerror( errno ) ); |
---|
199 | optind += 1; |
---|
200 | } else { // no input file name |
---|
201 | input = stdin; |
---|
202 | } // if |
---|
203 | |
---|
204 | Stats::Time::StartGlobal(); |
---|
205 | NewPass("Parse"); |
---|
206 | Stats::Time::StartBlock("Parse"); |
---|
207 | |
---|
208 | // read in the builtins, extras, and the prelude |
---|
209 | if ( ! nopreludep ) { // include gcc builtins |
---|
210 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. |
---|
211 | |
---|
212 | assertf( !PreludeDirector.empty(), "Can't find prelude without option --prelude-dir must be used." ); |
---|
213 | |
---|
214 | // Read to gcc builtins, if not generating the cfa library |
---|
215 | FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" ); |
---|
216 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); |
---|
217 | parse( gcc_builtins, LinkageSpec::Compiler ); |
---|
218 | |
---|
219 | // read the extra prelude in, if not generating the cfa library |
---|
220 | FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" ); |
---|
221 | assertf( extras, "cannot open extras.cf\n" ); |
---|
222 | parse( extras, LinkageSpec::BuiltinC ); |
---|
223 | |
---|
224 | if ( ! libcfap ) { |
---|
225 | // read the prelude in, if not generating the cfa library |
---|
226 | FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" ); |
---|
227 | assertf( prelude, "cannot open prelude.cfa\n" ); |
---|
228 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
229 | |
---|
230 | // Read to cfa builtins, if not generating the cfa library |
---|
231 | FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" ); |
---|
232 | assertf( builtins, "cannot open builtins.cf\n" ); |
---|
233 | parse( builtins, LinkageSpec::BuiltinCFA ); |
---|
234 | } // if |
---|
235 | } // if |
---|
236 | |
---|
237 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); |
---|
238 | |
---|
239 | if ( parsep ) { |
---|
240 | parseTree->printList( cout ); |
---|
241 | delete parseTree; |
---|
242 | return EXIT_SUCCESS; |
---|
243 | } // if |
---|
244 | |
---|
245 | buildList( parseTree, translationUnit ); |
---|
246 | delete parseTree; |
---|
247 | parseTree = nullptr; |
---|
248 | |
---|
249 | if ( astp ) { |
---|
250 | dump( translationUnit ); |
---|
251 | return EXIT_SUCCESS; |
---|
252 | } // if |
---|
253 | |
---|
254 | // Temporary: fill locations after parsing so that every node has a location, for early error messages. |
---|
255 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution |
---|
256 | // works okay for now. |
---|
257 | CodeTools::fillLocations( translationUnit ); |
---|
258 | Stats::Time::StopBlock(); |
---|
259 | |
---|
260 | // add the assignment statement after the initialization of a type parameter |
---|
261 | PASS( "Validate", SymTab::validate( translationUnit, symtabp ) ); |
---|
262 | if ( symtabp ) { |
---|
263 | deleteAll( translationUnit ); |
---|
264 | return EXIT_SUCCESS; |
---|
265 | } // if |
---|
266 | |
---|
267 | if ( expraltp ) { |
---|
268 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); |
---|
269 | acceptAll( translationUnit, printer ); |
---|
270 | return EXIT_SUCCESS; |
---|
271 | } // if |
---|
272 | |
---|
273 | if ( validp ) { |
---|
274 | dump( translationUnit ); |
---|
275 | return EXIT_SUCCESS; |
---|
276 | } // if |
---|
277 | |
---|
278 | PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); |
---|
279 | PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); |
---|
280 | PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); |
---|
281 | PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) ); |
---|
282 | if ( libcfap ) { |
---|
283 | // generate the bodies of cfa library functions |
---|
284 | LibCfa::makeLibCfa( translationUnit ); |
---|
285 | } // if |
---|
286 | |
---|
287 | if ( declstatsp ) { |
---|
288 | CodeTools::printDeclStats( translationUnit ); |
---|
289 | deleteAll( translationUnit ); |
---|
290 | return EXIT_SUCCESS; |
---|
291 | } // if |
---|
292 | |
---|
293 | if ( bresolvep ) { |
---|
294 | dump( translationUnit ); |
---|
295 | return EXIT_SUCCESS; |
---|
296 | } // if |
---|
297 | |
---|
298 | CodeTools::fillLocations( translationUnit ); |
---|
299 | |
---|
300 | if ( resolvprotop ) { |
---|
301 | CodeTools::dumpAsResolvProto( translationUnit ); |
---|
302 | return EXIT_SUCCESS; |
---|
303 | } // if |
---|
304 | |
---|
305 | PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); |
---|
306 | if ( exprp ) { |
---|
307 | dump( translationUnit ); |
---|
308 | return EXIT_SUCCESS; |
---|
309 | } // if |
---|
310 | |
---|
311 | // fix ObjectDecl - replaces ConstructorInit nodes |
---|
312 | PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); |
---|
313 | if ( ctorinitp ) { |
---|
314 | dump ( translationUnit ); |
---|
315 | return EXIT_SUCCESS; |
---|
316 | } // if |
---|
317 | |
---|
318 | PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent 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 |
---|
319 | |
---|
320 | PASS( "Translate EHM" , ControlStruct::translateEHM( translationUnit ) ); |
---|
321 | |
---|
322 | PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) ); |
---|
323 | |
---|
324 | PASS( "Convert Specializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded |
---|
325 | |
---|
326 | PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? |
---|
327 | |
---|
328 | if ( tuplep ) { |
---|
329 | dump( translationUnit ); |
---|
330 | return EXIT_SUCCESS; |
---|
331 | } // if |
---|
332 | |
---|
333 | PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM |
---|
334 | |
---|
335 | PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) ); |
---|
336 | if ( genericsp ) { |
---|
337 | dump( translationUnit ); |
---|
338 | return EXIT_SUCCESS; |
---|
339 | } // if |
---|
340 | |
---|
341 | PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) ); |
---|
342 | |
---|
343 | if ( bboxp ) { |
---|
344 | dump( translationUnit ); |
---|
345 | return EXIT_SUCCESS; |
---|
346 | } // if |
---|
347 | PASS( "Box", GenPoly::box( translationUnit ) ); |
---|
348 | |
---|
349 | if ( bcodegenp ) { |
---|
350 | dump( translationUnit ); |
---|
351 | return EXIT_SUCCESS; |
---|
352 | } // if |
---|
353 | |
---|
354 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name |
---|
355 | output = new ofstream( argv[ optind ] ); |
---|
356 | } // if |
---|
357 | |
---|
358 | CodeTools::fillLocations( translationUnit ); |
---|
359 | PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) ); |
---|
360 | |
---|
361 | CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() ); |
---|
362 | if ( output != &cout ) { |
---|
363 | delete output; |
---|
364 | } // if |
---|
365 | } catch ( SemanticErrorException &e ) { |
---|
366 | if ( errorp ) { |
---|
367 | cerr << "---AST at error:---" << endl; |
---|
368 | dump( translationUnit, cerr ); |
---|
369 | cerr << endl << "---End of AST, begin error message:---\n" << endl; |
---|
370 | } // if |
---|
371 | e.print(); |
---|
372 | if ( output != &cout ) { |
---|
373 | delete output; |
---|
374 | } // if |
---|
375 | return EXIT_FAILURE; |
---|
376 | } catch ( UnimplementedError &e ) { |
---|
377 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; |
---|
378 | if ( output != &cout ) { |
---|
379 | delete output; |
---|
380 | } // if |
---|
381 | return EXIT_FAILURE; |
---|
382 | } catch ( CompilerError &e ) { |
---|
383 | cerr << "Compiler Error: " << e.get_what() << endl; |
---|
384 | cerr << "(please report bugs to [REDACTED])" << endl; |
---|
385 | if ( output != &cout ) { |
---|
386 | delete output; |
---|
387 | } // if |
---|
388 | return EXIT_FAILURE; |
---|
389 | } catch ( ... ) { |
---|
390 | std::exception_ptr eptr = std::current_exception(); |
---|
391 | try { |
---|
392 | if (eptr) { |
---|
393 | std::rethrow_exception(eptr); |
---|
394 | } else { |
---|
395 | std::cerr << "Exception Uncaught and Unknown" << std::endl; |
---|
396 | } // if |
---|
397 | } catch(const std::exception& e) { |
---|
398 | std::cerr << "Uncaught Exception \"" << e.what() << "\"\n"; |
---|
399 | } // try |
---|
400 | return EXIT_FAILURE; |
---|
401 | } // try |
---|
402 | |
---|
403 | deleteAll( translationUnit ); |
---|
404 | Stats::print(); |
---|
405 | return EXIT_SUCCESS; |
---|
406 | } // main |
---|
407 | |
---|
408 | |
---|
409 | static const char optstring[] = ":hlLmNnpP:S:twW:D:"; |
---|
410 | |
---|
411 | enum { PreludeDir = 128 }; |
---|
412 | static struct option long_opts[] = { |
---|
413 | { "help", no_argument, nullptr, 'h' }, |
---|
414 | { "libcfa", no_argument, nullptr, 'l' }, |
---|
415 | { "linemarks", no_argument, nullptr, 'L' }, |
---|
416 | { "no-main", no_argument, 0, 'm' }, |
---|
417 | { "no-linemarks", no_argument, nullptr, 'N' }, |
---|
418 | { "no-prelude", no_argument, nullptr, 'n' }, |
---|
419 | { "prototypes", no_argument, nullptr, 'p' }, |
---|
420 | { "print", required_argument, nullptr, 'P' }, |
---|
421 | { "prelude-dir", required_argument, nullptr, PreludeDir }, |
---|
422 | { "statistics", required_argument, nullptr, 'S' }, |
---|
423 | { "tree", no_argument, nullptr, 't' }, |
---|
424 | { "gdb", no_argument, nullptr, 'g' }, |
---|
425 | { "", no_argument, nullptr, 0 }, // -w |
---|
426 | { "", no_argument, nullptr, 0 }, // -W |
---|
427 | { "", no_argument, nullptr, 0 }, // -D |
---|
428 | { nullptr, 0, nullptr, 0 } |
---|
429 | }; // long_opts |
---|
430 | |
---|
431 | static const char * description[] = { |
---|
432 | "print help message", // -h |
---|
433 | "generate libcfa.c", // -l |
---|
434 | "generate line marks", // -L |
---|
435 | "do not replace main", // -m |
---|
436 | "do not generate line marks", // -N |
---|
437 | "do not read prelude", // -n |
---|
438 | "generate prototypes for prelude functions", // -p |
---|
439 | "print", // -P |
---|
440 | "<directory> prelude directory for debug/nodebug", // no flag |
---|
441 | "<option-list> enable profiling information:\n counters,heap,time,all,none", // -S |
---|
442 | "building cfa standard lib", // -t |
---|
443 | "wait for gdb to attach", // -g |
---|
444 | "", // -w |
---|
445 | "", // -W |
---|
446 | "", // -D |
---|
447 | }; // description |
---|
448 | |
---|
449 | static_assert( sizeof( long_opts ) / sizeof( long_opts[0] ) - 1 == sizeof( description ) / sizeof( description[0] ), "Long opts and description must match" ); |
---|
450 | |
---|
451 | static struct Printopts { |
---|
452 | const char * name; |
---|
453 | int & flag; |
---|
454 | int val; |
---|
455 | const char * descript; |
---|
456 | } printopts[] = { |
---|
457 | { "ascodegen", codegenp, true, "print AST as codegen rather than AST" }, |
---|
458 | { "asterr", errorp, true, "print AST on error" }, |
---|
459 | { "declstats", declstatsp, true, "code property statistics" }, |
---|
460 | { "parse", yydebug, true, "yacc (parsing) debug information" }, |
---|
461 | { "pretty", prettycodegenp, true, "prettyprint for ascodegen flag" }, |
---|
462 | { "rproto", resolvprotop, true, "resolver-proto instance" }, |
---|
463 | { "rsteps", resolvep, true, "print resolver steps" }, |
---|
464 | { "tree", parsep, true, "print parse tree" }, |
---|
465 | // code dumps |
---|
466 | { "ast", astp, true, "print AST after parsing" }, |
---|
467 | { "symevt", symtabp, true, "print AST after symbol table events" }, |
---|
468 | { "altexpr", expraltp, true, "print alternatives for expressions" }, |
---|
469 | { "astdecl", validp, true, "print AST after declaration validation pass" }, |
---|
470 | { "resolver", bresolvep, true, "print AST before resolver step" }, |
---|
471 | { "astexpr", exprp, true, "print AST after expression analysis" }, |
---|
472 | { "ctordtor", ctorinitp, true, "print AST after ctor/dtor are replaced" }, |
---|
473 | { "tuple", tuplep, true, "print AST after tuple expansion" }, |
---|
474 | { "astgen", genericsp, true, "print AST after instantiate generics" }, |
---|
475 | { "box", bboxp, true, "print AST before box step" }, |
---|
476 | { "codegen", bcodegenp, true, "print AST before code generation" }, |
---|
477 | }; |
---|
478 | enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) }; |
---|
479 | |
---|
480 | static void usage( char *argv[] ) { |
---|
481 | cout << "Usage: " << argv[0] << " [options] [input-file (default stdin)] [output-file (default stdout)], where options are:" << endl; |
---|
482 | int i = 0, j = 1; // j skips starting colon |
---|
483 | for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) { |
---|
484 | if ( long_opts[i].name[0] != '\0' ) { // hidden option, internal usage only |
---|
485 | if ( strcmp( long_opts[i].name, "prelude-dir" ) != 0 ) { // flag |
---|
486 | cout << " -" << optstring[j] << ","; |
---|
487 | } else { // no flag |
---|
488 | j -= 1; // compensate |
---|
489 | cout << " "; |
---|
490 | } // if |
---|
491 | cout << " --" << left << setw(12) << long_opts[i].name << " "; |
---|
492 | if ( strcmp( long_opts[i].name, "print" ) == 0 ) { |
---|
493 | cout << "one of: " << endl; |
---|
494 | for ( int i = 0; i < printoptsSize; i += 1 ) { |
---|
495 | cout << setw(10) << " " << left << setw(10) << printopts[i].name << " " << printopts[i].descript << endl; |
---|
496 | } // for |
---|
497 | } else { |
---|
498 | cout << description[i] << endl; |
---|
499 | } // if |
---|
500 | } // if |
---|
501 | if ( optstring[j + 1] == ':' ) j += 1; |
---|
502 | } // for |
---|
503 | if ( long_opts[i].name != 0 || optstring[j] != '\0' ) assertf( false, "internal error, mismatch of option flags and names\n" ); |
---|
504 | exit( EXIT_FAILURE ); |
---|
505 | } // usage |
---|
506 | |
---|
507 | static void parse_cmdline( int argc, char * argv[] ) { |
---|
508 | opterr = 0; // (global) prevent getopt from printing error messages |
---|
509 | |
---|
510 | bool Wsuppress = false, Werror = false; |
---|
511 | int c; |
---|
512 | while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { |
---|
513 | switch ( c ) { |
---|
514 | case 'h': // help message |
---|
515 | usage( argv ); // no return |
---|
516 | break; |
---|
517 | case 'l': // generate libcfa.c |
---|
518 | libcfap = true; |
---|
519 | break; |
---|
520 | case 'L': // generate line marks |
---|
521 | linemarks = true; |
---|
522 | break; |
---|
523 | case 'm': // do not replace main |
---|
524 | nomainp = true; |
---|
525 | break; |
---|
526 | case 'N': // do not generate line marks |
---|
527 | linemarks = false; |
---|
528 | break; |
---|
529 | case 'n': // do not read prelude |
---|
530 | nopreludep = true; |
---|
531 | break; |
---|
532 | case 'p': // generate prototypes for prelude functions |
---|
533 | genproto = true; |
---|
534 | break; |
---|
535 | case 'P': // print options |
---|
536 | for ( int i = 0;; i += 1 ) { |
---|
537 | if ( i == printoptsSize ) { |
---|
538 | cout << "Unknown --print option " << optarg << endl; |
---|
539 | goto Default; |
---|
540 | } // if |
---|
541 | if ( strcmp( optarg, printopts[i].name ) == 0 ) { |
---|
542 | printopts[i].flag = printopts[i].val; |
---|
543 | break; |
---|
544 | } // if |
---|
545 | } // for |
---|
546 | break; |
---|
547 | case PreludeDir: // prelude directory for debug/nodebug, hidden |
---|
548 | PreludeDirector = optarg; |
---|
549 | break; |
---|
550 | case 'S': // enable profiling information, argument comma separated list of names |
---|
551 | Stats::parse_params( optarg ); |
---|
552 | break; |
---|
553 | case 't': // building cfa stdlib |
---|
554 | treep = true; |
---|
555 | break; |
---|
556 | case 'g': // wait for gdb |
---|
557 | waiting_for_gdb = true; |
---|
558 | break; |
---|
559 | case 'w': // suppress all warnings, hidden |
---|
560 | Wsuppress = true; |
---|
561 | break; |
---|
562 | case 'W': // coordinate gcc -W with CFA, hidden |
---|
563 | if ( strcmp( optarg, "all" ) == 0 ) { |
---|
564 | SemanticWarning_EnableAll(); |
---|
565 | } else if ( strcmp( optarg, "error" ) == 0 ) { |
---|
566 | Werror = true; |
---|
567 | } else { |
---|
568 | char * warning = optarg; |
---|
569 | Severity s; |
---|
570 | if ( strncmp( optarg, "no-", 3 ) == 0 ) { |
---|
571 | warning += 3; |
---|
572 | s = Severity::Suppress; |
---|
573 | } else { |
---|
574 | s = Severity::Warn; |
---|
575 | } // if |
---|
576 | SemanticWarning_Set( warning, s ); |
---|
577 | } // if |
---|
578 | break; |
---|
579 | case 'D': // ignore -Dxxx, forwarded by cpp, hidden |
---|
580 | break; |
---|
581 | case '?': // unknown option |
---|
582 | if ( optopt ) { // short option ? |
---|
583 | cout << "Unknown option -" << (char)optopt << endl; |
---|
584 | } else { |
---|
585 | cout << "Unknown option " << argv[optind - 1] << endl; |
---|
586 | } // if |
---|
587 | goto Default; |
---|
588 | case ':': // missing option |
---|
589 | if ( optopt ) { // short option ? |
---|
590 | cout << "Missing option for -" << (char)optopt << endl; |
---|
591 | } else { |
---|
592 | cout << "Missing option for " << argv[optind - 1] << endl; |
---|
593 | } // if |
---|
594 | goto Default; |
---|
595 | Default: |
---|
596 | default: |
---|
597 | usage( argv ); // no return |
---|
598 | } // switch |
---|
599 | } // while |
---|
600 | |
---|
601 | if ( Werror ) { |
---|
602 | SemanticWarning_WarningAsError(); |
---|
603 | } // if |
---|
604 | if ( Wsuppress ) { |
---|
605 | SemanticWarning_SuppressAll(); |
---|
606 | } // if |
---|
607 | // for ( const auto w : WarningFormats ) { |
---|
608 | // cout << w.name << ' ' << (int)w.severity << endl; |
---|
609 | // } // for |
---|
610 | } // parse_cmdline |
---|
611 | |
---|
612 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { |
---|
613 | extern int yyparse( void ); |
---|
614 | extern FILE * yyin; |
---|
615 | extern int yylineno; |
---|
616 | |
---|
617 | ::linkage = linkage; // set globals |
---|
618 | yyin = input; |
---|
619 | yylineno = 1; |
---|
620 | int parseStatus = yyparse(); |
---|
621 | |
---|
622 | fclose( input ); |
---|
623 | if ( shouldExit || parseStatus != 0 ) { |
---|
624 | exit( parseStatus ); |
---|
625 | } // if |
---|
626 | } // parse |
---|
627 | |
---|
628 | static bool notPrelude( Declaration * decl ) { |
---|
629 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
630 | } // notPrelude |
---|
631 | |
---|
632 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { |
---|
633 | list< Declaration * > decls; |
---|
634 | |
---|
635 | if ( genproto ) { |
---|
636 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); |
---|
637 | } else { |
---|
638 | decls = translationUnit; |
---|
639 | } // if |
---|
640 | |
---|
641 | // depending on commandline options, either generate code or dump the AST |
---|
642 | if ( codegenp ) { |
---|
643 | CodeGen::generate( decls, out, ! genproto, prettycodegenp ); |
---|
644 | } else { |
---|
645 | printAll( decls, out ); |
---|
646 | } // if |
---|
647 | deleteAll( translationUnit ); |
---|
648 | } // dump |
---|
649 | |
---|
650 | // Local Variables: // |
---|
651 | // tab-width: 4 // |
---|
652 | // mode: c++ // |
---|
653 | // compile-command: "make install" // |
---|
654 | // End: // |
---|