source: src/main.cc@ 77e6fcb

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 77e6fcb was 13de47bc, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Proper bootloader boilerplate implemented

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