Changeset e6d39fe for src/GenPoly


Ignore:
Timestamp:
Apr 20, 2018, 9:04:41 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
Children:
22bdc34
Parents:
9181f1d (diff), 88f15ae (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/GenPoly
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r9181f1d re6d39fe  
    184184                        /// change the type of generic aggregate members to char[]
    185185                        void mutateMembers( AggregateDecl * aggrDecl );
     186                        /// returns the calculated sizeof expression for ty, or nullptr for use C sizeof()
     187                        Expression* genSizeof( Type* ty );
    186188
    187189                        /// Enters a new scope for type-variables, adding the type variables from ty
     
    382384                unsigned long n_members = 0;
    383385                bool firstMember = true;
    384                 for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) {
    385                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
     386                for ( Declaration* member : structDecl->get_members() ) {
     387                        DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( member );
    386388                        assert( dwt );
    387389                        Type *memberType = dwt->get_type();
     
    17471749                }
    17481750
     1751                Expression * PolyGenericCalculator::genSizeof( Type* ty ) {
     1752                        if ( ArrayType * aty = dynamic_cast<ArrayType *>(ty) ) {
     1753                                // generate calculated size for possibly generic array
     1754                                Expression * sizeofBase = genSizeof( aty->get_base() );
     1755                                if ( ! sizeofBase ) return nullptr;
     1756                                Expression * dim = aty->get_dimension();
     1757                                aty->set_dimension( nullptr );
     1758                                return makeOp( "?*?", sizeofBase, dim );
     1759                        } else if ( findGeneric( ty ) ) {
     1760                                // generate calculated size for generic type
     1761                                return new NameExpr( sizeofName( mangleType( ty ) ) );
     1762                        } else return nullptr;
     1763                }
     1764
    17491765                Expression *PolyGenericCalculator::postmutate( SizeofExpr *sizeofExpr ) {
    1750                         Type *ty = sizeofExpr->get_isType() ? sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
    1751                         if ( findGeneric( ty ) ) {
    1752                                 Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) );
     1766                        Type *ty = sizeofExpr->get_isType() ?
     1767                                sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
     1768                       
     1769                        Expression * gen = genSizeof( ty );
     1770                        if ( gen ) {
    17531771                                delete sizeofExpr;
    1754                                 return ret;
    1755                         }
    1756                         return sizeofExpr;
     1772                                return gen;
     1773                        } else return sizeofExpr;
    17571774                }
    17581775
  • src/GenPoly/InstantiateGeneric.cc

    r9181f1d re6d39fe  
    496496        Expression * FixDtypeStatic::fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr ) {
    497497                // need to cast dtype-static member expressions to their actual type before that type is erased.
     498                // NOTE: the casts here have the third argument (isGenerated) set to false so that these casts persist until Box, where they are needed.
    498499                auto & baseParams = *inst->get_baseParameters();
    499500                if ( isDtypeStatic( baseParams ) ) {
     
    515516                                        // Note: this currently creates more temporaries than is strictly necessary, since it does not check for duplicate uses of the same member expression.
    516517                                        static UniqueName tmpNamer( "_dtype_static_member_" );
    517                                         Expression * init = new CastExpr( new AddressExpr( memberExpr ), new PointerType( Type::Qualifiers(), concType->clone() ) );
     518                                        Expression * init = new CastExpr( new AddressExpr( memberExpr ), new PointerType( Type::Qualifiers(), concType->clone() ), false );
    518519                                        ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), new ReferenceType( Type::Qualifiers(), concType ), new SingleInit( init ) );
    519520                                        stmtsToAddBefore.push_back( new DeclStmt( tmp ) );
     
    521522                                } else {
    522523                                        // can simply add a cast to actual type
    523                                         return new CastExpr( memberExpr, concType );
     524                                        return new CastExpr( memberExpr, concType, false );
    524525                                }
    525526                        }
  • src/GenPoly/Lvalue.cc

    r9181f1d re6d39fe  
    9898                };
    9999
    100                 struct AddrRef final : public WithGuards {
     100                struct AddrRef final : public WithGuards, public WithVisitorRef<AddrRef>, public WithShortCircuiting {
    101101                        void premutate( AddressExpr * addrExpr );
    102102                        Expression * postmutate( AddressExpr * addrExpr );
    103103                        void premutate( Expression * expr );
     104                        void premutate( ApplicationExpr * appExpr );
     105                        void premutate( SingleInit * init );
     106
     107                        void handleNonAddr( Expression * );
    104108
    105109                        bool first = true;
    106110                        bool current = false;
    107111                        int refDepth = 0;
     112                        bool addCast = false;
    108113                };
    109114        } // namespace
     
    208213                                                if ( function->get_linkage() != LinkageSpec::Intrinsic && isIntrinsicReference( arg ) ) {
    209214                                                        // if argument is dereference or array subscript, the result isn't REALLY a reference, but non-intrinsic functions expect a reference: take address
     215
     216                                                        // NOTE: previously, this condition fixed
     217                                                        //   void f(int *&);
     218                                                        //   int & x = ...;
     219                                                        //   f(&x);
     220                                                        // But now this is taken care of by a reference cast added by AddrRef. Need to find a new
     221                                                        // example or remove this branch.
     222
    210223                                                        PRINT(
    211224                                                                std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
     
    233246
    234247                // idea: &&&E: get outer &, inner &
    235                 // at inner &, record depth D of reference type
     248                // at inner &, record depth D of reference type of argument of &
    236249                // at outer &, add D derefs.
    237                 void AddrRef::premutate( Expression * ) {
     250                void AddrRef::handleNonAddr( Expression * ) {
     251                        // non-address-of: reset status variables:
     252                        // * current expr is NOT the first address-of expr in an address-of chain
     253                        // * next seen address-of expr IS the first in the chain.
    238254                        GuardValue( current );
    239255                        GuardValue( first );
     
    242258                }
    243259
     260                void AddrRef::premutate( Expression * expr ) {
     261                        handleNonAddr( expr );
     262                        GuardValue( addCast );
     263                        addCast = false;
     264                }
     265
    244266                void AddrRef::premutate( AddressExpr * ) {
    245267                        GuardValue( current );
    246268                        GuardValue( first );
    247                         current = first;
    248                         first = false;
    249                         if ( current ) {
     269                        current = first; // is this the first address-of in the chain?
     270                        first = false;   // from here out, no longer possible for next address-of to be first in chain
     271                        if ( current ) { // this is the outermost address-of in a chain
    250272                                GuardValue( refDepth );
    251                                 refDepth = 0;
     273                                refDepth = 0;  // set depth to 0 so that postmutate can find the innermost address-of easily
    252274                        }
    253275                }
    254276
    255277                Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
     278                        PRINT( std::cerr << "addr ref at " << addrExpr << std::endl; )
    256279                        if ( refDepth == 0 ) {
     280                                PRINT( std::cerr << "depth 0, get new depth..." << std::endl; )
     281                                // this is the innermost address-of in a chain, record depth D
    257282                                if ( ! isIntrinsicReference( addrExpr->arg ) ) {
    258283                                        // try to avoid ?[?]
     284                                        // xxx - is this condition still necessary? intrinsicReferences should have a cast around them at this point, so I don't think this condition ever fires.
    259285                                        refDepth = addrExpr->arg->result->referenceDepth();
    260                                 }
    261                         }
    262                         if ( current ) {
     286                                        PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; )
     287                                } else {
     288                                        assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( addrExpr->arg ) );
     289                                }
     290                        }
     291                        if ( current ) { // this is the outermost address-of in a chain
     292                                PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; )
    263293                                Expression * ret = addrExpr;
    264294                                while ( refDepth ) {
     295                                        // add one dereference for each
    265296                                        ret = mkDeref( ret );
    266297                                        refDepth--;
    267298                                }
     299
     300                                if ( addCast ) {
     301                                        PRINT( std::cerr << "adding cast..." << std::endl; )
     302                                        return new CastExpr( ret, addrExpr->result->clone() );
     303                                }
    268304                                return ret;
    269305                        }
     306                        PRINT( std::cerr << "not current..." << std::endl; )
    270307                        return addrExpr;
    271308                }
     309
     310                void AddrRef::premutate( ApplicationExpr * appExpr ) {
     311                        visit_children = false;
     312                        GuardValue( addCast );
     313                        handleNonAddr( appExpr );
     314                        for ( Expression *& arg : appExpr->args ) {
     315                                // each argument with address-of requires a cast
     316                                addCast = true;
     317                                arg = arg->acceptMutator( *visitor );
     318                        }
     319                }
     320
     321                void AddrRef::premutate( SingleInit * ) {
     322                        GuardValue( addCast );
     323                        // each initialization context with address-of requires a cast
     324                        addCast = true;
     325                }
     326
    272327
    273328                Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
     
    376431                                assert( diff == 0 );
    377432                                // conversion between references of the same depth
     433                                if ( ResolvExpr::typesCompatible( castExpr->result, castExpr->arg->result, SymTab::Indexer() ) && castExpr->isGenerated ) {
     434                                        // Remove useless generated casts
     435                                        PRINT(
     436                                                std::cerr << "types are compatible, removing cast: " << castExpr << std::endl;
     437                                                std::cerr << "-- " << castExpr->result << std::endl;
     438                                                std::cerr << "-- " << castExpr->arg->result << std::endl;
     439                                        )
     440                                        Expression * ret = castExpr->arg;
     441                                        castExpr->arg = nullptr;
     442                                        std::swap( castExpr->env, ret->env );
     443                                        delete castExpr;
     444                                        return ret;
     445                                }
    378446                                return castExpr;
    379447                        }
Note: See TracChangeset for help on using the changeset viewer.