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