source: src/main.cc@ fc4a0fa

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since fc4a0fa was b542bfb, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

print stack trace for assertion failure

  • Property mode set to 100644
File size: 13.0 KB
RevLine 
[b87a5ed]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//
[71f4e4f]7// main.cc --
[b87a5ed]8//
[843054c2]9// Author : Richard C. Bilson
[b87a5ed]10// Created On : Fri May 15 23:12:02 2015
[dd51906]11// Last Modified By : Peter A. Buhr
[b542bfb]12// Last Modified On : Mon Aug 29 17:34:39 2016
13// Update Count : 426
[b87a5ed]14//
15
[51b73452]16#include <iostream>
17#include <fstream>
[e6955b1]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
23using namespace std;
24
[0da3e2c]25#include "Parser/lex.h"
26#include "Parser/parser.h"
27#include "Parser/TypedefTable.h"
[51b73452]28#include "GenPoly/Lvalue.h"
29#include "GenPoly/Specialize.h"
30#include "GenPoly/Box.h"
31#include "GenPoly/CopyParams.h"
[ea5daeb]32#include "GenPoly/InstantiateGeneric.h"
[51b73452]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"
[a0fdbd5]40#include "InitTweak/GenInit.h"
[71f4e4f]41#include "InitTweak/FixInit.h"
[d3b7937]42#include "Common/UnimplementedError.h"
[51b73452]43#include "../config.h"
44
45using namespace std;
46
[e6955b1]47#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
[81419b5]48
[0da3e2c]49
[8b7ee09]50LinkageSpec::Spec linkage = LinkageSpec::Cforall;
[0da3e2c]51TypedefTable typedefTable;
[cbaee0d]52DeclarationNode * parseTree = nullptr; // program parse tree
[81419b5]53
[926af74]54extern int yydebug; // set for -g flag (Grammar)
[b87a5ed]55bool
56 astp = false,
[de62360d]57 bresolvep = false,
[fea7ca7]58 bboxp = false,
[ca1c11f]59 ctorinitp = false,
[b87a5ed]60 exprp = false,
61 expraltp = false,
62 libcfap = false,
[de62360d]63 nopreludep = false,
[1ab4ce2]64 noprotop = false,
[de62360d]65 parsep = false,
[b87a5ed]66 resolvep = false, // used in AlternativeFinder
67 symtabp = false,
[d3b7937]68 treep = false,
[b87a5ed]69 validp = false,
[de62360d]70 errorp = false,
71 codegenp = false;
[b87a5ed]72
[926af74]73static void parse_cmdline( int argc, char *argv[], const char *& filename );
[8b7ee09]74static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
[e6955b1]75static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
76
[b542bfb]77void backtrace( int start ) { // skip first N stack frames
[e6955b1]78 enum { Frames = 50 };
79 void * array[Frames];
80 int size = backtrace( array, Frames );
[46f6134]81 char ** messages = backtrace_symbols( array, size );
[e6955b1]82
[b542bfb]83 // skip last 2 stack frames after main
84 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
[e6955b1]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 == '(') {
[46f6134]88 mangled_name = p;
[e6955b1]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
[b542bfb]98 int frameNo = i - start;
[e6955b1]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
[b542bfb]104 int status, frameNo = i - start;
[e6955b1]105 char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
106 if ( status == 0 ) { // demangling successful ?
[b542bfb]107 cerr << "(" << frameNo << ") " << messages[i] << " : "
[e6955b1]108 << real_name << "+" << offset_begin << offset_end << endl;
109
110 } else { // otherwise, output mangled name
[b542bfb]111 cerr << "(" << frameNo << ") " << messages[i] << " : "
[e6955b1]112 << mangled_name << "+" << offset_begin << offset_end << endl;
113 } // if
114 free( real_name );
115 } else { // otherwise, print the whole line
[b542bfb]116 cerr << "(" << frameNo << ") " << messages[i] << endl;
[e6955b1]117 } // if
118 } // for
[b542bfb]119
[e6955b1]120 free( messages );
[b542bfb]121} // backtrace
122
123void 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
[e6955b1]128 exit( EXIT_FAILURE );
129} // sigSegvBusHandler
[0da3e2c]130
[b542bfb]131void 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
[cbaee0d]138int main( int argc, char * argv[] ) {
[3b8e52c]139 FILE * input; // use FILE rather than istream because yyin is FILE
[e6955b1]140 ostream *output = & cout;
[926af74]141 const char *filename = nullptr;
[e6955b1]142 list< Declaration * > translationUnit;
143
144 signal( SIGSEGV, sigSegvBusHandler );
145 signal( SIGBUS, sigSegvBusHandler );
[b542bfb]146 signal( SIGABRT, sigAbortHandler );
[b87a5ed]147
[0da3e2c]148 parse_cmdline( argc, argv, filename ); // process command-line arguments
[b87a5ed]149
150 try {
[81419b5]151 // choose to read the program from a file or stdin
[3b8e52c]152 if ( optind < argc ) { // any commands after the flags ? => input file name
[b87a5ed]153 input = fopen( argv[ optind ], "r" );
[3b8e52c]154 assertf( input, "cannot open %s\n", argv[ optind ] );
[d029162e]155 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
[926af74]156 if ( filename == nullptr ) filename = argv[ optind ];
[37024fd]157 // prelude filename comes in differently
158 if ( libcfap ) filename = "prelude.cf";
[b87a5ed]159 optind += 1;
[3b8e52c]160 } else { // no input file name
[b87a5ed]161 input = stdin;
[2a7e29b]162 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
163 // a fake name along
[926af74]164 if ( filename == nullptr ) filename = "stdin";
[b87a5ed]165 } // if
166
[3b8e52c]167 if ( optind < argc ) { // any commands after the flags and input file ? => output file name
[b87a5ed]168 output = new ofstream( argv[ optind ] );
169 } // if
[71f4e4f]170
[159c62e]171 // read in the builtins, extras, and the prelude
[de62360d]172 if ( ! nopreludep ) { // include gcc builtins
[faf8857]173 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]174 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[3b8e52c]175 assertf( builtins, "cannot open builtins.cf\n" );
[81419b5]176 parse( builtins, LinkageSpec::Compiler );
177
[159c62e]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" );
[3b8e52c]180 assertf( extras, "cannot open extras.cf\n" );
[159c62e]181 parse( extras, LinkageSpec::C );
182
[81419b5]183 if ( ! libcfap ) {
[faf8857]184 // read the prelude in, if not generating the cfa library
[d3b7937]185 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[3b8e52c]186 assertf( prelude, "cannot open prelude.cf\n" );
[35304009]187 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]188 } // if
189 } // if
[81419b5]190
[926af74]191 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
[71f4e4f]192
[b87a5ed]193 if ( parsep ) {
[e6955b1]194 parseTree->printList( cout );
[0da3e2c]195 delete parseTree;
[b87a5ed]196 return 0;
197 } // if
198
[0da3e2c]199 buildList( parseTree, translationUnit );
200 delete parseTree;
[cbaee0d]201 parseTree = nullptr;
[b87a5ed]202
203 if ( astp ) {
[1ab4ce2]204 dump( translationUnit );
[b87a5ed]205 return 0;
206 } // if
207
[839ccbb]208 // add the assignment statement after the initialization of a type parameter
[81419b5]209 OPTPRINT( "validate" )
210 SymTab::validate( translationUnit, symtabp );
211 if ( symtabp ) {
[46f6134]212 deleteAll( translationUnit );
[b87a5ed]213 return 0;
214 } // if
215
[81419b5]216 if ( expraltp ) {
[e6955b1]217 ResolvExpr::AlternativePrinter printer( cout );
[81419b5]218 acceptAll( translationUnit, printer );
[b87a5ed]219 return 0;
220 } // if
221
222 if ( validp ) {
[1ab4ce2]223 dump( translationUnit );
[b87a5ed]224 return 0;
225 } // if
226
[81419b5]227 OPTPRINT( "mutate" )
228 ControlStruct::mutate( translationUnit );
[71f4e4f]229 OPTPRINT( "fixNames" )
[81419b5]230 CodeGen::fixNames( translationUnit );
[f1e012b]231 OPTPRINT( "tweakInit" )
[a0fdbd5]232 InitTweak::genInit( translationUnit );
[b87a5ed]233
[81419b5]234 if ( libcfap ) {
235 // generate the bodies of cfa library functions
236 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]237 } // if
238
[de62360d]239 if ( bresolvep ) {
[1ab4ce2]240 dump( translationUnit );
[de62360d]241 return 0;
242 } // if
243
[81419b5]244 OPTPRINT( "resolve" )
[b87a5ed]245 ResolvExpr::resolve( translationUnit );
[81419b5]246 if ( exprp ) {
[1ab4ce2]247 dump( translationUnit );
[ca1c11f]248 return 0;
[926af74]249 } // if
[81419b5]250
[71f4e4f]251 // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]252 OPTPRINT( "fixInit" )
253 InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]254 if ( ctorinitp ) {
255 dump ( translationUnit );
256 return 0;
[926af74]257 } // if
[71f4e4f]258
[ea5daeb]259 OPTPRINT("instantiateGenerics")
260 GenPoly::instantiateGeneric( translationUnit );
[81419b5]261 OPTPRINT( "copyParams" );
[b87a5ed]262 GenPoly::copyParams( translationUnit );
[81419b5]263 OPTPRINT( "convertSpecializations" )
[698664b3]264 GenPoly::convertSpecializations( translationUnit );
[81419b5]265 OPTPRINT( "convertLvalue" )
[b87a5ed]266 GenPoly::convertLvalue( translationUnit );
[fea7ca7]267
268 if ( bboxp ) {
269 dump( translationUnit );
270 return 0;
[926af74]271 } // if
[81419b5]272 OPTPRINT( "box" )
[b87a5ed]273 GenPoly::box( translationUnit );
[81419b5]274
[2871210]275 // print tree right before code generation
[81419b5]276 if ( codegenp ) {
[1ab4ce2]277 dump( translationUnit );
[81419b5]278 return 0;
279 } // if
[b87a5ed]280
[1ab4ce2]281 CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]282
[e6955b1]283 if ( output != &cout ) {
[b87a5ed]284 delete output;
285 } // if
286 } catch ( SemanticError &e ) {
287 if ( errorp ) {
[e6955b1]288 cerr << "---AST at error:---" << endl;
289 dump( translationUnit, cerr );
290 cerr << endl << "---End of AST, begin error message:---\n" << endl;
[926af74]291 } // if
[e6955b1]292 e.print( cerr );
293 if ( output != &cout ) {
[b87a5ed]294 delete output;
295 } // if
296 return 1;
297 } catch ( UnimplementedError &e ) {
[e6955b1]298 cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
299 if ( output != &cout ) {
[b87a5ed]300 delete output;
301 } // if
302 return 1;
303 } catch ( CompilerError &e ) {
[e6955b1]304 cerr << "Compiler Error: " << e.get_what() << endl;
305 cerr << "(please report bugs to " << endl;
306 if ( output != &cout ) {
[b87a5ed]307 delete output;
308 } // if
309 return 1;
310 } // try
311
[39786813]312 deleteAll( translationUnit );
[b87a5ed]313 return 0;
[d9a0e76]314} // main
[51b73452]315
[cbaee0d]316void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[0da3e2c]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)
[926af74]369 yydebug = true;
[0da3e2c]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 '?':
[3b8e52c]414 assertf( false, "Unknown option: '%c'\n", (char)optopt );
[0da3e2c]415 default:
416 abort();
417 } // switch
418 } // while
419} // parse_cmdline
420
[8b7ee09]421static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]422 extern int yyparse( void );
[cbaee0d]423 extern FILE * yyin;
[0da3e2c]424 extern int yylineno;
425
[8b7ee09]426 ::linkage = linkage; // set globals
[0da3e2c]427 yyin = input;
428 yylineno = 1;
429 typedefTable.enterScope();
430 int parseStatus = yyparse();
[81419b5]431
432 fclose( input );
[0da3e2c]433 if ( shouldExit || parseStatus != 0 ) {
434 exit( parseStatus );
[81419b5]435 } // if
[0da3e2c]436} // parse
[81419b5]437
[1ab4ce2]438static bool notPrelude( Declaration * decl ) {
439 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]440} // notPrelude
[1ab4ce2]441
[e6955b1]442static void dump( list< Declaration * > & translationUnit, ostream & out ) {
443 list< Declaration * > decls;
[926af74]444
[1ab4ce2]445 if ( noprotop ) {
[e6955b1]446 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
[1ab4ce2]447 } else {
448 decls = translationUnit;
[926af74]449 } // if
[1ab4ce2]450
[f77f12e2]451 printAll( decls, out );
[7f5566b]452 deleteAll( translationUnit );
[0da3e2c]453} // dump
[1ab4ce2]454
[51b73452]455// Local Variables: //
[b87a5ed]456// tab-width: 4 //
457// mode: c++ //
458// compile-command: "make install" //
[51b73452]459// End: //
Note: See TracBrowser for help on using the repository browser.