source: src/main.cc@ 4fc45ff

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 4fc45ff was bcda04c, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Fixed autogen constructors for concurrent sues

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