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