Changeset 24d6572 for src/main.cc


Ignore:
Timestamp:
Jun 12, 2023, 2:45:32 PM (2 years ago)
Author:
Fangren Yu <f37yu@…>
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.
Message:

Merge branch 'master' into ast-experimental

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r34b4268 r24d6572  
    99// Author           : Peter Buhr and Rob Schluntz
    1010// Created On       : Fri May 15 23:12:02 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Oct  5 12:06:00 2022
    13 // Update Count     : 679
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Apr 10 21:12:17 2023
     13// Update Count     : 682
    1414//
    1515
     
    3232
    3333#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
    3437#include "CompilationState.h"
    3538#include "../config.h"                      // for CFA_LIBDIR
     
    4043#include "CodeTools/TrackLoc.h"             // for fillLocations
    4144#include "Common/CodeLocationTools.hpp"     // for forceFillCodeLocations
    42 #include "Common/CompilerError.h"           // for CompilerError
    4345#include "Common/DeclStats.hpp"             // for printDeclStats
    4446#include "Common/ResolvProtoDump.hpp"       // for dumpAsResolverProto
    4547#include "Common/Stats.h"                   // for Stats
    46 #include "Common/UnimplementedError.h"      // for UnimplementedError
    4748#include "Common/utility.h"                 // for deleteAll, filter, printAll
     49#include "Concurrency/Actors.hpp"           // for implementActors
    4850#include "Concurrency/Keywords.h"           // for implementMutex, implement...
    4951#include "Concurrency/Waitfor.h"            // for generateWaitfor
     52#include "Concurrency/Waituntil.hpp"        // for generateWaitUntil
    5053#include "ControlStruct/ExceptDecl.h"       // for translateExcept
    5154#include "ControlStruct/ExceptTranslate.h"  // for translateThrows, translat...
     
    5962#include "InitTweak/GenInit.h"              // for genInit
    6063#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,...
    6365#include "ResolvExpr/CandidatePrinter.hpp"  // for printCandidates
    6466#include "ResolvExpr/Resolver.h"            // for resolve
     
    8486#include "Validate/VerifyCtorDtorAssign.hpp" // for verifyCtorDtorAssign
    8587#include "Virtual/ExpandCasts.h"            // for expandCasts
     88#include "Virtual/VirtualDtor.hpp"           // for implementVirtDtors
    8689
    8790static void NewPass( const char * const name ) {
     
    102105}
    103106
    104 #define PASS( name, pass )                  \
     107// Helpers for checkInvariant:
     108void checkInvariants( std::list< Declaration * > & ) {}
     109using ast::checkInvariants;
     110
     111#define PASS( name, pass, unit, ... )       \
    105112        if ( errorp ) { cerr << name << endl; } \
    106113        NewPass(name);                          \
    107114        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        }
    114126
    115127static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
     
    118130
    119131static void parse_cmdline( int argc, char * argv[] );
    120 static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
    121132static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
    122133static void dump( ast::TranslationUnit && transUnit, ostream & out = cout );
     
    234245        ostream * output = & cout;
    235246        list< Declaration * > translationUnit;
     247        ast::TranslationUnit transUnit;
    236248
    237249        Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO );
     
    278290                        FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" );
    279291                        assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" );
    280                         parse( gcc_builtins, LinkageSpec::Compiler );
     292                        parse( gcc_builtins, ast::Linkage::Compiler );
    281293
    282294                        // read the extra prelude in, if not generating the cfa library
    283295                        FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" );
    284296                        assertf( extras, "cannot open extras.cf\n" );
    285                         parse( extras, LinkageSpec::BuiltinC );
     297                        parse( extras, ast::Linkage::BuiltinC );
    286298
    287299                        if ( ! libcfap ) {
     
    289301                                FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" );
    290302                                assertf( prelude, "cannot open prelude.cfa\n" );
    291                                 parse( prelude, LinkageSpec::Intrinsic );
     303                                parse( prelude, ast::Linkage::Intrinsic );
    292304
    293305                                // Read to cfa builtins, if not generating the cfa library
    294306                                FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" );
    295307                                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
    321318                Stats::Time::StopBlock();
    322319
     
    325322                        ast::pass_visitor_stats.max = Stats::Counters::build<Stats::Counters::MaxCounter<double>>("Max depth - New");
    326323                }
    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 );
    369358
    370359                if ( symtabp ) {
     
    377366                } // if
    378367
    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 );
    389375
    390376                if ( libcfap ) {
     
    398384                } // if
    399385
    400                 if ( bresolvep ) {
    401                         dump( std::move( transUnit ) );
    402                         return EXIT_SUCCESS;
    403                 } // if
     386                DUMP( bresolvep, std::move( transUnit ) );
    404387
    405388                if ( resolvprotop ) {
     
    408391                } // if
    409392
    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() );
    419397
    420398                // 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 ) );
    425400
    426401                // 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 reused
    428 
    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 );
    431406
    432407                // 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 ) );
    441412
    442413                // 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 );
    452420
    453421                translationUnit = convert( std::move( transUnit ) );
    454422
    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 );
    462427
    463428                // Code has been lowered to C, now we can start generation.
    464429
    465                 if ( bcodegenp ) {
    466                         dump( translationUnit );
    467                         return EXIT_SUCCESS;
    468                 } // if
     430                DUMP( bcodegenp, translationUnit );
    469431
    470432                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
     
    473435
    474436                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 );
    476438
    477439                CodeGen::FixMain::fix( translationUnit, *output,
     
    483445                if ( errorp ) {
    484446                        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                        }
    486454                        cerr << endl << "---End of AST, begin error message:---\n" << endl;
    487455                } // if
    488456                e.print();
    489                 if ( output != &cout ) {
    490                         delete output;
    491                 } // if
    492                 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                 } // if
    498                 return EXIT_FAILURE;
    499         } catch ( CompilerError & e ) {
    500                 cerr << "Compiler Error: " << e.get_what() << endl;
    501                 cerr << "(please report bugs to [REDACTED])" << endl;
    502457                if ( output != &cout ) {
    503458                        delete output;
     
    528483
    529484
    530 static const char optstring[] = ":c:ghlLmNnpdP:S:twW:D:";
     485static const char optstring[] = ":c:ghilLmNnpdP:S:twW:D:";
    531486
    532487enum { PreludeDir = 128 };
     
    535490        { "gdb", no_argument, nullptr, 'g' },
    536491        { "help", no_argument, nullptr, 'h' },
     492        { "invariant", no_argument, nullptr, 'i' },
    537493        { "libcfa", no_argument, nullptr, 'l' },
    538494        { "linemarks", no_argument, nullptr, 'L' },
    539         { "no-main", no_argument, 0, 'm' },
     495        { "no-main", no_argument, nullptr, 'm' },
    540496        { "no-linemarks", no_argument, nullptr, 'N' },
    541497        { "no-prelude", no_argument, nullptr, 'n' },
     
    556512        "wait for gdb to attach",                                                       // -g
    557513        "print translator help message",                                        // -h
     514        "invariant checking during AST passes",                         // -i
    558515        "generate libcfa.c",                                                            // -l
    559516        "generate line marks",                                                          // -L
     
    587544        { "rproto", resolvprotop, true, "resolver-proto instance" },
    588545        { "rsteps", resolvep, true, "print resolver steps" },
    589         { "tree", parsep, true, "print parse tree" },
    590546        // code dumps
    591547        { "ast", astp, true, "print AST after parsing" },
     
    650606                        usage( argv );                                                          // no return
    651607                        break;
     608                  case 'i':                                                                             // invariant checking
     609                        invariant = true;
     610                        break;
    652611                  case 'l':                                                                             // generate libcfa.c
    653612                        libcfap = true;
     
    748707} // parse_cmdline
    749708
    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 globals
    756         yyin = input;
    757         yylineno = 1;
    758         int parseStatus = yyparse();
    759 
    760         fclose( input );
    761         if ( shouldExit || parseStatus != 0 ) {
    762                 exit( parseStatus );
    763         } // if
    764 } // parse
    765 
    766709static bool notPrelude( Declaration * decl ) {
    767710        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
Note: See TracChangeset for help on using the changeset viewer.