Changeset 3da470c
- Timestamp:
- Apr 8, 2016, 5:23:19 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 9827c7ba
- Parents:
- 630a82a (diff), 224e52f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src
- Files:
-
- 1 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r630a82a r3da470c 455 455 456 456 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) { 457 assert( false );457 assert( false && "UntypedOffsetofExpr should not reach code generation" ); 458 458 } 459 459 … … 464 464 output << ", " << mangleName( offsetofExpr->get_member() ); 465 465 output << ")"; 466 } 467 468 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) { 469 assert( false && "OffsetPackExpr should not reach code generation" ); 466 470 } 467 471 -
src/CodeGen/CodeGenerator.h
r630a82a r3da470c 65 65 virtual void visit( UntypedOffsetofExpr *offsetofExpr ); 66 66 virtual void visit( OffsetofExpr *offsetofExpr ); 67 virtual void visit( OffsetPackExpr *offsetPackExpr ); 67 68 virtual void visit( LogicalExpr *logicalExpr ); 68 69 virtual void visit( ConditionalExpr *conditionalExpr ); -
src/GenPoly/Box.cc
r630a82a r3da470c 30 30 #include "FindFunction.h" 31 31 #include "ScopedMap.h" 32 #include "ScopedSet.h" 32 33 #include "ScrubTyVars.h" 33 34 … … 62 63 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); 63 64 64 /// Key for a unique concrete type; generic base type paired with type parameter list 65 struct ConcreteType { 66 ConcreteType() : base(NULL), params() {} 67 68 ConcreteType(AggregateDecl *_base, const std::list< Type* >& _params) : base(_base), params() { cloneAll(_params, params); } 69 70 ConcreteType(const ConcreteType& that) : base(that.base), params() { cloneAll(that.params, params); } 65 /// Abstracts type equality for a list of parameter types 66 struct TypeList { 67 TypeList() : params() {} 68 TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); } 69 TypeList( std::list< Type* > &&_params ) : params( _params ) {} 70 71 TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); } 72 TypeList( TypeList &&that ) : params( std::move( that.params ) ) {} 71 73 72 74 /// Extracts types from a list of TypeExpr* 73 ConcreteType(AggregateDecl *_base, const std::list< TypeExpr* >& _params) : base(_base),params() {75 TypeList( const std::list< TypeExpr* >& _params ) : params() { 74 76 for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) { 75 77 params.push_back( (*param)->get_type()->clone() ); … … 77 79 } 78 80 79 ConcreteType& operator= (const ConcreteType& that) {81 TypeList& operator= ( const TypeList &that ) { 80 82 deleteAll( params ); 83 81 84 params.clear(); 82 83 base = that.base;84 85 cloneAll( that.params, params ); 85 86 … … 87 88 } 88 89 89 ~ConcreteType() { deleteAll( params ); } 90 91 bool operator== (const ConcreteType& that) const { 92 if ( base != that.base ) return false; 90 TypeList& operator= ( TypeList &&that ) { 91 deleteAll( params ); 92 93 params = std::move( that.params ); 94 95 return *this; 96 } 97 98 ~TypeList() { deleteAll( params ); } 99 100 bool operator== ( const TypeList& that ) const { 101 if ( params.size() != that.params.size() ) return false; 93 102 94 103 SymTab::Indexer dummy; 95 if ( params.size() != that.params.size() ) return false;96 104 for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) { 97 105 if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false; … … 100 108 } 101 109 102 AggregateDecl *base; ///< Base generic type103 110 std::list< Type* > params; ///< Instantiation parameters 104 111 }; 105 112 106 /// Maps a concrete typeto the some value, accounting for scope107 template< typename Value >113 /// Maps a key and a TypeList to the some value, accounting for scope 114 template< typename Key, typename Value > 108 115 class InstantiationMap { 109 /// Information about a specific instantiation of a generic type 110 struct Instantiation { 111 ConcreteType key; ///< Instantiation parameters for this type 112 Value *value; ///< Value for this instantiation 113 114 Instantiation() : key(), value(0) {} 115 Instantiation(const ConcreteType &_key, Value *_value) : key(_key), value(_value) {} 116 }; 117 /// Map of generic types to instantiations of them 118 typedef std::map< AggregateDecl*, std::vector< Instantiation > > Scope; 119 120 std::vector< Scope > scopes; ///< list of scopes, from outermost to innermost 116 /// Wraps value for a specific (Key, TypeList) combination 117 typedef std::pair< TypeList, Value* > Instantiation; 118 /// List of TypeLists paired with their appropriate values 119 typedef std::vector< Instantiation > ValueList; 120 /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values 121 typedef ScopedMap< Key*, ValueList > InnerMap; 122 123 InnerMap instantiations; ///< instantiations 121 124 122 125 public: 123 126 /// Starts a new scope 124 void beginScope() { 125 Scope scope; 126 scopes.push_back(scope); 127 } 127 void beginScope() { instantiations.beginScope(); } 128 128 129 129 /// Ends a scope 130 void endScope() { 131 scopes.pop_back(); 132 } 133 134 /// Default constructor initializes with one scope 135 InstantiationMap() { beginScope(); } 136 137 // private: 138 /// Gets the value for the concrete instantiation of this type, assuming it has already been instantiated in the current scope. 139 /// Returns NULL on none such. 140 Value *lookup( AggregateDecl *generic, const std::list< TypeExpr* >& params ) { 141 ConcreteType key(generic, params); 142 // scan scopes from innermost out 143 for ( typename std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) { 144 // skip scope if no instantiations of this generic type 145 typename Scope::const_iterator insts = scope->find( generic ); 146 if ( insts == scope->end() ) continue; 147 // look through instantiations for matches to concrete type 148 for ( typename std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) { 149 if ( inst->key == key ) return inst->value; 130 void endScope() { instantiations.endScope(); } 131 132 /// Gets the value for the (key, typeList) pair, returns NULL on none such. 133 Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const { 134 TypeList typeList( params ); 135 136 // scan scopes for matches to the key 137 for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) { 138 for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) { 139 if ( inst->first == typeList ) return inst->second; 150 140 } 151 141 } 152 // no matching instantiation found142 // no matching instantiations found 153 143 return 0; 154 144 } 155 public: 156 // StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)lookup( inst->get_baseStruct(), typeSubs ); } 157 // UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)lookup( inst->get_baseUnion(), typeSubs ); } 158 159 // private: 160 /// Adds a value for a concrete type to the current scope 161 void insert( AggregateDecl *generic, const std::list< TypeExpr* > ¶ms, Value *value ) { 162 ConcreteType key(generic, params); 163 scopes.back()[generic].push_back( Instantiation( key, value ) ); 164 } 165 // public: 166 // void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { insert( inst->get_baseStruct(), typeSubs, decl ); } 167 // void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { insert( inst->get_baseUnion(), typeSubs, decl ); } 145 146 /// Adds a value for a (key, typeList) pair to the current scope 147 void insert( Key *key, const std::list< TypeExpr* > ¶ms, Value *value ) { 148 instantiations[ key ].push_back( Instantiation( TypeList( params ), value ) ); 149 } 168 150 }; 169 151 … … 197 179 virtual void doEndScope(); 198 180 private: 199 /// Makes a new temporary array holding the offsets of the fields of `type`, and returns a new variable expression referencing it200 Expression *makeOffsetArray( StructInstType *type );201 181 /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application 202 182 void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ); … … 225 205 ObjectDecl *makeTemporary( Type *type ); 226 206 227 std::map< std::string, DeclarationWithType *> assignOps; 228 ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; 229 ScopedMap< std::string, DeclarationWithType* > adapters; 207 std::map< std::string, DeclarationWithType *> assignOps; ///< Currently known type variable assignment operators 208 ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; ///< Currently known assignment operators 209 ScopedMap< std::string, DeclarationWithType* > adapters; ///< Set of adapter functions in the current scope 210 230 211 DeclarationWithType *retval; 231 212 bool useRetval; … … 233 214 }; 234 215 235 /// Moves polymorphic returns in function types to pointer-type parameters, adds type size and assertion parameters to parameter lists as well 216 /// * Moves polymorphic returns in function types to pointer-type parameters 217 /// * adds type size and assertion parameters to parameter lists 236 218 class Pass2 : public PolyMutator { 237 219 public: … … 244 226 virtual Type *mutate( PointerType *pointerType ); 245 227 virtual Type *mutate( FunctionType *funcType ); 228 246 229 private: 247 230 void addAdapters( FunctionType *functionType ); … … 253 236 class GenericInstantiator : public DeclMutator { 254 237 /// Map of (generic type, parameter list) pairs to concrete type instantiations 255 InstantiationMap< AggregateDecl > instantiations;238 InstantiationMap< AggregateDecl, AggregateDecl > instantiations; 256 239 /// Namer for concrete types 257 240 UniqueName typeNamer; … … 278 261 }; 279 262 280 /// Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference; 281 /// also fixes offsetof expressions. 282 class MemberExprFixer : public PolyMutator { 283 public: 263 /// Replaces member and size/align/offsetof expressions on polymorphic generic types with calculated expressions. 264 /// * Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference 265 /// * Calculates polymorphic offsetof expressions from offset array 266 /// * Inserts dynamic calculation of polymorphic type layouts where needed 267 class PolyGenericCalculator : public PolyMutator { 268 public: 284 269 template< typename DeclClass > 285 270 DeclClass *handleDecl( DeclClass *decl, Type *type ); … … 292 277 virtual Type *mutate( FunctionType *funcType ); 293 278 virtual Expression *mutate( MemberExpr *memberExpr ); 279 virtual Expression *mutate( SizeofExpr *sizeofExpr ); 280 virtual Expression *mutate( AlignofExpr *alignofExpr ); 294 281 virtual Expression *mutate( OffsetofExpr *offsetofExpr ); 282 virtual Expression *mutate( OffsetPackExpr *offsetPackExpr ); 283 284 virtual void doBeginScope(); 285 virtual void doEndScope(); 286 287 private: 288 /// Makes a new variable in the current scope with the given name, type & optional initializer 289 ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 ); 290 /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns 291 bool findGeneric( Type *ty ); 292 /// adds type parameters to the layout call; will generate the appropriate parameters if needed 293 void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); 294 295 ScopedSet< std::string > knownLayouts; ///< Set of generic type layouts known in the current scope, indexed by sizeofName 296 ScopedSet< std::string > knownOffsets; ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName 295 297 }; 296 298 … … 342 344 Pass2 pass2; 343 345 GenericInstantiator instantiator; 344 MemberExprFixer memberFixer;346 PolyGenericCalculator polyCalculator; 345 347 Pass3 pass3; 346 348 … … 348 350 mutateTranslationUnit/*All*/( translationUnit, pass1 ); 349 351 mutateTranslationUnit/*All*/( translationUnit, pass2 ); 350 // instantiateGeneric( translationUnit );351 352 instantiator.mutateDeclarationList( translationUnit ); 352 mutateTranslationUnit/*All*/( translationUnit, memberFixer );353 mutateTranslationUnit/*All*/( translationUnit, polyCalculator ); 353 354 mutateTranslationUnit/*All*/( translationUnit, pass3 ); 354 355 } … … 653 654 654 655 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { 655 // if this is a polymorphicassignment function, put it in the map for this scope656 // if this is a assignment function, put it in the map for this scope 656 657 if ( Type *assignedType = isAssignment( functionDecl ) ) { 657 658 if ( ! dynamic_cast< TypeInstType* >( assignedType ) ) { … … 743 744 } 744 745 745 Expression *Pass1::makeOffsetArray( StructInstType *ty ) {746 std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members();747 748 // make a new temporary array749 Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );750 std::stringstream lenGen;751 lenGen << baseMembers.size();752 ConstantExpr *lenExpr = new ConstantExpr( Constant( offsetType->clone(), lenGen.str() ) );753 ObjectDecl *arrayTemp = makeTemporary( new ArrayType( Type::Qualifiers(), offsetType, lenExpr, false, false ) );754 755 // build initializer list for temporary756 std::list< Initializer* > inits;757 for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) {758 DeclarationWithType *memberDecl;759 if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) {760 memberDecl = origMember->clone();761 } else {762 memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 );763 }764 inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) );765 }766 arrayTemp->set_init( new ListInit( inits ) );767 768 // return variable pointing to temporary769 return new VariableExpr( arrayTemp );770 }771 772 746 void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { 773 747 Type *polyBase = hasPolyBase( parmType, exprTyVars ); … … 782 756 if ( dynamic_cast< StructInstType* >( polyBase ) ) { 783 757 if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { 784 arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) ); 785 arg++; 758 // zero-length arrays are forbidden by C, so don't pass offset for empty struct 759 if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) { 760 arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) ); 761 arg++; 762 } 786 763 } else { 787 764 throw SemanticError( "Cannot pass non-struct type for generic struct" ); … … 931 908 return; 932 909 } else if ( arg->get_results().front()->get_isLvalue() ) { 933 // VariableExpr and MemberExpr are lvalues 934 arg = new AddressExpr( arg ); 910 // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue) 911 if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) { 912 commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) ); 913 } else { 914 arg = new AddressExpr( arg ); 915 } 935 916 } else { 936 917 // use type computed in unification to declare boxed variables … … 1027 1008 } // for 1028 1009 } 1029 1030 1031 1010 1032 1011 FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { … … 1428 1407 std::list< TypeDecl* >::const_iterator forallIt = forallParams.begin(); 1429 1408 for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) { 1430 if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; // skip types with no assign op (ftype/dtype) 1431 1432 std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); 1433 assert( ! asserts.empty() && "Type param needs assignment operator assertion" ); 1434 DeclarationWithType *actualDecl = asserts.front(); 1435 TypeInstType *actualType = isTypeInstAssignment( actualDecl ); 1436 assert( actualType && "First assertion of type with assertions should be assignment operator" ); 1409 // Add appropriate mapping to assignment expression environment 1437 1410 TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt ); 1438 1411 assert( formalTypeExpr && "type parameters must be type expressions" ); 1439 1412 Type *formalType = formalTypeExpr->get_type(); 1440 assignExpr->get_env()->add( actualType->get_name(), formalType ); 1441 1413 assignExpr->get_env()->add( (*forallIt)->get_name(), formalType ); 1414 1415 // skip types with no assign op (ftype/dtype) 1416 if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; 1417 1418 // find assignment operator for formal type 1442 1419 DeclarationWithType *assertAssign = 0; 1443 1420 if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { … … 1453 1430 } 1454 1431 } 1455 1456 1432 1433 // add inferred parameter for field assignment operator to assignment expression 1434 std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); 1435 assert( ! asserts.empty() && "Type param needs assignment operator assertion" ); 1436 DeclarationWithType *actualDecl = asserts.front(); 1457 1437 assignExpr->get_inferParams()[ actualDecl->get_uniqueId() ] 1458 1438 = ParamEntry( assertAssign->get_uniqueId(), assertAssign->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertAssign ) ); … … 1587 1567 ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, 1588 1568 new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 ); 1589 // ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 );1590 1569 for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { 1591 1570 ObjectDecl *sizeParm, *alignParm; … … 1631 1610 ++last; 1632 1611 1633 if ( dynamic_cast< StructInstType* >( polyBase ) ) { 1634 offsetParm = newPtr.clone(); 1635 offsetParm->set_name( offsetofName( polyBase ) ); 1636 last = funcType->get_parameters().insert( last, offsetParm ); 1637 ++last; 1612 if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyBase ) ) { 1613 // NOTE zero-length arrays are illegal in C, so empty structs have no offset array 1614 if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { 1615 offsetParm = newPtr.clone(); 1616 offsetParm->set_name( offsetofName( polyBase ) ); 1617 last = funcType->get_parameters().insert( last, offsetParm ); 1618 ++last; 1619 } 1638 1620 } 1639 1621 … … 1844 1826 1845 1827 template< typename DeclClass > 1846 DeclClass * MemberExprFixer::handleDecl( DeclClass *decl, Type *type ) {1828 DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { 1847 1829 TyVarMap oldtyVars = scopeTyVars; 1848 1830 makeTyVarMap( type, scopeTyVars ); … … 1854 1836 } 1855 1837 1856 ObjectDecl * MemberExprFixer::mutate( ObjectDecl *objectDecl ) {1838 ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) { 1857 1839 return handleDecl( objectDecl, objectDecl->get_type() ); 1858 1840 } 1859 1841 1860 DeclarationWithType * MemberExprFixer::mutate( FunctionDecl *functionDecl ) {1842 DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) { 1861 1843 return handleDecl( functionDecl, functionDecl->get_functionType() ); 1862 1844 } 1863 1845 1864 TypedefDecl * MemberExprFixer::mutate( TypedefDecl *typedefDecl ) {1846 TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) { 1865 1847 return handleDecl( typedefDecl, typedefDecl->get_base() ); 1866 1848 } 1867 1849 1868 TypeDecl * MemberExprFixer::mutate( TypeDecl *typeDecl ) {1850 TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) { 1869 1851 scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind(); 1870 1852 return Mutator::mutate( typeDecl ); 1871 1853 } 1872 1854 1873 Type * MemberExprFixer::mutate( PointerType *pointerType ) {1855 Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { 1874 1856 TyVarMap oldtyVars = scopeTyVars; 1875 1857 makeTyVarMap( pointerType, scopeTyVars ); … … 1881 1863 } 1882 1864 1883 Type * MemberExprFixer::mutate( FunctionType *functionType ) {1865 Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { 1884 1866 TyVarMap oldtyVars = scopeTyVars; 1885 makeTyVarMap( functionType, scopeTyVars ); 1886 1887 Type *ret = Mutator::mutate( functionType ); 1867 makeTyVarMap( funcType, scopeTyVars ); 1868 1869 // make sure that any type information passed into the function is accounted for 1870 for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) { 1871 // condition here duplicates that in Pass2::mutate( FunctionType* ) 1872 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars ); 1873 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 1874 knownLayouts.insert( sizeofName( polyBase ) ); 1875 } 1876 } 1877 1878 Type *ret = Mutator::mutate( funcType ); 1888 1879 1889 1880 scopeTyVars = oldtyVars; … … 1891 1882 } 1892 1883 1893 Statement * MemberExprFixer::mutate( DeclStmt *declStmt ) {1884 Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) { 1894 1885 if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { 1895 if ( isPolyType( objectDecl->get_type(), scopeTyVars) ) {1886 if ( findGeneric( objectDecl->get_type() ) ) { 1896 1887 // change initialization of a polymorphic value object 1897 1888 // to allocate storage with alloca … … 1945 1936 } 1946 1937 1947 Expression * MemberExprFixer::mutate( MemberExpr *memberExpr ) {1938 Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) { 1948 1939 // mutate, exiting early if no longer MemberExpr 1949 1940 Expression *expr = Mutator::mutate( memberExpr ); … … 1962 1953 Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth ); 1963 1954 if ( ! objectType ) return memberExpr; 1955 findGeneric( objectType ); // ensure layout for this type is available 1964 1956 1965 1957 Expression *newMemberExpr = 0; … … 1993 1985 } 1994 1986 1995 Expression *MemberExprFixer::mutate( OffsetofExpr *offsetofExpr ) { 1987 ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) { 1988 ObjectDecl *newObj = new ObjectDecl( name, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, init ); 1989 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); 1990 return newObj; 1991 } 1992 1993 void PolyGenericCalculator::addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ) { 1994 for ( std::list< Type* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { 1995 if ( findGeneric( *param ) ) { 1996 // push size/align vars for a generic parameter back 1997 layoutCall->get_args().push_back( new NameExpr( sizeofName( *param ) ) ); 1998 layoutCall->get_args().push_back( new NameExpr( alignofName( *param ) ) ); 1999 } else { 2000 layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); 2001 layoutCall->get_args().push_back( new AlignofExpr( (*param)->clone() ) ); 2002 } 2003 } 2004 } 2005 2006 /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list 2007 bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) { 2008 bool hasDynamicLayout = false; 2009 2010 std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); 2011 std::list< Expression* >::const_iterator typeParam = typeParams.begin(); 2012 for ( ; baseParam != baseParams.end() && typeParam != typeParams.end(); ++baseParam, ++typeParam ) { 2013 // skip non-otype parameters 2014 if ( (*baseParam)->get_kind() != TypeDecl::Any ) continue; 2015 TypeExpr *typeExpr = dynamic_cast< TypeExpr* >( *typeParam ); 2016 assert( typeExpr && "all otype parameters should be type expressions" ); 2017 2018 Type *type = typeExpr->get_type(); 2019 out.push_back( type ); 2020 if ( isPolyType( type ) ) hasDynamicLayout = true; 2021 } 2022 assert( baseParam == baseParams.end() && typeParam == typeParams.end() ); 2023 2024 return hasDynamicLayout; 2025 } 2026 2027 bool PolyGenericCalculator::findGeneric( Type *ty ) { 2028 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) { 2029 // duplicate logic from isPolyType() 2030 if ( env ) { 2031 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 2032 return findGeneric( newType ); 2033 } // if 2034 } // if 2035 if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) { 2036 // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set 2037 return true; 2038 } 2039 return false; 2040 } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { 2041 // check if this type already has a layout generated for it 2042 std::string sizeName = sizeofName( ty ); 2043 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true; 2044 2045 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized 2046 std::list< Type* > otypeParams; 2047 if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false; 2048 2049 // insert local variables for layout and generate call to layout function 2050 knownLayouts.insert( sizeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2051 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2052 2053 int n_members = structTy->get_baseStruct()->get_members().size(); 2054 if ( n_members == 0 ) { 2055 // all empty structs have the same layout - size 1, align 1 2056 makeVar( sizeName, layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); 2057 makeVar( alignofName( ty ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); 2058 // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array 2059 } else { 2060 ObjectDecl *sizeVar = makeVar( sizeName, layoutType ); 2061 ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() ); 2062 ObjectDecl *offsetVar = makeVar( offsetofName( ty ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) ); 2063 2064 // generate call to layout function 2065 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + structTy->get_baseStruct()->get_name() ) ); 2066 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2067 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); 2068 layoutCall->get_args().push_back( new VariableExpr( offsetVar ) ); 2069 addOtypeParamsToLayoutCall( layoutCall, otypeParams ); 2070 2071 stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); 2072 } 2073 2074 return true; 2075 } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { 2076 // check if this type already has a layout generated for it 2077 std::string sizeName = sizeofName( ty ); 2078 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true; 2079 2080 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized 2081 std::list< Type* > otypeParams; 2082 if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false; 2083 2084 // insert local variables for layout and generate call to layout function 2085 knownLayouts.insert( sizeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2086 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2087 2088 ObjectDecl *sizeVar = makeVar( sizeName, layoutType ); 2089 ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() ); 2090 2091 // generate call to layout function 2092 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + unionTy->get_baseUnion()->get_name() ) ); 2093 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2094 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); 2095 addOtypeParamsToLayoutCall( layoutCall, otypeParams ); 2096 2097 stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); 2098 2099 return true; 2100 } 2101 2102 return false; 2103 } 2104 2105 Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) { 2106 Type *ty = sizeofExpr->get_type(); 2107 if ( findGeneric( ty ) ) { 2108 Expression *ret = new NameExpr( sizeofName( ty ) ); 2109 delete sizeofExpr; 2110 return ret; 2111 } 2112 return sizeofExpr; 2113 } 2114 2115 Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) { 2116 Type *ty = alignofExpr->get_type(); 2117 if ( findGeneric( ty ) ) { 2118 Expression *ret = new NameExpr( alignofName( ty ) ); 2119 delete alignofExpr; 2120 return ret; 2121 } 2122 return alignofExpr; 2123 } 2124 2125 Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) { 1996 2126 // mutate, exiting early if no longer OffsetofExpr 1997 2127 Expression *expr = Mutator::mutate( offsetofExpr ); … … 2000 2130 2001 2131 // only mutate expressions for polymorphic structs/unions 2002 Type *ty = isPolyType( offsetofExpr->get_type(), scopeTyVars);2003 if ( ! ty) return offsetofExpr;2004 2132 Type *ty = offsetofExpr->get_type(); 2133 if ( ! findGeneric( ty ) ) return offsetofExpr; 2134 2005 2135 if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) { 2006 2136 // replace offsetof expression by index into offset array … … 2018 2148 } 2019 2149 2150 Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) { 2151 StructInstType *ty = offsetPackExpr->get_type(); 2152 2153 Expression *ret = 0; 2154 if ( findGeneric( ty ) ) { 2155 // pull offset back from generated type information 2156 ret = new NameExpr( offsetofName( ty ) ); 2157 } else { 2158 std::string offsetName = offsetofName( ty ); 2159 if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { 2160 // use the already-generated offsets for this type 2161 ret = new NameExpr( offsetName ); 2162 } else { 2163 knownOffsets.insert( offsetName ); 2164 2165 std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members(); 2166 Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2167 2168 // build initializer list for offset array 2169 std::list< Initializer* > inits; 2170 for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) { 2171 DeclarationWithType *memberDecl; 2172 if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) { 2173 memberDecl = origMember->clone(); 2174 } else { 2175 memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 ); 2176 } 2177 inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) ); 2178 } 2179 2180 // build the offset array and replace the pack with a reference to it 2181 ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from( baseMembers.size() ) ), false, false ), 2182 new ListInit( inits ) ); 2183 ret = new VariableExpr( offsetArray ); 2184 } 2185 } 2186 2187 delete offsetPackExpr; 2188 return ret; 2189 } 2190 2191 void PolyGenericCalculator::doBeginScope() { 2192 knownLayouts.beginScope(); 2193 knownOffsets.beginScope(); 2194 } 2195 2196 void PolyGenericCalculator::doEndScope() { 2197 knownLayouts.endScope(); 2198 knownOffsets.beginScope(); 2199 } 2200 2020 2201 ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// 2021 2202 -
src/GenPoly/ScopedMap.h
r630a82a r3da470c 17 17 #define _SCOPEDMAP_H 18 18 19 #include <cassert> 19 20 #include <iterator> 20 21 #include <map> … … 164 165 void endScope() { 165 166 scopes.pop_back(); 167 assert( ! scopes.empty() ); 166 168 } 167 169 … … 188 190 return end(); 189 191 } 190 const_iterator find( const Key &key ) const { return const_iterator( find( key ) ); } 192 const_iterator find( const Key &key ) const { 193 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) ); 194 } 191 195 192 196 /// Finds the given key in the outermost scope inside the given scope where it occurs … … 200 204 return end(); 201 205 } 202 const_iterator findNext( const_iterator &it, const Key &key ) const { return const_iterator( findNext( it, key ) ); } 206 const_iterator findNext( const_iterator &it, const Key &key ) const { 207 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) ); 208 } 203 209 204 210 /// Inserts the given key-value pair into the outermost scope … … 208 214 } 209 215 std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); } 210 216 217 Value& operator[] ( const Key &key ) { 218 iterator slot = find( key ); 219 if ( slot != end() ) return slot->second; 220 return insert( key, Value() ).first->second; 221 } 211 222 }; 212 223 } // namespace GenPoly -
src/InitTweak/InitModel.h
r630a82a r3da470c 75 75 void visit( UntypedOffsetofExpr * ) { throw 0; } 76 76 void visit( OffsetofExpr * ) { throw 0; } 77 void visit( OffsetPackExpr * ) { throw 0; } 77 78 void visit( AttrExpr * ) { throw 0; } 78 79 void visit( LogicalExpr * ) { throw 0; } -
src/ResolvExpr/AlternativeFinder.cc
r630a82a r3da470c 848 848 } 849 849 850 void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) { 851 alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) ); 852 } 853 850 854 void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) { 851 855 // assume no polymorphism -
src/ResolvExpr/AlternativeFinder.h
r630a82a r3da470c 59 59 virtual void visit( UntypedOffsetofExpr *offsetofExpr ); 60 60 virtual void visit( OffsetofExpr *offsetofExpr ); 61 virtual void visit( OffsetPackExpr *offsetPackExpr ); 61 62 virtual void visit( AttrExpr *attrExpr ); 62 63 virtual void visit( LogicalExpr *logicalExpr ); -
src/SymTab/Indexer.cc
r630a82a r3da470c 344 344 maybeAccept( offsetofExpr->get_type(), *this ); 345 345 maybeAccept( offsetofExpr->get_member(), *this ); 346 } 347 348 void Indexer::visit( OffsetPackExpr *offsetPackExpr ) { 349 acceptAllNewScope( offsetPackExpr->get_results(), *this ); 350 maybeAccept( offsetPackExpr->get_type(), *this ); 346 351 } 347 352 -
src/SymTab/Indexer.h
r630a82a r3da470c 59 59 virtual void visit( UntypedOffsetofExpr *offsetofExpr ); 60 60 virtual void visit( OffsetofExpr *offsetofExpr ); 61 virtual void visit( OffsetPackExpr *offsetPackExpr ); 61 62 virtual void visit( AttrExpr *attrExpr ); 62 63 virtual void visit( LogicalExpr *logicalExpr ); -
src/SynTree/Expression.cc
r630a82a r3da470c 223 223 } 224 224 225 OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) { 226 add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) ); 227 } 228 229 OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {} 230 231 OffsetPackExpr::~OffsetPackExpr() { delete type; } 232 233 void OffsetPackExpr::print( std::ostream &os, int indent ) const { 234 os << std::string( indent, ' ' ) << "Offset pack expression on "; 235 236 if ( type ) { 237 type->print(os, indent + 2); 238 } else { 239 os << "<NULL>"; 240 } 241 242 os << std::endl; 243 Expression::print( os, indent ); 244 } 245 225 246 AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : 226 247 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { -
src/SynTree/Expression.h
r630a82a r3da470c 362 362 }; 363 363 364 /// Expression representing a pack of field-offsets for a generic type 365 class OffsetPackExpr : public Expression { 366 public: 367 OffsetPackExpr( StructInstType *type_, Expression *aname_ = 0 ); 368 OffsetPackExpr( const OffsetPackExpr &other ); 369 virtual ~OffsetPackExpr(); 370 371 StructInstType *get_type() const { return type; } 372 void set_type( StructInstType *newValue ) { type = newValue; } 373 374 virtual OffsetPackExpr *clone() const { return new OffsetPackExpr( *this ); } 375 virtual void accept( Visitor &v ) { v.visit( this ); } 376 virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } 377 378 virtual void print( std::ostream &os, int indent = 0 ) const; 379 380 private: 381 StructInstType *type; 382 }; 383 364 384 /// AttrExpr represents an @attribute expression (like sizeof, but user-defined) 365 385 class AttrExpr : public Expression { -
src/SynTree/Mutator.cc
r630a82a r3da470c 274 274 } 275 275 276 Expression *Mutator::mutate( OffsetPackExpr *offsetPackExpr ) { 277 mutateAll( offsetPackExpr->get_results(), *this ); 278 offsetPackExpr->set_type( maybeMutate( offsetPackExpr->get_type(), *this ) ); 279 return offsetPackExpr; 280 } 281 276 282 Expression *Mutator::mutate( AttrExpr *attrExpr ) { 277 283 mutateAll( attrExpr->get_results(), *this ); -
src/SynTree/Mutator.h
r630a82a r3da470c 67 67 virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ); 68 68 virtual Expression* mutate( OffsetofExpr *offsetofExpr ); 69 virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ); 69 70 virtual Expression* mutate( AttrExpr *attrExpr ); 70 71 virtual Expression* mutate( LogicalExpr *logicalExpr ); -
src/SynTree/SynTree.h
r630a82a r3da470c 72 72 class UntypedOffsetofExpr; 73 73 class OffsetofExpr; 74 class OffsetPackExpr; 74 75 class AttrExpr; 75 76 class LogicalExpr; -
src/SynTree/Visitor.cc
r630a82a r3da470c 230 230 } 231 231 232 void Visitor::visit( OffsetPackExpr *offsetPackExpr ) { 233 acceptAll( offsetPackExpr->get_results(), *this ); 234 maybeAccept( offsetPackExpr->get_type(), *this ); 235 } 236 232 237 void Visitor::visit( AttrExpr *attrExpr ) { 233 238 acceptAll( attrExpr->get_results(), *this ); -
src/SynTree/Visitor.h
r630a82a r3da470c 67 67 virtual void visit( UntypedOffsetofExpr *offsetofExpr ); 68 68 virtual void visit( OffsetofExpr *offsetofExpr ); 69 virtual void visit( OffsetPackExpr *offsetPackExpr ); 69 70 virtual void visit( AttrExpr *attrExpr ); 70 71 virtual void visit( LogicalExpr *logicalExpr ); -
src/Tuples/FlattenTuple.cc
r630a82a r3da470c 49 49 void FlattenTuple::CollectArgs::visit( UntypedOffsetofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 50 50 void FlattenTuple::CollectArgs::visit( OffsetofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 51 void FlattenTuple::CollectArgs::visit( OffsetPackExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 51 52 void FlattenTuple::CollectArgs::visit( AttrExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 52 53 void FlattenTuple::CollectArgs::visit( LogicalExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } -
src/Tuples/FlattenTuple.h
r630a82a r3da470c 45 45 virtual void visit( UntypedOffsetofExpr * ); 46 46 virtual void visit( OffsetofExpr * ); 47 virtual void visit( OffsetPackExpr * ); 47 48 virtual void visit( AttrExpr * ); 48 49 virtual void visit( LogicalExpr * );
Note: See TracChangeset
for help on using the changeset viewer.