Changeset d60ccbf for src/GenPoly


Ignore:
Timestamp:
Aug 12, 2015, 2:27:31 PM (10 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
f32c7f4
Parents:
e45215c (diff), e869d663 (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 designate

Conflicts:

src/CodeGen/CodeGenerator.h

Location:
src/GenPoly
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    re45215c rd60ccbf  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jun 24 16:19:07 2015
    13 // Update Count     : 10
     12// Last Modified On : Tue Aug 11 16:22:35 2015
     13// Update Count     : 89
    1414//
    1515
     
    7777                        Expression *handleIntrinsics( ApplicationExpr *appExpr );
    7878                        ObjectDecl *makeTemporary( Type *type );
    79  
     79
     80                        typedef std::map< std::string, FunctionDecl *> AdapterMap;
    8081                        std::map< std::string, DeclarationWithType *> assignOps;
    81                         typedef std::map< std::string, FunctionDecl *> AdapterMap;
    8282                        std::stack< AdapterMap > adapters;
    8383                        DeclarationWithType *retval;
     
    170170                }
    171171
     172                bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
     173                        std::string dummyString;
     174                        return isPolyRet( function, dummyString, otherTyVars );
     175                }
     176
    172177                Pass1::Pass1()
    173178                        : useRetval( false ), tempNamer( "_temp" ) {
     
    204209                }
    205210
    206                 DeclarationWithType *
    207                 Pass1::mutate( FunctionDecl *functionDecl ) {
     211                DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
    208212                        if ( functionDecl->get_statements() ) {
    209213                                TyVarMap oldtyVars = scopeTyVars;
     
    527531                }
    528532
    529                 void Pass1::passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ) {
     533                void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
     534                        // collect a list of function types passed as parameters or implicit parameters (assertions)
    530535                        std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
    531536                        std::list< FunctionType *> functions;
     
    538543                                findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
    539544                        } // for
     545
     546                        // parameter function types for which an appropriate adapter has been generated.
     547                        // we cannot use the types after applying substitutions, since two different
     548                        // parameter types may be unified to the same type
    540549                        std::set< std::string > adaptersDone;
     550
    541551                        for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
     552                                FunctionType *originalFunction = (*funType)->clone();
    542553                                FunctionType *realFunction = (*funType)->clone();
    543                                 assert( env );
    544                                 env->apply( realFunction );
    545 
    546554                                std::string mangleName = SymTab::Mangler::mangle( realFunction );
     555
     556                                // only attempt to create an adapter or pass one as a parameter if we haven't
     557                                // already done so for this pre-substitution parameter function type.
    547558                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
    548                                         AdapterMap & adapters = Pass1::adapters.top();
    549                                         AdapterMap::iterator adapter = adapters.find( mangleName );
     559                                        std::string mangleName = SymTab::Mangler::mangle( realFunction );
     560                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
     561                                       
     562                                        // apply substitution to type variables to figure out what the
     563                                        // adapter's type should look like
     564                                        assert( env );
     565                                        env->apply( realFunction );
     566                                        mangleName = SymTab::Mangler::mangle( realFunction );
    550567
    551568                                        if ( needsAdapter( realFunction, exprTyVars, true ) ) {
     
    554571                                                // create a new adapter.
    555572                                                appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
    556                                                 continue;
    557                                         } else if ( adapter == adapters.end() ) {
    558                                                 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
    559                                                 adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
    560                                                 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
     573                                        } else {
     574                                                if ( isPolyRet( originalFunction, exprTyVars ) ) {
     575                                                        // if the return type involved polymorphic types, then
     576                                                        // the adapter will need to take those polymorphic types
     577                                                        // as pointers. Therefore, there can be two different
     578                                                        // functions with the same mangled name, so we need two adapter map
     579                                                        // stacks and also we need the mangled names to be different.
     580                                                        mangleName += "polyret_";
     581                                                }
     582
     583                                                AdapterMap & adapters = Pass1::adapters.top();
     584                                                AdapterMap::iterator adapter = adapters.find( mangleName );
     585                                                if ( adapter == adapters.end() ) {
     586                                                        // adapter has not been created yet in the current scope, so define it
     587                                                        FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
     588                                                        adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
     589                                                        stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
     590                                                } // if
     591                                                assert( adapter != adapters.end() );
     592
     593                                                // add the appropriate adapter as a parameter
     594                                                appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
    561595                                        } // if
    562                                         assert( adapter != adapters.end() );
    563                                         appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
    564                                         // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
    565                                         adaptersDone.insert( adaptersDone.begin(), mangleName );
    566596                                } // if
    567597                        } // for
     
    881911
    882912                void Pass1::doBeginScope() {
     913                        // actually, maybe this could (should?) push
     914                        // a copy of the current map
    883915                        adapters.push(AdapterMap());
    884916                }
     
    10521084                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    10531085                                if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) {
     1086                                        // change initialization of a polymorphic value object
     1087                                        // to allocate storage with alloca
    10541088                                        TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() );
    10551089                                        assert( typeInst );
    10561090                                        UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    10571091                                        alloc->get_args().push_back( new NameExpr( typeInst->get_name() ) );
    1058                                         UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
    1059                                         assign->get_args().push_back( new VariableExpr( objectDecl ) );
    1060                                         assign->get_args().push_back( alloc );
    1061                                         stmtsToAddAfter.push_back( new ExprStmt( noLabels, assign ) );
     1092
     1093                                        delete objectDecl->get_init();
     1094
     1095                                        std::list<Expression*> designators;
     1096                                        objectDecl->set_init( new SingleInit( alloc, designators ) );
    10621097                                }
    10631098                        }
  • src/GenPoly/Box.h

    re45215c rd60ccbf  
    2121
    2222namespace GenPoly {
     23        /// boxes polymorphic function calls
    2324        void box( std::list< Declaration* >& translationUnit );
    2425} // namespace GenPoly
  • src/GenPoly/CopyParams.h

    re45215c rd60ccbf  
    2020
    2121namespace GenPoly {
     22        /// Clones by-value parameters which have been passed by-reference for polymorphism
    2223        void copyParams( std::list< Declaration* > &translationUnit );
    2324} // namespace GenPoly
  • src/GenPoly/Lvalue.h

    re45215c rd60ccbf  
    2222
    2323namespace GenPoly {
     24        /// replaces return type of `lvalue T` with `T*`, along with appropriate address-of and dereference operators
    2425        void convertLvalue( std::list< Declaration* >& translationUnit );
    2526} // namespace GenPoly
  • src/GenPoly/Specialize.h

    re45215c rd60ccbf  
    2222
    2323namespace GenPoly {
     24        /// generates thunks where needed
    2425        void convertSpecializations( std::list< Declaration* >& translationUnit );
    2526} // namespace GenPoly
Note: See TracChangeset for help on using the changeset viewer.