source: src/main.cc@ 02153feb

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 02153feb was c850687, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Add -L flag to turn of line marks. Updated the keyword list.

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