Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rb4cd03b7 r2a4b088  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Fri Feb 05 12:23:10 2016
    13 // Update Count     : 280
     12// Last Modified On : Fri Dec 18 14:53:08 2015
     13// Update Count     : 217
    1414//
    1515
     
    123123                };
    124124
    125                 /// Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference
     125                /// Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference;
     126                /// also fixes offsetof expressions.
    126127                class MemberExprFixer : public PolyMutator {
    127128                  public:
     
    136137                        virtual Type *mutate( FunctionType *funcType );
    137138                        virtual Expression *mutate( MemberExpr *memberExpr );
     139                        virtual Expression *mutate( OffsetofExpr *offsetofExpr );
    138140                };
    139 
     141               
    140142                /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable
    141143                class Pass3 : public PolyMutator {
     
    176178                                                seenIntrinsic = false; // break on this line when debugging for end of prelude
    177179                                        }
    178 
     180                                       
    179181                                        *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) );
    180182                                        assert( *i );
     
    284286                                }
    285287                        }
    286 
     288                       
    287289                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    288290                                doBeginScope();
     
    318320                                        findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
    319321                                } // for
    320 
     322                               
    321323                                AdapterMap & adapters = Pass1::adapters.top();
    322324                                for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
     
    371373                Expression *Pass1::makeOffsetArray( StructInstType *ty ) {
    372374                        std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members();
    373 
     375                       
    374376                        // make a new temporary array
    375377                        Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     
    395397                        return new VariableExpr( arrayTemp );
    396398                }
    397 
     399               
    398400                void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
    399401                        // pass size/align for type variables
     
    491493                        }
    492494                }
    493 
     495               
    494496                Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) {
    495497                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
     
    529531                        std::string adapterName = makeAdapterName( mangleName );
    530532
    531                         // cast adaptee to void (*)(), since it may have any type inside a polymorphic function
    532                         Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) );
    533                         appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) );
     533                        appExpr->get_args().push_front( appExpr->get_function() );
    534534                        appExpr->set_function( new NameExpr( adapterName ) );
    535535
     
    559559                }
    560560
    561                 /// cast parameters to polymorphic functions so that types are replaced with
    562                 /// void * if they are type parameters in the formal type.
    563                 /// this gets rid of warnings from gcc.
    564561                void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
    565                         Type * newType = formal->clone();
    566                         if ( getFunctionType( newType ) ) {
    567                                 newType = ScrubTyVars::scrub( newType, tyVars );
     562                        Type *newType = formal->clone();
     563                        std::list< FunctionType *> functions;
     564                        // instead of functions needing adapters, this really ought to look for
     565                        // any function mentioning a polymorphic type
     566                        findAndReplaceFunction( newType, functions, tyVars, needsAdapter );
     567                        if ( ! functions.empty() ) {
    568568                                actual = new CastExpr( actual, newType );
     569                        } else {
     570                                delete newType;
    569571                        } // if
    570572                }
     
    12831285                }
    12841286
     1287                /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present
     1288                long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) {
     1289                        long i = 0;
     1290                        for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) {
     1291                                if ( memberDecl->get_name() != (*decl)->get_name() ) continue;
     1292
     1293                                if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) {
     1294                                        if ( memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
     1295                                        else continue;
     1296                                } else return i;
     1297                        }
     1298                        return -1;
     1299                }
     1300
     1301                /// Returns an index expression into the offset array for a type
     1302                Expression *makeOffsetIndex( Type *objectType, long i ) {
     1303                        std::stringstream offset_namer;
     1304                        offset_namer << i;
     1305                        ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
     1306                        UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
     1307                        fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) );
     1308                        fieldOffset->get_args().push_back( fieldIndex );
     1309                        return fieldOffset;
     1310                }
     1311
     1312                /// Returns an expression dereferenced n times
     1313                Expression *makeDerefdVar( Expression *derefdVar, long n ) {
     1314                        for ( int i = 1; i < n; ++i ) {
     1315                                UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
     1316                                derefExpr->get_args().push_back( derefdVar );
     1317                                derefdVar = derefExpr;
     1318                        }
     1319                        return derefdVar;
     1320                }
     1321               
    12851322                Expression *MemberExprFixer::mutate( MemberExpr *memberExpr ) {
    12861323                        // mutate, exiting early if no longer MemberExpr
     
    13011338                        if ( ! objectType ) return memberExpr;
    13021339
    1303                         // get base aggregate for type so members can be looked up
    1304                         AggregateDecl *memberBase = 0;
    13051340                        if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) {
    1306                                 memberBase = structType->get_baseStruct();
     1341                                // look up offset index
     1342                                long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() );
     1343                                if ( i == -1 ) return memberExpr;
     1344
     1345                                // replace member expression with pointer to base plus offset
     1346                                UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) );
     1347                                fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) );
     1348                                fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) );
     1349
     1350                                delete memberExpr;
     1351                                return fieldLoc;
    13071352                        } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType ) ) {
    1308                                 memberBase = unionType->get_baseUnion();
     1353                                // union members are all at offset zero, so build appropriately-dereferenced variable
     1354                                Expression *derefdVar = makeDerefdVar( varExpr->clone(), varDepth );
     1355                                delete memberExpr;
     1356                                return derefdVar;
    13091357                        } else return memberExpr;
    1310 
    1311                         // look up numeric index of member in base aggregate
    1312                         DeclarationWithType *memberDecl = memberExpr->get_member();
    1313                         std::list< Declaration* > &baseDecls = memberBase->get_members();
    1314                         std::list< Declaration* >::const_iterator decl = baseDecls.begin();
    1315                         unsigned long i = 0;
    1316                         for( ; decl != baseDecls.end(); ++decl, ++i ) {
    1317                                 if ( memberDecl->get_name() != (*decl)->get_name() ) continue;
    1318 
    1319                                 if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) {
    1320                                         if ( memberDecl->get_mangleName() == declWithType->get_mangleName() ) break;
    1321                                         else continue;
    1322                                 } else break;
    1323                         }
    1324                         if ( decl == baseDecls.end() ) return memberExpr;
    1325 
    1326                         // replace member expression with pointer to base plus offset
    1327                         // get offset for field
    1328                         std::stringstream offset_namer;
    1329                         offset_namer << i;
    1330                         ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
    1331                         UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
    1332                         fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) );
    1333                         fieldOffset->get_args().push_back( fieldIndex );
    1334                         // build appropriately-dereferenced variable
    1335                         Expression *derefdVar = varExpr->clone();
    1336                         for ( int i = 1; i < varDepth; ++i ) {
    1337                                 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
    1338                                 derefExpr->get_args().push_back( derefdVar );
    1339                                 derefdVar = derefExpr;
    1340                         }
    1341                         // add offset to deref'd variable
    1342                         UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) );
    1343                         fieldLoc->get_args().push_back( derefdVar );
    1344                         fieldLoc->get_args().push_back( fieldOffset );
    1345 
    1346                         delete memberExpr;
    1347                         return fieldLoc;
     1358                }
     1359
     1360                Expression *MemberExprFixer::mutate( OffsetofExpr *offsetofExpr ) {
     1361                        // mutate, exiting early if no longer OffsetofExpr
     1362                        Expression *expr = Mutator::mutate( offsetofExpr );
     1363                        offsetofExpr = dynamic_cast< OffsetofExpr* >( expr );
     1364                        if ( ! offsetofExpr ) return expr;
     1365
     1366                        // only mutate expressions for polymorphic structs/unions
     1367                        Type *ty = isPolyType( offsetofExpr->get_type(), scopeTyVars );
     1368                        if ( ! ty ) return offsetofExpr;
     1369
     1370                        if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) {
     1371                                // replace offsetof expression by index into offset array
     1372                                long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() );
     1373                                if ( i == -1 ) return offsetofExpr;
     1374
     1375                                Expression *offsetInd = makeOffsetIndex( ty, i );
     1376                                delete offsetofExpr;
     1377                                return offsetInd;
     1378                        } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( ty ) ) {
     1379                                // all union members are at offset zero
     1380                                delete offsetofExpr;
     1381                                return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::string("0") ) );
     1382                        } else return offsetofExpr;
    13481383                }
    13491384
Note: See TracChangeset for help on using the changeset viewer.