source: src/main.cc@ 903f7c3

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 903f7c3 was 4810867, checked in by ajbeach <ajbeach@…>, 8 years ago

Added basic line directives to code generation.

  • Property mode set to 100644
File size: 15.2 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"
[64adb03]34#include "Concurrency/Keywords.h"
[51b73452]35#include "CodeGen/Generate.h"
36#include "CodeGen/FixNames.h"
[13de47bc]37#include "CodeGen/FixMain.h"
[41a7137]38#include "CodeTools/DeclStats.h"
[4810867]39#include "CodeTools/TrackLoc.h"
[51b73452]40#include "ControlStruct/Mutate.h"
41#include "SymTab/Validate.h"
42#include "ResolvExpr/AlternativePrinter.h"
43#include "ResolvExpr/Resolver.h"
44#include "MakeLibCfa.h"
[a0fdbd5]45#include "InitTweak/GenInit.h"
[71f4e4f]46#include "InitTweak/FixInit.h"
[d3b7937]47#include "Common/UnimplementedError.h"
[51b73452]48#include "../config.h"
[6eb8948]49#include "Tuples/Tuples.h"
[51b73452]50
51using namespace std;
52
[e6955b1]53#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
[81419b5]54
[0da3e2c]55
[8b7ee09]56LinkageSpec::Spec linkage = LinkageSpec::Cforall;
[0da3e2c]57TypedefTable typedefTable;
[cbaee0d]58DeclarationNode * parseTree = nullptr; // program parse tree
[81419b5]59
[926af74]60extern int yydebug; // set for -g flag (Grammar)
[b87a5ed]61bool
62 astp = false,
[de62360d]63 bresolvep = false,
[fea7ca7]64 bboxp = false,
[ca1c11f]65 ctorinitp = false,
[41a7137]66 declstatsp = false,
[b87a5ed]67 exprp = false,
68 expraltp = false,
69 libcfap = false,
[de62360d]70 nopreludep = false,
[1ab4ce2]71 noprotop = false,
[3fe34ae]72 nomainp = false,
[de62360d]73 parsep = false,
[b87a5ed]74 resolvep = false, // used in AlternativeFinder
75 symtabp = false,
[d3b7937]76 treep = false,
[626dbc10]77 tuplep = false,
[b87a5ed]78 validp = false,
[de62360d]79 errorp = false,
[35b1bf4]80 codegenp = false,
81 prettycodegenp = false;
[b87a5ed]82
[926af74]83static void parse_cmdline( int argc, char *argv[], const char *& filename );
[8b7ee09]84static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
[e6955b1]85static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
86
[0afffee]87static void backtrace( int start ) { // skip first N stack frames
[e6955b1]88 enum { Frames = 50 };
89 void * array[Frames];
[0afffee]90 int size = ::backtrace( array, Frames );
91 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
92
93 *index( messages[0], '(' ) = '\0'; // find executable name
94 cerr << "Stack back trace for: " << messages[0] << endl;
[e6955b1]95
[b542bfb]96 // skip last 2 stack frames after main
97 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
[e6955b1]98 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
99 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset
[0afffee]100 if ( *p == '(' ) {
[46f6134]101 mangled_name = p;
[0afffee]102 } else if ( *p == '+' ) {
[e6955b1]103 offset_begin = p;
[0afffee]104 } else if ( *p == ')' ) {
[e6955b1]105 offset_end = p;
106 break;
107 } // if
108 } // for
109
110 // if line contains symbol, attempt to demangle
[b542bfb]111 int frameNo = i - start;
[e6955b1]112 if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
[0afffee]113 *mangled_name++ = '\0'; // delimit strings
[e6955b1]114 *offset_begin++ = '\0';
115 *offset_end++ = '\0';
116
[0afffee]117 int status;
[e6955b1]118 char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
[0afffee]119 // bug in __cxa_demangle for single-character lower-case non-mangled names
[e6955b1]120 if ( status == 0 ) { // demangling successful ?
[b542bfb]121 cerr << "(" << frameNo << ") " << messages[i] << " : "
[e6955b1]122 << real_name << "+" << offset_begin << offset_end << endl;
123 } else { // otherwise, output mangled name
[b542bfb]124 cerr << "(" << frameNo << ") " << messages[i] << " : "
[0afffee]125 << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
[e6955b1]126 } // if
[0afffee]127
[e6955b1]128 free( real_name );
129 } else { // otherwise, print the whole line
[b542bfb]130 cerr << "(" << frameNo << ") " << messages[i] << endl;
[e6955b1]131 } // if
132 } // for
[b542bfb]133
[e6955b1]134 free( messages );
[b542bfb]135} // backtrace
136
137void sigSegvBusHandler( int sig_num ) {
138 cerr << "*CFA runtime error* program cfa-cpp terminated with "
139 << (sig_num == SIGSEGV ? "segment fault" : "bus error")
[0afffee]140 << "." << endl;
[b542bfb]141 backtrace( 2 ); // skip first 2 stack frames
[e6955b1]142 exit( EXIT_FAILURE );
143} // sigSegvBusHandler
[0da3e2c]144
[b542bfb]145void sigAbortHandler( int sig_num ) {
146 backtrace( 6 ); // skip first 6 stack frames
147 signal( SIGABRT, SIG_DFL); // reset default signal handler
148 raise( SIGABRT ); // reraise SIGABRT
149} // sigAbortHandler
150
151
[cbaee0d]152int main( int argc, char * argv[] ) {
[3b8e52c]153 FILE * input; // use FILE rather than istream because yyin is FILE
[e6955b1]154 ostream *output = & cout;
[926af74]155 const char *filename = nullptr;
[e6955b1]156 list< Declaration * > translationUnit;
157
158 signal( SIGSEGV, sigSegvBusHandler );
159 signal( SIGBUS, sigSegvBusHandler );
[b542bfb]160 signal( SIGABRT, sigAbortHandler );
[b87a5ed]161
[0da3e2c]162 parse_cmdline( argc, argv, filename ); // process command-line arguments
[13de47bc]163 CodeGen::FixMain::setReplaceMain( !nomainp );
[b87a5ed]164
165 try {
[81419b5]166 // choose to read the program from a file or stdin
[3b8e52c]167 if ( optind < argc ) { // any commands after the flags ? => input file name
[b87a5ed]168 input = fopen( argv[ optind ], "r" );
[3b8e52c]169 assertf( input, "cannot open %s\n", argv[ optind ] );
[d029162e]170 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
[926af74]171 if ( filename == nullptr ) filename = argv[ optind ];
[37024fd]172 // prelude filename comes in differently
173 if ( libcfap ) filename = "prelude.cf";
[b87a5ed]174 optind += 1;
[3b8e52c]175 } else { // no input file name
[b87a5ed]176 input = stdin;
[2a7e29b]177 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
178 // a fake name along
[926af74]179 if ( filename == nullptr ) filename = "stdin";
[b87a5ed]180 } // if
181
[159c62e]182 // read in the builtins, extras, and the prelude
[de62360d]183 if ( ! nopreludep ) { // include gcc builtins
[faf8857]184 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[375a068]185 FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[3b8e52c]186 assertf( builtins, "cannot open builtins.cf\n" );
[81419b5]187 parse( builtins, LinkageSpec::Compiler );
188
[159c62e]189 // read the extra prelude in, if not generating the cfa library
[375a068]190 FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
[3b8e52c]191 assertf( extras, "cannot open extras.cf\n" );
[159c62e]192 parse( extras, LinkageSpec::C );
193
[81419b5]194 if ( ! libcfap ) {
[faf8857]195 // read the prelude in, if not generating the cfa library
[375a068]196 FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[3b8e52c]197 assertf( prelude, "cannot open prelude.cf\n" );
[35304009]198 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]199 } // if
200 } // if
[81419b5]201
[926af74]202 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
[71f4e4f]203
[b87a5ed]204 if ( parsep ) {
[e6955b1]205 parseTree->printList( cout );
[0da3e2c]206 delete parseTree;
[b87a5ed]207 return 0;
208 } // if
209
[0da3e2c]210 buildList( parseTree, translationUnit );
211 delete parseTree;
[cbaee0d]212 parseTree = nullptr;
[b87a5ed]213
214 if ( astp ) {
[1ab4ce2]215 dump( translationUnit );
[b87a5ed]216 return 0;
217 } // if
218
[bcda04c]219 // OPTPRINT( "Concurrency" )
220 // Concurrency::applyKeywords( translationUnit );
221
[839ccbb]222 // add the assignment statement after the initialization of a type parameter
[81419b5]223 OPTPRINT( "validate" )
224 SymTab::validate( translationUnit, symtabp );
225 if ( symtabp ) {
[46f6134]226 deleteAll( translationUnit );
[b87a5ed]227 return 0;
228 } // if
229
[81419b5]230 if ( expraltp ) {
[e6955b1]231 ResolvExpr::AlternativePrinter printer( cout );
[81419b5]232 acceptAll( translationUnit, printer );
[b87a5ed]233 return 0;
234 } // if
235
236 if ( validp ) {
[1ab4ce2]237 dump( translationUnit );
[b87a5ed]238 return 0;
239 } // if
240
[81419b5]241 OPTPRINT( "mutate" )
242 ControlStruct::mutate( translationUnit );
[71f4e4f]243 OPTPRINT( "fixNames" )
[81419b5]244 CodeGen::fixNames( translationUnit );
[ef42b143]245 OPTPRINT( "genInit" )
[a0fdbd5]246 InitTweak::genInit( translationUnit );
[bf32bb8]247 OPTPRINT( "expandMemberTuples" );
248 Tuples::expandMemberTuples( translationUnit );
[81419b5]249 if ( libcfap ) {
250 // generate the bodies of cfa library functions
251 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]252 } // if
253
[fa2de95]254 if ( declstatsp ) {
255 CodeTools::printDeclStats( translationUnit );
256 deleteAll( translationUnit );
257 return 0;
258 }
259
[de62360d]260 if ( bresolvep ) {
[1ab4ce2]261 dump( translationUnit );
[de62360d]262 return 0;
263 } // if
264
[81419b5]265 OPTPRINT( "resolve" )
[b87a5ed]266 ResolvExpr::resolve( translationUnit );
[81419b5]267 if ( exprp ) {
[1ab4ce2]268 dump( translationUnit );
[ca1c11f]269 return 0;
[926af74]270 } // if
[81419b5]271
[71f4e4f]272 // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]273 OPTPRINT( "fixInit" )
274 InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]275 if ( ctorinitp ) {
276 dump ( translationUnit );
277 return 0;
[926af74]278 } // if
[71f4e4f]279
[141b786]280 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
281 Tuples::expandUniqueExpr( translationUnit );
[626dbc10]282
283 OPTPRINT( "convertSpecializations" ) // needs to happen before tuple types are expanded
284 GenPoly::convertSpecializations( translationUnit );
285
[d9fa60a]286 OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
287 Tuples::expandTuples( translationUnit );
[626dbc10]288 if ( tuplep ) {
289 dump( translationUnit );
290 return 0;
291 }
[141b786]292
[ea5daeb]293 OPTPRINT("instantiateGenerics")
294 GenPoly::instantiateGeneric( translationUnit );
[81419b5]295 OPTPRINT( "copyParams" );
[b87a5ed]296 GenPoly::copyParams( translationUnit );
[81419b5]297 OPTPRINT( "convertLvalue" )
[b87a5ed]298 GenPoly::convertLvalue( translationUnit );
[fea7ca7]299
300 if ( bboxp ) {
301 dump( translationUnit );
302 return 0;
[926af74]303 } // if
[81419b5]304 OPTPRINT( "box" )
[b87a5ed]305 GenPoly::box( translationUnit );
[81419b5]306
[13de47bc]307 if ( optind < argc ) { // any commands after the flags and input file ? => output file name
308 output = new ofstream( argv[ optind ] );
309 } // if
[0270824]310
[4810867]311 CodeTools::fillLocations( translationUnit );
[e39241b]312 CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true );
[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:
[e39241b]391 case 'c': // print after constructors and destructors are replaced
[0da3e2c]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;
[e39241b]448 case 'y': // dump AST on error
[0da3e2c]449 errorp = true;
450 break;
[e39241b]451 case 'z': // dump as codegen rather than AST
[0da3e2c]452 codegenp = true;
[e39241b]453 break;
454 case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.)
[35b1bf4]455 prettycodegenp = true;
[0da3e2c]456 break;
457 case 'D': // ignore -Dxxx
458 break;
459 case 'F': // source file-name without suffix
460 filename = optarg;
461 break;
462 case '?':
[3b8e52c]463 assertf( false, "Unknown option: '%c'\n", (char)optopt );
[0da3e2c]464 default:
465 abort();
466 } // switch
467 } // while
468} // parse_cmdline
469
[8b7ee09]470static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]471 extern int yyparse( void );
[cbaee0d]472 extern FILE * yyin;
[0da3e2c]473 extern int yylineno;
474
[8b7ee09]475 ::linkage = linkage; // set globals
[0da3e2c]476 yyin = input;
477 yylineno = 1;
478 typedefTable.enterScope();
479 int parseStatus = yyparse();
[81419b5]480
481 fclose( input );
[0da3e2c]482 if ( shouldExit || parseStatus != 0 ) {
483 exit( parseStatus );
[81419b5]484 } // if
[0da3e2c]485} // parse
[81419b5]486
[1ab4ce2]487static bool notPrelude( Declaration * decl ) {
488 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]489} // notPrelude
[1ab4ce2]490
[e6955b1]491static void dump( list< Declaration * > & translationUnit, ostream & out ) {
492 list< Declaration * > decls;
[926af74]493
[1ab4ce2]494 if ( noprotop ) {
[e6955b1]495 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
[1ab4ce2]496 } else {
497 decls = translationUnit;
[926af74]498 } // if
[1ab4ce2]499
[e39241b]500 // depending on commandline options, either generate code or dump the AST
501 if ( codegenp ) {
502 CodeGen::generate( decls, out, ! noprotop, prettycodegenp );
503 } else {
504 printAll( decls, out );
505 }
[7f5566b]506 deleteAll( translationUnit );
[0da3e2c]507} // dump
[1ab4ce2]508
[51b73452]509// Local Variables: //
[b87a5ed]510// tab-width: 4 //
511// mode: c++ //
512// compile-command: "make install" //
[51b73452]513// End: //
Note: See TracBrowser for help on using the repository browser.