source: src/main.cc@ bd9f8be

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 bd9f8be was e6955b1, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

abort on assert rather than exit, print backtrace on cfa-cpp errors

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