source: src/GenPoly/Lvalue.cc@ 6d267ca

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6d267ca was 9191a8e, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove reference to rvalue cast on intrinsic results

  • Property mode set to 100644
File size: 12.4 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 {
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 void fixArg( Expression *& arg, Type * formal ) {
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 );
155 arg = mkDeref( arg );
156 }
157 }
158
159 // xxx - might need to & every * (or every * that is an arg to non-intrinsic function??)
160 Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
161 // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
162 if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
163 if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
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 );
169 PRINT(
170 std::cerr << "pair<0>: " << arg << std::endl;
171 std::cerr << "pair<1>: " << formal->get_type() << std::endl;
172 )
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
175 PRINT( std::cerr << "skipping intrinsic reference" << std::endl; )
176 continue;
177 } else {
178 fixArg( arg, formal->get_type() );
179 }
180 }
181 }
182 }
183 return appExpr;
184 }
185
186 Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
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
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;
200 PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; )
201 if ( isIntrinsicReference( castExpr->get_arg() ) ) {
202 Expression * callExpr = castExpr->get_arg();
203 PRINT(
204 std::cerr << "but arg is deref -- &" << std::endl;
205 std::cerr << callExpr << std::endl;
206 )
207 // move environment out to new top-level
208 callExpr->set_env( castExpr->get_env() );
209 castExpr->set_arg( nullptr );
210 castExpr->set_env( nullptr );
211 delete castExpr;
212 return callExpr;
213 }
214 assertf( false, "non-intrinsic reference with cast of reference to reference not yet supported: ", toString( castExpr ) );
215 PRINT( std::cerr << castExpr << std::endl; )
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
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 }
232 ret->set_env( castExpr->get_env() );
233 castExpr->set_env( nullptr );
234 castExpr->set_arg( nullptr );
235 delete castExpr;
236 return ret;
237 } else {
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() ) ) {
242 // conversion from reference to rvalue
243 PRINT( std::cerr << "convert reference to rvalue -- *" << std::endl; )
244 PRINT( std::cerr << "was = " << castExpr << std::endl; )
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() );
251 castExpr->set_arg( nullptr );
252 castExpr->set_env( nullptr );
253 delete castExpr;
254 PRINT( std::cerr << "now: " << ret << std::endl; )
255 return ret;
256 }
257 return castExpr;
258 }
259
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 );
265 }
266
267 Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
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 }
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 }
321 } // namespace
322} // namespace GenPoly
323
324// Local Variables: //
325// tab-width: 4 //
326// mode: c++ //
327// compile-command: "make install" //
328// End: //
Note: See TracBrowser for help on using the repository browser.