[51587aa] | 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 | //
|
---|
[ae63a18] | 7 | // Box.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[ca35c51] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Wed Jun 29 21:43:03 2016
|
---|
| 13 | // Update Count : 296
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[4e284ea6] | 16 | #include <algorithm>
|
---|
| 17 | #include <iterator>
|
---|
| 18 | #include <list>
|
---|
| 19 | #include <map>
|
---|
[51b73452] | 20 | #include <set>
|
---|
[b1a6d6b] | 21 | #include <stack>
|
---|
[51b73452] | 22 | #include <string>
|
---|
[4e284ea6] | 23 | #include <utility>
|
---|
| 24 | #include <vector>
|
---|
[51b73452] | 25 | #include <cassert>
|
---|
| 26 |
|
---|
| 27 | #include "Box.h"
|
---|
[9d7b3ea] | 28 | #include "DeclMutator.h"
|
---|
[51b73452] | 29 | #include "PolyMutator.h"
|
---|
| 30 | #include "FindFunction.h"
|
---|
[89173242] | 31 | #include "ScopedSet.h"
|
---|
[51b73452] | 32 | #include "ScrubTyVars.h"
|
---|
| 33 |
|
---|
[68cd1ce] | 34 | #include "Parser/ParseNode.h"
|
---|
| 35 |
|
---|
[cdec5af] | 36 | #include "SynTree/Constant.h"
|
---|
[4e284ea6] | 37 | #include "SynTree/Declaration.h"
|
---|
[51b73452] | 38 | #include "SynTree/Expression.h"
|
---|
| 39 | #include "SynTree/Initializer.h"
|
---|
| 40 | #include "SynTree/Mutator.h"
|
---|
[4e284ea6] | 41 | #include "SynTree/Statement.h"
|
---|
| 42 | #include "SynTree/Type.h"
|
---|
| 43 | #include "SynTree/TypeSubstitution.h"
|
---|
[68cd1ce] | 44 |
|
---|
[51b73452] | 45 | #include "ResolvExpr/TypeEnvironment.h"
|
---|
[dc12481] | 46 | #include "ResolvExpr/TypeMap.h"
|
---|
| 47 | #include "ResolvExpr/typeops.h"
|
---|
[68cd1ce] | 48 |
|
---|
[4e284ea6] | 49 | #include "SymTab/Indexer.h"
|
---|
[51b73452] | 50 | #include "SymTab/Mangler.h"
|
---|
| 51 |
|
---|
[e491159] | 52 | #include "Common/ScopedMap.h"
|
---|
[d3b7937] | 53 | #include "Common/SemanticError.h"
|
---|
| 54 | #include "Common/UniqueName.h"
|
---|
| 55 | #include "Common/utility.h"
|
---|
[51b73452] | 56 |
|
---|
| 57 | #include <ext/functional> // temporary
|
---|
| 58 |
|
---|
| 59 | namespace GenPoly {
|
---|
[01aeade] | 60 | namespace {
|
---|
| 61 | const std::list<Label> noLabels;
|
---|
| 62 |
|
---|
[e56cfdb0] | 63 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
|
---|
| 64 |
|
---|
[9d7b3ea] | 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 | };
|
---|
[70a06f6] | 75 |
|
---|
[f8b961b] | 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
|
---|
[01aeade] | 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 );
|
---|
[cf16f94] | 87 | virtual Statement * mutate( ReturnStmt *returnStmt );
|
---|
[01aeade] | 88 | virtual Type *mutate( PointerType *pointerType );
|
---|
[cf16f94] | 89 | virtual Type * mutate( FunctionType *functionType );
|
---|
[ae63a18] | 90 |
|
---|
[01aeade] | 91 | virtual void doBeginScope();
|
---|
| 92 | virtual void doEndScope();
|
---|
| 93 | private:
|
---|
[5c52b06] | 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 );
|
---|
[05d47278] | 96 | /// passes extra type parameters into a polymorphic function application
|
---|
[5c52b06] | 97 | void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
[48ca586] | 98 | /// wraps a function application with a new temporary for the out-parameter return value
|
---|
[01aeade] | 99 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
|
---|
[48ca586] | 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
|
---|
[3bb195cb] | 106 | Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg );
|
---|
[01aeade] | 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 );
|
---|
[1194734] | 111 | /// Stores assignment operators from assertion list in local map of assignment operations
|
---|
[8c49c0e] | 112 | void findTypeOps( const Type::ForallList &forall );
|
---|
[01aeade] | 113 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars );
|
---|
| 114 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
|
---|
[05d47278] | 115 | /// Replaces intrinsic operator functions with their arithmetic desugaring
|
---|
[01aeade] | 116 | Expression *handleIntrinsics( ApplicationExpr *appExpr );
|
---|
[05d47278] | 117 | /// Inserts a new temporary variable into the current scope with an auto-generated name
|
---|
[01aeade] | 118 | ObjectDecl *makeTemporary( Type *type );
|
---|
[c29d9ce] | 119 |
|
---|
[c2ad3c9] | 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
|
---|
[89173242] | 124 | ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; ///< Currently known assignment operators
|
---|
[c2ad3c9] | 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
|
---|
[89173242] | 128 | ScopedMap< std::string, DeclarationWithType* > adapters; ///< Set of adapter functions in the current scope
|
---|
[70a06f6] | 129 |
|
---|
[01aeade] | 130 | DeclarationWithType *retval;
|
---|
| 131 | bool useRetval;
|
---|
| 132 | UniqueName tempNamer;
|
---|
| 133 | };
|
---|
| 134 |
|
---|
[89173242] | 135 | /// * Moves polymorphic returns in function types to pointer-type parameters
|
---|
| 136 | /// * adds type size and assertion parameters to parameter lists
|
---|
[01aeade] | 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 );
|
---|
[70a06f6] | 147 |
|
---|
[01aeade] | 148 | private:
|
---|
| 149 | void addAdapters( FunctionType *functionType );
|
---|
[ae63a18] | 150 |
|
---|
[01aeade] | 151 | std::map< UniqueId, std::string > adapterName;
|
---|
| 152 | };
|
---|
| 153 |
|
---|
[8a34677] | 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:
|
---|
[1ba88a0] | 160 | typedef PolyMutator Parent;
|
---|
| 161 | using Parent::mutate;
|
---|
| 162 |
|
---|
[05d47278] | 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 );
|
---|
[8a34677] | 173 | virtual Expression *mutate( SizeofExpr *sizeofExpr );
|
---|
| 174 | virtual Expression *mutate( AlignofExpr *alignofExpr );
|
---|
[2a4b088] | 175 | virtual Expression *mutate( OffsetofExpr *offsetofExpr );
|
---|
[8a34677] | 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 );
|
---|
[aa19ccf] | 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();
|
---|
[70a06f6] | 193 |
|
---|
[8a34677] | 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
|
---|
[05d47278] | 196 | };
|
---|
[b4cd03b7] | 197 |
|
---|
[f8b961b] | 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
|
---|
[01aeade] | 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 |
|
---|
[05d47278] | 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 | }
|
---|
[b4cd03b7] | 227 |
|
---|
[05d47278] | 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 |
|
---|
[01aeade] | 240 | void box( std::list< Declaration *>& translationUnit ) {
|
---|
[9d7b3ea] | 241 | LayoutFunctionBuilder layoutBuilder;
|
---|
[01aeade] | 242 | Pass1 pass1;
|
---|
| 243 | Pass2 pass2;
|
---|
[8a34677] | 244 | PolyGenericCalculator polyCalculator;
|
---|
[01aeade] | 245 | Pass3 pass3;
|
---|
[70a06f6] | 246 |
|
---|
[9d7b3ea] | 247 | layoutBuilder.mutateDeclarationList( translationUnit );
|
---|
[05d47278] | 248 | mutateTranslationUnit/*All*/( translationUnit, pass1 );
|
---|
| 249 | mutateTranslationUnit/*All*/( translationUnit, pass2 );
|
---|
[8a34677] | 250 | mutateTranslationUnit/*All*/( translationUnit, polyCalculator );
|
---|
[05d47278] | 251 | mutateTranslationUnit/*All*/( translationUnit, pass3 );
|
---|
[6c3744e] | 252 | }
|
---|
| 253 |
|
---|
[9d7b3ea] | 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 | }
|
---|
[70a06f6] | 264 |
|
---|
[9d7b3ea] | 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 ) {
|
---|
[3627356] | 270 | if ( (*decl)->get_kind() == TypeDecl::Any ) {
|
---|
[9d7b3ea] | 271 | otypeDecls.push_back( *decl );
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
[70a06f6] | 274 |
|
---|
[9d7b3ea] | 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 );
|
---|
[70a06f6] | 281 |
|
---|
[9d7b3ea] | 282 | for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
|
---|
[2e60a1a] | 283 | TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
|
---|
[adc6781] | 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 ) );
|
---|
[9d7b3ea] | 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /// Builds a layout function declaration
|
---|
[adc6781] | 291 | FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
|
---|
[9d7b3ea] | 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(
|
---|
[adc6781] | 295 | layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
|
---|
[9d7b3ea] | 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 ) {
|
---|
[3627356] | 322 | return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 );
|
---|
[9d7b3ea] | 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 | }
|
---|
[70a06f6] | 338 |
|
---|
[9d7b3ea] | 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 | }
|
---|
[70a06f6] | 348 |
|
---|
[3627356] | 349 | Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) {
|
---|
[9d7b3ea] | 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 );
|
---|
[70a06f6] | 361 |
|
---|
[adc6781] | 362 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
|
---|
[9d7b3ea] | 363 | layoutFnType->get_parameters().push_back( sizeParam );
|
---|
[adc6781] | 364 | ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
|
---|
[9d7b3ea] | 365 | layoutFnType->get_parameters().push_back( alignParam );
|
---|
[adc6781] | 366 | ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
|
---|
[9d7b3ea] | 367 | layoutFnType->get_parameters().push_back( offsetParam );
|
---|
| 368 | addOtypeParams( layoutFnType, otypeParams );
|
---|
| 369 |
|
---|
| 370 | // build function decl
|
---|
[adc6781] | 371 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
|
---|
[9d7b3ea] | 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 );
|
---|
[bd91e2a] | 383 | Type *memberType = dwt->get_type();
|
---|
[9d7b3ea] | 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
|
---|
[bd91e2a] | 389 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) );
|
---|
[9d7b3ea] | 390 | }
|
---|
[70a06f6] | 391 |
|
---|
[9d7b3ea] | 392 | // place current size in the current offset index
|
---|
[cb4c607] | 393 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( n_members ) ) ),
|
---|
[9d7b3ea] | 394 | derefVar( sizeParam ) ) );
|
---|
| 395 | ++n_members;
|
---|
| 396 |
|
---|
| 397 | // add member size to current size
|
---|
[bd91e2a] | 398 | addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
|
---|
[70a06f6] | 399 |
|
---|
[9d7b3ea] | 400 | // take max of member alignment and global alignment
|
---|
[bd91e2a] | 401 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
|
---|
[9d7b3ea] | 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 | }
|
---|
[70a06f6] | 409 |
|
---|
[3627356] | 410 | Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) {
|
---|
[9d7b3ea] | 411 | // do not generate layout function for "empty" tag unions
|
---|
| 412 | if ( unionDecl->get_members().empty() ) return unionDecl;
|
---|
[70a06f6] | 413 |
|
---|
[9d7b3ea] | 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 );
|
---|
[70a06f6] | 422 |
|
---|
[adc6781] | 423 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
|
---|
[9d7b3ea] | 424 | layoutFnType->get_parameters().push_back( sizeParam );
|
---|
[adc6781] | 425 | ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
|
---|
[9d7b3ea] | 426 | layoutFnType->get_parameters().push_back( alignParam );
|
---|
| 427 | addOtypeParams( layoutFnType, otypeParams );
|
---|
| 428 |
|
---|
| 429 | // build function decl
|
---|
[adc6781] | 430 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
|
---|
[9d7b3ea] | 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 );
|
---|
[bd91e2a] | 438 | Type *memberType = dwt->get_type();
|
---|
[70a06f6] | 439 |
|
---|
[9d7b3ea] | 440 | // take max member size and global size
|
---|
[bd91e2a] | 441 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
|
---|
[70a06f6] | 442 |
|
---|
[9d7b3ea] | 443 | // take max of member alignment and global alignment
|
---|
[bd91e2a] | 444 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
|
---|
[9d7b3ea] | 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 | }
|
---|
[70a06f6] | 452 |
|
---|
[01aeade] | 453 | ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
|
---|
| 454 |
|
---|
| 455 | namespace {
|
---|
[bdf1954] | 456 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
|
---|
| 457 | std::stringstream name;
|
---|
| 458 |
|
---|
[ed1065c] | 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 |
|
---|
[ae63a18] | 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
|
---|
[bdf1954] | 464 | // with the same mangled name, so we need to further mangle the names.
|
---|
[ed1065c] | 465 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
|
---|
[ffad73a] | 466 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
|
---|
[ed1065c] | 467 | name << "P";
|
---|
| 468 | } else {
|
---|
| 469 | name << "M";
|
---|
| 470 | }
|
---|
[bdf1954] | 471 | }
|
---|
| 472 | name << "_";
|
---|
| 473 | std::list< DeclarationWithType *> ¶mList = function->get_parameters();
|
---|
| 474 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
[ffad73a] | 475 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
|
---|
[bdf1954] | 476 | name << "P";
|
---|
| 477 | } else {
|
---|
[ae63a18] | 478 | name << "M";
|
---|
[bdf1954] | 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 |
|
---|
[01aeade] | 488 | std::string makeAdapterName( const std::string &mangleName ) {
|
---|
| 489 | return "_adapter" + mangleName;
|
---|
| 490 | }
|
---|
[6c3744e] | 491 |
|
---|
[6635c74] | 492 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {}
|
---|
[01aeade] | 493 |
|
---|
[c2ad3c9] | 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 | }
|
---|
[9799ec8] | 507 |
|
---|
[c2ad3c9] | 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;
|
---|
[01aeade] | 517 | } // if
|
---|
| 518 | } // if
|
---|
| 519 | } // if
|
---|
| 520 | } // if
|
---|
| 521 | } // if
|
---|
| 522 | } // if
|
---|
[1194734] | 523 | return 0;
|
---|
[01aeade] | 524 | }
|
---|
[9799ec8] | 525 |
|
---|
[c2ad3c9] | 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 | }
|
---|
[70a06f6] | 530 |
|
---|
[c2ad3c9] | 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 | }
|
---|
[9799ec8] | 565 |
|
---|
[c2ad3c9] | 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;
|
---|
[dc12481] | 583 | } // if
|
---|
| 584 | } // if
|
---|
| 585 | } // if
|
---|
| 586 | } // if
|
---|
| 587 | return 0;
|
---|
| 588 | }
|
---|
[01aeade] | 589 |
|
---|
[c2ad3c9] | 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 |
|
---|
[8c49c0e] | 614 | void Pass1::findTypeOps( const Type::ForallList &forall ) {
|
---|
[ed1065c] | 615 | // what if a nested function uses an assignment operator?
|
---|
| 616 | // assignOps.clear();
|
---|
[8c49c0e] | 617 | for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
|
---|
[01aeade] | 618 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) {
|
---|
| 619 | std::string typeName;
|
---|
[dc12481] | 620 | if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) {
|
---|
[1194734] | 621 | assignOps[ typeInst->get_name() ] = *assert;
|
---|
[c2ad3c9] | 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;
|
---|
[01aeade] | 628 | } // if
|
---|
| 629 | } // for
|
---|
| 630 | } // for
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[82dd287] | 633 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
|
---|
[89173242] | 634 | // if this is a assignment function, put it in the map for this scope
|
---|
[c2ad3c9] | 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 );
|
---|
[1194734] | 650 | }
|
---|
| 651 | }
|
---|
[b4cd03b7] | 652 |
|
---|
[e56cfdb0] | 653 | if ( functionDecl->get_statements() ) { // empty routine body ?
|
---|
| 654 | doBeginScope();
|
---|
[4b8f918] | 655 | scopeTyVars.beginScope();
|
---|
[63c0dbf] | 656 | assignOps.beginScope();
|
---|
[c2ad3c9] | 657 | ctorOps.beginScope();
|
---|
| 658 | copyOps.beginScope();
|
---|
| 659 | dtorOps.beginScope();
|
---|
[9799ec8] | 660 |
|
---|
[01aeade] | 661 | DeclarationWithType *oldRetval = retval;
|
---|
| 662 | bool oldUseRetval = useRetval;
|
---|
[e56cfdb0] | 663 |
|
---|
| 664 | // process polymorphic return value
|
---|
[01aeade] | 665 | retval = 0;
|
---|
[3bb195cb] | 666 | if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) {
|
---|
[01aeade] | 667 | retval = functionDecl->get_functionType()->get_returnVals().front();
|
---|
[ae63a18] | 668 |
|
---|
[01aeade] | 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
|
---|
[ae63a18] | 675 |
|
---|
[e56cfdb0] | 676 | FunctionType *functionType = functionDecl->get_functionType();
|
---|
[01aeade] | 677 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
|
---|
[c2ad3c9] | 678 | findTypeOps( functionDecl->get_functionType()->get_forall() );
|
---|
[e56cfdb0] | 679 |
|
---|
| 680 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
| 681 | std::list< FunctionType *> functions;
|
---|
[8c49c0e] | 682 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
[e56cfdb0] | 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
|
---|
[b4cd03b7] | 690 |
|
---|
[e56cfdb0] | 691 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
[bdf1954] | 692 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
[e56cfdb0] | 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 |
|
---|
[01aeade] | 699 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
|
---|
[ae63a18] | 700 |
|
---|
[6f49cdf] | 701 | scopeTyVars.endScope();
|
---|
[63c0dbf] | 702 | assignOps.endScope();
|
---|
[c2ad3c9] | 703 | ctorOps.endScope();
|
---|
| 704 | copyOps.endScope();
|
---|
| 705 | dtorOps.endScope();
|
---|
[01aeade] | 706 | retval = oldRetval;
|
---|
| 707 | useRetval = oldUseRetval;
|
---|
[e56cfdb0] | 708 | doEndScope();
|
---|
[01aeade] | 709 | } // if
|
---|
| 710 | return functionDecl;
|
---|
| 711 | }
|
---|
[6c3744e] | 712 |
|
---|
[01aeade] | 713 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
|
---|
| 714 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
| 715 | return Mutator::mutate( typeDecl );
|
---|
| 716 | }
|
---|
[6c3744e] | 717 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 726 |
|
---|
[01aeade] | 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;
|
---|
[6c3744e] | 735 |
|
---|
[01aeade] | 736 | }
|
---|
[6c3744e] | 737 |
|
---|
[5c52b06] | 738 | void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
|
---|
[4b8f918] | 739 | Type *polyType = isPolyType( parmType, exprTyVars );
|
---|
| 740 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
|
---|
| 741 | std::string typeName = mangleType( polyType );
|
---|
[adc6781] | 742 | if ( seenTypes.count( typeName ) ) return;
|
---|
[5c52b06] | 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++;
|
---|
[4b8f918] | 748 | if ( dynamic_cast< StructInstType* >( polyType ) ) {
|
---|
[5c52b06] | 749 | if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
|
---|
[89173242] | 750 | // zero-length arrays are forbidden by C, so don't pass offset for empty struct
|
---|
| 751 | if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) {
|
---|
[d75038c] | 752 | arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) );
|
---|
[89173242] | 753 | arg++;
|
---|
| 754 | }
|
---|
[5c52b06] | 755 | } else {
|
---|
| 756 | throw SemanticError( "Cannot pass non-struct type for generic struct" );
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
| 759 |
|
---|
[adc6781] | 760 | seenTypes.insert( typeName );
|
---|
[5c52b06] | 761 | }
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
[7754cde] | 765 | // pass size/align for type variables
|
---|
[01aeade] | 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++;
|
---|
[db0b3ce] | 774 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
|
---|
| 775 | arg++;
|
---|
[01aeade] | 776 | } else {
|
---|
[ea5daeb] | 777 | // xxx - should this be an assertion?
|
---|
[540de412] | 778 | throw SemanticError( "unbound type variable: " + tyParm->first + " in application ", appExpr );
|
---|
[01aeade] | 779 | } // if
|
---|
| 780 | } // if
|
---|
| 781 | } // for
|
---|
[7754cde] | 782 |
|
---|
| 783 | // add size/align for generic types to parameter list
|
---|
[906e24d] | 784 | if ( ! appExpr->get_function()->has_result() ) return;
|
---|
| 785 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_result() );
|
---|
[7754cde] | 786 | assert( funcType );
|
---|
[ae63a18] | 787 |
|
---|
[7754cde] | 788 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
|
---|
| 789 | std::list< Expression* >::const_iterator fnArg = arg;
|
---|
[ea5daeb] | 790 | std::set< std::string > seenTypes; ///< names for generic types we've seen
|
---|
[7754cde] | 791 |
|
---|
[5c52b06] | 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 | }
|
---|
[70a06f6] | 797 |
|
---|
[5c52b06] | 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 );
|
---|
[906e24d] | 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 );
|
---|
[7754cde] | 803 | }
|
---|
[01aeade] | 804 | }
|
---|
[6c3744e] | 805 |
|
---|
[01aeade] | 806 | ObjectDecl *Pass1::makeTemporary( Type *type ) {
|
---|
[68cd1ce] | 807 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
|
---|
[01aeade] | 808 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
| 809 | return newObj;
|
---|
| 810 | }
|
---|
[6c3744e] | 811 |
|
---|
[01aeade] | 812 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
|
---|
[cf16f94] | 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 );
|
---|
[5c52b06] | 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.
|
---|
[5f6c42c] | 828 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
|
---|
[cf16f94] | 829 | paramExpr = new AddressExpr( paramExpr );
|
---|
[01aeade] | 830 | } // if
|
---|
[cf16f94] | 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;
|
---|
[01aeade] | 840 | }
|
---|
[6c3744e] | 841 |
|
---|
[48ca586] | 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 | }
|
---|
[b4cd03b7] | 849 |
|
---|
[48ca586] | 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 |
|
---|
[3bb195cb] | 873 | Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *dynType, std::list< Expression *>::iterator &arg ) {
|
---|
[01aeade] | 874 | assert( env );
|
---|
[3bb195cb] | 875 | Type *concrete = replaceWithConcrete( appExpr, dynType );
|
---|
[70a06f6] | 876 | // add out-parameter for return value
|
---|
[01aeade] | 877 | return addRetParam( appExpr, function, concrete, arg );
|
---|
| 878 | }
|
---|
[6c3744e] | 879 |
|
---|
[01aeade] | 880 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
| 881 | Expression *ret = appExpr;
|
---|
[3bb195cb] | 882 | // if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
| 883 | if ( isDynRet( function, tyVars ) ) {
|
---|
[01aeade] | 884 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
|
---|
| 885 | } // if
|
---|
[bdf1954] | 886 | std::string mangleName = mangleAdapterName( function, tyVars );
|
---|
[01aeade] | 887 | std::string adapterName = makeAdapterName( mangleName );
|
---|
[b1a6d6b] | 888 |
|
---|
[b4cd03b7] | 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 ) );
|
---|
[906e24d] | 892 | appExpr->set_function( new NameExpr( adapterName ) ); // xxx - result is never set on NameExpr
|
---|
[ae63a18] | 893 |
|
---|
[01aeade] | 894 | return ret;
|
---|
| 895 | }
|
---|
[6c3744e] | 896 |
|
---|
[01aeade] | 897 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
|
---|
[906e24d] | 898 | assert( arg->has_result() );
|
---|
[1cced28] | 899 | if ( isPolyType( param, exprTyVars ) ) {
|
---|
[906e24d] | 900 | if ( isPolyType( arg->get_result() ) ) {
|
---|
[9407ed8] | 901 | // if the argument's type is polymorphic, we don't need to box again!
|
---|
[01aeade] | 902 | return;
|
---|
[906e24d] | 903 | } else if ( arg->get_result()->get_isLvalue() ) {
|
---|
[b3ab8f0] | 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)
|
---|
[fea7ca7] | 905 | // xxx - need to test that this code is still reachable
|
---|
[b3ab8f0] | 906 | if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) {
|
---|
[bc1ab61] | 907 | commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) );
|
---|
[b3ab8f0] | 908 | } else {
|
---|
| 909 | arg = new AddressExpr( arg );
|
---|
| 910 | }
|
---|
[01aeade] | 911 | } else {
|
---|
[bd85400] | 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 );
|
---|
[f6d7e0f] | 916 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 926 |
|
---|
[b4cd03b7] | 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.
|
---|
[01aeade] | 930 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
|
---|
[ea5daeb] | 931 | if ( getFunctionType( formal ) ) {
|
---|
| 932 | Type * newType = formal->clone();
|
---|
[b4cd03b7] | 933 | newType = ScrubTyVars::scrub( newType, tyVars );
|
---|
[01aeade] | 934 | actual = new CastExpr( actual, newType );
|
---|
| 935 | } // if
|
---|
| 936 | }
|
---|
[6c3744e] | 937 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 945 |
|
---|
[01aeade] | 946 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
| 947 | std::list< Expression *>::iterator cur = arg;
|
---|
[8c49c0e] | 948 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
[01aeade] | 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() );
|
---|
[698664b3] | 951 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 959 |
|
---|
[01aeade] | 960 | void makeRetParm( FunctionType *funcType ) {
|
---|
| 961 | DeclarationWithType *retParm = funcType->get_returnVals().front();
|
---|
[6c3744e] | 962 |
|
---|
[01aeade] | 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 );
|
---|
[6c3744e] | 966 |
|
---|
[01aeade] | 967 | // we don't need the return value any more
|
---|
| 968 | funcType->get_returnVals().clear();
|
---|
| 969 | }
|
---|
[6c3744e] | 970 |
|
---|
[01aeade] | 971 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
| 972 | // actually make the adapter type
|
---|
| 973 | FunctionType *adapter = adaptee->clone();
|
---|
[3bb195cb] | 974 | // if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
| 975 | if ( isDynRet( adapter, tyVars ) ) {
|
---|
[01aeade] | 976 | makeRetParm( adapter );
|
---|
| 977 | } // if
|
---|
[68cd1ce] | 978 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
|
---|
[01aeade] | 979 | return adapter;
|
---|
| 980 | }
|
---|
[6c3744e] | 981 |
|
---|
[01aeade] | 982 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
|
---|
| 983 | assert( param );
|
---|
| 984 | assert( arg );
|
---|
[ffad73a] | 985 | if ( isPolyType( realParam->get_type(), tyVars ) ) {
|
---|
[30aeb27] | 986 | if ( ! isPolyType( arg->get_type() ) ) {
|
---|
[e56cfdb0] | 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() ) ) );
|
---|
[906e24d] | 989 | deref->set_result( arg->get_type()->clone() );
|
---|
[e56cfdb0] | 990 | return deref;
|
---|
| 991 | } // if
|
---|
[01aeade] | 992 | } // if
|
---|
| 993 | return new VariableExpr( param );
|
---|
| 994 | }
|
---|
[6c3744e] | 995 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 1006 |
|
---|
[01aeade] | 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;
|
---|
[ae63a18] | 1014 |
|
---|
[8c49c0e] | 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();
|
---|
[01aeade] | 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
|
---|
[ae63a18] | 1028 |
|
---|
[01aeade] | 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();
|
---|
[cc3528f] | 1032 | param++; // skip adaptee parameter in the adapter type
|
---|
[01aeade] | 1033 | if ( realType->get_returnVals().empty() ) {
|
---|
[cc3528f] | 1034 | // void return
|
---|
[01aeade] | 1035 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
| 1036 | bodyStmt = new ExprStmt( noLabels, adapteeApp );
|
---|
[3bb195cb] | 1037 | // } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
| 1038 | } else if ( isDynType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
[cc3528f] | 1039 | // return type T
|
---|
[01aeade] | 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 );
|
---|
[de62360d] | 1059 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
|
---|
[01aeade] | 1060 | }
|
---|
[6c3744e] | 1061 |
|
---|
[c29d9ce] | 1062 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
|
---|
[e497c1d] | 1063 | // collect a list of function types passed as parameters or implicit parameters (assertions)
|
---|
[01aeade] | 1064 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
| 1065 | std::list< FunctionType *> functions;
|
---|
[8c49c0e] | 1066 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
[01aeade] | 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
|
---|
[e497c1d] | 1074 |
|
---|
[e56cfdb0] | 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
|
---|
[01aeade] | 1077 | std::set< std::string > adaptersDone;
|
---|
[e497c1d] | 1078 |
|
---|
[01aeade] | 1079 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
[c29d9ce] | 1080 | FunctionType *originalFunction = (*funType)->clone();
|
---|
[01aeade] | 1081 | FunctionType *realFunction = (*funType)->clone();
|
---|
| 1082 | std::string mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
[e497c1d] | 1083 |
|
---|
[e56cfdb0] | 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.
|
---|
[01aeade] | 1086 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
[e497c1d] | 1087 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
[ae63a18] | 1088 |
|
---|
[e56cfdb0] | 1089 | // apply substitution to type variables to figure out what the adapter's type should look like
|
---|
[e497c1d] | 1090 | assert( env );
|
---|
| 1091 | env->apply( realFunction );
|
---|
[ae63a18] | 1092 | mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
[bdf1954] | 1093 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
|
---|
[e497c1d] | 1094 |
|
---|
[6635c74] | 1095 | typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter;
|
---|
| 1096 | AdapterIter adapter = adapters.find( mangleName );
|
---|
[e56cfdb0] | 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 );
|
---|
[6635c74] | 1100 | std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
|
---|
| 1101 | adapter = answer.first;
|
---|
[e56cfdb0] | 1102 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
|
---|
[c29d9ce] | 1103 | } // if
|
---|
[e56cfdb0] | 1104 | assert( adapter != adapters.end() );
|
---|
| 1105 |
|
---|
| 1106 | // add the appropriate adapter as a parameter
|
---|
| 1107 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
|
---|
[01aeade] | 1108 | } // if
|
---|
| 1109 | } // for
|
---|
[e56cfdb0] | 1110 | } // passAdapters
|
---|
[6c3744e] | 1111 |
|
---|
[78dd0da] | 1112 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
|
---|
[01aeade] | 1113 | NameExpr *opExpr;
|
---|
| 1114 | if ( isIncr ) {
|
---|
| 1115 | opExpr = new NameExpr( "?+=?" );
|
---|
| 1116 | } else {
|
---|
| 1117 | opExpr = new NameExpr( "?-=?" );
|
---|
[6c3744e] | 1118 | } // if
|
---|
[01aeade] | 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() );
|
---|
[6c3744e] | 1124 | } // if
|
---|
[adc6781] | 1125 | addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) );
|
---|
[906e24d] | 1126 | addAssign->set_result( appExpr->get_result()->clone() );
|
---|
[01aeade] | 1127 | if ( appExpr->get_env() ) {
|
---|
| 1128 | addAssign->set_env( appExpr->get_env() );
|
---|
[6c3744e] | 1129 | appExpr->set_env( 0 );
|
---|
| 1130 | } // if
|
---|
[01aeade] | 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() == "?[?]" ) {
|
---|
[906e24d] | 1140 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1141 | assert( appExpr->get_args().size() == 2 );
|
---|
[906e24d] | 1142 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env );
|
---|
| 1143 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env );
|
---|
[ae63a18] | 1144 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers
|
---|
[01aeade] | 1145 | UntypedExpr *ret = 0;
|
---|
[ae63a18] | 1146 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer
|
---|
[01aeade] | 1147 | ret = new UntypedExpr( new NameExpr( "?+?" ) );
|
---|
| 1148 | } // if
|
---|
[ffad73a] | 1149 | if ( baseType1 ) {
|
---|
[01aeade] | 1150 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
| 1151 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
[adc6781] | 1152 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
|
---|
[01aeade] | 1153 | ret->get_args().push_back( appExpr->get_args().front() );
|
---|
| 1154 | ret->get_args().push_back( multiply );
|
---|
[ffad73a] | 1155 | } else if ( baseType2 ) {
|
---|
[01aeade] | 1156 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
| 1157 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
[adc6781] | 1158 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
|
---|
[01aeade] | 1159 | ret->get_args().push_back( multiply );
|
---|
| 1160 | ret->get_args().push_back( appExpr->get_args().back() );
|
---|
| 1161 | } // if
|
---|
[ffad73a] | 1162 | if ( baseType1 || baseType2 ) {
|
---|
[906e24d] | 1163 | ret->set_result( appExpr->get_result()->clone() );
|
---|
[01aeade] | 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() == "*?" ) {
|
---|
[906e24d] | 1173 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1174 | assert( ! appExpr->get_args().empty() );
|
---|
[906e24d] | 1175 | if ( isPolyType( appExpr->get_result(), scopeTyVars, env ) ) {
|
---|
[01aeade] | 1176 | Expression *ret = appExpr->get_args().front();
|
---|
[906e24d] | 1177 | delete ret->get_result();
|
---|
| 1178 | ret->set_result( appExpr->get_result()->clone() );
|
---|
[01aeade] | 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() == "?--" ) {
|
---|
[906e24d] | 1188 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1189 | assert( appExpr->get_args().size() == 1 );
|
---|
[906e24d] | 1190 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) {
|
---|
| 1191 | Type *tempType = appExpr->get_result()->clone();
|
---|
[01aeade] | 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
|
---|
[ffad73a] | 1204 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
|
---|
[01aeade] | 1205 | return new CommaExpr( firstComma, tempExpr );
|
---|
| 1206 | } // if
|
---|
| 1207 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
|
---|
[906e24d] | 1208 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1209 | assert( appExpr->get_args().size() == 1 );
|
---|
[906e24d] | 1210 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) {
|
---|
[ffad73a] | 1211 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
|
---|
[01aeade] | 1212 | } // if
|
---|
| 1213 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
|
---|
[906e24d] | 1214 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1215 | assert( appExpr->get_args().size() == 2 );
|
---|
[906e24d] | 1216 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env );
|
---|
| 1217 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env );
|
---|
[ffad73a] | 1218 | if ( baseType1 && baseType2 ) {
|
---|
[01aeade] | 1219 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
|
---|
| 1220 | divide->get_args().push_back( appExpr );
|
---|
[adc6781] | 1221 | divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
|
---|
[906e24d] | 1222 | divide->set_result( appExpr->get_result()->clone() );
|
---|
[01aeade] | 1223 | if ( appExpr->get_env() ) {
|
---|
| 1224 | divide->set_env( appExpr->get_env() );
|
---|
| 1225 | appExpr->set_env( 0 );
|
---|
| 1226 | } // if
|
---|
| 1227 | return divide;
|
---|
[ffad73a] | 1228 | } else if ( baseType1 ) {
|
---|
[01aeade] | 1229 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
| 1230 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
[adc6781] | 1231 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
|
---|
[01aeade] | 1232 | appExpr->get_args().back() = multiply;
|
---|
[ffad73a] | 1233 | } else if ( baseType2 ) {
|
---|
[01aeade] | 1234 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
| 1235 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
[adc6781] | 1236 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
|
---|
[01aeade] | 1237 | appExpr->get_args().front() = multiply;
|
---|
| 1238 | } // if
|
---|
| 1239 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
|
---|
[906e24d] | 1240 | assert( appExpr->has_result() );
|
---|
[01aeade] | 1241 | assert( appExpr->get_args().size() == 2 );
|
---|
[906e24d] | 1242 | Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env );
|
---|
[ffad73a] | 1243 | if ( baseType ) {
|
---|
[01aeade] | 1244 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
| 1245 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
[adc6781] | 1246 | multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) );
|
---|
[01aeade] | 1247 | appExpr->get_args().back() = multiply;
|
---|
| 1248 | } // if
|
---|
| 1249 | } // if
|
---|
| 1250 | return appExpr;
|
---|
| 1251 | } // if
|
---|
[6c3744e] | 1252 | } // if
|
---|
[01aeade] | 1253 | return 0;
|
---|
| 1254 | }
|
---|
[6c3744e] | 1255 |
|
---|
[01aeade] | 1256 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
|
---|
[e56cfdb0] | 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";
|
---|
[01aeade] | 1262 | bool oldUseRetval = useRetval;
|
---|
| 1263 | useRetval = false;
|
---|
| 1264 | appExpr->get_function()->acceptMutator( *this );
|
---|
| 1265 | mutateAll( appExpr->get_args(), *this );
|
---|
| 1266 | useRetval = oldUseRetval;
|
---|
[ae63a18] | 1267 |
|
---|
[906e24d] | 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() );
|
---|
[ae63a18] | 1271 |
|
---|
[01aeade] | 1272 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
|
---|
| 1273 | return newExpr;
|
---|
| 1274 | } // if
|
---|
[ae63a18] | 1275 |
|
---|
[01aeade] | 1276 | Expression *ret = appExpr;
|
---|
[ae63a18] | 1277 |
|
---|
[01aeade] | 1278 | std::list< Expression *>::iterator arg = appExpr->get_args().begin();
|
---|
| 1279 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
|
---|
[ae63a18] | 1280 |
|
---|
[bfae637] | 1281 | TyVarMap exprTyVars( (TypeDecl::Kind)-1 );
|
---|
[5c52b06] | 1282 | makeTyVarMap( function, exprTyVars );
|
---|
[3bb195cb] | 1283 | ReferenceToType *dynRetType = isDynRet( function, exprTyVars );
|
---|
[5c52b06] | 1284 |
|
---|
[3bb195cb] | 1285 | if ( dynRetType ) {
|
---|
| 1286 | ret = addDynRetParam( appExpr, function, dynRetType, arg );
|
---|
[01aeade] | 1287 | } else if ( needsAdapter( function, scopeTyVars ) ) {
|
---|
[e56cfdb0] | 1288 | // std::cerr << "needs adapter: ";
|
---|
[2e3a379] | 1289 | // printTyVarMap( std::cerr, scopeTyVars );
|
---|
| 1290 | // std::cerr << *env << std::endl;
|
---|
[01aeade] | 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();
|
---|
[ae63a18] | 1295 |
|
---|
[3bb195cb] | 1296 | passTypeVars( appExpr, dynRetType, arg, exprTyVars );
|
---|
[01aeade] | 1297 | addInferredParams( appExpr, function, arg, exprTyVars );
|
---|
[51b73452] | 1298 |
|
---|
[01aeade] | 1299 | arg = paramBegin;
|
---|
[ae63a18] | 1300 |
|
---|
[01aeade] | 1301 | boxParams( appExpr, function, arg, exprTyVars );
|
---|
| 1302 | passAdapters( appExpr, function, exprTyVars );
|
---|
[6c3744e] | 1303 |
|
---|
[01aeade] | 1304 | return ret;
|
---|
| 1305 | }
|
---|
[6c3744e] | 1306 |
|
---|
[01aeade] | 1307 | Expression *Pass1::mutate( UntypedExpr *expr ) {
|
---|
[906e24d] | 1308 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) {
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 1320 |
|
---|
[01aeade] | 1321 | Expression *Pass1::mutate( AddressExpr *addrExpr ) {
|
---|
[906e24d] | 1322 | assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() );
|
---|
[cf16f94] | 1323 |
|
---|
| 1324 | bool needs = false;
|
---|
| 1325 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
|
---|
[906e24d] | 1326 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) {
|
---|
[cf16f94] | 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() ) ) {
|
---|
[906e24d] | 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() );
|
---|
[cf16f94] | 1333 | needs = needsAdapter( function, scopeTyVars );
|
---|
| 1334 | } // if
|
---|
| 1335 | } // if
|
---|
| 1336 | } // if
|
---|
| 1337 | } // if
|
---|
| 1338 | } // if
|
---|
[fea7ca7] | 1339 | // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward
|
---|
| 1340 | // out of the if condition.
|
---|
[906e24d] | 1341 | bool polytype = isPolyType( addrExpr->get_arg()->get_result(), scopeTyVars, env );
|
---|
[01aeade] | 1342 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
|
---|
[fea7ca7] | 1343 | if ( polytype || needs ) {
|
---|
[01aeade] | 1344 | Expression *ret = addrExpr->get_arg();
|
---|
[906e24d] | 1345 | delete ret->get_result();
|
---|
| 1346 | ret->set_result( addrExpr->get_result()->clone() );
|
---|
[01aeade] | 1347 | addrExpr->set_arg( 0 );
|
---|
| 1348 | delete addrExpr;
|
---|
| 1349 | return ret;
|
---|
| 1350 | } else {
|
---|
| 1351 | return addrExpr;
|
---|
| 1352 | } // if
|
---|
| 1353 | }
|
---|
[6c3744e] | 1354 |
|
---|
[b10c9959] | 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
|
---|
[9799ec8] | 1358 | // xxx - functionObj is never added to a list of declarations...
|
---|
[b10c9959] | 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() );
|
---|
[9799ec8] | 1362 | functionObj->set_scopeLevel( functionDecl->get_scopeLevel() );
|
---|
[b10c9959] | 1363 | return new VariableExpr( functionObj );
|
---|
| 1364 | }
|
---|
[70a06f6] | 1365 |
|
---|
[c2ad3c9] | 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 | }
|
---|
[9799ec8] | 1381 |
|
---|
[cf16f94] | 1382 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
|
---|
| 1383 | if ( retval && returnStmt->get_expr() ) {
|
---|
[906e24d] | 1384 | assert( returnStmt->get_expr()->has_result() && ! returnStmt->get_expr()->get_result()->isVoid() );
|
---|
[cf16f94] | 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() ) {
|
---|
[ae63a18] | 1387 | // by this point, a cast expr on a polymorphic return value is redundant
|
---|
[cf16f94] | 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;
|
---|
[5f6c42c] | 1394 | } //while
|
---|
[1194734] | 1395 |
|
---|
| 1396 | // find assignment operator for (polymorphic) return type
|
---|
[b10c9959] | 1397 | ApplicationExpr *assignExpr = 0;
|
---|
[1194734] | 1398 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
|
---|
[b10c9959] | 1399 | // find assignment operator for type variable
|
---|
[63c0dbf] | 1400 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
|
---|
[1194734] | 1401 | if ( assignIter == assignOps.end() ) {
|
---|
| 1402 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
|
---|
| 1403 | } // if
|
---|
[b10c9959] | 1404 | assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
|
---|
[1194734] | 1405 | } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) {
|
---|
[b10c9959] | 1406 | // find assignment operator for generic type
|
---|
[dc12481] | 1407 | DeclarationWithType *functionDecl = scopedAssignOps.find( refType );
|
---|
| 1408 | if ( ! functionDecl ) {
|
---|
[1194734] | 1409 | throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() );
|
---|
| 1410 | }
|
---|
[b10c9959] | 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();
|
---|
[8c49c0e] | 1418 | Type::ForallList &forallParams = functionDecl->get_type()->get_forall();
|
---|
[b10c9959] | 1419 | std::list< Expression* >::const_iterator tyIt = tyParams.begin();
|
---|
[8c49c0e] | 1420 | Type::ForallList::const_iterator forallIt = forallParams.begin();
|
---|
[b10c9959] | 1421 | for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) {
|
---|
[ae7014e] | 1422 | // Add appropriate mapping to assignment expression environment
|
---|
[b10c9959] | 1423 | TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt );
|
---|
| 1424 | assert( formalTypeExpr && "type parameters must be type expressions" );
|
---|
| 1425 | Type *formalType = formalTypeExpr->get_type();
|
---|
[ae7014e] | 1426 | assignExpr->get_env()->add( (*forallIt)->get_name(), formalType );
|
---|
| 1427 |
|
---|
[c2ad3c9] | 1428 | // skip non-otype parameters (ftype/dtype)
|
---|
[ae7014e] | 1429 | if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue;
|
---|
| 1430 |
|
---|
[c2ad3c9] | 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 );
|
---|
[b10c9959] | 1437 |
|
---|
[c2ad3c9] | 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 );
|
---|
[9799ec8] | 1443 |
|
---|
[c2ad3c9] | 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
|
---|
[ae7014e] | 1446 | std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions();
|
---|
[c2ad3c9] | 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 );
|
---|
[9799ec8] | 1457 |
|
---|
[b10c9959] | 1458 | }
|
---|
[1194734] | 1459 | }
|
---|
[b10c9959] | 1460 | assert( assignExpr );
|
---|
[1194734] | 1461 |
|
---|
| 1462 | // replace return statement with appropriate assignment to out parameter
|
---|
[cf16f94] | 1463 | Expression *retParm = new NameExpr( retval->get_name() );
|
---|
[906e24d] | 1464 | retParm->set_result( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
|
---|
[cf16f94] | 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 );
|
---|
[01aeade] | 1474 | } else {
|
---|
[cf16f94] | 1475 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
|
---|
[01aeade] | 1476 | } // if
|
---|
[cf16f94] | 1477 | return returnStmt;
|
---|
[01aeade] | 1478 | }
|
---|
[6c3744e] | 1479 |
|
---|
[01aeade] | 1480 | Type * Pass1::mutate( PointerType *pointerType ) {
|
---|
[6f49cdf] | 1481 | scopeTyVars.beginScope();
|
---|
[01aeade] | 1482 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
[ae63a18] | 1483 |
|
---|
[01aeade] | 1484 | Type *ret = Mutator::mutate( pointerType );
|
---|
[ae63a18] | 1485 |
|
---|
[6f49cdf] | 1486 | scopeTyVars.endScope();
|
---|
[01aeade] | 1487 | return ret;
|
---|
| 1488 | }
|
---|
[6c3744e] | 1489 |
|
---|
[01aeade] | 1490 | Type * Pass1::mutate( FunctionType *functionType ) {
|
---|
[6f49cdf] | 1491 | scopeTyVars.beginScope();
|
---|
[01aeade] | 1492 | makeTyVarMap( functionType, scopeTyVars );
|
---|
[ae63a18] | 1493 |
|
---|
[01aeade] | 1494 | Type *ret = Mutator::mutate( functionType );
|
---|
[ae63a18] | 1495 |
|
---|
[6f49cdf] | 1496 | scopeTyVars.endScope();
|
---|
[01aeade] | 1497 | return ret;
|
---|
| 1498 | }
|
---|
[51b73452] | 1499 |
|
---|
[01aeade] | 1500 | void Pass1::doBeginScope() {
|
---|
[6635c74] | 1501 | adapters.beginScope();
|
---|
[1194734] | 1502 | scopedAssignOps.beginScope();
|
---|
[c2ad3c9] | 1503 | scopedCtorOps.beginScope();
|
---|
| 1504 | scopedCopyOps.beginScope();
|
---|
| 1505 | scopedDtorOps.beginScope();
|
---|
[01aeade] | 1506 | }
|
---|
[b1a6d6b] | 1507 |
|
---|
[01aeade] | 1508 | void Pass1::doEndScope() {
|
---|
[6635c74] | 1509 | adapters.endScope();
|
---|
[1194734] | 1510 | scopedAssignOps.endScope();
|
---|
[c2ad3c9] | 1511 | scopedCtorOps.endScope();
|
---|
| 1512 | scopedCopyOps.endScope();
|
---|
| 1513 | scopedDtorOps.endScope();
|
---|
[01aeade] | 1514 | }
|
---|
[51b73452] | 1515 |
|
---|
| 1516 | ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
|
---|
| 1517 |
|
---|
[01aeade] | 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 ) {
|
---|
[bdf1954] | 1528 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
[01aeade] | 1529 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
| 1530 | std::string adapterName = makeAdapterName( mangleName );
|
---|
[68cd1ce] | 1531 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
|
---|
[01aeade] | 1532 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
| 1533 | }
|
---|
| 1534 | }
|
---|
[5f6c42c] | 1535 | // deleteAll( functions );
|
---|
[01aeade] | 1536 | }
|
---|
[6c3744e] | 1537 |
|
---|
[01aeade] | 1538 | template< typename DeclClass >
|
---|
| 1539 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
|
---|
| 1540 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
[6c3744e] | 1541 |
|
---|
[01aeade] | 1542 | return ret;
|
---|
| 1543 | }
|
---|
[6c3744e] | 1544 |
|
---|
[01aeade] | 1545 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
|
---|
| 1546 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
| 1547 | }
|
---|
[6c3744e] | 1548 |
|
---|
[01aeade] | 1549 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
|
---|
| 1550 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
| 1551 | }
|
---|
[6c3744e] | 1552 |
|
---|
[01aeade] | 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 | }
|
---|
[6c3744e] | 1561 |
|
---|
[01aeade] | 1562 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
|
---|
| 1563 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
| 1564 | }
|
---|
[6c3744e] | 1565 |
|
---|
[01aeade] | 1566 | Type * Pass2::mutate( PointerType *pointerType ) {
|
---|
[6f49cdf] | 1567 | scopeTyVars.beginScope();
|
---|
[01aeade] | 1568 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
[ae63a18] | 1569 |
|
---|
[01aeade] | 1570 | Type *ret = Mutator::mutate( pointerType );
|
---|
[ae63a18] | 1571 |
|
---|
[6f49cdf] | 1572 | scopeTyVars.endScope();
|
---|
[01aeade] | 1573 | return ret;
|
---|
| 1574 | }
|
---|
[6c3744e] | 1575 |
|
---|
[01aeade] | 1576 | Type *Pass2::mutate( FunctionType *funcType ) {
|
---|
[6f49cdf] | 1577 | scopeTyVars.beginScope();
|
---|
[01aeade] | 1578 | makeTyVarMap( funcType, scopeTyVars );
|
---|
[7754cde] | 1579 |
|
---|
| 1580 | // move polymorphic return type to parameter list
|
---|
[3bb195cb] | 1581 | if ( isDynRet( funcType ) ) {
|
---|
[01aeade] | 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 | }
|
---|
[7754cde] | 1587 |
|
---|
| 1588 | // add size/align and assertions for type parameters to parameter list
|
---|
[01aeade] | 1589 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
|
---|
| 1590 | std::list< DeclarationWithType *> inferredParams;
|
---|
[78dd0da] | 1591 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
|
---|
[05d47278] | 1592 | ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0,
|
---|
| 1593 | new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
|
---|
[8c49c0e] | 1594 | for ( Type::ForallList::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
|
---|
[db0b3ce] | 1595 | ObjectDecl *sizeParm, *alignParm;
|
---|
| 1596 | // add all size and alignment parameters to parameter list
|
---|
[01aeade] | 1597 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
|
---|
[78dd0da] | 1598 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
|
---|
[adc6781] | 1599 | std::string parmName = mangleType( &parmType );
|
---|
[ae63a18] | 1600 |
|
---|
[78dd0da] | 1601 | sizeParm = newObj.clone();
|
---|
[adc6781] | 1602 | sizeParm->set_name( sizeofName( parmName ) );
|
---|
[db0b3ce] | 1603 | last = funcType->get_parameters().insert( last, sizeParm );
|
---|
| 1604 | ++last;
|
---|
[78dd0da] | 1605 |
|
---|
| 1606 | alignParm = newObj.clone();
|
---|
[adc6781] | 1607 | alignParm->set_name( alignofName( parmName ) );
|
---|
[db0b3ce] | 1608 | last = funcType->get_parameters().insert( last, alignParm );
|
---|
[01aeade] | 1609 | ++last;
|
---|
| 1610 | }
|
---|
[e56cfdb0] | 1611 | // move all assertions into parameter list
|
---|
[01aeade] | 1612 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
|
---|
[f8b961b] | 1613 | // *assert = (*assert)->acceptMutator( *this );
|
---|
[01aeade] | 1614 | inferredParams.push_back( *assert );
|
---|
| 1615 | }
|
---|
| 1616 | (*tyParm)->get_assertions().clear();
|
---|
| 1617 | }
|
---|
[7754cde] | 1618 |
|
---|
[5c52b06] | 1619 | // add size/align for generic parameter types to parameter list
|
---|
[b18b0b5] | 1620 | std::set< std::string > seenTypes; // sizeofName for generic types we've seen
|
---|
[7754cde] | 1621 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {
|
---|
[4b8f918] | 1622 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
|
---|
| 1623 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
|
---|
| 1624 | std::string typeName = mangleType( polyType );
|
---|
[adc6781] | 1625 | if ( seenTypes.count( typeName ) ) continue;
|
---|
[ae63a18] | 1626 |
|
---|
[05d47278] | 1627 | ObjectDecl *sizeParm, *alignParm, *offsetParm;
|
---|
[7754cde] | 1628 | sizeParm = newObj.clone();
|
---|
[adc6781] | 1629 | sizeParm->set_name( sizeofName( typeName ) );
|
---|
[7754cde] | 1630 | last = funcType->get_parameters().insert( last, sizeParm );
|
---|
| 1631 | ++last;
|
---|
| 1632 |
|
---|
| 1633 | alignParm = newObj.clone();
|
---|
[adc6781] | 1634 | alignParm->set_name( alignofName( typeName ) );
|
---|
[7754cde] | 1635 | last = funcType->get_parameters().insert( last, alignParm );
|
---|
| 1636 | ++last;
|
---|
| 1637 |
|
---|
[4b8f918] | 1638 | if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) {
|
---|
[89173242] | 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();
|
---|
[adc6781] | 1642 | offsetParm->set_name( offsetofName( typeName ) );
|
---|
[89173242] | 1643 | last = funcType->get_parameters().insert( last, offsetParm );
|
---|
| 1644 | ++last;
|
---|
| 1645 | }
|
---|
[05d47278] | 1646 | }
|
---|
| 1647 |
|
---|
[adc6781] | 1648 | seenTypes.insert( typeName );
|
---|
[7754cde] | 1649 | }
|
---|
| 1650 | }
|
---|
| 1651 |
|
---|
| 1652 | // splice assertion parameters into parameter list
|
---|
[01aeade] | 1653 | funcType->get_parameters().splice( last, inferredParams );
|
---|
| 1654 | addAdapters( funcType );
|
---|
| 1655 | mutateAll( funcType->get_returnVals(), *this );
|
---|
| 1656 | mutateAll( funcType->get_parameters(), *this );
|
---|
[ae63a18] | 1657 |
|
---|
[6f49cdf] | 1658 | scopeTyVars.endScope();
|
---|
[01aeade] | 1659 | return funcType;
|
---|
| 1660 | }
|
---|
[51b73452] | 1661 |
|
---|
[4b8f918] | 1662 | ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
|
---|
[51b73452] | 1663 |
|
---|
[aa19ccf] | 1664 | void PolyGenericCalculator::beginTypeScope( Type *ty ) {
|
---|
| 1665 | scopeTyVars.beginScope();
|
---|
| 1666 | makeTyVarMap( ty, scopeTyVars );
|
---|
| 1667 | }
|
---|
| 1668 |
|
---|
| 1669 | void PolyGenericCalculator::endTypeScope() {
|
---|
| 1670 | scopeTyVars.endScope();
|
---|
| 1671 | }
|
---|
[51b73452] | 1672 |
|
---|
[01aeade] | 1673 | template< typename DeclClass >
|
---|
[8a34677] | 1674 | DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
|
---|
[aa19ccf] | 1675 | beginTypeScope( type );
|
---|
[1ba88a0] | 1676 | // knownLayouts.beginScope();
|
---|
| 1677 | // knownOffsets.beginScope();
|
---|
[ae63a18] | 1678 |
|
---|
[1ba88a0] | 1679 | DeclClass *ret = static_cast< DeclClass *>( Parent::mutate( decl ) );
|
---|
[6c3744e] | 1680 |
|
---|
[1ba88a0] | 1681 | // knownOffsets.endScope();
|
---|
| 1682 | // knownLayouts.endScope();
|
---|
[aa19ccf] | 1683 | endTypeScope();
|
---|
[01aeade] | 1684 | return ret;
|
---|
| 1685 | }
|
---|
[6c3744e] | 1686 |
|
---|
[8a34677] | 1687 | ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) {
|
---|
[01aeade] | 1688 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
| 1689 | }
|
---|
[6c3744e] | 1690 |
|
---|
[8a34677] | 1691 | DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) {
|
---|
[1ba88a0] | 1692 | knownLayouts.beginScope();
|
---|
| 1693 | knownOffsets.beginScope();
|
---|
| 1694 |
|
---|
| 1695 | DeclarationWithType * decl = handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
| 1696 | knownOffsets.endScope();
|
---|
| 1697 | knownLayouts.endScope();
|
---|
| 1698 | return decl;
|
---|
[01aeade] | 1699 | }
|
---|
[6c3744e] | 1700 |
|
---|
[8a34677] | 1701 | TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) {
|
---|
[01aeade] | 1702 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
| 1703 | }
|
---|
[6c3744e] | 1704 |
|
---|
[8a34677] | 1705 | TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) {
|
---|
[01aeade] | 1706 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
[1ba88a0] | 1707 | return Parent::mutate( typeDecl );
|
---|
[01aeade] | 1708 | }
|
---|
[51b73452] | 1709 |
|
---|
[8a34677] | 1710 | Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
|
---|
[aa19ccf] | 1711 | beginTypeScope( pointerType );
|
---|
[ae63a18] | 1712 |
|
---|
[1ba88a0] | 1713 | Type *ret = Parent::mutate( pointerType );
|
---|
[ae63a18] | 1714 |
|
---|
[aa19ccf] | 1715 | endTypeScope();
|
---|
[01aeade] | 1716 | return ret;
|
---|
| 1717 | }
|
---|
[6c3744e] | 1718 |
|
---|
[8a34677] | 1719 | Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
|
---|
[aa19ccf] | 1720 | beginTypeScope( funcType );
|
---|
[ae63a18] | 1721 |
|
---|
[8a34677] | 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* )
|
---|
[4b8f918] | 1725 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
|
---|
| 1726 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
|
---|
| 1727 | knownLayouts.insert( mangleType( polyType ) );
|
---|
[8a34677] | 1728 | }
|
---|
| 1729 | }
|
---|
[70a06f6] | 1730 |
|
---|
[1ba88a0] | 1731 | Type *ret = Parent::mutate( funcType );
|
---|
[ae63a18] | 1732 |
|
---|
[aa19ccf] | 1733 | endTypeScope();
|
---|
[01aeade] | 1734 | return ret;
|
---|
[6c3744e] | 1735 | }
|
---|
[51b73452] | 1736 |
|
---|
[8a34677] | 1737 | Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) {
|
---|
[01aeade] | 1738 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
|
---|
[8a34677] | 1739 | if ( findGeneric( objectDecl->get_type() ) ) {
|
---|
[e01559c] | 1740 | // change initialization of a polymorphic value object
|
---|
| 1741 | // to allocate storage with alloca
|
---|
[ffad73a] | 1742 | Type *declType = objectDecl->get_type();
|
---|
[01aeade] | 1743 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
|
---|
[adc6781] | 1744 | alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) );
|
---|
[e01559c] | 1745 |
|
---|
| 1746 | delete objectDecl->get_init();
|
---|
| 1747 |
|
---|
| 1748 | std::list<Expression*> designators;
|
---|
[974906e2] | 1749 | objectDecl->set_init( new SingleInit( alloc, designators, false ) ); // not constructed
|
---|
[01aeade] | 1750 | }
|
---|
| 1751 | }
|
---|
[1ba88a0] | 1752 | return Parent::mutate( declStmt );
|
---|
[01aeade] | 1753 | }
|
---|
[05d47278] | 1754 |
|
---|
[2a4b088] | 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 ) ) {
|
---|
[bed4d37c] | 1762 | if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty()
|
---|
| 1763 | || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
|
---|
[2a4b088] | 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( "?[?]" ) );
|
---|
[adc6781] | 1776 | fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) );
|
---|
[2a4b088] | 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 );
|
---|
[10a7775] | 1786 | // xxx - should set results on derefExpr
|
---|
[2a4b088] | 1787 | derefdVar = derefExpr;
|
---|
| 1788 | }
|
---|
| 1789 | return derefdVar;
|
---|
| 1790 | }
|
---|
[d63eeb0] | 1791 |
|
---|
[8a34677] | 1792 | Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) {
|
---|
[05d47278] | 1793 | // mutate, exiting early if no longer MemberExpr
|
---|
[1ba88a0] | 1794 | Expression *expr = Parent::mutate( memberExpr );
|
---|
[05d47278] | 1795 | memberExpr = dynamic_cast< MemberExpr* >( expr );
|
---|
| 1796 | if ( ! memberExpr ) return expr;
|
---|
| 1797 |
|
---|
| 1798 | // get declaration for base struct, exiting early if not found
|
---|
[8488c715] | 1799 | int varDepth;
|
---|
| 1800 | VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth );
|
---|
[05d47278] | 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
|
---|
[8488c715] | 1806 | int tyDepth;
|
---|
| 1807 | Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth );
|
---|
[05d47278] | 1808 | if ( ! objectType ) return memberExpr;
|
---|
[8a34677] | 1809 | findGeneric( objectType ); // ensure layout for this type is available
|
---|
[05d47278] | 1810 |
|
---|
[ea5daeb] | 1811 | // replace member expression with dynamically-computed layout expression
|
---|
[4318107] | 1812 | Expression *newMemberExpr = 0;
|
---|
[05d47278] | 1813 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) {
|
---|
[2a4b088] | 1814 | // look up offset index
|
---|
| 1815 | long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() );
|
---|
| 1816 | if ( i == -1 ) return memberExpr;
|
---|
[05d47278] | 1817 |
|
---|
[2a4b088] | 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 ) );
|
---|
[4318107] | 1822 | newMemberExpr = fieldLoc;
|
---|
[98735ef] | 1823 | } else if ( dynamic_cast< UnionInstType* >( objectType ) ) {
|
---|
[2a4b088] | 1824 | // union members are all at offset zero, so build appropriately-dereferenced variable
|
---|
[4318107] | 1825 | newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth );
|
---|
[2a4b088] | 1826 | } else return memberExpr;
|
---|
[4318107] | 1827 | assert( newMemberExpr );
|
---|
| 1828 |
|
---|
[4067aa8] | 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() ) );
|
---|
[4318107] | 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;
|
---|
[2a4b088] | 1840 | }
|
---|
[05d47278] | 1841 |
|
---|
[8a34677] | 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
|
---|
[adc6781] | 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 ) ) );
|
---|
[8a34677] | 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 ) {
|
---|
[c2ad3c9] | 1884 | ty = replaceTypeInst( ty, env );
|
---|
[9799ec8] | 1885 |
|
---|
[8a34677] | 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
|
---|
[adc6781] | 1894 | std::string typeName = mangleType( ty );
|
---|
| 1895 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
|
---|
[8a34677] | 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
|
---|
[adc6781] | 1902 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call
|
---|
[8a34677] | 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
|
---|
[cc3528f] | 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 ) ) ) );
|
---|
[8a34677] | 1910 | // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array
|
---|
| 1911 | } else {
|
---|
[adc6781] | 1912 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
|
---|
| 1913 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
|
---|
[cb4c607] | 1914 | ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from_int( n_members ) ), false, false ) );
|
---|
[8a34677] | 1915 |
|
---|
| 1916 | // generate call to layout function
|
---|
[adc6781] | 1917 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) );
|
---|
[8a34677] | 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
|
---|
[adc6781] | 1929 | std::string typeName = mangleType( ty );
|
---|
| 1930 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
|
---|
[8a34677] | 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
|
---|
[adc6781] | 1937 | knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call
|
---|
[8a34677] | 1938 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
| 1939 |
|
---|
[adc6781] | 1940 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
|
---|
| 1941 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
|
---|
[8a34677] | 1942 |
|
---|
| 1943 | // generate call to layout function
|
---|
[adc6781] | 1944 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) );
|
---|
[8a34677] | 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 ) ) {
|
---|
[adc6781] | 1960 | Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) );
|
---|
[8a34677] | 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 ) ) {
|
---|
[adc6781] | 1970 | Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) );
|
---|
[8a34677] | 1971 | delete alignofExpr;
|
---|
| 1972 | return ret;
|
---|
| 1973 | }
|
---|
| 1974 | return alignofExpr;
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
| 1977 | Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) {
|
---|
[2a4b088] | 1978 | // mutate, exiting early if no longer OffsetofExpr
|
---|
[1ba88a0] | 1979 | Expression *expr = Parent::mutate( offsetofExpr );
|
---|
[2a4b088] | 1980 | offsetofExpr = dynamic_cast< OffsetofExpr* >( expr );
|
---|
| 1981 | if ( ! offsetofExpr ) return expr;
|
---|
| 1982 |
|
---|
| 1983 | // only mutate expressions for polymorphic structs/unions
|
---|
[8a34677] | 1984 | Type *ty = offsetofExpr->get_type();
|
---|
| 1985 | if ( ! findGeneric( ty ) ) return offsetofExpr;
|
---|
[2a4b088] | 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;
|
---|
[5c52b06] | 1995 | } else if ( dynamic_cast< UnionInstType* >( ty ) ) {
|
---|
[2a4b088] | 1996 | // all union members are at offset zero
|
---|
| 1997 | delete offsetofExpr;
|
---|
[ca35c51] | 1998 | return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "0" ) );
|
---|
[2a4b088] | 1999 | } else return offsetofExpr;
|
---|
[05d47278] | 2000 | }
|
---|
| 2001 |
|
---|
[8a34677] | 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
|
---|
[adc6781] | 2008 | ret = new NameExpr( offsetofName( mangleType( ty ) ) );
|
---|
[8a34677] | 2009 | } else {
|
---|
[adc6781] | 2010 | std::string offsetName = offsetofName( mangleType( ty ) );
|
---|
[8a34677] | 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
|
---|
[cb4c607] | 2033 | ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from_ulong( baseMembers.size() ) ), false, false ),
|
---|
[8a34677] | 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();
|
---|
[adc6781] | 2050 | knownOffsets.endScope();
|
---|
[8a34677] | 2051 | }
|
---|
| 2052 |
|
---|
[05d47278] | 2053 | ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
|
---|
| 2054 |
|
---|
| 2055 | template< typename DeclClass >
|
---|
| 2056 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
|
---|
[6f49cdf] | 2057 | scopeTyVars.beginScope();
|
---|
[05d47278] | 2058 | makeTyVarMap( type, scopeTyVars );
|
---|
| 2059 |
|
---|
| 2060 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
[f18a711] | 2061 | ScrubTyVars::scrub( decl, scopeTyVars );
|
---|
[05d47278] | 2062 |
|
---|
[6f49cdf] | 2063 | scopeTyVars.endScope();
|
---|
[05d47278] | 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 ) {
|
---|
[6f49cdf] | 2093 | scopeTyVars.beginScope();
|
---|
[05d47278] | 2094 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
| 2095 |
|
---|
| 2096 | Type *ret = Mutator::mutate( pointerType );
|
---|
| 2097 |
|
---|
[6f49cdf] | 2098 | scopeTyVars.endScope();
|
---|
[05d47278] | 2099 | return ret;
|
---|
| 2100 | }
|
---|
| 2101 |
|
---|
| 2102 | Type * Pass3::mutate( FunctionType *functionType ) {
|
---|
[6f49cdf] | 2103 | scopeTyVars.beginScope();
|
---|
[05d47278] | 2104 | makeTyVarMap( functionType, scopeTyVars );
|
---|
| 2105 |
|
---|
| 2106 | Type *ret = Mutator::mutate( functionType );
|
---|
| 2107 |
|
---|
[6f49cdf] | 2108 | scopeTyVars.endScope();
|
---|
[05d47278] | 2109 | return ret;
|
---|
| 2110 | }
|
---|
[01aeade] | 2111 | } // anonymous namespace
|
---|
[51b73452] | 2112 | } // namespace GenPoly
|
---|
[01aeade] | 2113 |
|
---|
[51587aa] | 2114 | // Local Variables: //
|
---|
| 2115 | // tab-width: 4 //
|
---|
| 2116 | // mode: c++ //
|
---|
| 2117 | // compile-command: "make install" //
|
---|
| 2118 | // End: //
|
---|