[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 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 |
|
---|
| 18 | #include "Lvalue.h"
|
---|
| 19 |
|
---|
[02ad3f5] | 20 | #include "GenPoly.h"
|
---|
| 21 |
|
---|
[51b73452] | 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"
|
---|
[51b73452] | 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"
|
---|
[51b73452] | 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 |
|
---|
[51b73452] | 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
|
---|
[cb43451] | 243 | PRINT( std::cerr << "convert reference to rvalue -- *" << std::endl; )
|
---|
| 244 | PRINT( std::cerr << "was = " << castExpr << std::endl; )
|
---|
[9191a8e] | 245 | Expression * ret = castExpr->get_arg();
|
---|
| 246 | if ( ! isIntrinsicReference( ret ) ) {
|
---|
| 247 | // dereference if not already dereferenced
|
---|
| 248 | ret = mkDeref( ret );
|
---|
| 249 | }
|
---|
| 250 | ret->set_env( castExpr->get_env() );
|
---|
[9a34b5a] | 251 | castExpr->set_arg( nullptr );
|
---|
[1d776fd] | 252 | castExpr->set_env( nullptr );
|
---|
[9a34b5a] | 253 | delete castExpr;
|
---|
[9191a8e] | 254 | PRINT( std::cerr << "now: " << ret << std::endl; )
|
---|
| 255 | return ret;
|
---|
[1d776fd] | 256 | }
|
---|
| 257 | return castExpr;
|
---|
[01aeade] | 258 | }
|
---|
[b6fd751] | 259 |
|
---|
[1d776fd] | 260 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
|
---|
| 261 | Type * base = refType->get_base();
|
---|
| 262 | refType->set_base( nullptr );
|
---|
| 263 | delete refType;
|
---|
| 264 | return new PointerType( Type::Qualifiers(), base );
|
---|
[ce8c12f] | 265 | }
|
---|
| 266 |
|
---|
[1d776fd] | 267 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
|
---|
[b6fd751] | 268 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
|
---|
| 269 | Expression * arg1 = commaExpr->get_arg1()->clone();
|
---|
| 270 | Expression * arg2 = commaExpr->get_arg2()->clone();
|
---|
| 271 | delete addrExpr;
|
---|
| 272 | return new CommaExpr( arg1, new AddressExpr( arg2 ) );
|
---|
| 273 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
|
---|
| 274 | Expression * arg1 = condExpr->get_arg1()->clone();
|
---|
| 275 | Expression * arg2 = condExpr->get_arg2()->clone();
|
---|
| 276 | Expression * arg3 = condExpr->get_arg3()->clone();
|
---|
| 277 | delete addrExpr;
|
---|
| 278 | return new ConditionalExpr( arg1, new AddressExpr( arg2 ), new AddressExpr( arg3 ) );
|
---|
| 279 | }
|
---|
| 280 | return addrExpr;
|
---|
| 281 | }
|
---|
[cb43451] | 282 |
|
---|
| 283 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addressExpr ) {
|
---|
| 284 | Expression * arg = addressExpr->get_arg();
|
---|
| 285 | if ( isIntrinsicReference( arg ) ) {
|
---|
| 286 | std::string fname = InitTweak::getFunctionName( arg );
|
---|
| 287 | if ( fname == "*?" ) {
|
---|
| 288 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
|
---|
| 289 | Expression * ret = arg0;
|
---|
| 290 | ret->set_env( addressExpr->get_env() );
|
---|
| 291 | arg0 = nullptr;
|
---|
| 292 | addressExpr->set_env( nullptr );
|
---|
| 293 | delete addressExpr;
|
---|
| 294 | return ret;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | return addressExpr;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
|
---|
| 301 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
| 302 | std::string fname = InitTweak::getFunctionName( appExpr );
|
---|
| 303 | if ( fname == "*?" ) {
|
---|
| 304 | Expression * arg = InitTweak::getCallArg( appExpr, 0 );
|
---|
| 305 | // xxx - this isn't right, because it can remove casts that should be there...
|
---|
| 306 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
|
---|
| 307 | // arg = castExpr->get_arg();
|
---|
| 308 | // }
|
---|
| 309 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
|
---|
| 310 | Expression * ret = addrExpr->get_arg();
|
---|
| 311 | ret->set_env( appExpr->get_env() );
|
---|
| 312 | addrExpr->set_arg( nullptr );
|
---|
| 313 | appExpr->set_env( nullptr );
|
---|
| 314 | delete appExpr;
|
---|
| 315 | return ret;
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | return appExpr;
|
---|
| 320 | }
|
---|
[01aeade] | 321 | } // namespace
|
---|
[51b73452] | 322 | } // namespace GenPoly
|
---|
[01aeade] | 323 |
|
---|
[51587aa] | 324 | // Local Variables: //
|
---|
| 325 | // tab-width: 4 //
|
---|
| 326 | // mode: c++ //
|
---|
| 327 | // compile-command: "make install" //
|
---|
| 328 | // End: //
|
---|