[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[906e24d] | 7 | // Lvalue.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr |
---|
[615a096] | 12 | // Last Modified On : Fri Mar 17 09:11:18 2017 |
---|
| 13 | // Update Count : 5 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <cassert> |
---|
| 17 | |
---|
| 18 | #include "Lvalue.h" |
---|
| 19 | |
---|
[02ad3f5] | 20 | #include "GenPoly.h" |
---|
| 21 | |
---|
[51b7345] | 22 | #include "SynTree/Declaration.h" |
---|
| 23 | #include "SynTree/Type.h" |
---|
| 24 | #include "SynTree/Expression.h" |
---|
| 25 | #include "SynTree/Statement.h" |
---|
| 26 | #include "SynTree/Visitor.h" |
---|
| 27 | #include "SynTree/Mutator.h" |
---|
| 28 | #include "SymTab/Indexer.h" |
---|
[9a34b5a] | 29 | #include "SymTab/Autogen.h" |
---|
[51b7345] | 30 | #include "ResolvExpr/Resolver.h" |
---|
| 31 | #include "ResolvExpr/typeops.h" |
---|
| 32 | |
---|
[d3b7937] | 33 | #include "Common/UniqueName.h" |
---|
| 34 | #include "Common/utility.h" |
---|
[ce8c12f] | 35 | #include "InitTweak/InitTweak.h" |
---|
[51b7345] | 36 | |
---|
[1d776fd] | 37 | #include "Common/PassVisitor.h" |
---|
| 38 | |
---|
| 39 | // need to be careful about polymorphic references... e.g. in *? (___operator_deref__A0_1_0_0__Fd0_Pd0_intrinsic___1) |
---|
| 40 | // the variable is automatically dereferenced and this causes errors dereferencing void*. |
---|
| 41 | |
---|
[cb43451] | 42 | #if 0 |
---|
| 43 | #define PRINT(x) x |
---|
| 44 | #else |
---|
| 45 | #define PRINT(x) |
---|
| 46 | #endif |
---|
| 47 | |
---|
[51b7345] | 48 | namespace GenPoly { |
---|
[01aeade] | 49 | namespace { |
---|
[9a34b5a] | 50 | // TODO: fold this into the general createDeref function?? |
---|
| 51 | Expression * mkDeref( Expression * arg ) { |
---|
| 52 | if ( SymTab::dereferenceOperator ) { |
---|
| 53 | VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator ); |
---|
| 54 | deref->set_result( new PointerType( Type::Qualifiers(), deref->get_result() ) ); |
---|
| 55 | Type * base = InitTweak::getPointerBase( arg->get_result() ); |
---|
| 56 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->get_result() ).c_str() ); |
---|
| 57 | ApplicationExpr * ret = new ApplicationExpr( deref, { arg } ); |
---|
| 58 | delete ret->get_result(); |
---|
| 59 | ret->set_result( new ReferenceType( Type::Qualifiers(), base->clone() ) ); |
---|
| 60 | return ret; |
---|
| 61 | } else { |
---|
| 62 | return UntypedExpr::createDeref( arg ); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
[1d776fd] | 66 | struct ReferenceConversions final { |
---|
| 67 | Expression * postmutate( CastExpr * castExpr ); |
---|
[8a6cf7e] | 68 | Expression * postmutate( AddressExpr * addrExpr ); |
---|
[01aeade] | 69 | }; |
---|
| 70 | |
---|
[1d776fd] | 71 | /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced. |
---|
| 72 | struct FixIntrinsicArgs final { |
---|
| 73 | Expression * postmutate( ApplicationExpr *appExpr ); |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /// Replace reference types with pointer types |
---|
| 78 | struct ReferenceTypeElimination final { |
---|
| 79 | Type * postmutate( ReferenceType * refType ); |
---|
[01aeade] | 80 | }; |
---|
[b6fd751] | 81 | |
---|
| 82 | /// GCC-like Generalized Lvalues (which have since been removed from GCC) |
---|
| 83 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues |
---|
| 84 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c) |
---|
[d335627] | 85 | struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> { |
---|
[1d776fd] | 86 | Expression * postmutate( AddressExpr * addressExpr ); |
---|
[b6fd751] | 87 | }; |
---|
[cb43451] | 88 | |
---|
| 89 | /// Removes redundant &*/*& pattern that this pass can generate |
---|
| 90 | struct CollapseAddrDeref final { |
---|
| 91 | Expression * postmutate( AddressExpr * addressExpr ); |
---|
| 92 | Expression * postmutate( ApplicationExpr * appExpr ); |
---|
| 93 | }; |
---|
[01aeade] | 94 | } // namespace |
---|
| 95 | |
---|
[b0440b7] | 96 | static bool referencesEliminated = false; |
---|
| 97 | // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type. |
---|
| 98 | bool referencesPermissable() { |
---|
| 99 | return ! referencesEliminated; |
---|
| 100 | } |
---|
| 101 | |
---|
[01aeade] | 102 | void convertLvalue( std::list< Declaration* >& translationUnit ) { |
---|
[1d776fd] | 103 | std::cerr << "convertLvalue" << std::endl; |
---|
| 104 | PassVisitor<ReferenceConversions> refCvt; |
---|
| 105 | PassVisitor<ReferenceTypeElimination> elim; |
---|
| 106 | PassVisitor<GeneralizedLvalue> genLval; |
---|
| 107 | PassVisitor<FixIntrinsicArgs> fixer; |
---|
[cb43451] | 108 | PassVisitor<CollapseAddrDeref> collapser; |
---|
[1d776fd] | 109 | mutateAll( translationUnit, refCvt ); |
---|
| 110 | mutateAll( translationUnit, fixer ); |
---|
[b6fd751] | 111 | mutateAll( translationUnit, genLval ); |
---|
[cb43451] | 112 | mutateAll( translationUnit, collapser ); |
---|
[8a6cf7e] | 113 | mutateAll( translationUnit, elim ); // last because other passes need reference types to work |
---|
[b0440b7] | 114 | |
---|
| 115 | // from this point forward, no other pass should create reference types. |
---|
| 116 | referencesEliminated = true; |
---|
[01aeade] | 117 | } |
---|
| 118 | |
---|
| 119 | namespace { |
---|
[8a6cf7e] | 120 | // true for intrinsic function calls that return a reference |
---|
[1d776fd] | 121 | bool isIntrinsicReference( Expression * expr ) { |
---|
[8a6cf7e] | 122 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 123 | std::string fname = InitTweak::getFunctionName( untyped ); |
---|
| 124 | // known intrinsic-reference prelude functions |
---|
| 125 | return fname == "*?" || fname == "?[?]"; |
---|
[1d776fd] | 126 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 127 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) { |
---|
[8a6cf7e] | 128 | // use type of return variable rather than expr result type, since it may have been changed to a pointer type |
---|
| 129 | FunctionType * ftype = GenPoly::getFunctionType( func->get_type() ); |
---|
| 130 | Type * ret = ftype->get_returnVals().empty() ? nullptr : ftype->get_returnVals().front()->get_type(); |
---|
| 131 | return func->get_linkage() == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret ); |
---|
[1d776fd] | 132 | } |
---|
[ce8c12f] | 133 | } |
---|
[1d776fd] | 134 | return false; |
---|
[ce8c12f] | 135 | } |
---|
| 136 | |
---|
[cb43451] | 137 | // xxx - might need to & every * (or every * that is an arg to non-intrinsic function??) |
---|
[1d776fd] | 138 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) { |
---|
[9a34b5a] | 139 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments. |
---|
[1d776fd] | 140 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) { |
---|
[d335627] | 141 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() ); |
---|
| 142 | assertf( ftype, "Function declaration does not have function type." ); |
---|
| 143 | // can be of differing lengths only when function is variadic |
---|
| 144 | assertf( ftype->get_parameters().size() == appExpr->get_args().size() || ftype->get_isVarArgs(), "ApplicationExpr args do not match formal parameter type." ); |
---|
[b0440b7] | 145 | |
---|
| 146 | if ( isIntrinsicReference( appExpr ) ) { |
---|
| 147 | // eliminate reference types from intrinsic applications - now they return lvalues |
---|
| 148 | appExpr->set_result( appExpr->get_result()->stripReferences() ); |
---|
| 149 | appExpr->get_result()->set_lvalue( true ); |
---|
| 150 | } |
---|
| 151 | |
---|
[d335627] | 152 | unsigned int i = 0; |
---|
| 153 | const unsigned int end = ftype->get_parameters().size(); |
---|
| 154 | for ( auto p : unsafe_group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) { |
---|
| 155 | if (i == end) break; |
---|
| 156 | Expression *& arg = std::get<0>( p ); |
---|
| 157 | Type * formal = std::get<1>( p )->get_type(); |
---|
| 158 | PRINT( |
---|
| 159 | std::cerr << "pair<0>: " << arg << std::endl; |
---|
| 160 | std::cerr << "pair<1>: " << formal << std::endl; |
---|
| 161 | ) |
---|
| 162 | if ( dynamic_cast<ReferenceType*>( formal ) ) { |
---|
| 163 | if ( isIntrinsicReference( arg ) ) { |
---|
| 164 | if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references |
---|
| 165 | // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument |
---|
| 166 | PRINT( std::cerr << "is intrinsic arg in non-intrinsic call - adding address" << std::endl; ) |
---|
| 167 | arg = new AddressExpr( arg ); |
---|
| 168 | } |
---|
| 169 | } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
| 170 | // if the parameter is a reference, add a dereference to the reference-typed argument. |
---|
| 171 | Type * baseType = InitTweak::getPointerBase( arg->get_result() ); |
---|
| 172 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->get_result() ) ); |
---|
| 173 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() ); |
---|
| 174 | delete arg->get_result(); |
---|
| 175 | arg->set_result( ptrType ); |
---|
| 176 | arg = mkDeref( arg ); |
---|
[1d776fd] | 177 | } |
---|
| 178 | } |
---|
[d335627] | 179 | ++i; |
---|
[baba5d8] | 180 | } |
---|
[1d776fd] | 181 | } |
---|
| 182 | return appExpr; |
---|
[01aeade] | 183 | } |
---|
| 184 | |
---|
[8a6cf7e] | 185 | Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) { |
---|
| 186 | // Inner expression may have been lvalue to reference conversion, which becomes an address expression. |
---|
| 187 | // In this case, remove the outer address expression and return the argument. |
---|
| 188 | // TODO: It's possible that this might catch too much and require a more sophisticated check. |
---|
| 189 | if ( dynamic_cast<AddressExpr*>( addrExpr->get_arg() ) ) { |
---|
| 190 | Expression * arg = addrExpr->get_arg(); |
---|
| 191 | arg->set_env( addrExpr->get_env() ); |
---|
| 192 | addrExpr->set_arg( nullptr ); |
---|
| 193 | addrExpr->set_env( nullptr ); |
---|
| 194 | delete addrExpr; |
---|
| 195 | return arg; |
---|
| 196 | } |
---|
| 197 | return addrExpr; |
---|
| 198 | } |
---|
| 199 | |
---|
[1d776fd] | 200 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) { |
---|
[9a34b5a] | 201 | // xxx - is it possible to convert directly between reference types with a different base? E.g., |
---|
| 202 | // int x; |
---|
| 203 | // (double&)x; |
---|
| 204 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant |
---|
| 205 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct |
---|
| 206 | // pointer casts in the right places. |
---|
| 207 | |
---|
[1d776fd] | 208 | // conversion to reference type |
---|
| 209 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) { |
---|
| 210 | (void)refType; |
---|
| 211 | if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) { |
---|
| 212 | // nothing to do if casting from reference to reference. |
---|
| 213 | (void)otherRef; |
---|
[cb43451] | 214 | PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; ) |
---|
[1d776fd] | 215 | if ( isIntrinsicReference( castExpr->get_arg() ) ) { |
---|
| 216 | Expression * callExpr = castExpr->get_arg(); |
---|
[cb43451] | 217 | PRINT( |
---|
| 218 | std::cerr << "but arg is deref -- &" << std::endl; |
---|
| 219 | std::cerr << callExpr << std::endl; |
---|
| 220 | ) |
---|
[1d776fd] | 221 | // move environment out to new top-level |
---|
| 222 | callExpr->set_env( castExpr->get_env() ); |
---|
[9a34b5a] | 223 | castExpr->set_arg( nullptr ); |
---|
[1d776fd] | 224 | castExpr->set_env( nullptr ); |
---|
[9a34b5a] | 225 | delete castExpr; |
---|
[1d776fd] | 226 | return callExpr; |
---|
| 227 | } |
---|
[9a34b5a] | 228 | assertf( false, "non-intrinsic reference with cast of reference to reference not yet supported: ", toString( castExpr ) ); |
---|
[cb43451] | 229 | PRINT( std::cerr << castExpr << std::endl; ) |
---|
[1d776fd] | 230 | return castExpr; |
---|
| 231 | } else if ( castExpr->get_arg()->get_result()->get_lvalue() ) { |
---|
| 232 | // conversion from lvalue to reference |
---|
| 233 | // xxx - keep cast, but turn into pointer cast?? |
---|
| 234 | // xxx - memory |
---|
[cb43451] | 235 | PRINT( |
---|
| 236 | std::cerr << "convert lvalue to reference -- &" << std::endl; |
---|
| 237 | std::cerr << castExpr->get_arg() << std::endl; |
---|
| 238 | ) |
---|
| 239 | AddressExpr * ret = new AddressExpr( castExpr->get_arg() ); |
---|
| 240 | if ( refType->get_base()->get_qualifiers() != castExpr->get_arg()->get_result()->get_qualifiers() ) { |
---|
| 241 | // must keep cast if cast-to type is different from the actual type |
---|
| 242 | castExpr->set_arg( ret ); |
---|
| 243 | |
---|
| 244 | return castExpr; |
---|
| 245 | } |
---|
[9a34b5a] | 246 | ret->set_env( castExpr->get_env() ); |
---|
| 247 | castExpr->set_env( nullptr ); |
---|
| 248 | castExpr->set_arg( nullptr ); |
---|
| 249 | delete castExpr; |
---|
| 250 | return ret; |
---|
[01aeade] | 251 | } else { |
---|
[1d776fd] | 252 | // rvalue to reference conversion -- introduce temporary |
---|
| 253 | } |
---|
| 254 | assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() ); |
---|
| 255 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) { |
---|
[d335627] | 256 | (void)refType; |
---|
[9a34b5a] | 257 | // conversion from reference to rvalue |
---|
[8a6cf7e] | 258 | PRINT( |
---|
| 259 | std::cerr << "convert reference to rvalue -- *" << std::endl; |
---|
| 260 | std::cerr << "was = " << castExpr << std::endl; |
---|
| 261 | ) |
---|
[9191a8e] | 262 | Expression * ret = castExpr->get_arg(); |
---|
[8a6cf7e] | 263 | TypeSubstitution * env = castExpr->get_env(); |
---|
| 264 | castExpr->set_env( nullptr ); |
---|
[9191a8e] | 265 | if ( ! isIntrinsicReference( ret ) ) { |
---|
| 266 | // dereference if not already dereferenced |
---|
| 267 | ret = mkDeref( ret ); |
---|
| 268 | } |
---|
[8a6cf7e] | 269 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( castExpr->get_result(), castExpr->get_arg()->get_result()->stripReferences(), SymTab::Indexer() ) ) { |
---|
[b0440b7] | 270 | // can remove cast if types are compatible, changing expression type to value type |
---|
| 271 | ret->set_result( castExpr->get_result()->clone() ); |
---|
[8a6cf7e] | 272 | castExpr->set_arg( nullptr ); |
---|
| 273 | delete castExpr; |
---|
| 274 | } else { |
---|
| 275 | // must keep cast if types are different |
---|
| 276 | castExpr->set_arg( ret ); |
---|
| 277 | ret = castExpr; |
---|
| 278 | } |
---|
| 279 | ret->set_env( env ); |
---|
[9191a8e] | 280 | PRINT( std::cerr << "now: " << ret << std::endl; ) |
---|
| 281 | return ret; |
---|
[1d776fd] | 282 | } |
---|
| 283 | return castExpr; |
---|
[01aeade] | 284 | } |
---|
[b6fd751] | 285 | |
---|
[1d776fd] | 286 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) { |
---|
| 287 | Type * base = refType->get_base(); |
---|
[8a6cf7e] | 288 | Type::Qualifiers qualifiers = refType->get_qualifiers(); |
---|
[1d776fd] | 289 | refType->set_base( nullptr ); |
---|
| 290 | delete refType; |
---|
[8a6cf7e] | 291 | return new PointerType( qualifiers, base ); |
---|
[ce8c12f] | 292 | } |
---|
| 293 | |
---|
[1d776fd] | 294 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) { |
---|
[b6fd751] | 295 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) { |
---|
| 296 | Expression * arg1 = commaExpr->get_arg1()->clone(); |
---|
| 297 | Expression * arg2 = commaExpr->get_arg2()->clone(); |
---|
| 298 | delete addrExpr; |
---|
[d335627] | 299 | return new CommaExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ) ); |
---|
[b6fd751] | 300 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) { |
---|
| 301 | Expression * arg1 = condExpr->get_arg1()->clone(); |
---|
| 302 | Expression * arg2 = condExpr->get_arg2()->clone(); |
---|
| 303 | Expression * arg3 = condExpr->get_arg3()->clone(); |
---|
| 304 | delete addrExpr; |
---|
[d335627] | 305 | return new ConditionalExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ), (new AddressExpr( arg3 ))->acceptMutator( *visitor ) ); |
---|
[b6fd751] | 306 | } |
---|
| 307 | return addrExpr; |
---|
| 308 | } |
---|
[cb43451] | 309 | |
---|
| 310 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addressExpr ) { |
---|
| 311 | Expression * arg = addressExpr->get_arg(); |
---|
| 312 | if ( isIntrinsicReference( arg ) ) { |
---|
| 313 | std::string fname = InitTweak::getFunctionName( arg ); |
---|
| 314 | if ( fname == "*?" ) { |
---|
| 315 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 ); |
---|
| 316 | Expression * ret = arg0; |
---|
| 317 | ret->set_env( addressExpr->get_env() ); |
---|
| 318 | arg0 = nullptr; |
---|
| 319 | addressExpr->set_env( nullptr ); |
---|
| 320 | delete addressExpr; |
---|
| 321 | return ret; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | return addressExpr; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) { |
---|
| 328 | if ( isIntrinsicReference( appExpr ) ) { |
---|
| 329 | std::string fname = InitTweak::getFunctionName( appExpr ); |
---|
| 330 | if ( fname == "*?" ) { |
---|
| 331 | Expression * arg = InitTweak::getCallArg( appExpr, 0 ); |
---|
| 332 | // xxx - this isn't right, because it can remove casts that should be there... |
---|
| 333 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) { |
---|
| 334 | // arg = castExpr->get_arg(); |
---|
| 335 | // } |
---|
| 336 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) { |
---|
| 337 | Expression * ret = addrExpr->get_arg(); |
---|
| 338 | ret->set_env( appExpr->get_env() ); |
---|
| 339 | addrExpr->set_arg( nullptr ); |
---|
| 340 | appExpr->set_env( nullptr ); |
---|
| 341 | delete appExpr; |
---|
| 342 | return ret; |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | return appExpr; |
---|
| 347 | } |
---|
[01aeade] | 348 | } // namespace |
---|
[51b7345] | 349 | } // namespace GenPoly |
---|
[01aeade] | 350 | |
---|
[51587aa] | 351 | // Local Variables: // |
---|
| 352 | // tab-width: 4 // |
---|
| 353 | // mode: c++ // |
---|
| 354 | // compile-command: "make install" // |
---|
| 355 | // End: // |
---|