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