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 | // Box.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 : Tue Dec 15 15:30:31 2015
|
---|
13 | // Update Count : 215
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <set>
|
---|
17 | #include <stack>
|
---|
18 | #include <string>
|
---|
19 | #include <iterator>
|
---|
20 | #include <algorithm>
|
---|
21 | #include <cassert>
|
---|
22 |
|
---|
23 | #include "Box.h"
|
---|
24 | #include "PolyMutator.h"
|
---|
25 | #include "FindFunction.h"
|
---|
26 | #include "ScrubTyVars.h"
|
---|
27 |
|
---|
28 | #include "Parser/ParseNode.h"
|
---|
29 |
|
---|
30 | #include "SynTree/Constant.h"
|
---|
31 | #include "SynTree/Type.h"
|
---|
32 | #include "SynTree/Expression.h"
|
---|
33 | #include "SynTree/Initializer.h"
|
---|
34 | #include "SynTree/Statement.h"
|
---|
35 | #include "SynTree/Mutator.h"
|
---|
36 |
|
---|
37 | #include "ResolvExpr/TypeEnvironment.h"
|
---|
38 |
|
---|
39 | #include "SymTab/Mangler.h"
|
---|
40 |
|
---|
41 | #include "SemanticError.h"
|
---|
42 | #include "UniqueName.h"
|
---|
43 | #include "utility.h"
|
---|
44 |
|
---|
45 | #include <ext/functional> // temporary
|
---|
46 |
|
---|
47 | namespace GenPoly {
|
---|
48 | namespace {
|
---|
49 | const std::list<Label> noLabels;
|
---|
50 |
|
---|
51 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
|
---|
52 |
|
---|
53 | /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call
|
---|
54 | class Pass1 : public PolyMutator {
|
---|
55 | public:
|
---|
56 | Pass1();
|
---|
57 | virtual Expression *mutate( ApplicationExpr *appExpr );
|
---|
58 | virtual Expression *mutate( AddressExpr *addrExpr );
|
---|
59 | virtual Expression *mutate( UntypedExpr *expr );
|
---|
60 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl );
|
---|
61 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
62 | virtual Expression *mutate( CommaExpr *commaExpr );
|
---|
63 | virtual Expression *mutate( ConditionalExpr *condExpr );
|
---|
64 | virtual Statement * mutate( ReturnStmt *returnStmt );
|
---|
65 | virtual Type *mutate( PointerType *pointerType );
|
---|
66 | virtual Type * mutate( FunctionType *functionType );
|
---|
67 |
|
---|
68 | virtual void doBeginScope();
|
---|
69 | virtual void doEndScope();
|
---|
70 | private:
|
---|
71 | void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
72 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
|
---|
73 | Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg );
|
---|
74 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
75 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars );
|
---|
76 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
77 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars );
|
---|
78 | void findAssignOps( const std::list< TypeDecl *> &forall );
|
---|
79 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars );
|
---|
80 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
|
---|
81 | Expression *handleIntrinsics( ApplicationExpr *appExpr );
|
---|
82 | ObjectDecl *makeTemporary( Type *type );
|
---|
83 |
|
---|
84 | typedef std::map< std::string, DeclarationWithType *> AdapterMap;
|
---|
85 | std::map< std::string, DeclarationWithType *> assignOps;
|
---|
86 | std::stack< AdapterMap > adapters;
|
---|
87 | DeclarationWithType *retval;
|
---|
88 | bool useRetval;
|
---|
89 | UniqueName tempNamer;
|
---|
90 | };
|
---|
91 |
|
---|
92 | /// Moves polymorphic returns in function types to pointer-type parameters, adds type size and assertion parameters to parameter lists as well
|
---|
93 | class Pass2 : public PolyMutator {
|
---|
94 | public:
|
---|
95 | template< typename DeclClass >
|
---|
96 | DeclClass *handleDecl( DeclClass *decl, Type *type );
|
---|
97 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
|
---|
98 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
|
---|
99 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
100 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl );
|
---|
101 | virtual Type *mutate( PointerType *pointerType );
|
---|
102 | virtual Type *mutate( FunctionType *funcType );
|
---|
103 | private:
|
---|
104 | void addAdapters( FunctionType *functionType );
|
---|
105 |
|
---|
106 | std::map< UniqueId, std::string > adapterName;
|
---|
107 | };
|
---|
108 |
|
---|
109 | /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable
|
---|
110 | class Pass3 : public PolyMutator {
|
---|
111 | public:
|
---|
112 | template< typename DeclClass >
|
---|
113 | DeclClass *handleDecl( DeclClass *decl, Type *type );
|
---|
114 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
|
---|
115 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
|
---|
116 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl );
|
---|
117 | virtual TypeDecl *mutate( TypeDecl *objectDecl );
|
---|
118 | virtual Statement *mutate( DeclStmt *declStmt );
|
---|
119 | virtual Type *mutate( PointerType *pointerType );
|
---|
120 | virtual Type *mutate( FunctionType *funcType );
|
---|
121 | private:
|
---|
122 | };
|
---|
123 |
|
---|
124 | } // anonymous namespace
|
---|
125 |
|
---|
126 | void printAllNotBuiltin( const std::list< Declaration *>& translationUnit, std::ostream &os ) {
|
---|
127 | for ( std::list< Declaration *>::const_iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
|
---|
128 | if ( ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) {
|
---|
129 | (*i)->print( os );
|
---|
130 | os << std::endl;
|
---|
131 | } // if
|
---|
132 | } // for
|
---|
133 | }
|
---|
134 |
|
---|
135 | void box( std::list< Declaration *>& translationUnit ) {
|
---|
136 | Pass1 pass1;
|
---|
137 | Pass2 pass2;
|
---|
138 | Pass3 pass3;
|
---|
139 | mutateAll( translationUnit, pass1 );
|
---|
140 | mutateAll( translationUnit, pass2 );
|
---|
141 | mutateAll( translationUnit, pass3 );
|
---|
142 | }
|
---|
143 |
|
---|
144 | ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
|
---|
145 |
|
---|
146 | namespace {
|
---|
147 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
|
---|
148 | std::stringstream name;
|
---|
149 |
|
---|
150 | // NOTE: this function previously used isPolyObj, which failed to produce
|
---|
151 | // the correct thing in some situations. It's not clear to me why this wasn't working.
|
---|
152 |
|
---|
153 | // if the return type or a parameter type involved polymorphic types, then the adapter will need
|
---|
154 | // to take those polymorphic types as pointers. Therefore, there can be two different functions
|
---|
155 | // with the same mangled name, so we need to further mangle the names.
|
---|
156 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
|
---|
157 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
|
---|
158 | name << "P";
|
---|
159 | } else {
|
---|
160 | name << "M";
|
---|
161 | }
|
---|
162 | }
|
---|
163 | name << "_";
|
---|
164 | std::list< DeclarationWithType *> ¶mList = function->get_parameters();
|
---|
165 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
166 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
|
---|
167 | name << "P";
|
---|
168 | } else {
|
---|
169 | name << "M";
|
---|
170 | }
|
---|
171 | } // for
|
---|
172 | return name.str();
|
---|
173 | }
|
---|
174 |
|
---|
175 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) {
|
---|
176 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars );
|
---|
177 | }
|
---|
178 |
|
---|
179 | std::string makeAdapterName( const std::string &mangleName ) {
|
---|
180 | return "_adapter" + mangleName;
|
---|
181 | }
|
---|
182 |
|
---|
183 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {
|
---|
184 | adapters.push(AdapterMap());
|
---|
185 | }
|
---|
186 |
|
---|
187 | bool checkAssignment( DeclarationWithType *decl, std::string &name ) {
|
---|
188 | if ( decl->get_name() == "?=?" ) {
|
---|
189 | if ( PointerType *ptrType = dynamic_cast< PointerType *>( decl->get_type() ) ) {
|
---|
190 | if ( FunctionType *funType = dynamic_cast< FunctionType *>( ptrType->get_base() ) ) {
|
---|
191 | if ( funType->get_parameters().size() == 2 ) {
|
---|
192 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
|
---|
193 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) {
|
---|
194 | if ( TypeInstType *typeInst2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) {
|
---|
195 | if ( typeInst->get_name() == typeInst2->get_name() ) {
|
---|
196 | name = typeInst->get_name();
|
---|
197 | return true;
|
---|
198 | } // if
|
---|
199 | } // if
|
---|
200 | } // if
|
---|
201 | } // if
|
---|
202 | } // if
|
---|
203 | } // if
|
---|
204 | } // if
|
---|
205 | } // if
|
---|
206 | return false;
|
---|
207 | }
|
---|
208 |
|
---|
209 | void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) {
|
---|
210 | // what if a nested function uses an assignment operator?
|
---|
211 | // assignOps.clear();
|
---|
212 | for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
|
---|
213 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) {
|
---|
214 | std::string typeName;
|
---|
215 | if ( checkAssignment( *assert, typeName ) ) {
|
---|
216 | assignOps[ typeName ] = *assert;
|
---|
217 | } // if
|
---|
218 | } // for
|
---|
219 | } // for
|
---|
220 | }
|
---|
221 |
|
---|
222 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
|
---|
223 | if ( functionDecl->get_statements() ) { // empty routine body ?
|
---|
224 | doBeginScope();
|
---|
225 | TyVarMap oldtyVars = scopeTyVars;
|
---|
226 | std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;
|
---|
227 | DeclarationWithType *oldRetval = retval;
|
---|
228 | bool oldUseRetval = useRetval;
|
---|
229 |
|
---|
230 | // process polymorphic return value
|
---|
231 | retval = 0;
|
---|
232 | std::string typeName;
|
---|
233 | if ( isPolyRet( functionDecl->get_functionType(), typeName ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) {
|
---|
234 | retval = functionDecl->get_functionType()->get_returnVals().front();
|
---|
235 |
|
---|
236 | // give names to unnamed return values
|
---|
237 | if ( retval->get_name() == "" ) {
|
---|
238 | retval->set_name( "_retparm" );
|
---|
239 | retval->set_linkage( LinkageSpec::C );
|
---|
240 | } // if
|
---|
241 | } // if
|
---|
242 |
|
---|
243 | FunctionType *functionType = functionDecl->get_functionType();
|
---|
244 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
|
---|
245 | findAssignOps( functionDecl->get_functionType()->get_forall() );
|
---|
246 |
|
---|
247 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
248 | std::list< FunctionType *> functions;
|
---|
249 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
250 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
251 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
|
---|
252 | } // for
|
---|
253 | } // for
|
---|
254 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
255 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
|
---|
256 | } // for
|
---|
257 | AdapterMap & adapters = Pass1::adapters.top();
|
---|
258 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
259 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
260 | if ( adapters.find( mangleName ) == adapters.end() ) {
|
---|
261 | std::string adapterName = makeAdapterName( mangleName );
|
---|
262 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
|
---|
263 | } // if
|
---|
264 | } // for
|
---|
265 |
|
---|
266 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
|
---|
267 |
|
---|
268 | scopeTyVars = oldtyVars;
|
---|
269 | assignOps = oldassignOps;
|
---|
270 | // std::cerr << "end FunctionDecl: ";
|
---|
271 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
272 | // std::cerr << i->first << " ";
|
---|
273 | // }
|
---|
274 | // std::cerr << "\n";
|
---|
275 | retval = oldRetval;
|
---|
276 | useRetval = oldUseRetval;
|
---|
277 | doEndScope();
|
---|
278 | } // if
|
---|
279 | return functionDecl;
|
---|
280 | }
|
---|
281 |
|
---|
282 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
|
---|
283 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
284 | return Mutator::mutate( typeDecl );
|
---|
285 | }
|
---|
286 |
|
---|
287 | Expression *Pass1::mutate( CommaExpr *commaExpr ) {
|
---|
288 | bool oldUseRetval = useRetval;
|
---|
289 | useRetval = false;
|
---|
290 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) );
|
---|
291 | useRetval = oldUseRetval;
|
---|
292 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) );
|
---|
293 | return commaExpr;
|
---|
294 | }
|
---|
295 |
|
---|
296 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) {
|
---|
297 | bool oldUseRetval = useRetval;
|
---|
298 | useRetval = false;
|
---|
299 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) );
|
---|
300 | useRetval = oldUseRetval;
|
---|
301 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) );
|
---|
302 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) );
|
---|
303 | return condExpr;
|
---|
304 |
|
---|
305 | }
|
---|
306 |
|
---|
307 | void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
308 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) {
|
---|
309 | ResolvExpr::EqvClass eqvClass;
|
---|
310 | assert( env );
|
---|
311 | if ( tyParm->second == TypeDecl::Any ) {
|
---|
312 | Type *concrete = env->lookup( tyParm->first );
|
---|
313 | if ( concrete ) {
|
---|
314 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
|
---|
315 | arg++;
|
---|
316 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
|
---|
317 | arg++;
|
---|
318 | } else {
|
---|
319 | throw SemanticError( "unbound type variable in application ", appExpr );
|
---|
320 | } // if
|
---|
321 | } // if
|
---|
322 | } // for
|
---|
323 | }
|
---|
324 |
|
---|
325 | ObjectDecl *Pass1::makeTemporary( Type *type ) {
|
---|
326 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
|
---|
327 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
328 | return newObj;
|
---|
329 | }
|
---|
330 |
|
---|
331 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
|
---|
332 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
|
---|
333 | // if ( useRetval ) {
|
---|
334 | // assert( retval );
|
---|
335 | // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
|
---|
336 | // arg++;
|
---|
337 | // } else {
|
---|
338 |
|
---|
339 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result
|
---|
340 | // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple
|
---|
341 | // return values.
|
---|
342 | ObjectDecl *newObj = makeTemporary( retType->clone() );
|
---|
343 | Expression *paramExpr = new VariableExpr( newObj );
|
---|
344 | // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the
|
---|
345 | // temporary is already boxed and can be used directly.
|
---|
346 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
|
---|
347 | paramExpr = new AddressExpr( paramExpr );
|
---|
348 | } // if
|
---|
349 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call
|
---|
350 | arg++;
|
---|
351 | // Build a comma expression to call the function and emulate a normal return.
|
---|
352 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) );
|
---|
353 | commaExpr->set_env( appExpr->get_env() );
|
---|
354 | appExpr->set_env( 0 );
|
---|
355 | return commaExpr;
|
---|
356 | // } // if
|
---|
357 | // return appExpr;
|
---|
358 | }
|
---|
359 |
|
---|
360 | Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ) {
|
---|
361 | ResolvExpr::EqvClass eqvClass;
|
---|
362 | assert( env );
|
---|
363 | Type *concrete = env->lookup( typeName );
|
---|
364 | if ( concrete == 0 ) {
|
---|
365 | throw SemanticError( "Unbound type variable " + typeName + " in ", appExpr );
|
---|
366 | } // if
|
---|
367 | return addRetParam( appExpr, function, concrete, arg );
|
---|
368 | }
|
---|
369 |
|
---|
370 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
371 | Expression *ret = appExpr;
|
---|
372 | if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
373 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
|
---|
374 | } // if
|
---|
375 | std::string mangleName = mangleAdapterName( function, tyVars );
|
---|
376 | std::string adapterName = makeAdapterName( mangleName );
|
---|
377 |
|
---|
378 | appExpr->get_args().push_front( appExpr->get_function() );
|
---|
379 | appExpr->set_function( new NameExpr( adapterName ) );
|
---|
380 |
|
---|
381 | return ret;
|
---|
382 | }
|
---|
383 |
|
---|
384 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
|
---|
385 | assert( ! arg->get_results().empty() );
|
---|
386 | // if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) {
|
---|
387 | TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param );
|
---|
388 | if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) {
|
---|
389 | if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) {
|
---|
390 | // if the argument's type is a type parameter, we don't need to box again!
|
---|
391 | return;
|
---|
392 | } else if ( arg->get_results().front()->get_isLvalue() ) {
|
---|
393 | // VariableExpr and MemberExpr are lvalues
|
---|
394 | arg = new AddressExpr( arg );
|
---|
395 | } else {
|
---|
396 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 );
|
---|
397 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
|
---|
398 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
399 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
400 | assign->get_args().push_back( new VariableExpr( newObj ) );
|
---|
401 | assign->get_args().push_back( arg );
|
---|
402 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) );
|
---|
403 | arg = new AddressExpr( new VariableExpr( newObj ) );
|
---|
404 | } // if
|
---|
405 | } // if
|
---|
406 | // }
|
---|
407 | }
|
---|
408 |
|
---|
409 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
|
---|
410 | Type *newType = formal->clone();
|
---|
411 | std::list< FunctionType *> functions;
|
---|
412 | // instead of functions needing adapters, this really ought to look for
|
---|
413 | // any function mentioning a polymorphic type
|
---|
414 | findAndReplaceFunction( newType, functions, tyVars, needsAdapter );
|
---|
415 | if ( ! functions.empty() ) {
|
---|
416 | actual = new CastExpr( actual, newType );
|
---|
417 | } else {
|
---|
418 | delete newType;
|
---|
419 | } // if
|
---|
420 | }
|
---|
421 |
|
---|
422 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
423 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
|
---|
424 | assert( arg != appExpr->get_args().end() );
|
---|
425 | addCast( *arg, (*param)->get_type(), exprTyVars );
|
---|
426 | boxParam( (*param)->get_type(), *arg, exprTyVars );
|
---|
427 | } // for
|
---|
428 | }
|
---|
429 |
|
---|
430 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
431 | std::list< Expression *>::iterator cur = arg;
|
---|
432 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
433 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
434 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() );
|
---|
435 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
|
---|
436 | Expression *newExpr = inferParam->second.expr->clone();
|
---|
437 | addCast( newExpr, (*assert)->get_type(), tyVars );
|
---|
438 | boxParam( (*assert)->get_type(), newExpr, tyVars );
|
---|
439 | appExpr->get_args().insert( cur, newExpr );
|
---|
440 | } // for
|
---|
441 | } // for
|
---|
442 | }
|
---|
443 |
|
---|
444 | void makeRetParm( FunctionType *funcType ) {
|
---|
445 | DeclarationWithType *retParm = funcType->get_returnVals().front();
|
---|
446 |
|
---|
447 | // make a new parameter that is a pointer to the type of the old return value
|
---|
448 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
|
---|
449 | funcType->get_parameters().push_front( retParm );
|
---|
450 |
|
---|
451 | // we don't need the return value any more
|
---|
452 | funcType->get_returnVals().clear();
|
---|
453 | }
|
---|
454 |
|
---|
455 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
456 | // actually make the adapter type
|
---|
457 | FunctionType *adapter = adaptee->clone();
|
---|
458 | if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
459 | makeRetParm( adapter );
|
---|
460 | } // if
|
---|
461 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
|
---|
462 | return adapter;
|
---|
463 | }
|
---|
464 |
|
---|
465 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
|
---|
466 | assert( param );
|
---|
467 | assert( arg );
|
---|
468 | if ( isPolyType( realParam->get_type(), tyVars ) ) {
|
---|
469 | // if ( dynamic_cast< PointerType *>( arg->get_type() ) ) {
|
---|
470 | // return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
|
---|
471 | // } else {
|
---|
472 | if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
|
---|
473 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
474 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
|
---|
475 | deref->get_results().push_back( arg->get_type()->clone() );
|
---|
476 | return deref;
|
---|
477 | } // if
|
---|
478 | // }
|
---|
479 | } // if
|
---|
480 | return new VariableExpr( param );
|
---|
481 | }
|
---|
482 |
|
---|
483 | void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) {
|
---|
484 | UniqueName paramNamer( "_p" );
|
---|
485 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) {
|
---|
486 | if ( (*param)->get_name() == "" ) {
|
---|
487 | (*param)->set_name( paramNamer.newName() );
|
---|
488 | (*param)->set_linkage( LinkageSpec::C );
|
---|
489 | } // if
|
---|
490 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) );
|
---|
491 | } // for
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 |
|
---|
496 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
|
---|
497 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
|
---|
498 | adapterType = ScrubTyVars::scrub( adapterType, tyVars );
|
---|
499 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front();
|
---|
500 | adapteeDecl->set_name( "_adaptee" );
|
---|
501 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) );
|
---|
502 | Statement *bodyStmt;
|
---|
503 |
|
---|
504 | std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin();
|
---|
505 | std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin();
|
---|
506 | std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin();
|
---|
507 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) {
|
---|
508 | assert( tyArg != realType->get_forall().end() );
|
---|
509 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin();
|
---|
510 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin();
|
---|
511 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin();
|
---|
512 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) {
|
---|
513 | assert( assertArg != (*tyArg)->get_assertions().end() );
|
---|
514 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) );
|
---|
515 | } // for
|
---|
516 | } // for
|
---|
517 |
|
---|
518 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin();
|
---|
519 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin();
|
---|
520 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin();
|
---|
521 | param++; // skip adaptee parameter
|
---|
522 | if ( realType->get_returnVals().empty() ) {
|
---|
523 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
524 | bodyStmt = new ExprStmt( noLabels, adapteeApp );
|
---|
525 | } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
526 | if ( (*param)->get_name() == "" ) {
|
---|
527 | (*param)->set_name( "_ret" );
|
---|
528 | (*param)->set_linkage( LinkageSpec::C );
|
---|
529 | } // if
|
---|
530 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
531 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
532 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) );
|
---|
533 | assign->get_args().push_back( deref );
|
---|
534 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
535 | assign->get_args().push_back( adapteeApp );
|
---|
536 | bodyStmt = new ExprStmt( noLabels, assign );
|
---|
537 | } else {
|
---|
538 | // adapter for a function that returns a monomorphic value
|
---|
539 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
540 | bodyStmt = new ReturnStmt( noLabels, adapteeApp );
|
---|
541 | } // if
|
---|
542 | CompoundStmt *adapterBody = new CompoundStmt( noLabels );
|
---|
543 | adapterBody->get_kids().push_back( bodyStmt );
|
---|
544 | std::string adapterName = makeAdapterName( mangleName );
|
---|
545 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
|
---|
546 | }
|
---|
547 |
|
---|
548 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
|
---|
549 | // collect a list of function types passed as parameters or implicit parameters (assertions)
|
---|
550 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
551 | std::list< FunctionType *> functions;
|
---|
552 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
553 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
554 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter );
|
---|
555 | } // for
|
---|
556 | } // for
|
---|
557 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
558 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
|
---|
559 | } // for
|
---|
560 |
|
---|
561 | // parameter function types for which an appropriate adapter has been generated. we cannot use the types
|
---|
562 | // after applying substitutions, since two different parameter types may be unified to the same type
|
---|
563 | std::set< std::string > adaptersDone;
|
---|
564 |
|
---|
565 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
566 | FunctionType *originalFunction = (*funType)->clone();
|
---|
567 | FunctionType *realFunction = (*funType)->clone();
|
---|
568 | std::string mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
569 |
|
---|
570 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
|
---|
571 | // pre-substitution parameter function type.
|
---|
572 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
573 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
574 |
|
---|
575 | // apply substitution to type variables to figure out what the adapter's type should look like
|
---|
576 | assert( env );
|
---|
577 | env->apply( realFunction );
|
---|
578 | mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
579 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
|
---|
580 |
|
---|
581 | AdapterMap & adapters = Pass1::adapters.top();
|
---|
582 | AdapterMap::iterator adapter = adapters.find( mangleName );
|
---|
583 | if ( adapter == adapters.end() ) {
|
---|
584 | // adapter has not been created yet in the current scope, so define it
|
---|
585 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
|
---|
586 | adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
|
---|
587 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
|
---|
588 | } // if
|
---|
589 | assert( adapter != adapters.end() );
|
---|
590 |
|
---|
591 | // add the appropriate adapter as a parameter
|
---|
592 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
|
---|
593 | } // if
|
---|
594 | } // for
|
---|
595 | } // passAdapters
|
---|
596 |
|
---|
597 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
|
---|
598 | NameExpr *opExpr;
|
---|
599 | if ( isIncr ) {
|
---|
600 | opExpr = new NameExpr( "?+=?" );
|
---|
601 | } else {
|
---|
602 | opExpr = new NameExpr( "?-=?" );
|
---|
603 | } // if
|
---|
604 | UntypedExpr *addAssign = new UntypedExpr( opExpr );
|
---|
605 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
|
---|
606 | addAssign->get_args().push_back( address->get_arg() );
|
---|
607 | } else {
|
---|
608 | addAssign->get_args().push_back( appExpr->get_args().front() );
|
---|
609 | } // if
|
---|
610 | addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) );
|
---|
611 | addAssign->get_results().front() = appExpr->get_results().front()->clone();
|
---|
612 | if ( appExpr->get_env() ) {
|
---|
613 | addAssign->set_env( appExpr->get_env() );
|
---|
614 | appExpr->set_env( 0 );
|
---|
615 | } // if
|
---|
616 | appExpr->get_args().clear();
|
---|
617 | delete appExpr;
|
---|
618 | return addAssign;
|
---|
619 | }
|
---|
620 |
|
---|
621 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
|
---|
622 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) {
|
---|
623 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
624 | if ( varExpr->get_var()->get_name() == "?[?]" ) {
|
---|
625 | assert( ! appExpr->get_results().empty() );
|
---|
626 | assert( appExpr->get_args().size() == 2 );
|
---|
627 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
|
---|
628 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
|
---|
629 | assert( ! baseType1 || ! baseType2 );
|
---|
630 | UntypedExpr *ret = 0;
|
---|
631 | if ( baseType1 || baseType2 ) {
|
---|
632 | ret = new UntypedExpr( new NameExpr( "?+?" ) );
|
---|
633 | } // if
|
---|
634 | if ( baseType1 ) {
|
---|
635 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
636 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
637 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
638 | ret->get_args().push_back( appExpr->get_args().front() );
|
---|
639 | ret->get_args().push_back( multiply );
|
---|
640 | } else if ( baseType2 ) {
|
---|
641 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
642 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
643 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
|
---|
644 | ret->get_args().push_back( multiply );
|
---|
645 | ret->get_args().push_back( appExpr->get_args().back() );
|
---|
646 | } // if
|
---|
647 | if ( baseType1 || baseType2 ) {
|
---|
648 | ret->get_results().push_front( appExpr->get_results().front()->clone() );
|
---|
649 | if ( appExpr->get_env() ) {
|
---|
650 | ret->set_env( appExpr->get_env() );
|
---|
651 | appExpr->set_env( 0 );
|
---|
652 | } // if
|
---|
653 | appExpr->get_args().clear();
|
---|
654 | delete appExpr;
|
---|
655 | return ret;
|
---|
656 | } // if
|
---|
657 | } else if ( varExpr->get_var()->get_name() == "*?" ) {
|
---|
658 | assert( ! appExpr->get_results().empty() );
|
---|
659 | assert( ! appExpr->get_args().empty() );
|
---|
660 | if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
661 | Expression *ret = appExpr->get_args().front();
|
---|
662 | delete ret->get_results().front();
|
---|
663 | ret->get_results().front() = appExpr->get_results().front()->clone();
|
---|
664 | if ( appExpr->get_env() ) {
|
---|
665 | ret->set_env( appExpr->get_env() );
|
---|
666 | appExpr->set_env( 0 );
|
---|
667 | } // if
|
---|
668 | appExpr->get_args().clear();
|
---|
669 | delete appExpr;
|
---|
670 | return ret;
|
---|
671 | } // if
|
---|
672 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) {
|
---|
673 | assert( ! appExpr->get_results().empty() );
|
---|
674 | assert( appExpr->get_args().size() == 1 );
|
---|
675 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
676 | Type *tempType = appExpr->get_results().front()->clone();
|
---|
677 | if ( env ) {
|
---|
678 | env->apply( tempType );
|
---|
679 | } // if
|
---|
680 | ObjectDecl *newObj = makeTemporary( tempType );
|
---|
681 | VariableExpr *tempExpr = new VariableExpr( newObj );
|
---|
682 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
683 | assignExpr->get_args().push_back( tempExpr->clone() );
|
---|
684 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
|
---|
685 | assignExpr->get_args().push_back( address->get_arg()->clone() );
|
---|
686 | } else {
|
---|
687 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() );
|
---|
688 | } // if
|
---|
689 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
|
---|
690 | return new CommaExpr( firstComma, tempExpr );
|
---|
691 | } // if
|
---|
692 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
|
---|
693 | assert( ! appExpr->get_results().empty() );
|
---|
694 | assert( appExpr->get_args().size() == 1 );
|
---|
695 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
696 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
|
---|
697 | } // if
|
---|
698 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
|
---|
699 | assert( ! appExpr->get_results().empty() );
|
---|
700 | assert( appExpr->get_args().size() == 2 );
|
---|
701 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
|
---|
702 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
|
---|
703 | if ( baseType1 && baseType2 ) {
|
---|
704 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
|
---|
705 | divide->get_args().push_back( appExpr );
|
---|
706 | divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
707 | divide->get_results().push_front( appExpr->get_results().front()->clone() );
|
---|
708 | if ( appExpr->get_env() ) {
|
---|
709 | divide->set_env( appExpr->get_env() );
|
---|
710 | appExpr->set_env( 0 );
|
---|
711 | } // if
|
---|
712 | return divide;
|
---|
713 | } else if ( baseType1 ) {
|
---|
714 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
715 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
716 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
717 | appExpr->get_args().back() = multiply;
|
---|
718 | } else if ( baseType2 ) {
|
---|
719 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
720 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
721 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
|
---|
722 | appExpr->get_args().front() = multiply;
|
---|
723 | } // if
|
---|
724 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
|
---|
725 | assert( ! appExpr->get_results().empty() );
|
---|
726 | assert( appExpr->get_args().size() == 2 );
|
---|
727 | Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env );
|
---|
728 | if ( baseType ) {
|
---|
729 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
730 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
731 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
|
---|
732 | appExpr->get_args().back() = multiply;
|
---|
733 | } // if
|
---|
734 | } // if
|
---|
735 | return appExpr;
|
---|
736 | } // if
|
---|
737 | } // if
|
---|
738 | return 0;
|
---|
739 | }
|
---|
740 |
|
---|
741 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
|
---|
742 | // std::cerr << "mutate appExpr: ";
|
---|
743 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
744 | // std::cerr << i->first << " ";
|
---|
745 | // }
|
---|
746 | // std::cerr << "\n";
|
---|
747 | bool oldUseRetval = useRetval;
|
---|
748 | useRetval = false;
|
---|
749 | appExpr->get_function()->acceptMutator( *this );
|
---|
750 | mutateAll( appExpr->get_args(), *this );
|
---|
751 | useRetval = oldUseRetval;
|
---|
752 |
|
---|
753 | assert( ! appExpr->get_function()->get_results().empty() );
|
---|
754 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
|
---|
755 | assert( pointer );
|
---|
756 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
|
---|
757 | assert( function );
|
---|
758 |
|
---|
759 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
|
---|
760 | return newExpr;
|
---|
761 | } // if
|
---|
762 |
|
---|
763 | Expression *ret = appExpr;
|
---|
764 |
|
---|
765 | std::list< Expression *>::iterator arg = appExpr->get_args().begin();
|
---|
766 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
|
---|
767 |
|
---|
768 | std::string typeName;
|
---|
769 | if ( isPolyRet( function, typeName ) ) {
|
---|
770 | ret = addPolyRetParam( appExpr, function, typeName, arg );
|
---|
771 | } else if ( needsAdapter( function, scopeTyVars ) ) {
|
---|
772 | // std::cerr << "needs adapter: ";
|
---|
773 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
774 | // std::cerr << i->first << " ";
|
---|
775 | // }
|
---|
776 | // std::cerr << "\n";
|
---|
777 | // change the application so it calls the adapter rather than the passed function
|
---|
778 | ret = applyAdapter( appExpr, function, arg, scopeTyVars );
|
---|
779 | } // if
|
---|
780 | arg = appExpr->get_args().begin();
|
---|
781 |
|
---|
782 | TyVarMap exprTyVars;
|
---|
783 | makeTyVarMap( function, exprTyVars );
|
---|
784 |
|
---|
785 | passTypeVars( appExpr, arg, exprTyVars );
|
---|
786 | addInferredParams( appExpr, function, arg, exprTyVars );
|
---|
787 |
|
---|
788 | arg = paramBegin;
|
---|
789 |
|
---|
790 | boxParams( appExpr, function, arg, exprTyVars );
|
---|
791 |
|
---|
792 | passAdapters( appExpr, function, exprTyVars );
|
---|
793 |
|
---|
794 | return ret;
|
---|
795 | }
|
---|
796 |
|
---|
797 | Expression *Pass1::mutate( UntypedExpr *expr ) {
|
---|
798 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
|
---|
799 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
|
---|
800 | if ( name->get_name() == "*?" ) {
|
---|
801 | Expression *ret = expr->get_args().front();
|
---|
802 | expr->get_args().clear();
|
---|
803 | delete expr;
|
---|
804 | return ret->acceptMutator( *this );
|
---|
805 | } // if
|
---|
806 | } // if
|
---|
807 | } // if
|
---|
808 | return PolyMutator::mutate( expr );
|
---|
809 | }
|
---|
810 |
|
---|
811 | Expression *Pass1::mutate( AddressExpr *addrExpr ) {
|
---|
812 | assert( ! addrExpr->get_arg()->get_results().empty() );
|
---|
813 |
|
---|
814 | bool needs = false;
|
---|
815 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
|
---|
816 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
|
---|
817 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
|
---|
818 | if ( name->get_name() == "*?" ) {
|
---|
819 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) {
|
---|
820 | assert( ! appExpr->get_function()->get_results().empty() );
|
---|
821 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
|
---|
822 | assert( pointer );
|
---|
823 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
|
---|
824 | assert( function );
|
---|
825 | needs = needsAdapter( function, scopeTyVars );
|
---|
826 | } // if
|
---|
827 | } // if
|
---|
828 | } // if
|
---|
829 | } // if
|
---|
830 | } // if
|
---|
831 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
|
---|
832 | if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) {
|
---|
833 | Expression *ret = addrExpr->get_arg();
|
---|
834 | delete ret->get_results().front();
|
---|
835 | ret->get_results().front() = addrExpr->get_results().front()->clone();
|
---|
836 | addrExpr->set_arg( 0 );
|
---|
837 | delete addrExpr;
|
---|
838 | return ret;
|
---|
839 | } else {
|
---|
840 | return addrExpr;
|
---|
841 | } // if
|
---|
842 | }
|
---|
843 |
|
---|
844 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
|
---|
845 | if ( retval && returnStmt->get_expr() ) {
|
---|
846 | assert( ! returnStmt->get_expr()->get_results().empty() );
|
---|
847 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
|
---|
848 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) {
|
---|
849 | // a cast expr on a polymorphic return value is either redundant or invalid
|
---|
850 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) {
|
---|
851 | returnStmt->set_expr( castExpr->get_arg() );
|
---|
852 | returnStmt->get_expr()->set_env( castExpr->get_env() );
|
---|
853 | castExpr->set_env( 0 );
|
---|
854 | castExpr->set_arg( 0 );
|
---|
855 | delete castExpr;
|
---|
856 | } //while
|
---|
857 | TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() );
|
---|
858 | assert( typeInst );
|
---|
859 | std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
|
---|
860 | if ( assignIter == assignOps.end() ) {
|
---|
861 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
|
---|
862 | } // if
|
---|
863 | ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
|
---|
864 | Expression *retParm = new NameExpr( retval->get_name() );
|
---|
865 | retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
|
---|
866 | assignExpr->get_args().push_back( retParm );
|
---|
867 | assignExpr->get_args().push_back( returnStmt->get_expr() );
|
---|
868 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
|
---|
869 | // } else {
|
---|
870 | // useRetval = true;
|
---|
871 | // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) );
|
---|
872 | // useRetval = false;
|
---|
873 | // } // if
|
---|
874 | returnStmt->set_expr( 0 );
|
---|
875 | } else {
|
---|
876 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
|
---|
877 | } // if
|
---|
878 | return returnStmt;
|
---|
879 | }
|
---|
880 |
|
---|
881 | Type * Pass1::mutate( PointerType *pointerType ) {
|
---|
882 | TyVarMap oldtyVars = scopeTyVars;
|
---|
883 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
884 |
|
---|
885 | Type *ret = Mutator::mutate( pointerType );
|
---|
886 |
|
---|
887 | scopeTyVars = oldtyVars;
|
---|
888 | return ret;
|
---|
889 | }
|
---|
890 |
|
---|
891 | Type * Pass1::mutate( FunctionType *functionType ) {
|
---|
892 | TyVarMap oldtyVars = scopeTyVars;
|
---|
893 | makeTyVarMap( functionType, scopeTyVars );
|
---|
894 |
|
---|
895 | Type *ret = Mutator::mutate( functionType );
|
---|
896 |
|
---|
897 | scopeTyVars = oldtyVars;
|
---|
898 | return ret;
|
---|
899 | }
|
---|
900 |
|
---|
901 | void Pass1::doBeginScope() {
|
---|
902 | // push a copy of the current map
|
---|
903 | adapters.push(adapters.top());
|
---|
904 | }
|
---|
905 |
|
---|
906 | void Pass1::doEndScope() {
|
---|
907 | adapters.pop();
|
---|
908 | }
|
---|
909 |
|
---|
910 | ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
|
---|
911 |
|
---|
912 | void Pass2::addAdapters( FunctionType *functionType ) {
|
---|
913 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
914 | std::list< FunctionType *> functions;
|
---|
915 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
916 | Type *orig = (*arg)->get_type();
|
---|
917 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
|
---|
918 | (*arg)->set_type( orig );
|
---|
919 | }
|
---|
920 | std::set< std::string > adaptersDone;
|
---|
921 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
922 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
923 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
924 | std::string adapterName = makeAdapterName( mangleName );
|
---|
925 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
|
---|
926 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
927 | }
|
---|
928 | }
|
---|
929 | // deleteAll( functions );
|
---|
930 | }
|
---|
931 |
|
---|
932 | template< typename DeclClass >
|
---|
933 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
|
---|
934 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
935 |
|
---|
936 | return ret;
|
---|
937 | }
|
---|
938 |
|
---|
939 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
|
---|
940 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
941 | }
|
---|
942 |
|
---|
943 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
|
---|
944 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
945 | }
|
---|
946 |
|
---|
947 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) {
|
---|
948 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
949 | if ( typeDecl->get_base() ) {
|
---|
950 | return handleDecl( typeDecl, typeDecl->get_base() );
|
---|
951 | } else {
|
---|
952 | return Mutator::mutate( typeDecl );
|
---|
953 | }
|
---|
954 | }
|
---|
955 |
|
---|
956 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
|
---|
957 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
958 | }
|
---|
959 |
|
---|
960 | Type * Pass2::mutate( PointerType *pointerType ) {
|
---|
961 | TyVarMap oldtyVars = scopeTyVars;
|
---|
962 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
963 |
|
---|
964 | Type *ret = Mutator::mutate( pointerType );
|
---|
965 |
|
---|
966 | scopeTyVars = oldtyVars;
|
---|
967 | return ret;
|
---|
968 | }
|
---|
969 |
|
---|
970 | Type *Pass2::mutate( FunctionType *funcType ) {
|
---|
971 | TyVarMap oldtyVars = scopeTyVars;
|
---|
972 | makeTyVarMap( funcType, scopeTyVars );
|
---|
973 |
|
---|
974 | std::string typeName;
|
---|
975 | if ( isPolyRet( funcType, typeName ) ) {
|
---|
976 | DeclarationWithType *ret = funcType->get_returnVals().front();
|
---|
977 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
|
---|
978 | funcType->get_parameters().push_front( ret );
|
---|
979 | funcType->get_returnVals().pop_front();
|
---|
980 | }
|
---|
981 |
|
---|
982 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
|
---|
983 | std::list< DeclarationWithType *> inferredParams;
|
---|
984 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
|
---|
985 | // ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 );
|
---|
986 | for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
|
---|
987 | ObjectDecl *sizeParm, *alignParm;
|
---|
988 | // add all size and alignment parameters to parameter list
|
---|
989 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
|
---|
990 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
|
---|
991 |
|
---|
992 | sizeParm = newObj.clone();
|
---|
993 | sizeParm->set_name( sizeofName( &parmType ) );
|
---|
994 | last = funcType->get_parameters().insert( last, sizeParm );
|
---|
995 | ++last;
|
---|
996 |
|
---|
997 | alignParm = newObj.clone();
|
---|
998 | alignParm->set_name( alignofName( &parmType ) );
|
---|
999 | last = funcType->get_parameters().insert( last, alignParm );
|
---|
1000 | ++last;
|
---|
1001 | }
|
---|
1002 | // move all assertions into parameter list
|
---|
1003 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
|
---|
1004 | // *assert = (*assert)->acceptMutator( *this );
|
---|
1005 | inferredParams.push_back( *assert );
|
---|
1006 | }
|
---|
1007 | (*tyParm)->get_assertions().clear();
|
---|
1008 | }
|
---|
1009 | funcType->get_parameters().splice( last, inferredParams );
|
---|
1010 | addAdapters( funcType );
|
---|
1011 | mutateAll( funcType->get_returnVals(), *this );
|
---|
1012 | mutateAll( funcType->get_parameters(), *this );
|
---|
1013 |
|
---|
1014 | scopeTyVars = oldtyVars;
|
---|
1015 | return funcType;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
|
---|
1019 |
|
---|
1020 | template< typename DeclClass >
|
---|
1021 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
|
---|
1022 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1023 | makeTyVarMap( type, scopeTyVars );
|
---|
1024 |
|
---|
1025 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
1026 | ScrubTyVars::scrub( decl, scopeTyVars );
|
---|
1027 |
|
---|
1028 | scopeTyVars = oldtyVars;
|
---|
1029 | return ret;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
|
---|
1033 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
|
---|
1037 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
|
---|
1041 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
|
---|
1045 | // Initializer *init = 0;
|
---|
1046 | // std::list< Expression *> designators;
|
---|
1047 | // scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1048 | // if ( typeDecl->get_base() ) {
|
---|
1049 | // init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators );
|
---|
1050 | // }
|
---|
1051 | // return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init );
|
---|
1052 |
|
---|
1053 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1054 | return Mutator::mutate( typeDecl );
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | Type * Pass3::mutate( PointerType *pointerType ) {
|
---|
1058 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1059 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
1060 |
|
---|
1061 | Type *ret = Mutator::mutate( pointerType );
|
---|
1062 |
|
---|
1063 | scopeTyVars = oldtyVars;
|
---|
1064 | return ret;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | Type * Pass3::mutate( FunctionType *functionType ) {
|
---|
1068 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1069 | makeTyVarMap( functionType, scopeTyVars );
|
---|
1070 |
|
---|
1071 | Type *ret = Mutator::mutate( functionType );
|
---|
1072 |
|
---|
1073 | scopeTyVars = oldtyVars;
|
---|
1074 | return ret;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | Statement *Pass3::mutate( DeclStmt *declStmt ) {
|
---|
1078 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
|
---|
1079 | if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) {
|
---|
1080 | // change initialization of a polymorphic value object
|
---|
1081 | // to allocate storage with alloca
|
---|
1082 | Type *declType = objectDecl->get_type();
|
---|
1083 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
|
---|
1084 | alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
|
---|
1085 |
|
---|
1086 | delete objectDecl->get_init();
|
---|
1087 |
|
---|
1088 | std::list<Expression*> designators;
|
---|
1089 | objectDecl->set_init( new SingleInit( alloc, designators ) );
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | return Mutator::mutate( declStmt );
|
---|
1093 | }
|
---|
1094 | } // anonymous namespace
|
---|
1095 | } // namespace GenPoly
|
---|
1096 |
|
---|
1097 | // Local Variables: //
|
---|
1098 | // tab-width: 4 //
|
---|
1099 | // mode: c++ //
|
---|
1100 | // compile-command: "make install" //
|
---|
1101 | // End: //
|
---|