Changes in src/GenPoly/Box.cc [aa19ccf:8a34677]
- File:
-
- 1 edited
-
src/GenPoly/Box.cc (modified) (44 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
raa19ccf r8a34677 205 205 ObjectDecl *makeTemporary( Type *type ); 206 206 207 ScopedMap< std::string, DeclarationWithType *> assignOps;///< Currently known type variable assignment operators207 std::map< std::string, DeclarationWithType *> assignOps; ///< Currently known type variable assignment operators 208 208 ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps; ///< Currently known assignment operators 209 209 ScopedMap< std::string, DeclarationWithType* > adapters; ///< Set of adapter functions in the current scope … … 292 292 /// adds type parameters to the layout call; will generate the appropriate parameters if needed 293 293 void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); 294 295 /// Enters a new scope for type-variables, adding the type variables from ty296 void beginTypeScope( Type *ty );297 /// Exits the type-variable scope298 void endTypeScope();299 294 300 295 ScopedSet< std::string > knownLayouts; ///< Set of generic type layouts known in the current scope, indexed by sizeofName … … 390 385 for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { 391 386 TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param ); 392 std::string paramName = mangleType( ¶mType ); 393 layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 394 layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 387 layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( ¶mType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 388 layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( ¶mType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 395 389 } 396 390 } 397 391 398 392 /// Builds a layout function declaration 399 FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {393 FunctionDecl *buildLayoutFunctionDecl( const std::string &typeName, unsigned int functionNesting, FunctionType *layoutFnType ) { 400 394 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units 401 395 // because each unit generates copies of the default routines for each aggregate. 402 396 FunctionDecl *layoutDecl = new FunctionDecl( 403 layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );397 "__layoutof_" + typeName, functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false ); 404 398 layoutDecl->fixUniqueId(); 405 399 return layoutDecl; … … 468 462 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); 469 463 470 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name()), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );464 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); 471 465 layoutFnType->get_parameters().push_back( sizeParam ); 472 ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name()), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );466 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 473 467 layoutFnType->get_parameters().push_back( alignParam ); 474 ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name()), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );468 ObjectDecl *offsetParam = new ObjectDecl( "__offsetof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 475 469 layoutFnType->get_parameters().push_back( offsetParam ); 476 470 addOtypeParams( layoutFnType, otypeParams ); 477 471 478 472 // build function decl 479 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl , functionNesting, layoutFnType );473 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl->get_name(), functionNesting, layoutFnType ); 480 474 481 475 // calculate struct layout in function body … … 529 523 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); 530 524 531 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name()), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );525 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); 532 526 layoutFnType->get_parameters().push_back( sizeParam ); 533 ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name()), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );527 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 534 528 layoutFnType->get_parameters().push_back( alignParam ); 535 529 addOtypeParams( layoutFnType, otypeParams ); 536 530 537 531 // build function decl 538 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl , functionNesting, layoutFnType );532 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl->get_name(), functionNesting, layoutFnType ); 539 533 540 534 // calculate union layout in function body … … 669 663 if ( functionDecl->get_statements() ) { // empty routine body ? 670 664 doBeginScope(); 671 scopeTyVars.beginScope();672 assignOps.beginScope();665 TyVarMap oldtyVars = scopeTyVars; 666 std::map< std::string, DeclarationWithType *> oldassignOps = assignOps; 673 667 DeclarationWithType *oldRetval = retval; 674 668 bool oldUseRetval = useRetval; … … 711 705 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); 712 706 713 scopeTyVars.endScope(); 714 assignOps.endScope(); 707 scopeTyVars = oldtyVars; 708 assignOps = oldassignOps; 709 // std::cerr << "end FunctionDecl: "; 710 // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { 711 // std::cerr << i->first << " "; 712 // } 713 // std::cerr << "\n"; 715 714 retval = oldRetval; 716 715 useRetval = oldUseRetval; … … 746 745 747 746 void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { 748 Type *poly Type = isPolyType( parmType, exprTyVars );749 if ( poly Type && ! dynamic_cast< TypeInstType* >( polyType ) ) {750 std::string typeName = mangleType( polyType );751 if ( seenTypes.count( typeName ) ) return;747 Type *polyBase = hasPolyBase( parmType, exprTyVars ); 748 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 749 std::string sizeName = sizeofName( polyBase ); 750 if ( seenTypes.count( sizeName ) ) return; 752 751 753 752 arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); … … 755 754 arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); 756 755 arg++; 757 if ( dynamic_cast< StructInstType* >( poly Type ) ) {756 if ( dynamic_cast< StructInstType* >( polyBase ) ) { 758 757 if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { 759 758 // zero-length arrays are forbidden by C, so don't pass offset for empty struct … … 767 766 } 768 767 769 seenTypes.insert( typeName );768 seenTypes.insert( sizeName ); 770 769 } 771 770 } … … 1125 1124 addAssign->get_args().push_back( appExpr->get_args().front() ); 1126 1125 } // if 1127 addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType )) ) );1126 addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) ); 1128 1127 addAssign->get_results().front() = appExpr->get_results().front()->clone(); 1129 1128 if ( appExpr->get_env() ) { … … 1152 1151 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1153 1152 multiply->get_args().push_back( appExpr->get_args().back() ); 1154 multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );1153 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 1155 1154 ret->get_args().push_back( appExpr->get_args().front() ); 1156 1155 ret->get_args().push_back( multiply ); … … 1158 1157 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1159 1158 multiply->get_args().push_back( appExpr->get_args().front() ); 1160 multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );1159 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); 1161 1160 ret->get_args().push_back( multiply ); 1162 1161 ret->get_args().push_back( appExpr->get_args().back() ); … … 1221 1220 UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); 1222 1221 divide->get_args().push_back( appExpr ); 1223 divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) );1222 divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 1224 1223 divide->get_results().push_front( appExpr->get_results().front()->clone() ); 1225 1224 if ( appExpr->get_env() ) { … … 1231 1230 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1232 1231 multiply->get_args().push_back( appExpr->get_args().back() ); 1233 multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );1232 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 1234 1233 appExpr->get_args().back() = multiply; 1235 1234 } else if ( baseType2 ) { 1236 1235 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1237 1236 multiply->get_args().push_back( appExpr->get_args().front() ); 1238 multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );1237 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); 1239 1238 appExpr->get_args().front() = multiply; 1240 1239 } // if … … 1246 1245 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1247 1246 multiply->get_args().push_back( appExpr->get_args().back() ); 1248 multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) );1247 multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) ); 1249 1248 appExpr->get_args().back() = multiply; 1250 1249 } // if … … 1283 1282 std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); 1284 1283 1285 TyVarMap exprTyVars ( (TypeDecl::Kind)-1 );1284 TyVarMap exprTyVars; 1286 1285 makeTyVarMap( function, exprTyVars ); 1287 1286 ReferenceToType *polyRetType = isPolyRet( function ); … … 1306 1305 1307 1306 boxParams( appExpr, function, arg, exprTyVars ); 1307 1308 1308 passAdapters( appExpr, function, exprTyVars ); 1309 1309 … … 1385 1385 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { 1386 1386 // find assignment operator for type variable 1387 ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );1387 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 1388 1388 if ( assignIter == assignOps.end() ) { 1389 1389 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); … … 1419 1419 DeclarationWithType *assertAssign = 0; 1420 1420 if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { 1421 ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );1421 std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() ); 1422 1422 if ( assertAssignIt == assignOps.end() ) { 1423 1423 throw SemanticError( "No assignment operation found for ", formalTypeInstType ); … … 1460 1460 1461 1461 Type * Pass1::mutate( PointerType *pointerType ) { 1462 scopeTyVars.beginScope();1462 TyVarMap oldtyVars = scopeTyVars; 1463 1463 makeTyVarMap( pointerType, scopeTyVars ); 1464 1464 1465 1465 Type *ret = Mutator::mutate( pointerType ); 1466 1466 1467 scopeTyVars .endScope();1467 scopeTyVars = oldtyVars; 1468 1468 return ret; 1469 1469 } 1470 1470 1471 1471 Type * Pass1::mutate( FunctionType *functionType ) { 1472 scopeTyVars.beginScope();1472 TyVarMap oldtyVars = scopeTyVars; 1473 1473 makeTyVarMap( functionType, scopeTyVars ); 1474 1474 1475 1475 Type *ret = Mutator::mutate( functionType ); 1476 1476 1477 scopeTyVars .endScope();1477 scopeTyVars = oldtyVars; 1478 1478 return ret; 1479 1479 } … … 1540 1540 1541 1541 Type * Pass2::mutate( PointerType *pointerType ) { 1542 scopeTyVars.beginScope();1542 TyVarMap oldtyVars = scopeTyVars; 1543 1543 makeTyVarMap( pointerType, scopeTyVars ); 1544 1544 1545 1545 Type *ret = Mutator::mutate( pointerType ); 1546 1546 1547 scopeTyVars .endScope();1547 scopeTyVars = oldtyVars; 1548 1548 return ret; 1549 1549 } 1550 1550 1551 1551 Type *Pass2::mutate( FunctionType *funcType ) { 1552 scopeTyVars.beginScope();1552 TyVarMap oldtyVars = scopeTyVars; 1553 1553 makeTyVarMap( funcType, scopeTyVars ); 1554 1554 … … 1572 1572 if ( (*tyParm)->get_kind() == TypeDecl::Any ) { 1573 1573 TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); 1574 std::string parmName = mangleType( &parmType );1575 1574 1576 1575 sizeParm = newObj.clone(); 1577 sizeParm->set_name( sizeofName( parmName ) );1576 sizeParm->set_name( sizeofName( &parmType ) ); 1578 1577 last = funcType->get_parameters().insert( last, sizeParm ); 1579 1578 ++last; 1580 1579 1581 1580 alignParm = newObj.clone(); 1582 alignParm->set_name( alignofName( parmName ) );1581 alignParm->set_name( alignofName( &parmType ) ); 1583 1582 last = funcType->get_parameters().insert( last, alignParm ); 1584 1583 ++last; … … 1595 1594 std::set< std::string > seenTypes; // sizeofName for generic types we've seen 1596 1595 for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { 1597 Type *poly Type = isPolyType( (*fnParm)->get_type(), scopeTyVars );1598 if ( poly Type && ! dynamic_cast< TypeInstType* >( polyType ) ) {1599 std::string typeName = mangleType( polyType );1600 if ( seenTypes.count( typeName ) ) continue;1596 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars ); 1597 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 1598 std::string sizeName = sizeofName( polyBase ); 1599 if ( seenTypes.count( sizeName ) ) continue; 1601 1600 1602 1601 ObjectDecl *sizeParm, *alignParm, *offsetParm; 1603 1602 sizeParm = newObj.clone(); 1604 sizeParm->set_name( size ofName( typeName ));1603 sizeParm->set_name( sizeName ); 1605 1604 last = funcType->get_parameters().insert( last, sizeParm ); 1606 1605 ++last; 1607 1606 1608 1607 alignParm = newObj.clone(); 1609 alignParm->set_name( alignofName( typeName ) );1608 alignParm->set_name( alignofName( polyBase ) ); 1610 1609 last = funcType->get_parameters().insert( last, alignParm ); 1611 1610 ++last; 1612 1611 1613 if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( poly Type ) ) {1612 if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyBase ) ) { 1614 1613 // NOTE zero-length arrays are illegal in C, so empty structs have no offset array 1615 1614 if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { 1616 1615 offsetParm = newPtr.clone(); 1617 offsetParm->set_name( offsetofName( typeName ) );1616 offsetParm->set_name( offsetofName( polyBase ) ); 1618 1617 last = funcType->get_parameters().insert( last, offsetParm ); 1619 1618 ++last; … … 1621 1620 } 1622 1621 1623 seenTypes.insert( typeName );1622 seenTypes.insert( sizeName ); 1624 1623 } 1625 1624 } … … 1631 1630 mutateAll( funcType->get_parameters(), *this ); 1632 1631 1633 scopeTyVars .endScope();1632 scopeTyVars = oldtyVars; 1634 1633 return funcType; 1635 1634 } … … 1824 1823 } 1825 1824 1826 ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////////////////////////// 1827 1828 void PolyGenericCalculator::beginTypeScope( Type *ty ) { 1829 scopeTyVars.beginScope(); 1830 makeTyVarMap( ty, scopeTyVars ); 1831 } 1832 1833 void PolyGenericCalculator::endTypeScope() { 1834 scopeTyVars.endScope(); 1835 } 1825 ////////////////////////////////////////// MemberExprFixer //////////////////////////////////////////////////// 1836 1826 1837 1827 template< typename DeclClass > 1838 1828 DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { 1839 beginTypeScope( type ); 1840 knownLayouts.beginScope(); 1841 knownOffsets.beginScope(); 1829 TyVarMap oldtyVars = scopeTyVars; 1830 makeTyVarMap( type, scopeTyVars ); 1842 1831 1843 1832 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); 1844 1833 1845 knownOffsets.endScope(); 1846 knownLayouts.endScope(); 1847 endTypeScope(); 1834 scopeTyVars = oldtyVars; 1848 1835 return ret; 1849 1836 } … … 1867 1854 1868 1855 Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { 1869 beginTypeScope( pointerType ); 1856 TyVarMap oldtyVars = scopeTyVars; 1857 makeTyVarMap( pointerType, scopeTyVars ); 1870 1858 1871 1859 Type *ret = Mutator::mutate( pointerType ); 1872 1860 1873 endTypeScope();1861 scopeTyVars = oldtyVars; 1874 1862 return ret; 1875 1863 } 1876 1864 1877 1865 Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { 1878 beginTypeScope( funcType ); 1866 TyVarMap oldtyVars = scopeTyVars; 1867 makeTyVarMap( funcType, scopeTyVars ); 1879 1868 1880 1869 // make sure that any type information passed into the function is accounted for 1881 1870 for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) { 1882 1871 // condition here duplicates that in Pass2::mutate( FunctionType* ) 1883 Type *poly Type = isPolyType( (*fnParm)->get_type(), scopeTyVars );1884 if ( poly Type && ! dynamic_cast< TypeInstType* >( polyType ) ) {1885 knownLayouts.insert( mangleType( polyType ) );1872 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars ); 1873 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 1874 knownLayouts.insert( sizeofName( polyBase ) ); 1886 1875 } 1887 1876 } … … 1889 1878 Type *ret = Mutator::mutate( funcType ); 1890 1879 1891 endTypeScope();1880 scopeTyVars = oldtyVars; 1892 1881 return ret; 1893 1882 } … … 1900 1889 Type *declType = objectDecl->get_type(); 1901 1890 UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); 1902 alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType )) ) );1891 alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) ); 1903 1892 1904 1893 delete objectDecl->get_init(); … … 1932 1921 ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) ); 1933 1922 UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); 1934 fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType )) ) );1923 fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) ); 1935 1924 fieldOffset->get_args().push_back( fieldIndex ); 1936 1925 return fieldOffset; … … 2006 1995 if ( findGeneric( *param ) ) { 2007 1996 // push size/align vars for a generic parameter back 2008 std::string paramName = mangleType( *param ); 2009 layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) ); 2010 layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) ); 1997 layoutCall->get_args().push_back( new NameExpr( sizeofName( *param ) ) ); 1998 layoutCall->get_args().push_back( new NameExpr( alignofName( *param ) ) ); 2011 1999 } else { 2012 2000 layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); … … 2052 2040 } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { 2053 2041 // check if this type already has a layout generated for it 2054 std::string typeName = mangleType( ty );2055 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;2042 std::string sizeName = sizeofName( ty ); 2043 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true; 2056 2044 2057 2045 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized … … 2060 2048 2061 2049 // insert local variables for layout and generate call to layout function 2062 knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call2050 knownLayouts.insert( sizeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2063 2051 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2064 2052 … … 2066 2054 if ( n_members == 0 ) { 2067 2055 // all empty structs have the same layout - size 1, align 1 2068 makeVar( size ofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );2069 makeVar( alignofName( ty peName), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)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 ) ) ) ); 2070 2058 // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array 2071 2059 } else { 2072 ObjectDecl *sizeVar = makeVar( size ofName( typeName ), layoutType );2073 ObjectDecl *alignVar = makeVar( alignofName( ty peName), layoutType->clone() );2074 ObjectDecl *offsetVar = makeVar( offsetofName( ty peName), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) );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 ) ); 2075 2063 2076 2064 // generate call to layout function 2077 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct()) ) );2065 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + structTy->get_baseStruct()->get_name() ) ); 2078 2066 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2079 2067 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); … … 2087 2075 } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { 2088 2076 // check if this type already has a layout generated for it 2089 std::string typeName = mangleType( ty );2090 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;2077 std::string sizeName = sizeofName( ty ); 2078 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true; 2091 2079 2092 2080 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized … … 2095 2083 2096 2084 // insert local variables for layout and generate call to layout function 2097 knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call2085 knownLayouts.insert( sizeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2098 2086 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2099 2087 2100 ObjectDecl *sizeVar = makeVar( size ofName( typeName ), layoutType );2101 ObjectDecl *alignVar = makeVar( alignofName( ty peName), layoutType->clone() );2088 ObjectDecl *sizeVar = makeVar( sizeName, layoutType ); 2089 ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() ); 2102 2090 2103 2091 // generate call to layout function 2104 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion()) ) );2092 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + unionTy->get_baseUnion()->get_name() ) ); 2105 2093 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2106 2094 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); … … 2118 2106 Type *ty = sizeofExpr->get_type(); 2119 2107 if ( findGeneric( ty ) ) { 2120 Expression *ret = new NameExpr( sizeofName( mangleType( ty )) );2108 Expression *ret = new NameExpr( sizeofName( ty ) ); 2121 2109 delete sizeofExpr; 2122 2110 return ret; … … 2128 2116 Type *ty = alignofExpr->get_type(); 2129 2117 if ( findGeneric( ty ) ) { 2130 Expression *ret = new NameExpr( alignofName( mangleType( ty )) );2118 Expression *ret = new NameExpr( alignofName( ty ) ); 2131 2119 delete alignofExpr; 2132 2120 return ret; … … 2166 2154 if ( findGeneric( ty ) ) { 2167 2155 // pull offset back from generated type information 2168 ret = new NameExpr( offsetofName( mangleType( ty )) );2156 ret = new NameExpr( offsetofName( ty ) ); 2169 2157 } else { 2170 std::string offsetName = offsetofName( mangleType( ty ));2158 std::string offsetName = offsetofName( ty ); 2171 2159 if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { 2172 2160 // use the already-generated offsets for this type … … 2208 2196 void PolyGenericCalculator::doEndScope() { 2209 2197 knownLayouts.endScope(); 2210 knownOffsets. endScope();2198 knownOffsets.beginScope(); 2211 2199 } 2212 2200 … … 2215 2203 template< typename DeclClass > 2216 2204 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { 2217 scopeTyVars.beginScope();2205 TyVarMap oldtyVars = scopeTyVars; 2218 2206 makeTyVarMap( type, scopeTyVars ); 2219 2207 … … 2221 2209 ScrubTyVars::scrub( decl, scopeTyVars ); 2222 2210 2223 scopeTyVars .endScope();2211 scopeTyVars = oldtyVars; 2224 2212 return ret; 2225 2213 } … … 2251 2239 2252 2240 Type * Pass3::mutate( PointerType *pointerType ) { 2253 scopeTyVars.beginScope();2241 TyVarMap oldtyVars = scopeTyVars; 2254 2242 makeTyVarMap( pointerType, scopeTyVars ); 2255 2243 2256 2244 Type *ret = Mutator::mutate( pointerType ); 2257 2245 2258 scopeTyVars .endScope();2246 scopeTyVars = oldtyVars; 2259 2247 return ret; 2260 2248 } 2261 2249 2262 2250 Type * Pass3::mutate( FunctionType *functionType ) { 2263 scopeTyVars.beginScope();2251 TyVarMap oldtyVars = scopeTyVars; 2264 2252 makeTyVarMap( functionType, scopeTyVars ); 2265 2253 2266 2254 Type *ret = Mutator::mutate( functionType ); 2267 2255 2268 scopeTyVars .endScope();2256 scopeTyVars = oldtyVars; 2269 2257 return ret; 2270 2258 }
Note:
See TracChangeset
for help on using the changeset viewer.