source: src/GenPoly/Lvalue.cc @ d335627

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d335627 was d335627, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add address-of to intrinsic reference-returning functions that are arguments to non-intrinsic functions

  • Property mode set to 100644
File size: 12.9 KB
Line 
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//
7// Lvalue.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 17 09:11:18 2017
13// Update Count     : 5
14//
15
16#include <cassert>
17
18#include "Lvalue.h"
19
20#include "GenPoly.h"
21
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"
29#include "SymTab/Autogen.h"
30#include "ResolvExpr/Resolver.h"
31#include "ResolvExpr/typeops.h"
32
33#include "Common/UniqueName.h"
34#include "Common/utility.h"
35#include "InitTweak/InitTweak.h"
36
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
42#if 0
43#define PRINT(x) x
44#else
45#define PRINT(x)
46#endif
47
48namespace GenPoly {
49        namespace {
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
66                struct ReferenceConversions final {
67                        Expression * postmutate( CastExpr * castExpr );
68                };
69
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 );
79                };
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)
84                struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
85                        Expression * postmutate( AddressExpr * addressExpr );
86                };
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                };
93        } // namespace
94
95        void convertLvalue( std::list< Declaration* >& translationUnit ) {
96                std::cerr << "convertLvalue" << std::endl;
97                PassVisitor<ReferenceConversions> refCvt;
98                PassVisitor<ReferenceTypeElimination> elim;
99                PassVisitor<GeneralizedLvalue> genLval;
100                PassVisitor<FixIntrinsicArgs> fixer;
101                PassVisitor<CollapseAddrDeref> collapser;
102                mutateAll( translationUnit, refCvt );
103                mutateAll( translationUnit, fixer );
104                mutateAll( translationUnit, elim );
105                mutateAll( translationUnit, genLval );
106                mutateAll( translationUnit, collapser );
107        }
108
109        namespace {
110                Type* isLvalueRet( FunctionType *function ) {
111                        if ( function->get_returnVals().empty() ) return 0;
112                        Type *ty = function->get_returnVals().front()->get_type();
113                        return dynamic_cast< ReferenceType * >( ty ) ;
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
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 ) == "*?";
130                                }
131                        }
132                        return false;
133                }
134
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                                }
143                        }
144                        return false;
145                }
146
147                // xxx - might need to & every * (or every * that is an arg to non-intrinsic function??)
148                Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
149                        // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
150                        if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
151                                FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
152                                assertf( ftype, "Function declaration does not have function type." );
153                                // can be of differing lengths only when function is variadic
154                                assertf( ftype->get_parameters().size() == appExpr->get_args().size() || ftype->get_isVarArgs(), "ApplicationExpr args do not match formal parameter type." );
155                                unsigned int i = 0;
156                                const unsigned int end = ftype->get_parameters().size();
157                                for ( auto p : unsafe_group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) {
158                                        if (i == end) break;
159                                        Expression *& arg = std::get<0>( p );
160                                        Type * formal = std::get<1>( p )->get_type();
161                                        PRINT(
162                                                std::cerr << "pair<0>: " << arg << std::endl;
163                                                std::cerr << "pair<1>: " << formal << std::endl;
164                                        )
165                                        if ( dynamic_cast<ReferenceType*>( formal ) ) {
166                                                if ( isIntrinsicReference( arg ) ) {
167                                                        if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
168                                                                // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument
169                                                                PRINT( std::cerr << "is intrinsic arg in non-intrinsic call - adding address" << std::endl; )
170                                                                arg = new AddressExpr( arg );
171                                                        }
172                                                } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
173                                                        // if the parameter is a reference, add a dereference to the reference-typed argument.
174                                                        Type * baseType = InitTweak::getPointerBase( arg->get_result() );
175                                                        assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->get_result() ) );
176                                                        PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
177                                                        delete arg->get_result();
178                                                        arg->set_result( ptrType );
179                                                        arg = mkDeref( arg );
180                                                }
181                                        }
182                                        ++i;
183                                }
184                        }
185                        return appExpr;
186                }
187
188                Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
189                        // xxx - is it possible to convert directly between reference types with a different base? E.g.,
190                        //   int x;
191                        //   (double&)x;
192                        // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
193                        // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
194                        // pointer casts in the right places.
195
196                        // conversion to reference type
197                        if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) {
198                                (void)refType;
199                                if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
200                                        // nothing to do if casting from reference to reference.
201                                        (void)otherRef;
202                                        PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; )
203                                        if ( isIntrinsicReference( castExpr->get_arg() ) ) {
204                                                Expression * callExpr = castExpr->get_arg();
205                                                PRINT(
206                                                        std::cerr << "but arg is deref -- &" << std::endl;
207                                                        std::cerr << callExpr << std::endl;
208                                                )
209                                                // move environment out to new top-level
210                                                callExpr->set_env( castExpr->get_env() );
211                                                castExpr->set_arg( nullptr );
212                                                castExpr->set_env( nullptr );
213                                                delete castExpr;
214                                                return callExpr;
215                                        }
216                                        assertf( false, "non-intrinsic reference with cast of reference to reference not yet supported: ", toString( castExpr ) );
217                                        PRINT( std::cerr << castExpr << std::endl; )
218                                        return castExpr;
219                                } else if ( castExpr->get_arg()->get_result()->get_lvalue() ) {
220                                        // conversion from lvalue to reference
221                                        // xxx - keep cast, but turn into pointer cast??
222                                        // xxx - memory
223                                        PRINT(
224                                                std::cerr << "convert lvalue to reference -- &" << std::endl;
225                                                std::cerr << castExpr->get_arg() << std::endl;
226                                        )
227                                        AddressExpr * ret = new AddressExpr( castExpr->get_arg() );
228                                        if ( refType->get_base()->get_qualifiers() != castExpr->get_arg()->get_result()->get_qualifiers() ) {
229                                                // must keep cast if cast-to type is different from the actual type
230                                                castExpr->set_arg( ret );
231
232                                                return castExpr;
233                                        }
234                                        ret->set_env( castExpr->get_env() );
235                                        castExpr->set_env( nullptr );
236                                        castExpr->set_arg( nullptr );
237                                        delete castExpr;
238                                        return ret;
239                                } else {
240                                        // rvalue to reference conversion -- introduce temporary
241                                }
242                                assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() );
243                        } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
244                                (void)refType;
245                                // conversion from reference to rvalue
246                                PRINT( std::cerr << "convert reference to rvalue -- *" << std::endl; )
247                                PRINT( std::cerr << "was = " << castExpr << std::endl; )
248                                Expression * ret = castExpr->get_arg();
249                                if ( ! isIntrinsicReference( ret ) ) {
250                                        // dereference if not already dereferenced
251                                        ret = mkDeref( ret );
252                                }
253                                ret->set_env( castExpr->get_env() );
254                                castExpr->set_arg( nullptr );
255                                castExpr->set_env( nullptr );
256                                delete castExpr;
257                                PRINT( std::cerr << "now: " << ret << std::endl; )
258                                return ret;
259                        }
260                        return castExpr;
261                }
262
263                Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
264                        Type * base = refType->get_base();
265                        refType->set_base( nullptr );
266                        delete refType;
267                        return new PointerType( Type::Qualifiers(), base );
268                }
269
270                Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
271                        if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
272                                Expression * arg1 = commaExpr->get_arg1()->clone();
273                                Expression * arg2 = commaExpr->get_arg2()->clone();
274                                delete addrExpr;
275                                return new CommaExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ) );
276                        } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
277                                Expression * arg1 = condExpr->get_arg1()->clone();
278                                Expression * arg2 = condExpr->get_arg2()->clone();
279                                Expression * arg3 = condExpr->get_arg3()->clone();
280                                delete addrExpr;
281                                return new ConditionalExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ), (new AddressExpr( arg3 ))->acceptMutator( *visitor ) );
282                        }
283                        return addrExpr;
284                }
285
286                Expression * CollapseAddrDeref::postmutate( AddressExpr * addressExpr ) {
287                        Expression * arg = addressExpr->get_arg();
288                        if ( isIntrinsicReference( arg ) ) {
289                                std::string fname = InitTweak::getFunctionName( arg );
290                                if ( fname == "*?" ) {
291                                        Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
292                                        Expression * ret = arg0;
293                                        ret->set_env( addressExpr->get_env() );
294                                        arg0 = nullptr;
295                                        addressExpr->set_env( nullptr );
296                                        delete addressExpr;
297                                        return ret;
298                                }
299                        }
300                        return addressExpr;
301                }
302
303                Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
304                        if ( isIntrinsicReference( appExpr ) ) {
305                                std::string fname = InitTweak::getFunctionName( appExpr );
306                                if ( fname == "*?" ) {
307                                        Expression * arg = InitTweak::getCallArg( appExpr, 0 );
308                                        // xxx - this isn't right, because it can remove casts that should be there...
309                                        // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
310                                        //      arg = castExpr->get_arg();
311                                        // }
312                                        if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
313                                                Expression * ret = addrExpr->get_arg();
314                                                ret->set_env( appExpr->get_env() );
315                                                addrExpr->set_arg( nullptr );
316                                                appExpr->set_env( nullptr );
317                                                delete appExpr;
318                                                return ret;
319                                        }
320                                }
321                        }
322                        return appExpr;
323                }
324        } // namespace
325} // namespace GenPoly
326
327// Local Variables: //
328// tab-width: 4 //
329// mode: c++ //
330// compile-command: "make install" //
331// End: //
Note: See TracBrowser for help on using the repository browser.