Changeset 24d6572 for src/main.cc
- Timestamp:
- Jun 12, 2023, 2:45:32 PM (2 years ago)
- Branches:
- ast-experimental, master
- Children:
- 62d62db
- Parents:
- 34b4268 (diff), 251ce80 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main.cc
r34b4268 r24d6572 9 9 // Author : Peter Buhr and Rob Schluntz 10 10 // Created On : Fri May 15 23:12:02 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Wed Oct 5 12:06:00 202213 // Update Count : 6 7911 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Apr 10 21:12:17 2023 13 // Update Count : 682 14 14 // 15 15 … … 32 32 33 33 #include "AST/Convert.hpp" 34 #include "AST/Pass.hpp" // for pass_visitor_stats 35 #include "AST/TranslationUnit.hpp" // for TranslationUnit 36 #include "AST/Util.hpp" // for checkInvariants 34 37 #include "CompilationState.h" 35 38 #include "../config.h" // for CFA_LIBDIR … … 40 43 #include "CodeTools/TrackLoc.h" // for fillLocations 41 44 #include "Common/CodeLocationTools.hpp" // for forceFillCodeLocations 42 #include "Common/CompilerError.h" // for CompilerError43 45 #include "Common/DeclStats.hpp" // for printDeclStats 44 46 #include "Common/ResolvProtoDump.hpp" // for dumpAsResolverProto 45 47 #include "Common/Stats.h" // for Stats 46 #include "Common/UnimplementedError.h" // for UnimplementedError47 48 #include "Common/utility.h" // for deleteAll, filter, printAll 49 #include "Concurrency/Actors.hpp" // for implementActors 48 50 #include "Concurrency/Keywords.h" // for implementMutex, implement... 49 51 #include "Concurrency/Waitfor.h" // for generateWaitfor 52 #include "Concurrency/Waituntil.hpp" // for generateWaitUntil 50 53 #include "ControlStruct/ExceptDecl.h" // for translateExcept 51 54 #include "ControlStruct/ExceptTranslate.h" // for translateThrows, translat... … … 59 62 #include "InitTweak/GenInit.h" // for genInit 60 63 #include "MakeLibCfa.h" // for makeLibCfa 61 #include "Parser/ParseNode.h" // for DeclarationNode, buildList 62 #include "Parser/TypedefTable.h" // for TypedefTable 64 #include "Parser/RunParser.hpp" // for buildList, dumpParseTree,... 63 65 #include "ResolvExpr/CandidatePrinter.hpp" // for printCandidates 64 66 #include "ResolvExpr/Resolver.h" // for resolve … … 84 86 #include "Validate/VerifyCtorDtorAssign.hpp" // for verifyCtorDtorAssign 85 87 #include "Virtual/ExpandCasts.h" // for expandCasts 88 #include "Virtual/VirtualDtor.hpp" // for implementVirtDtors 86 89 87 90 static void NewPass( const char * const name ) { … … 102 105 } 103 106 104 #define PASS( name, pass ) \ 107 // Helpers for checkInvariant: 108 void checkInvariants( std::list< Declaration * > & ) {} 109 using ast::checkInvariants; 110 111 #define PASS( name, pass, unit, ... ) \ 105 112 if ( errorp ) { cerr << name << endl; } \ 106 113 NewPass(name); \ 107 114 Stats::Time::StartBlock(name); \ 108 pass; \ 109 Stats::Time::StopBlock(); 110 111 LinkageSpec::Spec linkage = LinkageSpec::Cforall; 112 TypedefTable typedefTable; 113 DeclarationNode * parseTree = nullptr; // program parse tree 115 pass(unit,##__VA_ARGS__); \ 116 Stats::Time::StopBlock(); \ 117 if ( invariant ) { \ 118 checkInvariants(unit); \ 119 } 120 121 #define DUMP( cond, unit ) \ 122 if ( cond ) { \ 123 dump(unit); \ 124 return EXIT_SUCCESS; \ 125 } 114 126 115 127 static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start … … 118 130 119 131 static void parse_cmdline( int argc, char * argv[] ); 120 static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );121 132 static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); 122 133 static void dump( ast::TranslationUnit && transUnit, ostream & out = cout ); … … 234 245 ostream * output = & cout; 235 246 list< Declaration * > translationUnit; 247 ast::TranslationUnit transUnit; 236 248 237 249 Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO ); … … 278 290 FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" ); 279 291 assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); 280 parse( gcc_builtins, LinkageSpec::Compiler );292 parse( gcc_builtins, ast::Linkage::Compiler ); 281 293 282 294 // read the extra prelude in, if not generating the cfa library 283 295 FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" ); 284 296 assertf( extras, "cannot open extras.cf\n" ); 285 parse( extras, LinkageSpec::BuiltinC );297 parse( extras, ast::Linkage::BuiltinC ); 286 298 287 299 if ( ! libcfap ) { … … 289 301 FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" ); 290 302 assertf( prelude, "cannot open prelude.cfa\n" ); 291 parse( prelude, LinkageSpec::Intrinsic );303 parse( prelude, ast::Linkage::Intrinsic ); 292 304 293 305 // Read to cfa builtins, if not generating the cfa library 294 306 FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" ); 295 307 assertf( builtins, "cannot open builtins.cf\n" ); 296 parse( builtins, LinkageSpec::BuiltinCFA ); 297 } // if 298 } // if 299 300 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); 301 302 if ( parsep ) { 303 parseTree->printList( cout ); 304 delete parseTree; 305 return EXIT_SUCCESS; 306 } // if 307 308 buildList( parseTree, translationUnit ); 309 delete parseTree; 310 parseTree = nullptr; 311 312 if ( astp ) { 313 dump( translationUnit ); 314 return EXIT_SUCCESS; 315 } // if 316 317 // Temporary: fill locations after parsing so that every node has a location, for early error messages. 318 // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution 319 // works okay for now. 320 CodeTools::fillLocations( translationUnit ); 308 parse( builtins, ast::Linkage::BuiltinCFA ); 309 } // if 310 } // if 311 312 parse( input, libcfap ? ast::Linkage::Intrinsic : ast::Linkage::Cforall, yydebug ); 313 314 transUnit = buildUnit(); 315 316 DUMP( astp, std::move( transUnit ) ); 317 321 318 Stats::Time::StopBlock(); 322 319 … … 325 322 ast::pass_visitor_stats.max = Stats::Counters::build<Stats::Counters::MaxCounter<double>>("Max depth - New"); 326 323 } 327 auto transUnit = convert( std::move( translationUnit ) ); 328 329 forceFillCodeLocations( transUnit ); 330 331 PASS( "Translate Exception Declarations", ControlStruct::translateExcept( transUnit ) ); 332 if ( exdeclp ) { 333 dump( std::move( transUnit ) ); 334 return EXIT_SUCCESS; 335 } 336 337 PASS( "Verify Ctor, Dtor & Assign", Validate::verifyCtorDtorAssign( transUnit ) ); 338 PASS( "Hoist Type Decls", Validate::hoistTypeDecls( transUnit ) ); 339 // Hoist Type Decls pulls some declarations out of contexts where 340 // locations are not tracked. Perhaps they should be, but for now 341 // the full fill solves it. 342 forceFillCodeLocations( transUnit ); 343 344 PASS( "Replace Typedefs", Validate::replaceTypedef( transUnit ) ); 345 PASS( "Fix Return Types", Validate::fixReturnTypes( transUnit ) ); 346 PASS( "Enum and Pointer Decay", Validate::decayEnumsAndPointers( transUnit ) ); 347 348 PASS( "Link Reference To Types", Validate::linkReferenceToTypes( transUnit ) ); 349 350 PASS( "Fix Qualified Types", Validate::fixQualifiedTypes( transUnit ) ); 351 PASS( "Hoist Struct", Validate::hoistStruct( transUnit ) ); 352 PASS( "Eliminate Typedef", Validate::eliminateTypedef( transUnit ) ); 353 PASS( "Validate Generic Parameters", Validate::fillGenericParameters( transUnit ) ); 354 PASS( "Translate Dimensions", Validate::translateDimensionParameters( transUnit ) ); 355 PASS( "Check Function Returns", Validate::checkReturnStatements( transUnit ) ); 356 PASS( "Fix Return Statements", InitTweak::fixReturnStatements( transUnit ) ); 357 PASS( "Implement Concurrent Keywords", Concurrency::implementKeywords( transUnit ) ); 358 PASS( "Forall Pointer Decay", Validate::decayForallPointers( transUnit ) ); 359 PASS( "Hoist Control Declarations", ControlStruct::hoistControlDecls( transUnit ) ); 360 361 PASS( "Generate Autogen Routines", Validate::autogenerateRoutines( transUnit ) ); 362 363 PASS( "Implement Mutex", Concurrency::implementMutex( transUnit ) ); 364 PASS( "Implement Thread Start", Concurrency::implementThreadStarter( transUnit ) ); 365 PASS( "Compound Literal", Validate::handleCompoundLiterals( transUnit ) ); 366 PASS( "Set Length From Initializer", Validate::setLengthFromInitializer( transUnit ) ); 367 PASS( "Find Global Decls", Validate::findGlobalDecls( transUnit ) ); 368 PASS( "Fix Label Address", Validate::fixLabelAddresses( transUnit ) ); 324 325 PASS( "Hoist Type Decls", Validate::hoistTypeDecls, transUnit ); 326 327 PASS( "Translate Exception Declarations", ControlStruct::translateExcept, transUnit ); 328 DUMP( exdeclp, std::move( transUnit ) ); 329 PASS( "Verify Ctor, Dtor & Assign", Validate::verifyCtorDtorAssign, transUnit ); 330 PASS( "Replace Typedefs", Validate::replaceTypedef, transUnit ); 331 PASS( "Fix Return Types", Validate::fixReturnTypes, transUnit ); 332 PASS( "Enum and Pointer Decay", Validate::decayEnumsAndPointers, transUnit ); 333 334 PASS( "Link Reference To Types", Validate::linkReferenceToTypes, transUnit ); 335 336 PASS( "Fix Qualified Types", Validate::fixQualifiedTypes, transUnit ); 337 PASS( "Hoist Struct", Validate::hoistStruct, transUnit ); 338 PASS( "Eliminate Typedef", Validate::eliminateTypedef, transUnit ); 339 PASS( "Validate Generic Parameters", Validate::fillGenericParameters, transUnit ); 340 PASS( "Translate Dimensions", Validate::translateDimensionParameters, transUnit ); 341 PASS( "Check Function Returns", Validate::checkReturnStatements, transUnit ); 342 PASS( "Fix Return Statements", InitTweak::fixReturnStatements, transUnit ); 343 PASS( "Implement Concurrent Keywords", Concurrency::implementKeywords, transUnit ); 344 PASS( "Forall Pointer Decay", Validate::decayForallPointers, transUnit ); 345 PASS( "Implement Waituntil", Concurrency::generateWaitUntil, transUnit ); 346 PASS( "Hoist Control Declarations", ControlStruct::hoistControlDecls, transUnit ); 347 348 PASS( "Generate Autogen Routines", Validate::autogenerateRoutines, transUnit ); 349 350 PASS( "Implement Actors", Concurrency::implementActors, transUnit ); 351 PASS( "Implement Virtual Destructors", Virtual::implementVirtDtors, transUnit ); 352 PASS( "Implement Mutex", Concurrency::implementMutex, transUnit ); 353 PASS( "Implement Thread Start", Concurrency::implementThreadStarter, transUnit ); 354 PASS( "Compound Literal", Validate::handleCompoundLiterals, transUnit ); 355 PASS( "Set Length From Initializer", Validate::setLengthFromInitializer, transUnit ); 356 PASS( "Find Global Decls", Validate::findGlobalDecls, transUnit ); 357 PASS( "Fix Label Address", Validate::fixLabelAddresses, transUnit ); 369 358 370 359 if ( symtabp ) { … … 377 366 } // if 378 367 379 if ( validp ) { 380 dump( std::move( transUnit ) ); 381 return EXIT_SUCCESS; 382 } // if 383 384 PASS( "Translate Throws", ControlStruct::translateThrows( transUnit ) ); 385 PASS( "Fix Labels", ControlStruct::fixLabels( transUnit ) ); 386 PASS( "Fix Names", CodeGen::fixNames( transUnit ) ); 387 PASS( "Gen Init", InitTweak::genInit( transUnit ) ); 388 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( transUnit ) ); 368 DUMP( validp, std::move( transUnit ) ); 369 370 PASS( "Translate Throws", ControlStruct::translateThrows, transUnit ); 371 PASS( "Fix Labels", ControlStruct::fixLabels, transUnit ); 372 PASS( "Fix Names", CodeGen::fixNames, transUnit ); 373 PASS( "Gen Init", InitTweak::genInit, transUnit ); 374 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples, transUnit ); 389 375 390 376 if ( libcfap ) { … … 398 384 } // if 399 385 400 if ( bresolvep ) { 401 dump( std::move( transUnit ) ); 402 return EXIT_SUCCESS; 403 } // if 386 DUMP( bresolvep, std::move( transUnit ) ); 404 387 405 388 if ( resolvprotop ) { … … 408 391 } // if 409 392 410 PASS( "Resolve", ResolvExpr::resolve( transUnit ) ); 411 if ( exprp ) { 412 dump( std::move( transUnit ) ); 413 return EXIT_SUCCESS; 414 } // if 415 416 forceFillCodeLocations( transUnit ); 417 418 PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary())); 393 PASS( "Resolve", ResolvExpr::resolve, transUnit ); 394 DUMP( exprp, std::move( transUnit ) ); 395 396 PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() ); 419 397 420 398 // fix ObjectDecl - replaces ConstructorInit nodes 421 if ( ctorinitp ) { 422 dump( std::move( transUnit ) ); 423 return EXIT_SUCCESS; 424 } // if 399 DUMP( ctorinitp, std::move( transUnit ) ); 425 400 426 401 // Currently not working due to unresolved issues with UniqueExpr 427 PASS( "Expand Unique Expr", Tuples::expandUniqueExpr ( transUnit )); // xxx - is this the right place for this? want to expand ASAP so tha, sequent 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 reused428 429 PASS( "Translate Tries", ControlStruct::translateTries ( transUnit ));430 PASS( "Gen Waitfor", Concurrency::generateWaitFor ( transUnit ));402 PASS( "Expand Unique Expr", Tuples::expandUniqueExpr, transUnit ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent 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 403 404 PASS( "Translate Tries", ControlStruct::translateTries, transUnit ); 405 PASS( "Gen Waitfor", Concurrency::generateWaitFor, transUnit ); 431 406 432 407 // Needs to happen before tuple types are expanded. 433 PASS( "Convert Specializations", GenPoly::convertSpecializations( transUnit ) ); 434 435 PASS( "Expand Tuples", Tuples::expandTuples( transUnit ) ); 436 437 if ( tuplep ) { 438 dump( std::move( transUnit ) ); 439 return EXIT_SUCCESS; 440 } // if 408 PASS( "Convert Specializations", GenPoly::convertSpecializations, transUnit ); 409 410 PASS( "Expand Tuples", Tuples::expandTuples, transUnit ); 411 DUMP( tuplep, std::move( transUnit ) ); 441 412 442 413 // Must come after Translate Tries. 443 PASS( "Virtual Expand Casts", Virtual::expandCasts( transUnit ) ); 444 445 PASS( "Instantiate Generics", GenPoly::instantiateGeneric( transUnit ) ); 446 if ( genericsp ) { 447 dump( std::move( transUnit ) ); 448 return EXIT_SUCCESS; 449 } // if 450 451 PASS( "Convert L-Value", GenPoly::convertLvalue( transUnit ) ); 414 PASS( "Virtual Expand Casts", Virtual::expandCasts, transUnit ); 415 416 PASS( "Instantiate Generics", GenPoly::instantiateGeneric, transUnit ); 417 DUMP( genericsp, std::move( transUnit ) ); 418 419 PASS( "Convert L-Value", GenPoly::convertLvalue, transUnit ); 452 420 453 421 translationUnit = convert( std::move( transUnit ) ); 454 422 455 if ( bboxp ) { 456 dump( translationUnit ); 457 return EXIT_SUCCESS; 458 } // if 459 PASS( "Box", GenPoly::box( translationUnit ) ); 460 461 PASS( "Link-Once", CodeGen::translateLinkOnce( translationUnit ) ); 423 DUMP( bboxp, translationUnit ); 424 PASS( "Box", GenPoly::box, translationUnit ); 425 426 PASS( "Link-Once", CodeGen::translateLinkOnce, translationUnit ); 462 427 463 428 // Code has been lowered to C, now we can start generation. 464 429 465 if ( bcodegenp ) { 466 dump( translationUnit ); 467 return EXIT_SUCCESS; 468 } // if 430 DUMP( bcodegenp, translationUnit ); 469 431 470 432 if ( optind < argc ) { // any commands after the flags and input file ? => output file name … … 473 435 474 436 CodeTools::fillLocations( translationUnit ); 475 PASS( "Code Gen", CodeGen::generate ( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ));437 PASS( "Code Gen", CodeGen::generate, translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ); 476 438 477 439 CodeGen::FixMain::fix( translationUnit, *output, … … 483 445 if ( errorp ) { 484 446 cerr << "---AST at error:---" << endl; 485 dump( translationUnit, cerr ); 447 // We check which section the errors came from without looking at 448 // transUnit because std::move means it could look like anything. 449 if ( !translationUnit.empty() ) { 450 dump( translationUnit, cerr ); 451 } else { 452 dump( std::move( transUnit ), cerr ); 453 } 486 454 cerr << endl << "---End of AST, begin error message:---\n" << endl; 487 455 } // if 488 456 e.print(); 489 if ( output != &cout ) {490 delete output;491 } // if492 return EXIT_FAILURE;493 } catch ( UnimplementedError & e ) {494 cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;495 if ( output != &cout ) {496 delete output;497 } // if498 return EXIT_FAILURE;499 } catch ( CompilerError & e ) {500 cerr << "Compiler Error: " << e.get_what() << endl;501 cerr << "(please report bugs to [REDACTED])" << endl;502 457 if ( output != &cout ) { 503 458 delete output; … … 528 483 529 484 530 static const char optstring[] = ":c:gh lLmNnpdP:S:twW:D:";485 static const char optstring[] = ":c:ghilLmNnpdP:S:twW:D:"; 531 486 532 487 enum { PreludeDir = 128 }; … … 535 490 { "gdb", no_argument, nullptr, 'g' }, 536 491 { "help", no_argument, nullptr, 'h' }, 492 { "invariant", no_argument, nullptr, 'i' }, 537 493 { "libcfa", no_argument, nullptr, 'l' }, 538 494 { "linemarks", no_argument, nullptr, 'L' }, 539 { "no-main", no_argument, 0, 'm' },495 { "no-main", no_argument, nullptr, 'm' }, 540 496 { "no-linemarks", no_argument, nullptr, 'N' }, 541 497 { "no-prelude", no_argument, nullptr, 'n' }, … … 556 512 "wait for gdb to attach", // -g 557 513 "print translator help message", // -h 514 "invariant checking during AST passes", // -i 558 515 "generate libcfa.c", // -l 559 516 "generate line marks", // -L … … 587 544 { "rproto", resolvprotop, true, "resolver-proto instance" }, 588 545 { "rsteps", resolvep, true, "print resolver steps" }, 589 { "tree", parsep, true, "print parse tree" },590 546 // code dumps 591 547 { "ast", astp, true, "print AST after parsing" }, … … 650 606 usage( argv ); // no return 651 607 break; 608 case 'i': // invariant checking 609 invariant = true; 610 break; 652 611 case 'l': // generate libcfa.c 653 612 libcfap = true; … … 748 707 } // parse_cmdline 749 708 750 static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {751 extern int yyparse( void );752 extern FILE * yyin;753 extern int yylineno;754 755 ::linkage = linkage; // set globals756 yyin = input;757 yylineno = 1;758 int parseStatus = yyparse();759 760 fclose( input );761 if ( shouldExit || parseStatus != 0 ) {762 exit( parseStatus );763 } // if764 } // parse765 766 709 static bool notPrelude( Declaration * decl ) { 767 710 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
Note:
See TracChangeset
for help on using the changeset viewer.