Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    rbccd70a r7fffb1b  
    99// Author           : Peter Buhr and Rob Schluntz
    1010// Created On       : Fri May 15 23:12:02 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Apr 10 21:12:17 2023
    13 // Update Count     : 682
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Oct  5 12:06:00 2022
     13// Update Count     : 679
    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
    3734#include "CompilationState.h"
    3835#include "../config.h"                      // for CFA_LIBDIR
     
    4340#include "CodeTools/TrackLoc.h"             // for fillLocations
    4441#include "Common/CodeLocationTools.hpp"     // for forceFillCodeLocations
     42#include "Common/CompilerError.h"           // for CompilerError
    4543#include "Common/DeclStats.hpp"             // for printDeclStats
    4644#include "Common/ResolvProtoDump.hpp"       // for dumpAsResolverProto
    4745#include "Common/Stats.h"                   // for Stats
     46#include "Common/UnimplementedError.h"      // for UnimplementedError
    4847#include "Common/utility.h"                 // for deleteAll, filter, printAll
    49 #include "Concurrency/Actors.hpp"           // for implementActors
    5048#include "Concurrency/Keywords.h"           // for implementMutex, implement...
    5149#include "Concurrency/Waitfor.h"            // for generateWaitfor
    52 #include "Concurrency/Waituntil.hpp"        // for generateWaitUntil
    5350#include "ControlStruct/ExceptDecl.h"       // for translateExcept
    5451#include "ControlStruct/ExceptTranslate.h"  // for translateThrows, translat...
     
    6259#include "InitTweak/GenInit.h"              // for genInit
    6360#include "MakeLibCfa.h"                     // for makeLibCfa
    64 #include "Parser/RunParser.hpp"             // for buildList, dumpParseTree,...
     61#include "Parser/ParseNode.h"               // for DeclarationNode, buildList
     62#include "Parser/TypedefTable.h"            // for TypedefTable
    6563#include "ResolvExpr/CandidatePrinter.hpp"  // for printCandidates
    6664#include "ResolvExpr/Resolver.h"            // for resolve
     
    8684#include "Validate/VerifyCtorDtorAssign.hpp" // for verifyCtorDtorAssign
    8785#include "Virtual/ExpandCasts.h"            // for expandCasts
    88 #include "Virtual/VirtualDtor.hpp"           // for implementVirtDtors
    8986
    9087static void NewPass( const char * const name ) {
     
    105102}
    106103
    107 // Helpers for checkInvariant:
    108 void checkInvariants( std::list< Declaration * > & ) {}
    109 using ast::checkInvariants;
    110 
    111 #define PASS( name, pass, unit, ... )       \
     104#define PASS( name, pass )                  \
    112105        if ( errorp ) { cerr << name << endl; } \
    113106        NewPass(name);                          \
    114107        Stats::Time::StartBlock(name);          \
    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         }
     108        pass;                                   \
     109        Stats::Time::StopBlock();
     110
     111LinkageSpec::Spec linkage = LinkageSpec::Cforall;
     112TypedefTable typedefTable;
     113DeclarationNode * parseTree = nullptr;                                  // program parse tree
    126114
    127115static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
     
    130118
    131119static void parse_cmdline( int argc, char * argv[] );
     120static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
    132121static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
    133122static void dump( ast::TranslationUnit && transUnit, ostream & out = cout );
     
    245234        ostream * output = & cout;
    246235        list< Declaration * > translationUnit;
    247         ast::TranslationUnit transUnit;
    248236
    249237        Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO );
     
    290278                        FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" );
    291279                        assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" );
    292                         parse( gcc_builtins, ast::Linkage::Compiler );
     280                        parse( gcc_builtins, LinkageSpec::Compiler );
    293281
    294282                        // read the extra prelude in, if not generating the cfa library
    295283                        FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" );
    296284                        assertf( extras, "cannot open extras.cf\n" );
    297                         parse( extras, ast::Linkage::BuiltinC );
     285                        parse( extras, LinkageSpec::BuiltinC );
    298286
    299287                        if ( ! libcfap ) {
     
    301289                                FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" );
    302290                                assertf( prelude, "cannot open prelude.cfa\n" );
    303                                 parse( prelude, ast::Linkage::Intrinsic );
     291                                parse( prelude, LinkageSpec::Intrinsic );
    304292
    305293                                // Read to cfa builtins, if not generating the cfa library
    306294                                FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" );
    307295                                assertf( builtins, "cannot open builtins.cf\n" );
    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 
     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 );
    318321                Stats::Time::StopBlock();
    319322
     
    322325                        ast::pass_visitor_stats.max = Stats::Counters::build<Stats::Counters::MaxCounter<double>>("Max depth - New");
    323326                }
    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 );
     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 ) );
    358369
    359370                if ( symtabp ) {
     
    366377                } // if
    367378
    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 );
     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 ) );
    375389
    376390                if ( libcfap ) {
     
    384398                } // if
    385399
    386                 DUMP( bresolvep, std::move( transUnit ) );
     400                if ( bresolvep ) {
     401                        dump( std::move( transUnit ) );
     402                        return EXIT_SUCCESS;
     403                } // if
    387404
    388405                if ( resolvprotop ) {
     
    391408                } // if
    392409
    393                 PASS( "Resolve", ResolvExpr::resolve, transUnit );
    394                 DUMP( exprp, std::move( transUnit ) );
    395 
    396                 PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() );
     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()));
    397419
    398420                // fix ObjectDecl - replaces ConstructorInit nodes
    399                 DUMP( ctorinitp, std::move( transUnit ) );
     421                if ( ctorinitp ) {
     422                        dump( std::move( transUnit ) );
     423                        return EXIT_SUCCESS;
     424                } // if
    400425
    401426                // Currently not working due to unresolved issues with UniqueExpr
    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 );
     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 ) );
    406431
    407432                // Needs to happen before tuple types are expanded.
    408                 PASS( "Convert Specializations",  GenPoly::convertSpecializations, transUnit );
    409 
    410                 PASS( "Expand Tuples", Tuples::expandTuples, transUnit );
    411                 DUMP( tuplep, std::move( transUnit ) );
     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
    412441
    413442                // Must come after Translate Tries.
    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 );
     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 ) );
    420452
    421453                translationUnit = convert( std::move( transUnit ) );
    422454
    423                 DUMP( bboxp, translationUnit );
    424                 PASS( "Box", GenPoly::box, translationUnit );
    425 
    426                 PASS( "Link-Once", CodeGen::translateLinkOnce, translationUnit );
     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 ) );
    427462
    428463                // Code has been lowered to C, now we can start generation.
    429464
    430                 DUMP( bcodegenp, translationUnit );
     465                if ( bcodegenp ) {
     466                        dump( translationUnit );
     467                        return EXIT_SUCCESS;
     468                } // if
    431469
    432470                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
     
    435473
    436474                CodeTools::fillLocations( translationUnit );
    437                 PASS( "Code Gen", CodeGen::generate, translationUnit, *output, ! genproto, prettycodegenp, true, linemarks );
     475                PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) );
    438476
    439477                CodeGen::FixMain::fix( translationUnit, *output,
     
    445483                if ( errorp ) {
    446484                        cerr << "---AST at error:---" << endl;
    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                         }
     485                        dump( translationUnit, cerr );
    454486                        cerr << endl << "---End of AST, begin error message:---\n" << endl;
    455487                } // if
    456488                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;
    457502                if ( output != &cout ) {
    458503                        delete output;
     
    483528
    484529
    485 static const char optstring[] = ":c:ghilLmNnpdP:S:twW:D:";
     530static const char optstring[] = ":c:ghlLmNnpdP:S:twW:D:";
    486531
    487532enum { PreludeDir = 128 };
     
    490535        { "gdb", no_argument, nullptr, 'g' },
    491536        { "help", no_argument, nullptr, 'h' },
    492         { "invariant", no_argument, nullptr, 'i' },
    493537        { "libcfa", no_argument, nullptr, 'l' },
    494538        { "linemarks", no_argument, nullptr, 'L' },
    495         { "no-main", no_argument, nullptr, 'm' },
     539        { "no-main", no_argument, 0, 'm' },
    496540        { "no-linemarks", no_argument, nullptr, 'N' },
    497541        { "no-prelude", no_argument, nullptr, 'n' },
     
    512556        "wait for gdb to attach",                                                       // -g
    513557        "print translator help message",                                        // -h
    514         "invariant checking during AST passes",                         // -i
    515558        "generate libcfa.c",                                                            // -l
    516559        "generate line marks",                                                          // -L
     
    544587        { "rproto", resolvprotop, true, "resolver-proto instance" },
    545588        { "rsteps", resolvep, true, "print resolver steps" },
     589        { "tree", parsep, true, "print parse tree" },
    546590        // code dumps
    547591        { "ast", astp, true, "print AST after parsing" },
     
    606650                        usage( argv );                                                          // no return
    607651                        break;
    608                   case 'i':                                                                             // invariant checking
    609                         invariant = true;
    610                         break;
    611652                  case 'l':                                                                             // generate libcfa.c
    612653                        libcfap = true;
     
    707748} // parse_cmdline
    708749
     750static 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
    709766static bool notPrelude( Declaration * decl ) {
    710767        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
Note: See TracChangeset for help on using the changeset viewer.