[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 ); |
---|
[01aeade] | 68 | }; |
---|
| 69 | |
---|
[1d776fd] | 70 | /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced. |
---|
| 71 | struct FixIntrinsicArgs final { |
---|
| 72 | Expression * postmutate( ApplicationExpr *appExpr ); |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /// Replace reference types with pointer types |
---|
| 77 | struct ReferenceTypeElimination final { |
---|
| 78 | Type * postmutate( ReferenceType * refType ); |
---|
[01aeade] | 79 | }; |
---|
[b6fd751] | 80 | |
---|
| 81 | /// GCC-like Generalized Lvalues (which have since been removed from GCC) |
---|
| 82 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues |
---|
| 83 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c) |
---|
[1d776fd] | 84 | struct GeneralizedLvalue final { |
---|
| 85 | Expression * postmutate( AddressExpr * addressExpr ); |
---|
[b6fd751] | 86 | }; |
---|
[cb43451] | 87 | |
---|
| 88 | /// Removes redundant &*/*& pattern that this pass can generate |
---|
| 89 | struct CollapseAddrDeref final { |
---|
| 90 | Expression * postmutate( AddressExpr * addressExpr ); |
---|
| 91 | Expression * postmutate( ApplicationExpr * appExpr ); |
---|
| 92 | }; |
---|
[01aeade] | 93 | } // namespace |
---|
| 94 | |
---|
| 95 | void convertLvalue( std::list< Declaration* >& translationUnit ) { |
---|
[1d776fd] | 96 | std::cerr << "convertLvalue" << std::endl; |
---|
| 97 | PassVisitor<ReferenceConversions> refCvt; |
---|
| 98 | PassVisitor<ReferenceTypeElimination> elim; |
---|
| 99 | PassVisitor<GeneralizedLvalue> genLval; |
---|
| 100 | PassVisitor<FixIntrinsicArgs> fixer; |
---|
[cb43451] | 101 | PassVisitor<CollapseAddrDeref> collapser; |
---|
[1d776fd] | 102 | mutateAll( translationUnit, refCvt ); |
---|
| 103 | mutateAll( translationUnit, fixer ); |
---|
| 104 | mutateAll( translationUnit, elim ); |
---|
[b6fd751] | 105 | mutateAll( translationUnit, genLval ); |
---|
[cb43451] | 106 | mutateAll( translationUnit, collapser ); |
---|
[01aeade] | 107 | } |
---|
| 108 | |
---|
| 109 | namespace { |
---|
[02ad3f5] | 110 | Type* isLvalueRet( FunctionType *function ) { |
---|
| 111 | if ( function->get_returnVals().empty() ) return 0; |
---|
| 112 | Type *ty = function->get_returnVals().front()->get_type(); |
---|
[1d776fd] | 113 | return dynamic_cast< ReferenceType * >( ty ) ; |
---|
[01aeade] | 114 | } |
---|
| 115 | |
---|
| 116 | bool isIntrinsicApp( ApplicationExpr *appExpr ) { |
---|
| 117 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) { |
---|
| 118 | return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic; |
---|
| 119 | } else { |
---|
| 120 | return false; |
---|
| 121 | } // if |
---|
| 122 | } |
---|
| 123 | |
---|
[1d776fd] | 124 | bool isDeref( Expression * expr ) { |
---|
| 125 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 126 | return InitTweak::getFunctionName( untyped ) == "*?"; |
---|
| 127 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 128 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) { |
---|
| 129 | return func->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getFunctionName( appExpr ) == "*?"; |
---|
[01aeade] | 130 | } |
---|
[1d776fd] | 131 | } |
---|
| 132 | return false; |
---|
[ce8c12f] | 133 | } |
---|
| 134 | |
---|
[1d776fd] | 135 | bool isIntrinsicReference( Expression * expr ) { |
---|
| 136 | if ( isDeref( expr ) ) return true; |
---|
| 137 | else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
| 138 | return InitTweak::getFunctionName( untyped ) == "?[?]"; |
---|
| 139 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
| 140 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) { |
---|
| 141 | return func->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getFunctionName( appExpr ) == "?[?]"; |
---|
| 142 | } |
---|
[ce8c12f] | 143 | } |
---|
[1d776fd] | 144 | return false; |
---|
[ce8c12f] | 145 | } |
---|
| 146 | |
---|
[1d776fd] | 147 | void fixArg( Expression *& arg, Type * formal ) { |
---|
[9a34b5a] | 148 | if ( dynamic_cast<ReferenceType*>( formal ) ) { |
---|
| 149 | // if the parameter is a reference, add a dereference to the reference-typed argument. |
---|
| 150 | Type * baseType = InitTweak::getPointerBase( arg->get_result() ); |
---|
| 151 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->get_result() ) ); |
---|
| 152 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() ); |
---|
| 153 | delete arg->get_result(); |
---|
| 154 | arg->set_result( ptrType ); |
---|
[cb43451] | 155 | arg = mkDeref( arg ); |
---|
[9a34b5a] | 156 | } |
---|
[1d776fd] | 157 | } |
---|
[01aeade] | 158 | |
---|
[cb43451] | 159 | // xxx - might need to & every * (or every * that is an arg to non-intrinsic function??) |
---|
[1d776fd] | 160 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) { |
---|
[9a34b5a] | 161 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments. |
---|
[1d776fd] | 162 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) { |
---|
[9a34b5a] | 163 | if ( function->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
[1d776fd] | 164 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() ); |
---|
| 165 | assertf( ftype, "Function declaration does not have function type." ); |
---|
| 166 | for ( auto p : group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) { |
---|
| 167 | Expression *& arg = std::get<0>( p ); |
---|
| 168 | DeclarationWithType * formal = std::get<1>( p ); |
---|
[cb43451] | 169 | PRINT( |
---|
| 170 | std::cerr << "pair<0>: " << arg << std::endl; |
---|
| 171 | std::cerr << "pair<1>: " << formal->get_type() << std::endl; |
---|
| 172 | ) |
---|
[9a34b5a] | 173 | if ( isIntrinsicReference( arg ) ) { // intrinsic functions that turn pointers into references |
---|
| 174 | // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument |
---|
[cb43451] | 175 | PRINT( std::cerr << "skipping intrinsic reference" << std::endl; ) |
---|
[1d776fd] | 176 | continue; |
---|
| 177 | } else { |
---|
| 178 | fixArg( arg, formal->get_type() ); |
---|
| 179 | } |
---|
| 180 | } |
---|
[baba5d8] | 181 | } |
---|
[1d776fd] | 182 | } |
---|
| 183 | return appExpr; |
---|
[01aeade] | 184 | } |
---|
| 185 | |
---|
[1d776fd] | 186 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) { |
---|
[9a34b5a] | 187 | // xxx - is it possible to convert directly between reference types with a different base? E.g., |
---|
| 188 | // int x; |
---|
| 189 | // (double&)x; |
---|
| 190 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant |
---|
| 191 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct |
---|
| 192 | // pointer casts in the right places. |
---|
| 193 | |
---|
[1d776fd] | 194 | // conversion to reference type |
---|
| 195 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) { |
---|
| 196 | (void)refType; |
---|
| 197 | if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) { |
---|
| 198 | // nothing to do if casting from reference to reference. |
---|
| 199 | (void)otherRef; |
---|
[cb43451] | 200 | PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; ) |
---|
[1d776fd] | 201 | if ( isIntrinsicReference( castExpr->get_arg() ) ) { |
---|
| 202 | Expression * callExpr = castExpr->get_arg(); |
---|
[cb43451] | 203 | PRINT( |
---|
| 204 | std::cerr << "but arg is deref -- &" << std::endl; |
---|
| 205 | std::cerr << callExpr << std::endl; |
---|
| 206 | ) |
---|
[1d776fd] | 207 | // move environment out to new top-level |
---|
| 208 | callExpr->set_env( castExpr->get_env() ); |
---|
[9a34b5a] | 209 | castExpr->set_arg( nullptr ); |
---|
[1d776fd] | 210 | castExpr->set_env( nullptr ); |
---|
[9a34b5a] | 211 | delete castExpr; |
---|
[1d776fd] | 212 | return callExpr; |
---|
| 213 | } |
---|
[9a34b5a] | 214 | assertf( false, "non-intrinsic reference with cast of reference to reference not yet supported: ", toString( castExpr ) ); |
---|
[cb43451] | 215 | PRINT( std::cerr << castExpr << std::endl; ) |
---|
[1d776fd] | 216 | return castExpr; |
---|
| 217 | } else if ( castExpr->get_arg()->get_result()->get_lvalue() ) { |
---|
| 218 | // conversion from lvalue to reference |
---|
| 219 | // xxx - keep cast, but turn into pointer cast?? |
---|
| 220 | // xxx - memory |
---|
[cb43451] | 221 | PRINT( |
---|
| 222 | std::cerr << "convert lvalue to reference -- &" << std::endl; |
---|
| 223 | std::cerr << castExpr->get_arg() << std::endl; |
---|
| 224 | ) |
---|
| 225 | AddressExpr * ret = new AddressExpr( castExpr->get_arg() ); |
---|
| 226 | if ( refType->get_base()->get_qualifiers() != castExpr->get_arg()->get_result()->get_qualifiers() ) { |
---|
| 227 | // must keep cast if cast-to type is different from the actual type |
---|
| 228 | castExpr->set_arg( ret ); |
---|
| 229 | |
---|
| 230 | return castExpr; |
---|
| 231 | } |
---|
[9a34b5a] | 232 | ret->set_env( castExpr->get_env() ); |
---|
| 233 | castExpr->set_env( nullptr ); |
---|
| 234 | castExpr->set_arg( nullptr ); |
---|
| 235 | delete castExpr; |
---|
| 236 | return ret; |
---|
[01aeade] | 237 | } else { |
---|
[1d776fd] | 238 | // rvalue to reference conversion -- introduce temporary |
---|
| 239 | } |
---|
| 240 | assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() ); |
---|
| 241 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) { |
---|
[9a34b5a] | 242 | // conversion from reference to rvalue |
---|
[1d776fd] | 243 | // should be easy, just need to move deref code up here? |
---|
[cb43451] | 244 | PRINT( std::cerr << "convert reference to rvalue -- *" << std::endl; ) |
---|
[1d776fd] | 245 | if ( isIntrinsicReference( castExpr->get_arg() ) ) { |
---|
[cb43451] | 246 | PRINT( std::cerr << "but arg is intrinsic reference -- nop" << std::endl; ) |
---|
[1d776fd] | 247 | return castExpr; |
---|
| 248 | } |
---|
[cb43451] | 249 | PRINT( std::cerr << "was = " << castExpr << std::endl; ) |
---|
[1d776fd] | 250 | |
---|
[9a34b5a] | 251 | Expression * deref = mkDeref( castExpr->get_arg() ); |
---|
[1d776fd] | 252 | deref->set_env( castExpr->get_env() ); |
---|
[9a34b5a] | 253 | castExpr->set_arg( nullptr ); |
---|
[1d776fd] | 254 | castExpr->set_env( nullptr ); |
---|
[9a34b5a] | 255 | delete castExpr; |
---|
[cb43451] | 256 | PRINT( std::cerr << "now: " << deref << std::endl; ) |
---|
[1d776fd] | 257 | return deref; |
---|
| 258 | } |
---|
| 259 | return castExpr; |
---|
[01aeade] | 260 | } |
---|
[b6fd751] | 261 | |
---|
[1d776fd] | 262 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) { |
---|
| 263 | Type * base = refType->get_base(); |
---|
| 264 | refType->set_base( nullptr ); |
---|
| 265 | delete refType; |
---|
| 266 | return new PointerType( Type::Qualifiers(), base ); |
---|
[ce8c12f] | 267 | } |
---|
| 268 | |
---|
[1d776fd] | 269 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) { |
---|
[b6fd751] | 270 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) { |
---|
| 271 | Expression * arg1 = commaExpr->get_arg1()->clone(); |
---|
| 272 | Expression * arg2 = commaExpr->get_arg2()->clone(); |
---|
| 273 | delete addrExpr; |
---|
| 274 | return new CommaExpr( arg1, new AddressExpr( arg2 ) ); |
---|
| 275 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) { |
---|
| 276 | Expression * arg1 = condExpr->get_arg1()->clone(); |
---|
| 277 | Expression * arg2 = condExpr->get_arg2()->clone(); |
---|
| 278 | Expression * arg3 = condExpr->get_arg3()->clone(); |
---|
| 279 | delete addrExpr; |
---|
| 280 | return new ConditionalExpr( arg1, new AddressExpr( arg2 ), new AddressExpr( arg3 ) ); |
---|
| 281 | } |
---|
| 282 | return addrExpr; |
---|
| 283 | } |
---|
[cb43451] | 284 | |
---|
| 285 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addressExpr ) { |
---|
| 286 | Expression * arg = addressExpr->get_arg(); |
---|
| 287 | if ( isIntrinsicReference( arg ) ) { |
---|
| 288 | std::string fname = InitTweak::getFunctionName( arg ); |
---|
| 289 | if ( fname == "*?" ) { |
---|
| 290 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 ); |
---|
| 291 | Expression * ret = arg0; |
---|
| 292 | ret->set_env( addressExpr->get_env() ); |
---|
| 293 | arg0 = nullptr; |
---|
| 294 | addressExpr->set_env( nullptr ); |
---|
| 295 | delete addressExpr; |
---|
| 296 | return ret; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | return addressExpr; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) { |
---|
| 303 | if ( isIntrinsicReference( appExpr ) ) { |
---|
| 304 | std::string fname = InitTweak::getFunctionName( appExpr ); |
---|
| 305 | if ( fname == "*?" ) { |
---|
| 306 | Expression * arg = InitTweak::getCallArg( appExpr, 0 ); |
---|
| 307 | // xxx - this isn't right, because it can remove casts that should be there... |
---|
| 308 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) { |
---|
| 309 | // arg = castExpr->get_arg(); |
---|
| 310 | // } |
---|
| 311 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) { |
---|
| 312 | Expression * ret = addrExpr->get_arg(); |
---|
| 313 | ret->set_env( appExpr->get_env() ); |
---|
| 314 | addrExpr->set_arg( nullptr ); |
---|
| 315 | appExpr->set_env( nullptr ); |
---|
| 316 | delete appExpr; |
---|
| 317 | return ret; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | return appExpr; |
---|
| 322 | } |
---|
[01aeade] | 323 | } // namespace |
---|
[51b7345] | 324 | } // namespace GenPoly |
---|
[01aeade] | 325 | |
---|
[51587aa] | 326 | // Local Variables: // |
---|
| 327 | // tab-width: 4 // |
---|
| 328 | // mode: c++ // |
---|
| 329 | // compile-command: "make install" // |
---|
| 330 | // End: // |
---|