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