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