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 : Wed Jun 29 21:43:03 2016 |
---|
13 | // Update Count : 296 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> |
---|
17 | #include <iterator> |
---|
18 | #include <list> |
---|
19 | #include <map> |
---|
20 | #include <set> |
---|
21 | #include <stack> |
---|
22 | #include <string> |
---|
23 | #include <utility> |
---|
24 | #include <vector> |
---|
25 | #include <cassert> |
---|
26 | |
---|
27 | #include "Box.h" |
---|
28 | #include "DeclMutator.h" |
---|
29 | #include "PolyMutator.h" |
---|
30 | #include "FindFunction.h" |
---|
31 | #include "ScopedSet.h" |
---|
32 | #include "ScrubTyVars.h" |
---|
33 | |
---|
34 | #include "Parser/ParseNode.h" |
---|
35 | |
---|
36 | #include "SynTree/Constant.h" |
---|
37 | #include "SynTree/Declaration.h" |
---|
38 | #include "SynTree/Expression.h" |
---|
39 | #include "SynTree/Initializer.h" |
---|
40 | #include "SynTree/Mutator.h" |
---|
41 | #include "SynTree/Statement.h" |
---|
42 | #include "SynTree/Type.h" |
---|
43 | #include "SynTree/TypeSubstitution.h" |
---|
44 | |
---|
45 | #include "ResolvExpr/TypeEnvironment.h" |
---|
46 | #include "ResolvExpr/TypeMap.h" |
---|
47 | #include "ResolvExpr/typeops.h" |
---|
48 | |
---|
49 | #include "SymTab/Indexer.h" |
---|
50 | #include "SymTab/Mangler.h" |
---|
51 | |
---|
52 | #include "Common/ScopedMap.h" |
---|
53 | #include "Common/SemanticError.h" |
---|
54 | #include "Common/UniqueName.h" |
---|
55 | #include "Common/utility.h" |
---|
56 | |
---|
57 | #include <ext/functional> // temporary |
---|
58 | |
---|
59 | namespace GenPoly { |
---|
60 | namespace { |
---|
61 | const std::list<Label> noLabels; |
---|
62 | |
---|
63 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); |
---|
64 | |
---|
65 | /// Adds layout-generation functions to polymorphic types |
---|
66 | class LayoutFunctionBuilder final : public DeclMutator { |
---|
67 | unsigned int functionNesting; // current level of nested functions |
---|
68 | public: |
---|
69 | LayoutFunctionBuilder() : functionNesting( 0 ) {} |
---|
70 | |
---|
71 | using DeclMutator::mutate; |
---|
72 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; |
---|
73 | virtual Declaration *mutate( StructDecl *structDecl ) override; |
---|
74 | virtual Declaration *mutate( UnionDecl *unionDecl ) override; |
---|
75 | }; |
---|
76 | |
---|
77 | /// 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 |
---|
78 | class Pass1 final : public PolyMutator { |
---|
79 | public: |
---|
80 | Pass1(); |
---|
81 | |
---|
82 | using PolyMutator::mutate; |
---|
83 | virtual Expression *mutate( ApplicationExpr *appExpr ) override; |
---|
84 | virtual Expression *mutate( AddressExpr *addrExpr ) override; |
---|
85 | virtual Expression *mutate( UntypedExpr *expr ) override; |
---|
86 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override; |
---|
87 | virtual TypeDecl *mutate( TypeDecl *typeDecl ) override; |
---|
88 | virtual Expression *mutate( CommaExpr *commaExpr ) override; |
---|
89 | virtual Expression *mutate( ConditionalExpr *condExpr ) override; |
---|
90 | virtual Statement * mutate( ReturnStmt *returnStmt ) override; |
---|
91 | virtual Type *mutate( PointerType *pointerType ) override; |
---|
92 | virtual Type * mutate( FunctionType *functionType ) override; |
---|
93 | |
---|
94 | virtual void doBeginScope() override; |
---|
95 | virtual void doEndScope() override; |
---|
96 | private: |
---|
97 | /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application |
---|
98 | void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ); |
---|
99 | /// passes extra type parameters into a polymorphic function application |
---|
100 | void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
101 | /// wraps a function application with a new temporary for the out-parameter return value |
---|
102 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); |
---|
103 | /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment |
---|
104 | void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ); |
---|
105 | /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete). |
---|
106 | /// If `doClone` is set to false, will not clone interior types |
---|
107 | Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true ); |
---|
108 | /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value |
---|
109 | Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg ); |
---|
110 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
111 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars ); |
---|
112 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); |
---|
113 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ); |
---|
114 | /// Stores assignment operators from assertion list in local map of assignment operations |
---|
115 | void findTypeOps( const Type::ForallList &forall ); |
---|
116 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ); |
---|
117 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); |
---|
118 | /// Replaces intrinsic operator functions with their arithmetic desugaring |
---|
119 | Expression *handleIntrinsics( ApplicationExpr *appExpr ); |
---|
120 | /// Inserts a new temporary variable into the current scope with an auto-generated name |
---|
121 | ObjectDecl *makeTemporary( Type *type ); |
---|
122 | |
---|
123 | ScopedMap< std::string, DeclarationWithType* > assignOps; ///< Currently known type variable assignment operators |
---|
124 | ScopedMap< std::string, DeclarationWithType* > ctorOps; ///< Currently known type variable constructors |
---|
125 | ScopedMap< std::string, DeclarationWithType* > copyOps; ///< Currently known type variable copy constructors |
---|
126 | ScopedMap< std::string, DeclarationWithType* > dtorOps; ///< Currently known type variable destructors |
---|
127 | ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; ///< Currently known assignment operators |
---|
128 | ResolvExpr::TypeMap< DeclarationWithType > scopedCtorOps; ///< Currently known assignment operators |
---|
129 | ResolvExpr::TypeMap< DeclarationWithType > scopedCopyOps; ///< Currently known assignment operators |
---|
130 | ResolvExpr::TypeMap< DeclarationWithType > scopedDtorOps; ///< Currently known assignment operators |
---|
131 | ScopedMap< std::string, DeclarationWithType* > adapters; ///< Set of adapter functions in the current scope |
---|
132 | |
---|
133 | DeclarationWithType *retval; |
---|
134 | bool useRetval; |
---|
135 | UniqueName tempNamer; |
---|
136 | }; |
---|
137 | |
---|
138 | /// * Moves polymorphic returns in function types to pointer-type parameters |
---|
139 | /// * adds type size and assertion parameters to parameter lists |
---|
140 | class Pass2 final : public PolyMutator { |
---|
141 | public: |
---|
142 | template< typename DeclClass > |
---|
143 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
144 | |
---|
145 | using PolyMutator::mutate; |
---|
146 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; |
---|
147 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; |
---|
148 | virtual TypeDecl *mutate( TypeDecl *typeDecl ) override; |
---|
149 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl ) override; |
---|
150 | virtual Type *mutate( PointerType *pointerType ) override; |
---|
151 | virtual Type *mutate( FunctionType *funcType ) override; |
---|
152 | |
---|
153 | private: |
---|
154 | void addAdapters( FunctionType *functionType ); |
---|
155 | |
---|
156 | std::map< UniqueId, std::string > adapterName; |
---|
157 | }; |
---|
158 | |
---|
159 | /// Replaces member and size/align/offsetof expressions on polymorphic generic types with calculated expressions. |
---|
160 | /// * Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference |
---|
161 | /// * Calculates polymorphic offsetof expressions from offset array |
---|
162 | /// * Inserts dynamic calculation of polymorphic type layouts where needed |
---|
163 | class PolyGenericCalculator final : public PolyMutator { |
---|
164 | public: |
---|
165 | typedef PolyMutator Parent; |
---|
166 | using Parent::mutate; |
---|
167 | |
---|
168 | template< typename DeclClass > |
---|
169 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
170 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; |
---|
171 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; |
---|
172 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ) override; |
---|
173 | virtual TypeDecl *mutate( TypeDecl *objectDecl ) override; |
---|
174 | virtual Statement *mutate( DeclStmt *declStmt ) override; |
---|
175 | virtual Type *mutate( PointerType *pointerType ) override; |
---|
176 | virtual Type *mutate( FunctionType *funcType ) override; |
---|
177 | virtual Expression *mutate( MemberExpr *memberExpr ) override; |
---|
178 | virtual Expression *mutate( SizeofExpr *sizeofExpr ) override; |
---|
179 | virtual Expression *mutate( AlignofExpr *alignofExpr ) override; |
---|
180 | virtual Expression *mutate( OffsetofExpr *offsetofExpr ) override; |
---|
181 | virtual Expression *mutate( OffsetPackExpr *offsetPackExpr ) override; |
---|
182 | |
---|
183 | virtual void doBeginScope() override; |
---|
184 | virtual void doEndScope() override; |
---|
185 | |
---|
186 | private: |
---|
187 | /// Makes a new variable in the current scope with the given name, type & optional initializer |
---|
188 | ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 ); |
---|
189 | /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns |
---|
190 | bool findGeneric( Type *ty ); |
---|
191 | /// adds type parameters to the layout call; will generate the appropriate parameters if needed |
---|
192 | void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); |
---|
193 | |
---|
194 | /// Enters a new scope for type-variables, adding the type variables from ty |
---|
195 | void beginTypeScope( Type *ty ); |
---|
196 | /// Exits the type-variable scope |
---|
197 | void endTypeScope(); |
---|
198 | |
---|
199 | ScopedSet< std::string > knownLayouts; ///< Set of generic type layouts known in the current scope, indexed by sizeofName |
---|
200 | ScopedSet< std::string > knownOffsets; ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName |
---|
201 | }; |
---|
202 | |
---|
203 | /// 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 |
---|
204 | class Pass3 final : public PolyMutator { |
---|
205 | public: |
---|
206 | template< typename DeclClass > |
---|
207 | DeclClass *handleDecl( DeclClass *decl, Type *type ); |
---|
208 | |
---|
209 | using PolyMutator::mutate; |
---|
210 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; |
---|
211 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; |
---|
212 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ) override; |
---|
213 | virtual TypeDecl *mutate( TypeDecl *objectDecl ) override; |
---|
214 | virtual Type *mutate( PointerType *pointerType ) override; |
---|
215 | virtual Type *mutate( FunctionType *funcType ) override; |
---|
216 | private: |
---|
217 | }; |
---|
218 | |
---|
219 | } // anonymous namespace |
---|
220 | |
---|
221 | /// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging |
---|
222 | template< typename MutatorType > |
---|
223 | inline void mutateTranslationUnit( std::list< Declaration* > &translationUnit, MutatorType &mutator ) { |
---|
224 | bool seenIntrinsic = false; |
---|
225 | SemanticError errors; |
---|
226 | for ( typename std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
227 | try { |
---|
228 | if ( *i ) { |
---|
229 | if ( (*i)->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
230 | seenIntrinsic = true; |
---|
231 | } else if ( seenIntrinsic ) { |
---|
232 | seenIntrinsic = false; // break on this line when debugging for end of prelude |
---|
233 | } |
---|
234 | |
---|
235 | *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) ); |
---|
236 | assert( *i ); |
---|
237 | } // if |
---|
238 | } catch( SemanticError &e ) { |
---|
239 | errors.append( e ); |
---|
240 | } // try |
---|
241 | } // for |
---|
242 | if ( ! errors.isEmpty() ) { |
---|
243 | throw errors; |
---|
244 | } // if |
---|
245 | } |
---|
246 | |
---|
247 | void box( std::list< Declaration *>& translationUnit ) { |
---|
248 | LayoutFunctionBuilder layoutBuilder; |
---|
249 | Pass1 pass1; |
---|
250 | Pass2 pass2; |
---|
251 | PolyGenericCalculator polyCalculator; |
---|
252 | Pass3 pass3; |
---|
253 | |
---|
254 | layoutBuilder.mutateDeclarationList( translationUnit ); |
---|
255 | mutateTranslationUnit/*All*/( translationUnit, pass1 ); |
---|
256 | mutateTranslationUnit/*All*/( translationUnit, pass2 ); |
---|
257 | mutateTranslationUnit/*All*/( translationUnit, polyCalculator ); |
---|
258 | mutateTranslationUnit/*All*/( translationUnit, pass3 ); |
---|
259 | } |
---|
260 | |
---|
261 | ////////////////////////////////// LayoutFunctionBuilder //////////////////////////////////////////// |
---|
262 | |
---|
263 | DeclarationWithType *LayoutFunctionBuilder::mutate( FunctionDecl *functionDecl ) { |
---|
264 | functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); |
---|
265 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
266 | ++functionNesting; |
---|
267 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
268 | --functionNesting; |
---|
269 | return functionDecl; |
---|
270 | } |
---|
271 | |
---|
272 | /// Get a list of type declarations that will affect a layout function |
---|
273 | std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) { |
---|
274 | std::list< TypeDecl * > otypeDecls; |
---|
275 | |
---|
276 | for ( std::list< TypeDecl* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) { |
---|
277 | if ( (*decl)->get_kind() == TypeDecl::Any ) { |
---|
278 | otypeDecls.push_back( *decl ); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | return otypeDecls; |
---|
283 | } |
---|
284 | |
---|
285 | /// Adds parameters for otype layout to a function type |
---|
286 | void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) { |
---|
287 | BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
288 | |
---|
289 | for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { |
---|
290 | TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param ); |
---|
291 | std::string paramName = mangleType( ¶mType ); |
---|
292 | layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); |
---|
293 | layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | /// Builds a layout function declaration |
---|
298 | FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) { |
---|
299 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
300 | // because each unit generates copies of the default routines for each aggregate. |
---|
301 | FunctionDecl *layoutDecl = new FunctionDecl( |
---|
302 | layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false ); |
---|
303 | layoutDecl->fixUniqueId(); |
---|
304 | return layoutDecl; |
---|
305 | } |
---|
306 | |
---|
307 | /// Makes a unary operation |
---|
308 | Expression *makeOp( const std::string &name, Expression *arg ) { |
---|
309 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); |
---|
310 | expr->get_args().push_back( arg ); |
---|
311 | return expr; |
---|
312 | } |
---|
313 | |
---|
314 | /// Makes a binary operation |
---|
315 | Expression *makeOp( const std::string &name, Expression *lhs, Expression *rhs ) { |
---|
316 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); |
---|
317 | expr->get_args().push_back( lhs ); |
---|
318 | expr->get_args().push_back( rhs ); |
---|
319 | return expr; |
---|
320 | } |
---|
321 | |
---|
322 | /// Returns the dereference of a local pointer variable |
---|
323 | Expression *derefVar( ObjectDecl *var ) { |
---|
324 | return makeOp( "*?", new VariableExpr( var ) ); |
---|
325 | } |
---|
326 | |
---|
327 | /// makes an if-statement with a single-expression if-block and no then block |
---|
328 | Statement *makeCond( Expression *cond, Expression *ifPart ) { |
---|
329 | return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 ); |
---|
330 | } |
---|
331 | |
---|
332 | /// makes a statement that assigns rhs to lhs if lhs < rhs |
---|
333 | Statement *makeAssignMax( Expression *lhs, Expression *rhs ) { |
---|
334 | return makeCond( makeOp( "?<?", lhs, rhs ), makeOp( "?=?", lhs->clone(), rhs->clone() ) ); |
---|
335 | } |
---|
336 | |
---|
337 | /// makes a statement that aligns lhs to rhs (rhs should be an integer power of two) |
---|
338 | Statement *makeAlignTo( Expression *lhs, Expression *rhs ) { |
---|
339 | // check that the lhs is zeroed out to the level of rhs |
---|
340 | Expression *ifCond = makeOp( "?&?", lhs, makeOp( "?-?", rhs, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "1" ) ) ) ); |
---|
341 | // if not aligned, increment to alignment |
---|
342 | Expression *ifExpr = makeOp( "?+=?", lhs->clone(), makeOp( "?-?", rhs->clone(), ifCond->clone() ) ); |
---|
343 | return makeCond( ifCond, ifExpr ); |
---|
344 | } |
---|
345 | |
---|
346 | /// adds an expression to a compound statement |
---|
347 | void addExpr( CompoundStmt *stmts, Expression *expr ) { |
---|
348 | stmts->get_kids().push_back( new ExprStmt( noLabels, expr ) ); |
---|
349 | } |
---|
350 | |
---|
351 | /// adds a statement to a compound statement |
---|
352 | void addStmt( CompoundStmt *stmts, Statement *stmt ) { |
---|
353 | stmts->get_kids().push_back( stmt ); |
---|
354 | } |
---|
355 | |
---|
356 | Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) { |
---|
357 | // do not generate layout function for "empty" tag structs |
---|
358 | if ( structDecl->get_members().empty() ) return structDecl; |
---|
359 | |
---|
360 | // get parameters that can change layout, exiting early if none |
---|
361 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( structDecl->get_parameters() ); |
---|
362 | if ( otypeParams.empty() ) return structDecl; |
---|
363 | |
---|
364 | // build layout function signature |
---|
365 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); |
---|
366 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
367 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); |
---|
368 | |
---|
369 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); |
---|
370 | layoutFnType->get_parameters().push_back( sizeParam ); |
---|
371 | ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
372 | layoutFnType->get_parameters().push_back( alignParam ); |
---|
373 | ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
374 | layoutFnType->get_parameters().push_back( offsetParam ); |
---|
375 | addOtypeParams( layoutFnType, otypeParams ); |
---|
376 | |
---|
377 | // build function decl |
---|
378 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType ); |
---|
379 | |
---|
380 | // calculate struct layout in function body |
---|
381 | |
---|
382 | // initialize size and alignment to 0 and 1 (will have at least one member to re-edit size |
---|
383 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "0" ) ) ) ); |
---|
384 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
385 | unsigned long n_members = 0; |
---|
386 | bool firstMember = true; |
---|
387 | for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) { |
---|
388 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); |
---|
389 | assert( dwt ); |
---|
390 | Type *memberType = dwt->get_type(); |
---|
391 | |
---|
392 | if ( firstMember ) { |
---|
393 | firstMember = false; |
---|
394 | } else { |
---|
395 | // make sure all members after the first (automatically aligned at 0) are properly padded for alignment |
---|
396 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
397 | } |
---|
398 | |
---|
399 | // place current size in the current offset index |
---|
400 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( n_members ) ) ), |
---|
401 | derefVar( sizeParam ) ) ); |
---|
402 | ++n_members; |
---|
403 | |
---|
404 | // add member size to current size |
---|
405 | addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); |
---|
406 | |
---|
407 | // take max of member alignment and global alignment |
---|
408 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
409 | } |
---|
410 | // make sure the type is end-padded to a multiple of its alignment |
---|
411 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); |
---|
412 | |
---|
413 | addDeclarationAfter( layoutDecl ); |
---|
414 | return structDecl; |
---|
415 | } |
---|
416 | |
---|
417 | Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) { |
---|
418 | // do not generate layout function for "empty" tag unions |
---|
419 | if ( unionDecl->get_members().empty() ) return unionDecl; |
---|
420 | |
---|
421 | // get parameters that can change layout, exiting early if none |
---|
422 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( unionDecl->get_parameters() ); |
---|
423 | if ( otypeParams.empty() ) return unionDecl; |
---|
424 | |
---|
425 | // build layout function signature |
---|
426 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); |
---|
427 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
428 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); |
---|
429 | |
---|
430 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); |
---|
431 | layoutFnType->get_parameters().push_back( sizeParam ); |
---|
432 | ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); |
---|
433 | layoutFnType->get_parameters().push_back( alignParam ); |
---|
434 | addOtypeParams( layoutFnType, otypeParams ); |
---|
435 | |
---|
436 | // build function decl |
---|
437 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType ); |
---|
438 | |
---|
439 | // calculate union layout in function body |
---|
440 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
441 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); |
---|
442 | for ( std::list< Declaration* >::const_iterator member = unionDecl->get_members().begin(); member != unionDecl->get_members().end(); ++member ) { |
---|
443 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); |
---|
444 | assert( dwt ); |
---|
445 | Type *memberType = dwt->get_type(); |
---|
446 | |
---|
447 | // take max member size and global size |
---|
448 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); |
---|
449 | |
---|
450 | // take max of member alignment and global alignment |
---|
451 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); |
---|
452 | } |
---|
453 | // make sure the type is end-padded to a multiple of its alignment |
---|
454 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); |
---|
455 | |
---|
456 | addDeclarationAfter( layoutDecl ); |
---|
457 | return unionDecl; |
---|
458 | } |
---|
459 | |
---|
460 | ////////////////////////////////////////// Pass1 //////////////////////////////////////////////////// |
---|
461 | |
---|
462 | namespace { |
---|
463 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) { |
---|
464 | std::stringstream name; |
---|
465 | |
---|
466 | // NOTE: this function previously used isPolyObj, which failed to produce |
---|
467 | // the correct thing in some situations. It's not clear to me why this wasn't working. |
---|
468 | |
---|
469 | // if the return type or a parameter type involved polymorphic types, then the adapter will need |
---|
470 | // to take those polymorphic types as pointers. Therefore, there can be two different functions |
---|
471 | // with the same mangled name, so we need to further mangle the names. |
---|
472 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) { |
---|
473 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) { |
---|
474 | name << "P"; |
---|
475 | } else { |
---|
476 | name << "M"; |
---|
477 | } |
---|
478 | } |
---|
479 | name << "_"; |
---|
480 | std::list< DeclarationWithType *> ¶mList = function->get_parameters(); |
---|
481 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
482 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) { |
---|
483 | name << "P"; |
---|
484 | } else { |
---|
485 | name << "M"; |
---|
486 | } |
---|
487 | } // for |
---|
488 | return name.str(); |
---|
489 | } |
---|
490 | |
---|
491 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) { |
---|
492 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars ); |
---|
493 | } |
---|
494 | |
---|
495 | std::string makeAdapterName( const std::string &mangleName ) { |
---|
496 | return "_adapter" + mangleName; |
---|
497 | } |
---|
498 | |
---|
499 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {} |
---|
500 | |
---|
501 | /// Returns T if the given declaration is a function with parameter (T*) for some TypeInstType T, NULL otherwise |
---|
502 | TypeInstType *isTypeInstPtrFn( DeclarationWithType *decl ) { |
---|
503 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
504 | if ( funType->get_parameters().size() == 1 ) { |
---|
505 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { |
---|
506 | if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { |
---|
507 | return refType; |
---|
508 | } // if |
---|
509 | } // if |
---|
510 | } // if |
---|
511 | } // if |
---|
512 | return 0; |
---|
513 | } |
---|
514 | |
---|
515 | /// Returns T if the given declaration is a function with parameters (T*, T) for some TypeInstType T, NULL otherwise |
---|
516 | TypeInstType *isTypeInstPtrValFn( DeclarationWithType *decl ) { |
---|
517 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
518 | if ( funType->get_parameters().size() == 2 ) { |
---|
519 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { |
---|
520 | if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { |
---|
521 | if ( TypeInstType *refType2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { |
---|
522 | if ( refType->get_name() == refType2->get_name() ) { |
---|
523 | return refType; |
---|
524 | } // if |
---|
525 | } // if |
---|
526 | } // if |
---|
527 | } // if |
---|
528 | } // if |
---|
529 | } // if |
---|
530 | return 0; |
---|
531 | } |
---|
532 | |
---|
533 | /// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise |
---|
534 | TypeInstType *isTypeInstAssignment( DeclarationWithType *decl ) { |
---|
535 | return decl->get_name() == "?=?" ? isTypeInstPtrValFn( decl ) : 0; |
---|
536 | } |
---|
537 | |
---|
538 | /// Returns T if the given declaration is (*?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise |
---|
539 | TypeInstType *isTypeInstCtor( DeclarationWithType *decl ) { |
---|
540 | return decl->get_name() == "?{}" ? isTypeInstPtrFn( decl ) : 0; |
---|
541 | } |
---|
542 | |
---|
543 | /// Returns T if the given declaration is (*?{})(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise |
---|
544 | TypeInstType *isTypeInstCopy( DeclarationWithType *decl ) { |
---|
545 | return decl->get_name() == "?{}" ? isTypeInstPtrValFn( decl ) : 0; |
---|
546 | } |
---|
547 | |
---|
548 | /// Returns T if the given declaration is (*^?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise |
---|
549 | TypeInstType *isTypeInstDtor( DeclarationWithType *decl ) { |
---|
550 | return decl->get_name() == "^?{}" ? isTypeInstPtrFn( decl ) : 0; |
---|
551 | } |
---|
552 | |
---|
553 | /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified, |
---|
554 | /// NULL otherwise |
---|
555 | Type *isNoCvPtrFn( DeclarationWithType *decl ) { |
---|
556 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
557 | if ( funType->get_parameters().size() == 1 ) { |
---|
558 | Type::Qualifiers defaultQualifiers; |
---|
559 | Type *paramType = funType->get_parameters().front()->get_type(); |
---|
560 | if ( paramType->get_qualifiers() != defaultQualifiers ) return 0; |
---|
561 | |
---|
562 | if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType ) ) { |
---|
563 | Type *baseType = pointerType->get_base(); |
---|
564 | if ( baseType->get_qualifiers() == defaultQualifiers ) { |
---|
565 | return baseType; |
---|
566 | } // if |
---|
567 | } // if |
---|
568 | } // if |
---|
569 | } // if |
---|
570 | return 0; |
---|
571 | } |
---|
572 | |
---|
573 | /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified, |
---|
574 | /// NULL otherwise |
---|
575 | Type *isNoCvPtrValFn( DeclarationWithType *decl ) { |
---|
576 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { |
---|
577 | if ( funType->get_parameters().size() == 2 ) { |
---|
578 | Type::Qualifiers defaultQualifiers; |
---|
579 | Type *paramType1 = funType->get_parameters().front()->get_type(); |
---|
580 | if ( paramType1->get_qualifiers() != defaultQualifiers ) return 0; |
---|
581 | Type *paramType2 = funType->get_parameters().back()->get_type(); |
---|
582 | if ( paramType2->get_qualifiers() != defaultQualifiers ) return 0; |
---|
583 | |
---|
584 | if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType1 ) ) { |
---|
585 | Type *baseType1 = pointerType->get_base(); |
---|
586 | if ( baseType1->get_qualifiers() != defaultQualifiers ) return 0; |
---|
587 | SymTab::Indexer dummy; |
---|
588 | if ( ResolvExpr::typesCompatible( baseType1, paramType2, dummy ) ) { |
---|
589 | return baseType1; |
---|
590 | } // if |
---|
591 | } // if |
---|
592 | } // if |
---|
593 | } // if |
---|
594 | return 0; |
---|
595 | } |
---|
596 | |
---|
597 | /// returns T if the given declaration is: (*?=?)(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise |
---|
598 | /// Only picks assignments where neither parameter is cv-qualified |
---|
599 | Type *isAssignment( DeclarationWithType *decl ) { |
---|
600 | return decl->get_name() == "?=?" ? isNoCvPtrValFn( decl ) : 0; |
---|
601 | } |
---|
602 | |
---|
603 | /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise |
---|
604 | /// Only picks ctors where the parameter is not cv-qualified |
---|
605 | Type *isCtor( DeclarationWithType *decl ) { |
---|
606 | return decl->get_name() == "?{}" ? isNoCvPtrFn( decl ) : 0; |
---|
607 | } |
---|
608 | |
---|
609 | /// returns T if the given declaration is: (*?{})(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise |
---|
610 | /// Only picks copy constructors where neither parameter is cv-qualified |
---|
611 | Type *isCopy( DeclarationWithType *decl ) { |
---|
612 | return decl->get_name() == "?{}" ? isNoCvPtrValFn( decl ) : 0; |
---|
613 | } |
---|
614 | |
---|
615 | /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise |
---|
616 | /// Only picks ctors where the parameter is not cv-qualified |
---|
617 | Type *isDtor( DeclarationWithType *decl ) { |
---|
618 | return decl->get_name() == "^?{}" ? isNoCvPtrFn( decl ) : 0; |
---|
619 | } |
---|
620 | |
---|
621 | void Pass1::findTypeOps( const Type::ForallList &forall ) { |
---|
622 | // what if a nested function uses an assignment operator? |
---|
623 | // assignOps.clear(); |
---|
624 | for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) { |
---|
625 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
626 | std::string typeName; |
---|
627 | if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) { |
---|
628 | assignOps[ typeInst->get_name() ] = *assert; |
---|
629 | } else if ( TypeInstType *typeInst = isTypeInstCtor( *assert ) ) { |
---|
630 | ctorOps[ typeInst->get_name() ] = *assert; |
---|
631 | } else if ( TypeInstType *typeInst = isTypeInstCopy( *assert ) ) { |
---|
632 | copyOps[ typeInst->get_name() ] = *assert; |
---|
633 | } else if ( TypeInstType *typeInst = isTypeInstDtor( *assert ) ) { |
---|
634 | dtorOps[ typeInst->get_name() ] = *assert; |
---|
635 | } // if |
---|
636 | } // for |
---|
637 | } // for |
---|
638 | } |
---|
639 | |
---|
640 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { |
---|
641 | // if this is a assignment function, put it in the map for this scope |
---|
642 | if ( Type *paramType = isAssignment( functionDecl ) ) { |
---|
643 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { |
---|
644 | scopedAssignOps.insert( paramType, functionDecl ); |
---|
645 | } |
---|
646 | } else if ( Type *paramType = isCtor( functionDecl ) ) { |
---|
647 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { |
---|
648 | scopedCtorOps.insert( paramType, functionDecl ); |
---|
649 | } |
---|
650 | } else if ( Type *paramType = isCopy( functionDecl ) ) { |
---|
651 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { |
---|
652 | scopedCopyOps.insert( paramType, functionDecl ); |
---|
653 | } |
---|
654 | } else if ( Type *paramType = isDtor( functionDecl ) ) { |
---|
655 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { |
---|
656 | scopedDtorOps.insert( paramType, functionDecl ); |
---|
657 | } |
---|
658 | } |
---|
659 | |
---|
660 | if ( functionDecl->get_statements() ) { // empty routine body ? |
---|
661 | doBeginScope(); |
---|
662 | scopeTyVars.beginScope(); |
---|
663 | assignOps.beginScope(); |
---|
664 | ctorOps.beginScope(); |
---|
665 | copyOps.beginScope(); |
---|
666 | dtorOps.beginScope(); |
---|
667 | |
---|
668 | DeclarationWithType *oldRetval = retval; |
---|
669 | bool oldUseRetval = useRetval; |
---|
670 | |
---|
671 | // process polymorphic return value |
---|
672 | retval = 0; |
---|
673 | if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) { |
---|
674 | retval = functionDecl->get_functionType()->get_returnVals().front(); |
---|
675 | |
---|
676 | // give names to unnamed return values |
---|
677 | if ( retval->get_name() == "" ) { |
---|
678 | retval->set_name( "_retparm" ); |
---|
679 | retval->set_linkage( LinkageSpec::C ); |
---|
680 | } // if |
---|
681 | } // if |
---|
682 | |
---|
683 | FunctionType *functionType = functionDecl->get_functionType(); |
---|
684 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); |
---|
685 | findTypeOps( functionDecl->get_functionType()->get_forall() ); |
---|
686 | |
---|
687 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
688 | std::list< FunctionType *> functions; |
---|
689 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
690 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
691 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
692 | } // for |
---|
693 | } // for |
---|
694 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
695 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter ); |
---|
696 | } // for |
---|
697 | |
---|
698 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
699 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
700 | if ( adapters.find( mangleName ) == adapters.end() ) { |
---|
701 | std::string adapterName = makeAdapterName( mangleName ); |
---|
702 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) ); |
---|
703 | } // if |
---|
704 | } // for |
---|
705 | |
---|
706 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); |
---|
707 | |
---|
708 | scopeTyVars.endScope(); |
---|
709 | assignOps.endScope(); |
---|
710 | ctorOps.endScope(); |
---|
711 | copyOps.endScope(); |
---|
712 | dtorOps.endScope(); |
---|
713 | retval = oldRetval; |
---|
714 | useRetval = oldUseRetval; |
---|
715 | doEndScope(); |
---|
716 | } // if |
---|
717 | return functionDecl; |
---|
718 | } |
---|
719 | |
---|
720 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { |
---|
721 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
722 | return Mutator::mutate( typeDecl ); |
---|
723 | } |
---|
724 | |
---|
725 | Expression *Pass1::mutate( CommaExpr *commaExpr ) { |
---|
726 | bool oldUseRetval = useRetval; |
---|
727 | useRetval = false; |
---|
728 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) ); |
---|
729 | useRetval = oldUseRetval; |
---|
730 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) ); |
---|
731 | return commaExpr; |
---|
732 | } |
---|
733 | |
---|
734 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) { |
---|
735 | bool oldUseRetval = useRetval; |
---|
736 | useRetval = false; |
---|
737 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) ); |
---|
738 | useRetval = oldUseRetval; |
---|
739 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) ); |
---|
740 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) ); |
---|
741 | return condExpr; |
---|
742 | |
---|
743 | } |
---|
744 | |
---|
745 | void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { |
---|
746 | Type *polyType = isPolyType( parmType, exprTyVars ); |
---|
747 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
748 | std::string typeName = mangleType( polyType ); |
---|
749 | if ( seenTypes.count( typeName ) ) return; |
---|
750 | |
---|
751 | arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); |
---|
752 | arg++; |
---|
753 | arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); |
---|
754 | arg++; |
---|
755 | if ( dynamic_cast< StructInstType* >( polyType ) ) { |
---|
756 | if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { |
---|
757 | // zero-length arrays are forbidden by C, so don't pass offset for empty struct |
---|
758 | if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) { |
---|
759 | arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) ); |
---|
760 | arg++; |
---|
761 | } |
---|
762 | } else { |
---|
763 | throw SemanticError( "Cannot pass non-struct type for generic struct" ); |
---|
764 | } |
---|
765 | } |
---|
766 | |
---|
767 | seenTypes.insert( typeName ); |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
772 | // pass size/align for type variables |
---|
773 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { |
---|
774 | ResolvExpr::EqvClass eqvClass; |
---|
775 | assert( env ); |
---|
776 | if ( tyParm->second == TypeDecl::Any ) { |
---|
777 | Type *concrete = env->lookup( tyParm->first ); |
---|
778 | if ( concrete ) { |
---|
779 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); |
---|
780 | arg++; |
---|
781 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) ); |
---|
782 | arg++; |
---|
783 | } else { |
---|
784 | // xxx - should this be an assertion? |
---|
785 | throw SemanticError( "unbound type variable: " + tyParm->first + " in application ", appExpr ); |
---|
786 | } // if |
---|
787 | } // if |
---|
788 | } // for |
---|
789 | |
---|
790 | // add size/align for generic types to parameter list |
---|
791 | if ( ! appExpr->get_function()->has_result() ) return; |
---|
792 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_result() ); |
---|
793 | assert( funcType ); |
---|
794 | |
---|
795 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); |
---|
796 | std::list< Expression* >::const_iterator fnArg = arg; |
---|
797 | std::set< std::string > seenTypes; ///< names for generic types we've seen |
---|
798 | |
---|
799 | // a polymorphic return type may need to be added to the argument list |
---|
800 | if ( polyRetType ) { |
---|
801 | Type *concRetType = replaceWithConcrete( appExpr, polyRetType ); |
---|
802 | passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes ); |
---|
803 | } |
---|
804 | |
---|
805 | // add type information args for presently unseen types in parameter list |
---|
806 | for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) { |
---|
807 | VariableExpr *fnArgBase = getBaseVar( *fnArg ); |
---|
808 | if ( ! fnArgBase ) continue; // xxx - previously had check for non-empty fnArgBase results |
---|
809 | passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_result(), arg, exprTyVars, seenTypes ); |
---|
810 | } |
---|
811 | } |
---|
812 | |
---|
813 | ObjectDecl *Pass1::makeTemporary( Type *type ) { |
---|
814 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 ); |
---|
815 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
816 | return newObj; |
---|
817 | } |
---|
818 | |
---|
819 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { |
---|
820 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
821 | // if ( useRetval ) { |
---|
822 | // assert( retval ); |
---|
823 | // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) ); |
---|
824 | // arg++; |
---|
825 | // } else { |
---|
826 | |
---|
827 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result |
---|
828 | // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple |
---|
829 | // return values. |
---|
830 | ObjectDecl *newObj = makeTemporary( retType->clone() ); |
---|
831 | Expression *paramExpr = new VariableExpr( newObj ); |
---|
832 | |
---|
833 | // If the type of the temporary is not polymorphic, box temporary by taking its address; |
---|
834 | // otherwise the temporary is already boxed and can be used directly. |
---|
835 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { |
---|
836 | paramExpr = new AddressExpr( paramExpr ); |
---|
837 | } // if |
---|
838 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call |
---|
839 | arg++; |
---|
840 | // Build a comma expression to call the function and emulate a normal return. |
---|
841 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); |
---|
842 | commaExpr->set_env( appExpr->get_env() ); |
---|
843 | appExpr->set_env( 0 ); |
---|
844 | return commaExpr; |
---|
845 | // } // if |
---|
846 | // return appExpr; |
---|
847 | } |
---|
848 | |
---|
849 | void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) { |
---|
850 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
851 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
852 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
853 | paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) ); |
---|
854 | } |
---|
855 | } |
---|
856 | |
---|
857 | Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) { |
---|
858 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
859 | Type *concrete = env->lookup( typeInst->get_name() ); |
---|
860 | if ( concrete == 0 ) { |
---|
861 | throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr ); |
---|
862 | } // if |
---|
863 | return concrete; |
---|
864 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
865 | if ( doClone ) { |
---|
866 | structType = structType->clone(); |
---|
867 | } |
---|
868 | replaceParametersWithConcrete( appExpr, structType->get_parameters() ); |
---|
869 | return structType; |
---|
870 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
871 | if ( doClone ) { |
---|
872 | unionType = unionType->clone(); |
---|
873 | } |
---|
874 | replaceParametersWithConcrete( appExpr, unionType->get_parameters() ); |
---|
875 | return unionType; |
---|
876 | } |
---|
877 | return type; |
---|
878 | } |
---|
879 | |
---|
880 | Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *dynType, std::list< Expression *>::iterator &arg ) { |
---|
881 | assert( env ); |
---|
882 | Type *concrete = replaceWithConcrete( appExpr, dynType ); |
---|
883 | // add out-parameter for return value |
---|
884 | return addRetParam( appExpr, function, concrete, arg ); |
---|
885 | } |
---|
886 | |
---|
887 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
888 | Expression *ret = appExpr; |
---|
889 | // if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
890 | if ( isDynRet( function, tyVars ) ) { |
---|
891 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); |
---|
892 | } // if |
---|
893 | std::string mangleName = mangleAdapterName( function, tyVars ); |
---|
894 | std::string adapterName = makeAdapterName( mangleName ); |
---|
895 | |
---|
896 | // cast adaptee to void (*)(), since it may have any type inside a polymorphic function |
---|
897 | Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ); |
---|
898 | appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) ); |
---|
899 | appExpr->set_function( new NameExpr( adapterName ) ); // xxx - result is never set on NameExpr |
---|
900 | |
---|
901 | return ret; |
---|
902 | } |
---|
903 | |
---|
904 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { |
---|
905 | assert( arg->has_result() ); |
---|
906 | if ( isPolyType( param, exprTyVars ) ) { |
---|
907 | if ( isPolyType( arg->get_result() ) ) { |
---|
908 | // if the argument's type is polymorphic, we don't need to box again! |
---|
909 | return; |
---|
910 | } else if ( arg->get_result()->get_isLvalue() ) { |
---|
911 | // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue) |
---|
912 | // xxx - need to test that this code is still reachable |
---|
913 | if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) { |
---|
914 | commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) ); |
---|
915 | } else { |
---|
916 | arg = new AddressExpr( arg ); |
---|
917 | } |
---|
918 | } else { |
---|
919 | // use type computed in unification to declare boxed variables |
---|
920 | Type * newType = param->clone(); |
---|
921 | if ( env ) env->apply( newType ); |
---|
922 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, newType, 0 ); |
---|
923 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right??? |
---|
924 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
925 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
926 | assign->get_args().push_back( new VariableExpr( newObj ) ); |
---|
927 | assign->get_args().push_back( arg ); |
---|
928 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) ); |
---|
929 | arg = new AddressExpr( new VariableExpr( newObj ) ); |
---|
930 | } // if |
---|
931 | } // if |
---|
932 | } |
---|
933 | |
---|
934 | /// cast parameters to polymorphic functions so that types are replaced with |
---|
935 | /// void * if they are type parameters in the formal type. |
---|
936 | /// this gets rid of warnings from gcc. |
---|
937 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) { |
---|
938 | if ( getFunctionType( formal ) ) { |
---|
939 | Type * newType = formal->clone(); |
---|
940 | newType = ScrubTyVars::scrub( newType, tyVars ); |
---|
941 | actual = new CastExpr( actual, newType ); |
---|
942 | } // if |
---|
943 | } |
---|
944 | |
---|
945 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { |
---|
946 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { |
---|
947 | assert( arg != appExpr->get_args().end() ); |
---|
948 | addCast( *arg, (*param)->get_type(), exprTyVars ); |
---|
949 | boxParam( (*param)->get_type(), *arg, exprTyVars ); |
---|
950 | } // for |
---|
951 | } |
---|
952 | |
---|
953 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { |
---|
954 | std::list< Expression *>::iterator cur = arg; |
---|
955 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
956 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
957 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() ); |
---|
958 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" ); |
---|
959 | Expression *newExpr = inferParam->second.expr->clone(); |
---|
960 | addCast( newExpr, (*assert)->get_type(), tyVars ); |
---|
961 | boxParam( (*assert)->get_type(), newExpr, tyVars ); |
---|
962 | appExpr->get_args().insert( cur, newExpr ); |
---|
963 | } // for |
---|
964 | } // for |
---|
965 | } |
---|
966 | |
---|
967 | void makeRetParm( FunctionType *funcType ) { |
---|
968 | DeclarationWithType *retParm = funcType->get_returnVals().front(); |
---|
969 | |
---|
970 | // make a new parameter that is a pointer to the type of the old return value |
---|
971 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); |
---|
972 | funcType->get_parameters().push_front( retParm ); |
---|
973 | |
---|
974 | // we don't need the return value any more |
---|
975 | funcType->get_returnVals().clear(); |
---|
976 | } |
---|
977 | |
---|
978 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
979 | // actually make the adapter type |
---|
980 | FunctionType *adapter = adaptee->clone(); |
---|
981 | // if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
982 | if ( isDynRet( adapter, tyVars ) ) { |
---|
983 | makeRetParm( adapter ); |
---|
984 | } // if |
---|
985 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) ); |
---|
986 | return adapter; |
---|
987 | } |
---|
988 | |
---|
989 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { |
---|
990 | assert( param ); |
---|
991 | assert( arg ); |
---|
992 | if ( isPolyType( realParam->get_type(), tyVars ) ) { |
---|
993 | if ( ! isPolyType( arg->get_type() ) ) { |
---|
994 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
995 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); |
---|
996 | deref->set_result( arg->get_type()->clone() ); |
---|
997 | return deref; |
---|
998 | } // if |
---|
999 | } // if |
---|
1000 | return new VariableExpr( param ); |
---|
1001 | } |
---|
1002 | |
---|
1003 | 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 ) { |
---|
1004 | UniqueName paramNamer( "_p" ); |
---|
1005 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { |
---|
1006 | if ( (*param)->get_name() == "" ) { |
---|
1007 | (*param)->set_name( paramNamer.newName() ); |
---|
1008 | (*param)->set_linkage( LinkageSpec::C ); |
---|
1009 | } // if |
---|
1010 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) ); |
---|
1011 | } // for |
---|
1012 | } |
---|
1013 | |
---|
1014 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { |
---|
1015 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); |
---|
1016 | adapterType = ScrubTyVars::scrub( adapterType, tyVars ); |
---|
1017 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front(); |
---|
1018 | adapteeDecl->set_name( "_adaptee" ); |
---|
1019 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) ); |
---|
1020 | Statement *bodyStmt; |
---|
1021 | |
---|
1022 | Type::ForallList::iterator tyArg = realType->get_forall().begin(); |
---|
1023 | Type::ForallList::iterator tyParam = adapterType->get_forall().begin(); |
---|
1024 | Type::ForallList::iterator realTyParam = adaptee->get_forall().begin(); |
---|
1025 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { |
---|
1026 | assert( tyArg != realType->get_forall().end() ); |
---|
1027 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin(); |
---|
1028 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin(); |
---|
1029 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin(); |
---|
1030 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { |
---|
1031 | assert( assertArg != (*tyArg)->get_assertions().end() ); |
---|
1032 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) ); |
---|
1033 | } // for |
---|
1034 | } // for |
---|
1035 | |
---|
1036 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin(); |
---|
1037 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin(); |
---|
1038 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin(); |
---|
1039 | param++; // skip adaptee parameter in the adapter type |
---|
1040 | if ( realType->get_returnVals().empty() ) { |
---|
1041 | // void return |
---|
1042 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1043 | bodyStmt = new ExprStmt( noLabels, adapteeApp ); |
---|
1044 | // } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
1045 | } else if ( isDynType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
1046 | // return type T |
---|
1047 | if ( (*param)->get_name() == "" ) { |
---|
1048 | (*param)->set_name( "_ret" ); |
---|
1049 | (*param)->set_linkage( LinkageSpec::C ); |
---|
1050 | } // if |
---|
1051 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
1052 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1053 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) ); |
---|
1054 | assign->get_args().push_back( deref ); |
---|
1055 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1056 | assign->get_args().push_back( adapteeApp ); |
---|
1057 | bodyStmt = new ExprStmt( noLabels, assign ); |
---|
1058 | } else { |
---|
1059 | // adapter for a function that returns a monomorphic value |
---|
1060 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); |
---|
1061 | bodyStmt = new ReturnStmt( noLabels, adapteeApp ); |
---|
1062 | } // if |
---|
1063 | CompoundStmt *adapterBody = new CompoundStmt( noLabels ); |
---|
1064 | adapterBody->get_kids().push_back( bodyStmt ); |
---|
1065 | std::string adapterName = makeAdapterName( mangleName ); |
---|
1066 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false ); |
---|
1067 | } |
---|
1068 | |
---|
1069 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { |
---|
1070 | // collect a list of function types passed as parameters or implicit parameters (assertions) |
---|
1071 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
1072 | std::list< FunctionType *> functions; |
---|
1073 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { |
---|
1074 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { |
---|
1075 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
1076 | } // for |
---|
1077 | } // for |
---|
1078 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
1079 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter ); |
---|
1080 | } // for |
---|
1081 | |
---|
1082 | // parameter function types for which an appropriate adapter has been generated. we cannot use the types |
---|
1083 | // after applying substitutions, since two different parameter types may be unified to the same type |
---|
1084 | std::set< std::string > adaptersDone; |
---|
1085 | |
---|
1086 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
1087 | FunctionType *originalFunction = (*funType)->clone(); |
---|
1088 | FunctionType *realFunction = (*funType)->clone(); |
---|
1089 | std::string mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
1090 | |
---|
1091 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this |
---|
1092 | // pre-substitution parameter function type. |
---|
1093 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
1094 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
1095 | |
---|
1096 | // apply substitution to type variables to figure out what the adapter's type should look like |
---|
1097 | assert( env ); |
---|
1098 | env->apply( realFunction ); |
---|
1099 | mangleName = SymTab::Mangler::mangle( realFunction ); |
---|
1100 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars ); |
---|
1101 | |
---|
1102 | typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter; |
---|
1103 | AdapterIter adapter = adapters.find( mangleName ); |
---|
1104 | if ( adapter == adapters.end() ) { |
---|
1105 | // adapter has not been created yet in the current scope, so define it |
---|
1106 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); |
---|
1107 | std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) ); |
---|
1108 | adapter = answer.first; |
---|
1109 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); |
---|
1110 | } // if |
---|
1111 | assert( adapter != adapters.end() ); |
---|
1112 | |
---|
1113 | // add the appropriate adapter as a parameter |
---|
1114 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); |
---|
1115 | } // if |
---|
1116 | } // for |
---|
1117 | } // passAdapters |
---|
1118 | |
---|
1119 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { |
---|
1120 | NameExpr *opExpr; |
---|
1121 | if ( isIncr ) { |
---|
1122 | opExpr = new NameExpr( "?+=?" ); |
---|
1123 | } else { |
---|
1124 | opExpr = new NameExpr( "?-=?" ); |
---|
1125 | } // if |
---|
1126 | UntypedExpr *addAssign = new UntypedExpr( opExpr ); |
---|
1127 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
1128 | addAssign->get_args().push_back( address->get_arg() ); |
---|
1129 | } else { |
---|
1130 | addAssign->get_args().push_back( appExpr->get_args().front() ); |
---|
1131 | } // if |
---|
1132 | addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) ); |
---|
1133 | addAssign->set_result( appExpr->get_result()->clone() ); |
---|
1134 | if ( appExpr->get_env() ) { |
---|
1135 | addAssign->set_env( appExpr->get_env() ); |
---|
1136 | appExpr->set_env( 0 ); |
---|
1137 | } // if |
---|
1138 | appExpr->get_args().clear(); |
---|
1139 | delete appExpr; |
---|
1140 | return addAssign; |
---|
1141 | } |
---|
1142 | |
---|
1143 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) { |
---|
1144 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) { |
---|
1145 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
1146 | if ( varExpr->get_var()->get_name() == "?[?]" ) { |
---|
1147 | assert( appExpr->has_result() ); |
---|
1148 | assert( appExpr->get_args().size() == 2 ); |
---|
1149 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env ); |
---|
1150 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env ); |
---|
1151 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers |
---|
1152 | UntypedExpr *ret = 0; |
---|
1153 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer |
---|
1154 | ret = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
1155 | } // if |
---|
1156 | if ( baseType1 ) { |
---|
1157 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1158 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1159 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1160 | ret->get_args().push_back( appExpr->get_args().front() ); |
---|
1161 | ret->get_args().push_back( multiply ); |
---|
1162 | } else if ( baseType2 ) { |
---|
1163 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1164 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
1165 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); |
---|
1166 | ret->get_args().push_back( multiply ); |
---|
1167 | ret->get_args().push_back( appExpr->get_args().back() ); |
---|
1168 | } // if |
---|
1169 | if ( baseType1 || baseType2 ) { |
---|
1170 | ret->set_result( appExpr->get_result()->clone() ); |
---|
1171 | if ( appExpr->get_env() ) { |
---|
1172 | ret->set_env( appExpr->get_env() ); |
---|
1173 | appExpr->set_env( 0 ); |
---|
1174 | } // if |
---|
1175 | appExpr->get_args().clear(); |
---|
1176 | delete appExpr; |
---|
1177 | return ret; |
---|
1178 | } // if |
---|
1179 | } else if ( varExpr->get_var()->get_name() == "*?" ) { |
---|
1180 | assert( appExpr->has_result() ); |
---|
1181 | assert( ! appExpr->get_args().empty() ); |
---|
1182 | if ( isPolyType( appExpr->get_result(), scopeTyVars, env ) ) { |
---|
1183 | Expression *ret = appExpr->get_args().front(); |
---|
1184 | delete ret->get_result(); |
---|
1185 | ret->set_result( appExpr->get_result()->clone() ); |
---|
1186 | if ( appExpr->get_env() ) { |
---|
1187 | ret->set_env( appExpr->get_env() ); |
---|
1188 | appExpr->set_env( 0 ); |
---|
1189 | } // if |
---|
1190 | appExpr->get_args().clear(); |
---|
1191 | delete appExpr; |
---|
1192 | return ret; |
---|
1193 | } // if |
---|
1194 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) { |
---|
1195 | assert( appExpr->has_result() ); |
---|
1196 | assert( appExpr->get_args().size() == 1 ); |
---|
1197 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) { |
---|
1198 | Type *tempType = appExpr->get_result()->clone(); |
---|
1199 | if ( env ) { |
---|
1200 | env->apply( tempType ); |
---|
1201 | } // if |
---|
1202 | ObjectDecl *newObj = makeTemporary( tempType ); |
---|
1203 | VariableExpr *tempExpr = new VariableExpr( newObj ); |
---|
1204 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
1205 | assignExpr->get_args().push_back( tempExpr->clone() ); |
---|
1206 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { |
---|
1207 | assignExpr->get_args().push_back( address->get_arg()->clone() ); |
---|
1208 | } else { |
---|
1209 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); |
---|
1210 | } // if |
---|
1211 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) ); |
---|
1212 | return new CommaExpr( firstComma, tempExpr ); |
---|
1213 | } // if |
---|
1214 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) { |
---|
1215 | assert( appExpr->has_result() ); |
---|
1216 | assert( appExpr->get_args().size() == 1 ); |
---|
1217 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) { |
---|
1218 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" ); |
---|
1219 | } // if |
---|
1220 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { |
---|
1221 | assert( appExpr->has_result() ); |
---|
1222 | assert( appExpr->get_args().size() == 2 ); |
---|
1223 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env ); |
---|
1224 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env ); |
---|
1225 | if ( baseType1 && baseType2 ) { |
---|
1226 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); |
---|
1227 | divide->get_args().push_back( appExpr ); |
---|
1228 | divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1229 | divide->set_result( appExpr->get_result()->clone() ); |
---|
1230 | if ( appExpr->get_env() ) { |
---|
1231 | divide->set_env( appExpr->get_env() ); |
---|
1232 | appExpr->set_env( 0 ); |
---|
1233 | } // if |
---|
1234 | return divide; |
---|
1235 | } else if ( baseType1 ) { |
---|
1236 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1237 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1238 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); |
---|
1239 | appExpr->get_args().back() = multiply; |
---|
1240 | } else if ( baseType2 ) { |
---|
1241 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1242 | multiply->get_args().push_back( appExpr->get_args().front() ); |
---|
1243 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); |
---|
1244 | appExpr->get_args().front() = multiply; |
---|
1245 | } // if |
---|
1246 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) { |
---|
1247 | assert( appExpr->has_result() ); |
---|
1248 | assert( appExpr->get_args().size() == 2 ); |
---|
1249 | Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ); |
---|
1250 | if ( baseType ) { |
---|
1251 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); |
---|
1252 | multiply->get_args().push_back( appExpr->get_args().back() ); |
---|
1253 | multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) ); |
---|
1254 | appExpr->get_args().back() = multiply; |
---|
1255 | } // if |
---|
1256 | } // if |
---|
1257 | return appExpr; |
---|
1258 | } // if |
---|
1259 | } // if |
---|
1260 | return 0; |
---|
1261 | } |
---|
1262 | |
---|
1263 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) { |
---|
1264 | // std::cerr << "mutate appExpr: "; |
---|
1265 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { |
---|
1266 | // std::cerr << i->first << " "; |
---|
1267 | // } |
---|
1268 | // std::cerr << "\n"; |
---|
1269 | bool oldUseRetval = useRetval; |
---|
1270 | useRetval = false; |
---|
1271 | appExpr->get_function()->acceptMutator( *this ); |
---|
1272 | mutateAll( appExpr->get_args(), *this ); |
---|
1273 | useRetval = oldUseRetval; |
---|
1274 | |
---|
1275 | assert( appExpr->get_function()->has_result() ); |
---|
1276 | PointerType *pointer = safe_dynamic_cast< PointerType *>( appExpr->get_function()->get_result() ); |
---|
1277 | FunctionType *function = safe_dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
1278 | |
---|
1279 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { |
---|
1280 | return newExpr; |
---|
1281 | } // if |
---|
1282 | |
---|
1283 | Expression *ret = appExpr; |
---|
1284 | |
---|
1285 | std::list< Expression *>::iterator arg = appExpr->get_args().begin(); |
---|
1286 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); |
---|
1287 | |
---|
1288 | TyVarMap exprTyVars( (TypeDecl::Kind)-1 ); |
---|
1289 | makeTyVarMap( function, exprTyVars ); |
---|
1290 | ReferenceToType *dynRetType = isDynRet( function, exprTyVars ); |
---|
1291 | |
---|
1292 | if ( dynRetType ) { |
---|
1293 | ret = addDynRetParam( appExpr, function, dynRetType, arg ); |
---|
1294 | } else if ( needsAdapter( function, scopeTyVars ) ) { |
---|
1295 | // std::cerr << "needs adapter: "; |
---|
1296 | // printTyVarMap( std::cerr, scopeTyVars ); |
---|
1297 | // std::cerr << *env << std::endl; |
---|
1298 | // change the application so it calls the adapter rather than the passed function |
---|
1299 | ret = applyAdapter( appExpr, function, arg, scopeTyVars ); |
---|
1300 | } // if |
---|
1301 | arg = appExpr->get_args().begin(); |
---|
1302 | |
---|
1303 | passTypeVars( appExpr, dynRetType, arg, exprTyVars ); |
---|
1304 | addInferredParams( appExpr, function, arg, exprTyVars ); |
---|
1305 | |
---|
1306 | arg = paramBegin; |
---|
1307 | |
---|
1308 | boxParams( appExpr, function, arg, exprTyVars ); |
---|
1309 | passAdapters( appExpr, function, exprTyVars ); |
---|
1310 | |
---|
1311 | return ret; |
---|
1312 | } |
---|
1313 | |
---|
1314 | Expression *Pass1::mutate( UntypedExpr *expr ) { |
---|
1315 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) { |
---|
1316 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
1317 | if ( name->get_name() == "*?" ) { |
---|
1318 | Expression *ret = expr->get_args().front(); |
---|
1319 | expr->get_args().clear(); |
---|
1320 | delete expr; |
---|
1321 | return ret->acceptMutator( *this ); |
---|
1322 | } // if |
---|
1323 | } // if |
---|
1324 | } // if |
---|
1325 | return PolyMutator::mutate( expr ); |
---|
1326 | } |
---|
1327 | |
---|
1328 | Expression *Pass1::mutate( AddressExpr *addrExpr ) { |
---|
1329 | assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() ); |
---|
1330 | |
---|
1331 | bool needs = false; |
---|
1332 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { |
---|
1333 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) { |
---|
1334 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { |
---|
1335 | if ( name->get_name() == "*?" ) { |
---|
1336 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { |
---|
1337 | assert( appExpr->get_function()->has_result() ); |
---|
1338 | PointerType *pointer = safe_dynamic_cast< PointerType *>( appExpr->get_function()->get_result() ); |
---|
1339 | FunctionType *function = safe_dynamic_cast< FunctionType *>( pointer->get_base() ); |
---|
1340 | needs = needsAdapter( function, scopeTyVars ); |
---|
1341 | } // if |
---|
1342 | } // if |
---|
1343 | } // if |
---|
1344 | } // if |
---|
1345 | } // if |
---|
1346 | // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward |
---|
1347 | // out of the if condition. |
---|
1348 | bool polytype = isPolyType( addrExpr->get_arg()->get_result(), scopeTyVars, env ); |
---|
1349 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); |
---|
1350 | if ( polytype || needs ) { |
---|
1351 | Expression *ret = addrExpr->get_arg(); |
---|
1352 | delete ret->get_result(); |
---|
1353 | ret->set_result( addrExpr->get_result()->clone() ); |
---|
1354 | addrExpr->set_arg( 0 ); |
---|
1355 | delete addrExpr; |
---|
1356 | return ret; |
---|
1357 | } else { |
---|
1358 | return addrExpr; |
---|
1359 | } // if |
---|
1360 | } |
---|
1361 | |
---|
1362 | /// Wraps a function declaration in a new pointer-to-function variable expression |
---|
1363 | VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) { |
---|
1364 | // line below cloned from FixFunction.cc |
---|
1365 | // xxx - functionObj is never added to a list of declarations... |
---|
1366 | ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0, |
---|
1367 | new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 ); |
---|
1368 | functionObj->set_mangleName( functionDecl->get_mangleName() ); |
---|
1369 | functionObj->set_scopeLevel( functionDecl->get_scopeLevel() ); |
---|
1370 | return new VariableExpr( functionObj ); |
---|
1371 | } |
---|
1372 | |
---|
1373 | /// Finds the operation declaration for a given type in one of the two maps |
---|
1374 | DeclarationWithType* findOpForType( Type *formalType, const ScopedMap< std::string, DeclarationWithType* >& ops, ResolvExpr::TypeMap< DeclarationWithType >& scopedOps ) { |
---|
1375 | if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { |
---|
1376 | ScopedMap< std::string, DeclarationWithType *>::const_iterator opIt = ops.find( formalTypeInstType->get_name() ); |
---|
1377 | return opIt == ops.end() ? 0 : opIt->second; |
---|
1378 | } else { |
---|
1379 | return scopedOps.find( formalType ); |
---|
1380 | } |
---|
1381 | } |
---|
1382 | |
---|
1383 | /// Adds an assertion parameter to the application expression for the actual assertion declaration valued with the assert op |
---|
1384 | void addAssertionFor( ApplicationExpr *appExpr, DeclarationWithType *actualDecl, DeclarationWithType *assertOp ) { |
---|
1385 | appExpr->get_inferParams()[ actualDecl->get_uniqueId() ] |
---|
1386 | = ParamEntry( assertOp->get_uniqueId(), assertOp->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertOp ) ); |
---|
1387 | } |
---|
1388 | |
---|
1389 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) { |
---|
1390 | if ( retval && returnStmt->get_expr() ) { |
---|
1391 | assert( returnStmt->get_expr()->has_result() && ! returnStmt->get_expr()->get_result()->isVoid() ); |
---|
1392 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. |
---|
1393 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { |
---|
1394 | // by this point, a cast expr on a polymorphic return value is redundant |
---|
1395 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { |
---|
1396 | returnStmt->set_expr( castExpr->get_arg() ); |
---|
1397 | returnStmt->get_expr()->set_env( castExpr->get_env() ); |
---|
1398 | castExpr->set_env( 0 ); |
---|
1399 | castExpr->set_arg( 0 ); |
---|
1400 | delete castExpr; |
---|
1401 | } //while |
---|
1402 | |
---|
1403 | // find assignment operator for (polymorphic) return type |
---|
1404 | ApplicationExpr *assignExpr = 0; |
---|
1405 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { |
---|
1406 | // find assignment operator for type variable |
---|
1407 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); |
---|
1408 | if ( assignIter == assignOps.end() ) { |
---|
1409 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); |
---|
1410 | } // if |
---|
1411 | assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); |
---|
1412 | } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) { |
---|
1413 | // find assignment operator for generic type |
---|
1414 | DeclarationWithType *functionDecl = scopedAssignOps.find( refType ); |
---|
1415 | if ( ! functionDecl ) { |
---|
1416 | throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() ); |
---|
1417 | } |
---|
1418 | |
---|
1419 | // wrap it up in an application expression |
---|
1420 | assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) ); |
---|
1421 | assignExpr->set_env( env->clone() ); |
---|
1422 | |
---|
1423 | // find each of its needed secondary assignment operators |
---|
1424 | std::list< Expression* > &tyParams = refType->get_parameters(); |
---|
1425 | Type::ForallList &forallParams = functionDecl->get_type()->get_forall(); |
---|
1426 | std::list< Expression* >::const_iterator tyIt = tyParams.begin(); |
---|
1427 | Type::ForallList::const_iterator forallIt = forallParams.begin(); |
---|
1428 | for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) { |
---|
1429 | // Add appropriate mapping to assignment expression environment |
---|
1430 | TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt ); |
---|
1431 | assert( formalTypeExpr && "type parameters must be type expressions" ); |
---|
1432 | Type *formalType = formalTypeExpr->get_type(); |
---|
1433 | assignExpr->get_env()->add( (*forallIt)->get_name(), formalType ); |
---|
1434 | |
---|
1435 | // skip non-otype parameters (ftype/dtype) |
---|
1436 | if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; |
---|
1437 | |
---|
1438 | // find otype operators for formal type |
---|
1439 | DeclarationWithType *assertAssign = findOpForType( formalType, assignOps, scopedAssignOps ); |
---|
1440 | if ( ! assertAssign ) throw SemanticError( "No assignment operation found for ", formalType ); |
---|
1441 | |
---|
1442 | DeclarationWithType *assertCtor = findOpForType( formalType, ctorOps, scopedCtorOps ); |
---|
1443 | if ( ! assertCtor ) throw SemanticError( "No default constructor found for ", formalType ); |
---|
1444 | |
---|
1445 | DeclarationWithType *assertCopy = findOpForType( formalType, copyOps, scopedCopyOps ); |
---|
1446 | if ( ! assertCopy ) throw SemanticError( "No copy constructor found for ", formalType ); |
---|
1447 | |
---|
1448 | DeclarationWithType *assertDtor = findOpForType( formalType, dtorOps, scopedDtorOps ); |
---|
1449 | if ( ! assertDtor ) throw SemanticError( "No destructor found for ", formalType ); |
---|
1450 | |
---|
1451 | // add inferred parameters for otype operators to assignment expression |
---|
1452 | // NOTE: Code here assumes that first four assertions are assign op, ctor, copy ctor, dtor, in that order |
---|
1453 | std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); |
---|
1454 | assert( asserts.size() >= 4 && "Type param needs otype operator assertions" ); |
---|
1455 | |
---|
1456 | std::list< DeclarationWithType* >::iterator actualIt = asserts.begin(); |
---|
1457 | addAssertionFor( assignExpr, *actualIt, assertAssign ); |
---|
1458 | ++actualIt; |
---|
1459 | addAssertionFor( assignExpr, *actualIt, assertCtor ); |
---|
1460 | ++actualIt; |
---|
1461 | addAssertionFor( assignExpr, *actualIt, assertCopy ); |
---|
1462 | ++actualIt; |
---|
1463 | addAssertionFor( assignExpr, *actualIt, assertDtor ); |
---|
1464 | |
---|
1465 | } |
---|
1466 | } |
---|
1467 | assert( assignExpr ); |
---|
1468 | |
---|
1469 | // replace return statement with appropriate assignment to out parameter |
---|
1470 | Expression *retParm = new NameExpr( retval->get_name() ); |
---|
1471 | retParm->set_result( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); |
---|
1472 | assignExpr->get_args().push_back( retParm ); |
---|
1473 | assignExpr->get_args().push_back( returnStmt->get_expr() ); |
---|
1474 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); |
---|
1475 | // } else { |
---|
1476 | // useRetval = true; |
---|
1477 | // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); |
---|
1478 | // useRetval = false; |
---|
1479 | // } // if |
---|
1480 | returnStmt->set_expr( 0 ); |
---|
1481 | } else { |
---|
1482 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); |
---|
1483 | } // if |
---|
1484 | return returnStmt; |
---|
1485 | } |
---|
1486 | |
---|
1487 | Type * Pass1::mutate( PointerType *pointerType ) { |
---|
1488 | scopeTyVars.beginScope(); |
---|
1489 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
1490 | |
---|
1491 | Type *ret = Mutator::mutate( pointerType ); |
---|
1492 | |
---|
1493 | scopeTyVars.endScope(); |
---|
1494 | return ret; |
---|
1495 | } |
---|
1496 | |
---|
1497 | Type * Pass1::mutate( FunctionType *functionType ) { |
---|
1498 | scopeTyVars.beginScope(); |
---|
1499 | makeTyVarMap( functionType, scopeTyVars ); |
---|
1500 | |
---|
1501 | Type *ret = Mutator::mutate( functionType ); |
---|
1502 | |
---|
1503 | scopeTyVars.endScope(); |
---|
1504 | return ret; |
---|
1505 | } |
---|
1506 | |
---|
1507 | void Pass1::doBeginScope() { |
---|
1508 | adapters.beginScope(); |
---|
1509 | scopedAssignOps.beginScope(); |
---|
1510 | scopedCtorOps.beginScope(); |
---|
1511 | scopedCopyOps.beginScope(); |
---|
1512 | scopedDtorOps.beginScope(); |
---|
1513 | } |
---|
1514 | |
---|
1515 | void Pass1::doEndScope() { |
---|
1516 | adapters.endScope(); |
---|
1517 | scopedAssignOps.endScope(); |
---|
1518 | scopedCtorOps.endScope(); |
---|
1519 | scopedCopyOps.endScope(); |
---|
1520 | scopedDtorOps.endScope(); |
---|
1521 | } |
---|
1522 | |
---|
1523 | ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// |
---|
1524 | |
---|
1525 | void Pass2::addAdapters( FunctionType *functionType ) { |
---|
1526 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); |
---|
1527 | std::list< FunctionType *> functions; |
---|
1528 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { |
---|
1529 | Type *orig = (*arg)->get_type(); |
---|
1530 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter ); |
---|
1531 | (*arg)->set_type( orig ); |
---|
1532 | } |
---|
1533 | std::set< std::string > adaptersDone; |
---|
1534 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { |
---|
1535 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); |
---|
1536 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { |
---|
1537 | std::string adapterName = makeAdapterName( mangleName ); |
---|
1538 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); |
---|
1539 | adaptersDone.insert( adaptersDone.begin(), mangleName ); |
---|
1540 | } |
---|
1541 | } |
---|
1542 | // deleteAll( functions ); |
---|
1543 | } |
---|
1544 | |
---|
1545 | template< typename DeclClass > |
---|
1546 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) { |
---|
1547 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
1548 | |
---|
1549 | return ret; |
---|
1550 | } |
---|
1551 | |
---|
1552 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) { |
---|
1553 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
1554 | } |
---|
1555 | |
---|
1556 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) { |
---|
1557 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
1558 | } |
---|
1559 | |
---|
1560 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) { |
---|
1561 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
1562 | if ( typeDecl->get_base() ) { |
---|
1563 | return handleDecl( typeDecl, typeDecl->get_base() ); |
---|
1564 | } else { |
---|
1565 | return Mutator::mutate( typeDecl ); |
---|
1566 | } |
---|
1567 | } |
---|
1568 | |
---|
1569 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) { |
---|
1570 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
1571 | } |
---|
1572 | |
---|
1573 | Type * Pass2::mutate( PointerType *pointerType ) { |
---|
1574 | scopeTyVars.beginScope(); |
---|
1575 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
1576 | |
---|
1577 | Type *ret = Mutator::mutate( pointerType ); |
---|
1578 | |
---|
1579 | scopeTyVars.endScope(); |
---|
1580 | return ret; |
---|
1581 | } |
---|
1582 | |
---|
1583 | Type *Pass2::mutate( FunctionType *funcType ) { |
---|
1584 | scopeTyVars.beginScope(); |
---|
1585 | makeTyVarMap( funcType, scopeTyVars ); |
---|
1586 | |
---|
1587 | // move polymorphic return type to parameter list |
---|
1588 | if ( isDynRet( funcType ) ) { |
---|
1589 | DeclarationWithType *ret = funcType->get_returnVals().front(); |
---|
1590 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) ); |
---|
1591 | funcType->get_parameters().push_front( ret ); |
---|
1592 | funcType->get_returnVals().pop_front(); |
---|
1593 | } |
---|
1594 | |
---|
1595 | // add size/align and assertions for type parameters to parameter list |
---|
1596 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); |
---|
1597 | std::list< DeclarationWithType *> inferredParams; |
---|
1598 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); |
---|
1599 | ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, |
---|
1600 | new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 ); |
---|
1601 | for ( Type::ForallList::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { |
---|
1602 | ObjectDecl *sizeParm, *alignParm; |
---|
1603 | // add all size and alignment parameters to parameter list |
---|
1604 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) { |
---|
1605 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); |
---|
1606 | std::string parmName = mangleType( &parmType ); |
---|
1607 | |
---|
1608 | sizeParm = newObj.clone(); |
---|
1609 | sizeParm->set_name( sizeofName( parmName ) ); |
---|
1610 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
1611 | ++last; |
---|
1612 | |
---|
1613 | alignParm = newObj.clone(); |
---|
1614 | alignParm->set_name( alignofName( parmName ) ); |
---|
1615 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
1616 | ++last; |
---|
1617 | } |
---|
1618 | // move all assertions into parameter list |
---|
1619 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { |
---|
1620 | // *assert = (*assert)->acceptMutator( *this ); |
---|
1621 | inferredParams.push_back( *assert ); |
---|
1622 | } |
---|
1623 | (*tyParm)->get_assertions().clear(); |
---|
1624 | } |
---|
1625 | |
---|
1626 | // add size/align for generic parameter types to parameter list |
---|
1627 | std::set< std::string > seenTypes; // sizeofName for generic types we've seen |
---|
1628 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { |
---|
1629 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); |
---|
1630 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
1631 | std::string typeName = mangleType( polyType ); |
---|
1632 | if ( seenTypes.count( typeName ) ) continue; |
---|
1633 | |
---|
1634 | ObjectDecl *sizeParm, *alignParm, *offsetParm; |
---|
1635 | sizeParm = newObj.clone(); |
---|
1636 | sizeParm->set_name( sizeofName( typeName ) ); |
---|
1637 | last = funcType->get_parameters().insert( last, sizeParm ); |
---|
1638 | ++last; |
---|
1639 | |
---|
1640 | alignParm = newObj.clone(); |
---|
1641 | alignParm->set_name( alignofName( typeName ) ); |
---|
1642 | last = funcType->get_parameters().insert( last, alignParm ); |
---|
1643 | ++last; |
---|
1644 | |
---|
1645 | if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) { |
---|
1646 | // NOTE zero-length arrays are illegal in C, so empty structs have no offset array |
---|
1647 | if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { |
---|
1648 | offsetParm = newPtr.clone(); |
---|
1649 | offsetParm->set_name( offsetofName( typeName ) ); |
---|
1650 | last = funcType->get_parameters().insert( last, offsetParm ); |
---|
1651 | ++last; |
---|
1652 | } |
---|
1653 | } |
---|
1654 | |
---|
1655 | seenTypes.insert( typeName ); |
---|
1656 | } |
---|
1657 | } |
---|
1658 | |
---|
1659 | // splice assertion parameters into parameter list |
---|
1660 | funcType->get_parameters().splice( last, inferredParams ); |
---|
1661 | addAdapters( funcType ); |
---|
1662 | mutateAll( funcType->get_returnVals(), *this ); |
---|
1663 | mutateAll( funcType->get_parameters(), *this ); |
---|
1664 | |
---|
1665 | scopeTyVars.endScope(); |
---|
1666 | return funcType; |
---|
1667 | } |
---|
1668 | |
---|
1669 | ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////////////////////////// |
---|
1670 | |
---|
1671 | void PolyGenericCalculator::beginTypeScope( Type *ty ) { |
---|
1672 | scopeTyVars.beginScope(); |
---|
1673 | makeTyVarMap( ty, scopeTyVars ); |
---|
1674 | } |
---|
1675 | |
---|
1676 | void PolyGenericCalculator::endTypeScope() { |
---|
1677 | scopeTyVars.endScope(); |
---|
1678 | } |
---|
1679 | |
---|
1680 | template< typename DeclClass > |
---|
1681 | DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { |
---|
1682 | beginTypeScope( type ); |
---|
1683 | // knownLayouts.beginScope(); |
---|
1684 | // knownOffsets.beginScope(); |
---|
1685 | |
---|
1686 | DeclClass *ret = static_cast< DeclClass *>( Parent::mutate( decl ) ); |
---|
1687 | |
---|
1688 | // knownOffsets.endScope(); |
---|
1689 | // knownLayouts.endScope(); |
---|
1690 | endTypeScope(); |
---|
1691 | return ret; |
---|
1692 | } |
---|
1693 | |
---|
1694 | ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) { |
---|
1695 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
1696 | } |
---|
1697 | |
---|
1698 | DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) { |
---|
1699 | knownLayouts.beginScope(); |
---|
1700 | knownOffsets.beginScope(); |
---|
1701 | |
---|
1702 | DeclarationWithType * decl = handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
1703 | knownOffsets.endScope(); |
---|
1704 | knownLayouts.endScope(); |
---|
1705 | return decl; |
---|
1706 | } |
---|
1707 | |
---|
1708 | TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) { |
---|
1709 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
1710 | } |
---|
1711 | |
---|
1712 | TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) { |
---|
1713 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
1714 | return Parent::mutate( typeDecl ); |
---|
1715 | } |
---|
1716 | |
---|
1717 | Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { |
---|
1718 | beginTypeScope( pointerType ); |
---|
1719 | |
---|
1720 | Type *ret = Parent::mutate( pointerType ); |
---|
1721 | |
---|
1722 | endTypeScope(); |
---|
1723 | return ret; |
---|
1724 | } |
---|
1725 | |
---|
1726 | Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { |
---|
1727 | beginTypeScope( funcType ); |
---|
1728 | |
---|
1729 | // make sure that any type information passed into the function is accounted for |
---|
1730 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) { |
---|
1731 | // condition here duplicates that in Pass2::mutate( FunctionType* ) |
---|
1732 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); |
---|
1733 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { |
---|
1734 | knownLayouts.insert( mangleType( polyType ) ); |
---|
1735 | } |
---|
1736 | } |
---|
1737 | |
---|
1738 | Type *ret = Parent::mutate( funcType ); |
---|
1739 | |
---|
1740 | endTypeScope(); |
---|
1741 | return ret; |
---|
1742 | } |
---|
1743 | |
---|
1744 | Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) { |
---|
1745 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { |
---|
1746 | if ( findGeneric( objectDecl->get_type() ) ) { |
---|
1747 | // change initialization of a polymorphic value object |
---|
1748 | // to allocate storage with alloca |
---|
1749 | Type *declType = objectDecl->get_type(); |
---|
1750 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); |
---|
1751 | alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) ); |
---|
1752 | |
---|
1753 | delete objectDecl->get_init(); |
---|
1754 | |
---|
1755 | std::list<Expression*> designators; |
---|
1756 | objectDecl->set_init( new SingleInit( alloc, designators, false ) ); // not constructed |
---|
1757 | } |
---|
1758 | } |
---|
1759 | return Parent::mutate( declStmt ); |
---|
1760 | } |
---|
1761 | |
---|
1762 | /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present |
---|
1763 | long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) { |
---|
1764 | long i = 0; |
---|
1765 | for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) { |
---|
1766 | if ( memberDecl->get_name() != (*decl)->get_name() ) continue; |
---|
1767 | |
---|
1768 | if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) { |
---|
1769 | if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty() |
---|
1770 | || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i; |
---|
1771 | else continue; |
---|
1772 | } else return i; |
---|
1773 | } |
---|
1774 | return -1; |
---|
1775 | } |
---|
1776 | |
---|
1777 | /// Returns an index expression into the offset array for a type |
---|
1778 | Expression *makeOffsetIndex( Type *objectType, long i ) { |
---|
1779 | std::stringstream offset_namer; |
---|
1780 | offset_namer << i; |
---|
1781 | ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) ); |
---|
1782 | UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
1783 | fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) ); |
---|
1784 | fieldOffset->get_args().push_back( fieldIndex ); |
---|
1785 | return fieldOffset; |
---|
1786 | } |
---|
1787 | |
---|
1788 | /// Returns an expression dereferenced n times |
---|
1789 | Expression *makeDerefdVar( Expression *derefdVar, long n ) { |
---|
1790 | for ( int i = 1; i < n; ++i ) { |
---|
1791 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1792 | derefExpr->get_args().push_back( derefdVar ); |
---|
1793 | // xxx - should set results on derefExpr |
---|
1794 | derefdVar = derefExpr; |
---|
1795 | } |
---|
1796 | return derefdVar; |
---|
1797 | } |
---|
1798 | |
---|
1799 | Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) { |
---|
1800 | // mutate, exiting early if no longer MemberExpr |
---|
1801 | Expression *expr = Parent::mutate( memberExpr ); |
---|
1802 | memberExpr = dynamic_cast< MemberExpr* >( expr ); |
---|
1803 | if ( ! memberExpr ) return expr; |
---|
1804 | |
---|
1805 | // get declaration for base struct, exiting early if not found |
---|
1806 | int varDepth; |
---|
1807 | VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth ); |
---|
1808 | if ( ! varExpr ) return memberExpr; |
---|
1809 | ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() ); |
---|
1810 | if ( ! objectDecl ) return memberExpr; |
---|
1811 | |
---|
1812 | // only mutate member expressions for polymorphic types |
---|
1813 | int tyDepth; |
---|
1814 | Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth ); |
---|
1815 | if ( ! objectType ) return memberExpr; |
---|
1816 | findGeneric( objectType ); // ensure layout for this type is available |
---|
1817 | |
---|
1818 | // replace member expression with dynamically-computed layout expression |
---|
1819 | Expression *newMemberExpr = 0; |
---|
1820 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) { |
---|
1821 | // look up offset index |
---|
1822 | long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() ); |
---|
1823 | if ( i == -1 ) return memberExpr; |
---|
1824 | |
---|
1825 | // replace member expression with pointer to base plus offset |
---|
1826 | UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
1827 | fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) ); |
---|
1828 | fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) ); |
---|
1829 | newMemberExpr = fieldLoc; |
---|
1830 | } else if ( dynamic_cast< UnionInstType* >( objectType ) ) { |
---|
1831 | // union members are all at offset zero, so build appropriately-dereferenced variable |
---|
1832 | newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth ); |
---|
1833 | } else return memberExpr; |
---|
1834 | assert( newMemberExpr ); |
---|
1835 | |
---|
1836 | Type *memberType = memberExpr->get_member()->get_type(); |
---|
1837 | if ( ! isPolyType( memberType, scopeTyVars ) ) { |
---|
1838 | // Not all members of a polymorphic type are themselves of polymorphic type; in this case the member expression should be wrapped and dereferenced to form an lvalue |
---|
1839 | CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), memberType->clone() ) ); |
---|
1840 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
1841 | derefExpr->get_args().push_back( ptrCastExpr ); |
---|
1842 | newMemberExpr = derefExpr; |
---|
1843 | } |
---|
1844 | |
---|
1845 | delete memberExpr; |
---|
1846 | return newMemberExpr; |
---|
1847 | } |
---|
1848 | |
---|
1849 | ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) { |
---|
1850 | ObjectDecl *newObj = new ObjectDecl( name, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, init ); |
---|
1851 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
1852 | return newObj; |
---|
1853 | } |
---|
1854 | |
---|
1855 | void PolyGenericCalculator::addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ) { |
---|
1856 | for ( std::list< Type* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { |
---|
1857 | if ( findGeneric( *param ) ) { |
---|
1858 | // push size/align vars for a generic parameter back |
---|
1859 | std::string paramName = mangleType( *param ); |
---|
1860 | layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) ); |
---|
1861 | layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) ); |
---|
1862 | } else { |
---|
1863 | layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); |
---|
1864 | layoutCall->get_args().push_back( new AlignofExpr( (*param)->clone() ) ); |
---|
1865 | } |
---|
1866 | } |
---|
1867 | } |
---|
1868 | |
---|
1869 | /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list |
---|
1870 | bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) { |
---|
1871 | bool hasDynamicLayout = false; |
---|
1872 | |
---|
1873 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); |
---|
1874 | std::list< Expression* >::const_iterator typeParam = typeParams.begin(); |
---|
1875 | for ( ; baseParam != baseParams.end() && typeParam != typeParams.end(); ++baseParam, ++typeParam ) { |
---|
1876 | // skip non-otype parameters |
---|
1877 | if ( (*baseParam)->get_kind() != TypeDecl::Any ) continue; |
---|
1878 | TypeExpr *typeExpr = dynamic_cast< TypeExpr* >( *typeParam ); |
---|
1879 | assert( typeExpr && "all otype parameters should be type expressions" ); |
---|
1880 | |
---|
1881 | Type *type = typeExpr->get_type(); |
---|
1882 | out.push_back( type ); |
---|
1883 | if ( isPolyType( type ) ) hasDynamicLayout = true; |
---|
1884 | } |
---|
1885 | assert( baseParam == baseParams.end() && typeParam == typeParams.end() ); |
---|
1886 | |
---|
1887 | return hasDynamicLayout; |
---|
1888 | } |
---|
1889 | |
---|
1890 | bool PolyGenericCalculator::findGeneric( Type *ty ) { |
---|
1891 | ty = replaceTypeInst( ty, env ); |
---|
1892 | |
---|
1893 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) { |
---|
1894 | if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) { |
---|
1895 | // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set |
---|
1896 | return true; |
---|
1897 | } |
---|
1898 | return false; |
---|
1899 | } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { |
---|
1900 | // check if this type already has a layout generated for it |
---|
1901 | std::string typeName = mangleType( ty ); |
---|
1902 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; |
---|
1903 | |
---|
1904 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized |
---|
1905 | std::list< Type* > otypeParams; |
---|
1906 | if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false; |
---|
1907 | |
---|
1908 | // insert local variables for layout and generate call to layout function |
---|
1909 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call |
---|
1910 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
1911 | |
---|
1912 | int n_members = structTy->get_baseStruct()->get_members().size(); |
---|
1913 | if ( n_members == 0 ) { |
---|
1914 | // all empty structs have the same layout - size 1, align 1 |
---|
1915 | makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) ); |
---|
1916 | makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) ); |
---|
1917 | // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array |
---|
1918 | } else { |
---|
1919 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); |
---|
1920 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); |
---|
1921 | ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from_int( n_members ) ), false, false ) ); |
---|
1922 | |
---|
1923 | // generate call to layout function |
---|
1924 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) ); |
---|
1925 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); |
---|
1926 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); |
---|
1927 | layoutCall->get_args().push_back( new VariableExpr( offsetVar ) ); |
---|
1928 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); |
---|
1929 | |
---|
1930 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); |
---|
1931 | } |
---|
1932 | |
---|
1933 | return true; |
---|
1934 | } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { |
---|
1935 | // check if this type already has a layout generated for it |
---|
1936 | std::string typeName = mangleType( ty ); |
---|
1937 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; |
---|
1938 | |
---|
1939 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized |
---|
1940 | std::list< Type* > otypeParams; |
---|
1941 | if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false; |
---|
1942 | |
---|
1943 | // insert local variables for layout and generate call to layout function |
---|
1944 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call |
---|
1945 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
1946 | |
---|
1947 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); |
---|
1948 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); |
---|
1949 | |
---|
1950 | // generate call to layout function |
---|
1951 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) ); |
---|
1952 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); |
---|
1953 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); |
---|
1954 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); |
---|
1955 | |
---|
1956 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); |
---|
1957 | |
---|
1958 | return true; |
---|
1959 | } |
---|
1960 | |
---|
1961 | return false; |
---|
1962 | } |
---|
1963 | |
---|
1964 | Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) { |
---|
1965 | Type *ty = sizeofExpr->get_type(); |
---|
1966 | if ( findGeneric( ty ) ) { |
---|
1967 | Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) ); |
---|
1968 | delete sizeofExpr; |
---|
1969 | return ret; |
---|
1970 | } |
---|
1971 | return sizeofExpr; |
---|
1972 | } |
---|
1973 | |
---|
1974 | Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) { |
---|
1975 | Type *ty = alignofExpr->get_type(); |
---|
1976 | if ( findGeneric( ty ) ) { |
---|
1977 | Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) ); |
---|
1978 | delete alignofExpr; |
---|
1979 | return ret; |
---|
1980 | } |
---|
1981 | return alignofExpr; |
---|
1982 | } |
---|
1983 | |
---|
1984 | Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) { |
---|
1985 | // mutate, exiting early if no longer OffsetofExpr |
---|
1986 | Expression *expr = Parent::mutate( offsetofExpr ); |
---|
1987 | offsetofExpr = dynamic_cast< OffsetofExpr* >( expr ); |
---|
1988 | if ( ! offsetofExpr ) return expr; |
---|
1989 | |
---|
1990 | // only mutate expressions for polymorphic structs/unions |
---|
1991 | Type *ty = offsetofExpr->get_type(); |
---|
1992 | if ( ! findGeneric( ty ) ) return offsetofExpr; |
---|
1993 | |
---|
1994 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) { |
---|
1995 | // replace offsetof expression by index into offset array |
---|
1996 | long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() ); |
---|
1997 | if ( i == -1 ) return offsetofExpr; |
---|
1998 | |
---|
1999 | Expression *offsetInd = makeOffsetIndex( ty, i ); |
---|
2000 | delete offsetofExpr; |
---|
2001 | return offsetInd; |
---|
2002 | } else if ( dynamic_cast< UnionInstType* >( ty ) ) { |
---|
2003 | // all union members are at offset zero |
---|
2004 | delete offsetofExpr; |
---|
2005 | return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "0" ) ); |
---|
2006 | } else return offsetofExpr; |
---|
2007 | } |
---|
2008 | |
---|
2009 | Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) { |
---|
2010 | StructInstType *ty = offsetPackExpr->get_type(); |
---|
2011 | |
---|
2012 | Expression *ret = 0; |
---|
2013 | if ( findGeneric( ty ) ) { |
---|
2014 | // pull offset back from generated type information |
---|
2015 | ret = new NameExpr( offsetofName( mangleType( ty ) ) ); |
---|
2016 | } else { |
---|
2017 | std::string offsetName = offsetofName( mangleType( ty ) ); |
---|
2018 | if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { |
---|
2019 | // use the already-generated offsets for this type |
---|
2020 | ret = new NameExpr( offsetName ); |
---|
2021 | } else { |
---|
2022 | knownOffsets.insert( offsetName ); |
---|
2023 | |
---|
2024 | std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members(); |
---|
2025 | Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
2026 | |
---|
2027 | // build initializer list for offset array |
---|
2028 | std::list< Initializer* > inits; |
---|
2029 | for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) { |
---|
2030 | DeclarationWithType *memberDecl; |
---|
2031 | if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) { |
---|
2032 | memberDecl = origMember->clone(); |
---|
2033 | } else { |
---|
2034 | memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 ); |
---|
2035 | } |
---|
2036 | inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) ); |
---|
2037 | } |
---|
2038 | |
---|
2039 | // build the offset array and replace the pack with a reference to it |
---|
2040 | ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from_ulong( baseMembers.size() ) ), false, false ), |
---|
2041 | new ListInit( inits ) ); |
---|
2042 | ret = new VariableExpr( offsetArray ); |
---|
2043 | } |
---|
2044 | } |
---|
2045 | |
---|
2046 | delete offsetPackExpr; |
---|
2047 | return ret; |
---|
2048 | } |
---|
2049 | |
---|
2050 | void PolyGenericCalculator::doBeginScope() { |
---|
2051 | knownLayouts.beginScope(); |
---|
2052 | knownOffsets.beginScope(); |
---|
2053 | } |
---|
2054 | |
---|
2055 | void PolyGenericCalculator::doEndScope() { |
---|
2056 | knownLayouts.endScope(); |
---|
2057 | knownOffsets.endScope(); |
---|
2058 | } |
---|
2059 | |
---|
2060 | ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// |
---|
2061 | |
---|
2062 | template< typename DeclClass > |
---|
2063 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { |
---|
2064 | scopeTyVars.beginScope(); |
---|
2065 | makeTyVarMap( type, scopeTyVars ); |
---|
2066 | |
---|
2067 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); |
---|
2068 | ScrubTyVars::scrub( decl, scopeTyVars ); |
---|
2069 | |
---|
2070 | scopeTyVars.endScope(); |
---|
2071 | return ret; |
---|
2072 | } |
---|
2073 | |
---|
2074 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) { |
---|
2075 | return handleDecl( objectDecl, objectDecl->get_type() ); |
---|
2076 | } |
---|
2077 | |
---|
2078 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) { |
---|
2079 | return handleDecl( functionDecl, functionDecl->get_functionType() ); |
---|
2080 | } |
---|
2081 | |
---|
2082 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) { |
---|
2083 | return handleDecl( typedefDecl, typedefDecl->get_base() ); |
---|
2084 | } |
---|
2085 | |
---|
2086 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) { |
---|
2087 | // Initializer *init = 0; |
---|
2088 | // std::list< Expression *> designators; |
---|
2089 | // scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
2090 | // if ( typeDecl->get_base() ) { |
---|
2091 | // init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators ); |
---|
2092 | // } |
---|
2093 | // return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init ); |
---|
2094 | |
---|
2095 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); |
---|
2096 | return Mutator::mutate( typeDecl ); |
---|
2097 | } |
---|
2098 | |
---|
2099 | Type * Pass3::mutate( PointerType *pointerType ) { |
---|
2100 | scopeTyVars.beginScope(); |
---|
2101 | makeTyVarMap( pointerType, scopeTyVars ); |
---|
2102 | |
---|
2103 | Type *ret = Mutator::mutate( pointerType ); |
---|
2104 | |
---|
2105 | scopeTyVars.endScope(); |
---|
2106 | return ret; |
---|
2107 | } |
---|
2108 | |
---|
2109 | Type * Pass3::mutate( FunctionType *functionType ) { |
---|
2110 | scopeTyVars.beginScope(); |
---|
2111 | makeTyVarMap( functionType, scopeTyVars ); |
---|
2112 | |
---|
2113 | Type *ret = Mutator::mutate( functionType ); |
---|
2114 | |
---|
2115 | scopeTyVars.endScope(); |
---|
2116 | return ret; |
---|
2117 | } |
---|
2118 | } // anonymous namespace |
---|
2119 | } // namespace GenPoly |
---|
2120 | |
---|
2121 | // Local Variables: // |
---|
2122 | // tab-width: 4 // |
---|
2123 | // mode: c++ // |
---|
2124 | // compile-command: "make install" // |
---|
2125 | // End: // |
---|