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