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