Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Lvalue.cc

    r8ca3a72 r1d776fd  
    3232#include "Common/UniqueName.h"
    3333#include "Common/utility.h"
     34#include "InitTweak/InitTweak.h"
     35
     36#include "Common/PassVisitor.h"
     37
     38// need to be careful about polymorphic references... e.g. in *? (___operator_deref__A0_1_0_0__Fd0_Pd0_intrinsic___1)
     39// the variable is automatically dereferenced and this causes errors dereferencing void*.
    3440
    3541namespace GenPoly {
    3642        namespace {
    37                 /// Replace uses of lvalue returns with appropriate pointers
    38                 class Pass1 : public Mutator {
    39                   public:
    40                         Pass1();
    41 
    42                         virtual Expression *mutate( ApplicationExpr *appExpr );
    43                         virtual Statement *mutate( ReturnStmt *appExpr );
    44                         virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
    45                   private:
    46                         DeclarationWithType* retval;
    47                 };
    48 
    49                 /// Replace declarations of lvalue returns with appropriate pointers
    50                 class Pass2 : public Visitor {
    51                   public:
    52                         virtual void visit( FunctionType *funType );
    53                   private:
     43                struct ReferenceConversions final {
     44                        Expression * postmutate( CastExpr * castExpr );
     45                };
     46
     47                /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
     48                struct FixIntrinsicArgs final {
     49                        Expression * postmutate( ApplicationExpr *appExpr );
     50                };
     51
     52
     53                /// Replace reference types with pointer types
     54                struct ReferenceTypeElimination final {
     55                        Type * postmutate( ReferenceType * refType );
    5456                };
    5557
     
    5759                /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
    5860                /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
    59                 class GeneralizedLvalue : public Mutator {
    60                         typedef Mutator Parent;
    61 
    62                         virtual Expression * mutate( AddressExpr * addressExpr );
     61                struct GeneralizedLvalue final {
     62                        Expression * postmutate( AddressExpr * addressExpr );
    6363                };
    6464        } // namespace
    6565
    6666        void convertLvalue( std::list< Declaration* >& translationUnit ) {
    67                 Pass1 p1;
    68                 Pass2 p2;
    69                 GeneralizedLvalue genLval;
    70                 mutateAll( translationUnit, p1 );
    71                 acceptAll( translationUnit, p2 );
     67                std::cerr << "convertLvalue" << std::endl;
     68                PassVisitor<ReferenceConversions> refCvt;
     69                PassVisitor<ReferenceTypeElimination> elim;
     70                PassVisitor<GeneralizedLvalue> genLval;
     71                PassVisitor<FixIntrinsicArgs> fixer;
     72                mutateAll( translationUnit, refCvt );
     73                mutateAll( translationUnit, fixer );
     74                mutateAll( translationUnit, elim );
    7275                mutateAll( translationUnit, genLval );
    7376        }
     
    7780                        if ( function->get_returnVals().empty() ) return 0;
    7881                        Type *ty = function->get_returnVals().front()->get_type();
    79                         return ty->get_lvalue() ? ty : 0;
     82                        return dynamic_cast< ReferenceType * >( ty ) ;
    8083                }
    8184
     
    8891                }
    8992
    90                 Pass1::Pass1() {
    91                 }
    92 
    93                 DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) {
    94                         if ( funcDecl->get_statements() ) {
    95                                 DeclarationWithType* oldRetval = retval;
    96                                 retval = 0;
    97                                 if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
    98                                         retval = funcDecl->get_functionType()->get_returnVals().front();
    99                                 }
    100                                 // fix expressions and return statements in this function
    101                                 funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
    102                                 retval = oldRetval;
    103                         } // if
    104                         return funcDecl;
    105                 }
    106 
    107                 Expression * Pass1::mutate( ApplicationExpr *appExpr ) {
    108                         appExpr->get_function()->acceptMutator( *this );
    109                         mutateAll( appExpr->get_args(), *this );
    110 
    111                         PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
    112                         FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
    113 
    114                         Type *funType = isLvalueRet( function );
    115                         if ( funType && ! isIntrinsicApp( appExpr ) ) {
    116                                 Expression *expr = appExpr;
    117                                 Type *appType = appExpr->get_result();
    118                                 if ( isPolyType( funType ) && ! isPolyType( appType ) ) {
    119                                         // make sure cast for polymorphic type is inside dereference
    120                                         expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
    121                                 }
    122                                 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    123                                 deref->set_result( appType->clone() );
    124                                 appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) );
    125                                 deref->get_args().push_back( expr );
     93                bool isDeref( Expression * expr ) {
     94                        if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
     95                                return InitTweak::getFunctionName( untyped ) == "*?";
     96                        } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
     97                                if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
     98                                        return func->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getFunctionName( appExpr ) == "*?";
     99                                }
     100                        }
     101                        return false;
     102                }
     103
     104                bool isIntrinsicReference( Expression * expr ) {
     105                        if ( isDeref( expr ) ) return true;
     106                        else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
     107                                return InitTweak::getFunctionName( untyped ) == "?[?]";
     108                        } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
     109                                if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
     110                                        return func->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getFunctionName( appExpr ) == "?[?]";
     111                                }
     112                        }
     113                        return false;
     114                }
     115
     116                void fixArg( Expression *& arg, Type * formal ) {
     117                        // if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
     118                                if ( dynamic_cast<ReferenceType*>( formal ) ) { // xxx - but might be deref, in which case result isn't REALLY a reference, at least not in the sense that we need to add another deref...
     119                                        // doesn't work, for some reason left arg is skipped in assign
     120                                        ReferenceType * refType = safe_dynamic_cast< ReferenceType * >( arg->get_result() ) ;
     121                                        std::cerr << "appexpr arg is non-deref/index intrinsic call" << std::endl;
     122                                        std::cerr << arg << std::endl;
     123                                        PointerType * ptrType = new PointerType( Type::Qualifiers(), refType->get_base()->clone() );
     124                                        delete refType;
     125                                        arg->set_result( ptrType );
     126                                        arg = UntypedExpr::createDeref( arg );
     127                                }
     128
     129                        // }
     130                }
     131
     132                Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
     133                        if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
     134                                if ( function->get_linkage() == LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
     135                                        FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
     136                                        assertf( ftype, "Function declaration does not have function type." );
     137                                        for ( auto p : group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) {
     138                                                Expression *& arg = std::get<0>( p );
     139                                                DeclarationWithType * formal = std::get<1>( p );
     140                                                std::cerr << "pair<0>: " << arg << std::endl;
     141                                                std::cerr << "pair<1>: " << formal->get_type() << std::endl;
     142                                                if ( isIntrinsicReference( arg ) ) {
     143                                                        std::cerr << "skipping intrinsic reference" << std::endl;
     144                                                        continue;
     145                                                } else {
     146                                                        fixArg( arg, formal->get_type() );
     147                                                }
     148                                        }
     149                                }
     150                        }
     151                        return appExpr;
     152                }
     153
     154                Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
     155                        // conversion to reference type
     156                        if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) {
     157                                (void)refType;
     158                                if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
     159                                        // nothing to do if casting from reference to reference.
     160                                        (void)otherRef;
     161                                        std::cerr << "convert reference to reference -- nop" << std::endl;
     162                                        if ( isIntrinsicReference( castExpr->get_arg() ) ) {
     163                                                Expression * callExpr = castExpr->get_arg();
     164                                                Expression ** arg = nullptr;
     165                                                Expression *& arg0 = InitTweak::getCallArg( callExpr, 0 );
     166                                                if ( dynamic_cast<PointerType *>( arg0->get_result() ) ) {
     167                                                        arg = &arg0;
     168                                                } else {
     169                                                        arg = &InitTweak::getCallArg( callExpr, 1 );
     170                                                }
     171
     172                                                castExpr->set_arg( *arg );
     173                                                *arg = castExpr;
     174                                                std::cerr << "but arg is deref -- &" << std::endl;
     175                                                std::cerr << callExpr << std::endl;
     176                                                // castExpr->set_arg( new AddressExpr( castExpr->get_arg() ) );
     177
     178                                                // move environment out to new top-level
     179                                                callExpr->set_env( castExpr->get_env() );
     180                                                castExpr->set_env( nullptr );
     181                                                return callExpr;
     182                                        }
     183                                        std::cerr << castExpr << std::endl;
     184                                        return castExpr;
     185                                } else if ( castExpr->get_arg()->get_result()->get_lvalue() ) {
     186                                        // conversion from lvalue to reference
     187                                        // xxx - keep cast, but turn into pointer cast??
     188                                        // xxx - memory
     189                                        std::cerr << "convert lvalue to reference -- &" << std::endl;
     190                                        std::cerr << castExpr->get_arg() << std::endl;
     191                                        castExpr->set_arg( new AddressExpr( castExpr->get_arg() ) );
     192                                        // return new AddressExpr( castExpr->get_arg() );
     193                                        return castExpr;
     194                                } else {
     195                                        // rvalue to reference conversion -- introduce temporary
     196                                }
     197                                assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() );
     198                        } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
     199                                // should be easy, just need to move deref code up here?
     200                                std::cerr << "convert reference to rvalue -- *" << std::endl;
     201                                if ( isIntrinsicReference( castExpr->get_arg() ) ) {
     202                                        std::cerr << "but arg is intrinsic reference -- nop" << std::endl;
     203                                        return castExpr;
     204                                }
     205                                std::cerr << castExpr << std::endl;
     206
     207                                PointerType * ptrType = new PointerType( refType->get_qualifiers(), refType->get_base()->clone() );
     208                                delete castExpr->get_result();
     209                                castExpr->set_result( ptrType );
     210                                Expression * deref = UntypedExpr::createDeref( castExpr );
     211                                deref->set_env( castExpr->get_env() );
     212                                castExpr->set_env( nullptr );
    126213                                return deref;
    127                         } else {
    128                                 return appExpr;
    129                         } // if
    130                 }
    131 
    132                 Statement * Pass1::mutate(ReturnStmt *retStmt) {
    133                         if ( retval && retStmt->get_expr() ) {
    134                                 if ( retStmt->get_expr()->get_result()->get_lvalue() ) {
    135                                         // ***** Code Removal ***** because casts may be stripped already
    136 
    137                                         // strip casts because not allowed to take address of cast
    138                                         // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
    139                                         //      retStmt->set_expr( castExpr->get_arg() );
    140                                         //      retStmt->get_expr()->set_env( castExpr->get_env() );
    141                                         //      castExpr->set_env( 0 );
    142                                         //      castExpr->set_arg( 0 );
    143                                         //      delete castExpr;
    144                                         // } // while
    145                                         retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
    146                                 } else {
    147                                         throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
    148                                 } // if
    149                         } // if
    150                         return retStmt;
    151                 }
    152 
    153                 void Pass2::visit( FunctionType *funType ) {
    154                         std::string typeName;
    155                         if ( isLvalueRet( funType ) ) {
    156                                 DeclarationWithType *retParm = funType->get_returnVals().front();
    157 
    158                                 // make a new parameter that is a pointer to the type of the old return value
    159                                 retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
    160                         } // if
    161 
    162                         Visitor::visit( funType );
    163                 }
    164 
    165                 Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
    166                         addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
     214                                // assertf( false, "Conversions from reference types are not currently supported." );
     215                        }
     216                        return castExpr;
     217                }
     218
     219                Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
     220                        Type * base = refType->get_base();
     221                        refType->set_base( nullptr );
     222                        delete refType;
     223                        return new PointerType( Type::Qualifiers(), base );
     224                }
     225
     226                Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
    167227                        if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
    168228                                Expression * arg1 = commaExpr->get_arg1()->clone();
Note: See TracChangeset for help on using the changeset viewer.