source: src/main.cc@ 6152c81

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 6152c81 was 35b1bf4, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

added pretty print flag, which currently just turns off name mangling in code gen

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