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