Changes in / [e55ca05:df2be83]
- Location:
- src/GenPoly
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
re55ca05 rdf2be83 205 205 ObjectDecl *makeTemporary( Type *type ); 206 206 207 std::map< std::string, DeclarationWithType *> assignOps;///< Currently known type variable assignment operators207 ScopedMap< 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 … … 385 385 for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { 386 386 TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param ); 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 ) ); 387 std::string paramName = mangleType( ¶mType ); 388 layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 389 layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); 389 390 } 390 391 } 391 392 392 393 /// Builds a layout function declaration 393 FunctionDecl *buildLayoutFunctionDecl( const std::string &typeName, unsigned int functionNesting, FunctionType *layoutFnType ) {394 FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) { 394 395 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units 395 396 // because each unit generates copies of the default routines for each aggregate. 396 397 FunctionDecl *layoutDecl = new FunctionDecl( 397 "__layoutof_" + typeName, functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );398 layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false ); 398 399 layoutDecl->fixUniqueId(); 399 400 return layoutDecl; … … 462 463 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); 463 464 464 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );465 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); 465 466 layoutFnType->get_parameters().push_back( sizeParam ); 466 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );467 ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 467 468 layoutFnType->get_parameters().push_back( alignParam ); 468 ObjectDecl *offsetParam = new ObjectDecl( "__offsetof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );469 ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 469 470 layoutFnType->get_parameters().push_back( offsetParam ); 470 471 addOtypeParams( layoutFnType, otypeParams ); 471 472 472 473 // build function decl 473 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl ->get_name(), functionNesting, layoutFnType );474 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType ); 474 475 475 476 // calculate struct layout in function body … … 523 524 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); 524 525 525 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );526 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); 526 527 layoutFnType->get_parameters().push_back( sizeParam ); 527 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );528 ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); 528 529 layoutFnType->get_parameters().push_back( alignParam ); 529 530 addOtypeParams( layoutFnType, otypeParams ); 530 531 531 532 // build function decl 532 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl ->get_name(), functionNesting, layoutFnType );533 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType ); 533 534 534 535 // calculate union layout in function body … … 663 664 if ( functionDecl->get_statements() ) { // empty routine body ? 664 665 doBeginScope(); 665 TyVarMap oldtyVars = scopeTyVars;666 std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;666 scopeTyVars.beginScope();; 667 assignOps.beginScope(); 667 668 DeclarationWithType *oldRetval = retval; 668 669 bool oldUseRetval = useRetval; … … 705 706 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); 706 707 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"; 708 scopeTyVars.endScope(); 709 assignOps.endScope(); 714 710 retval = oldRetval; 715 711 useRetval = oldUseRetval; … … 747 743 Type *polyBase = hasPolyBase( parmType, exprTyVars ); 748 744 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 749 std::string sizeName = sizeofName( polyBase );750 if ( seenTypes.count( sizeName ) ) return;745 std::string typeName = mangleType( polyBase ); 746 if ( seenTypes.count( typeName ) ) return; 751 747 752 748 arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); … … 766 762 } 767 763 768 seenTypes.insert( sizeName );764 seenTypes.insert( typeName ); 769 765 } 770 766 } … … 1124 1120 addAssign->get_args().push_back( appExpr->get_args().front() ); 1125 1121 } // if 1126 addAssign->get_args().push_back( new NameExpr( sizeofName( polyType) ) );1122 addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) ); 1127 1123 addAssign->get_results().front() = appExpr->get_results().front()->clone(); 1128 1124 if ( appExpr->get_env() ) { … … 1151 1147 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1152 1148 multiply->get_args().push_back( appExpr->get_args().back() ); 1153 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1) ) );1149 multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); 1154 1150 ret->get_args().push_back( appExpr->get_args().front() ); 1155 1151 ret->get_args().push_back( multiply ); … … 1157 1153 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1158 1154 multiply->get_args().push_back( appExpr->get_args().front() ); 1159 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2) ) );1155 multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); 1160 1156 ret->get_args().push_back( multiply ); 1161 1157 ret->get_args().push_back( appExpr->get_args().back() ); … … 1220 1216 UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); 1221 1217 divide->get_args().push_back( appExpr ); 1222 divide->get_args().push_back( new NameExpr( sizeofName( baseType1) ) );1218 divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); 1223 1219 divide->get_results().push_front( appExpr->get_results().front()->clone() ); 1224 1220 if ( appExpr->get_env() ) { … … 1230 1226 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1231 1227 multiply->get_args().push_back( appExpr->get_args().back() ); 1232 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1) ) );1228 multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); 1233 1229 appExpr->get_args().back() = multiply; 1234 1230 } else if ( baseType2 ) { 1235 1231 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1236 1232 multiply->get_args().push_back( appExpr->get_args().front() ); 1237 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2) ) );1233 multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); 1238 1234 appExpr->get_args().front() = multiply; 1239 1235 } // if … … 1245 1241 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 1246 1242 multiply->get_args().push_back( appExpr->get_args().back() ); 1247 multiply->get_args().push_back( new NameExpr( sizeofName( baseType) ) );1243 multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) ); 1248 1244 appExpr->get_args().back() = multiply; 1249 1245 } // if … … 1282 1278 std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); 1283 1279 1284 TyVarMap exprTyVars ;1280 TyVarMap exprTyVars( (TypeDecl::Kind)-1 ); 1285 1281 makeTyVarMap( function, exprTyVars ); 1286 1282 ReferenceToType *polyRetType = isPolyRet( function ); … … 1385 1381 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { 1386 1382 // find assignment operator for type variable 1387 std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );1383 ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); 1388 1384 if ( assignIter == assignOps.end() ) { 1389 1385 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); … … 1419 1415 DeclarationWithType *assertAssign = 0; 1420 1416 if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { 1421 std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );1417 ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() ); 1422 1418 if ( assertAssignIt == assignOps.end() ) { 1423 1419 throw SemanticError( "No assignment operation found for ", formalTypeInstType ); … … 1460 1456 1461 1457 Type * Pass1::mutate( PointerType *pointerType ) { 1462 TyVarMap oldtyVars = scopeTyVars;1458 scopeTyVars.beginScope(); 1463 1459 makeTyVarMap( pointerType, scopeTyVars ); 1464 1460 1465 1461 Type *ret = Mutator::mutate( pointerType ); 1466 1462 1467 scopeTyVars = oldtyVars;1463 scopeTyVars.endScope(); 1468 1464 return ret; 1469 1465 } 1470 1466 1471 1467 Type * Pass1::mutate( FunctionType *functionType ) { 1472 TyVarMap oldtyVars = scopeTyVars;1468 scopeTyVars.beginScope(); 1473 1469 makeTyVarMap( functionType, scopeTyVars ); 1474 1470 1475 1471 Type *ret = Mutator::mutate( functionType ); 1476 1472 1477 scopeTyVars = oldtyVars;1473 scopeTyVars.endScope(); 1478 1474 return ret; 1479 1475 } … … 1540 1536 1541 1537 Type * Pass2::mutate( PointerType *pointerType ) { 1542 TyVarMap oldtyVars = scopeTyVars;1538 scopeTyVars.beginScope(); 1543 1539 makeTyVarMap( pointerType, scopeTyVars ); 1544 1540 1545 1541 Type *ret = Mutator::mutate( pointerType ); 1546 1542 1547 scopeTyVars = oldtyVars;1543 scopeTyVars.endScope(); 1548 1544 return ret; 1549 1545 } 1550 1546 1551 1547 Type *Pass2::mutate( FunctionType *funcType ) { 1552 TyVarMap oldtyVars = scopeTyVars;1548 scopeTyVars.beginScope(); 1553 1549 makeTyVarMap( funcType, scopeTyVars ); 1554 1550 … … 1572 1568 if ( (*tyParm)->get_kind() == TypeDecl::Any ) { 1573 1569 TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); 1570 std::string parmName = mangleType( &parmType ); 1574 1571 1575 1572 sizeParm = newObj.clone(); 1576 sizeParm->set_name( sizeofName( &parmType ) );1573 sizeParm->set_name( sizeofName( parmName ) ); 1577 1574 last = funcType->get_parameters().insert( last, sizeParm ); 1578 1575 ++last; 1579 1576 1580 1577 alignParm = newObj.clone(); 1581 alignParm->set_name( alignofName( &parmType ) );1578 alignParm->set_name( alignofName( parmName ) ); 1582 1579 last = funcType->get_parameters().insert( last, alignParm ); 1583 1580 ++last; … … 1596 1593 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars ); 1597 1594 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 1598 std::string sizeName = sizeofName( polyBase );1599 if ( seenTypes.count( sizeName ) ) continue;1595 std::string typeName = mangleType( polyBase ); 1596 if ( seenTypes.count( typeName ) ) continue; 1600 1597 1601 1598 ObjectDecl *sizeParm, *alignParm, *offsetParm; 1602 1599 sizeParm = newObj.clone(); 1603 sizeParm->set_name( size Name);1600 sizeParm->set_name( sizeofName( typeName ) ); 1604 1601 last = funcType->get_parameters().insert( last, sizeParm ); 1605 1602 ++last; 1606 1603 1607 1604 alignParm = newObj.clone(); 1608 alignParm->set_name( alignofName( polyBase ) );1605 alignParm->set_name( alignofName( typeName ) ); 1609 1606 last = funcType->get_parameters().insert( last, alignParm ); 1610 1607 ++last; … … 1614 1611 if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { 1615 1612 offsetParm = newPtr.clone(); 1616 offsetParm->set_name( offsetofName( polyBase ) );1613 offsetParm->set_name( offsetofName( typeName ) ); 1617 1614 last = funcType->get_parameters().insert( last, offsetParm ); 1618 1615 ++last; … … 1620 1617 } 1621 1618 1622 seenTypes.insert( sizeName );1619 seenTypes.insert( typeName ); 1623 1620 } 1624 1621 } … … 1630 1627 mutateAll( funcType->get_parameters(), *this ); 1631 1628 1632 scopeTyVars = oldtyVars;1629 scopeTyVars.endScope(); 1633 1630 return funcType; 1634 1631 } … … 1827 1824 template< typename DeclClass > 1828 1825 DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { 1829 TyVarMap oldtyVars = scopeTyVars;1826 scopeTyVars.beginScope(); 1830 1827 makeTyVarMap( type, scopeTyVars ); 1831 1828 1832 1829 DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); 1833 1830 1834 scopeTyVars = oldtyVars;1831 scopeTyVars.endScope(); 1835 1832 return ret; 1836 1833 } … … 1854 1851 1855 1852 Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { 1856 TyVarMap oldtyVars = scopeTyVars;1853 scopeTyVars.beginScope(); 1857 1854 makeTyVarMap( pointerType, scopeTyVars ); 1858 1855 1859 1856 Type *ret = Mutator::mutate( pointerType ); 1860 1857 1861 scopeTyVars = oldtyVars;1858 scopeTyVars.endScope(); 1862 1859 return ret; 1863 1860 } 1864 1861 1865 1862 Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { 1866 TyVarMap oldtyVars = scopeTyVars;1863 scopeTyVars.beginScope(); 1867 1864 makeTyVarMap( funcType, scopeTyVars ); 1868 1865 … … 1872 1869 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars ); 1873 1870 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) { 1874 knownLayouts.insert( sizeofName( polyBase ) );1871 knownLayouts.insert( mangleType( polyBase ) ); 1875 1872 } 1876 1873 } … … 1878 1875 Type *ret = Mutator::mutate( funcType ); 1879 1876 1880 scopeTyVars = oldtyVars;1877 scopeTyVars.endScope(); 1881 1878 return ret; 1882 1879 } … … 1889 1886 Type *declType = objectDecl->get_type(); 1890 1887 UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); 1891 alloc->get_args().push_back( new NameExpr( sizeofName( declType) ) );1888 alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) ); 1892 1889 1893 1890 delete objectDecl->get_init(); … … 1921 1918 ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) ); 1922 1919 UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); 1923 fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType) ) );1920 fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) ); 1924 1921 fieldOffset->get_args().push_back( fieldIndex ); 1925 1922 return fieldOffset; … … 1995 1992 if ( findGeneric( *param ) ) { 1996 1993 // 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 ) ) ); 1994 std::string paramName = mangleType( *param ); 1995 layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) ); 1996 layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) ); 1999 1997 } else { 2000 1998 layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); … … 2040 2038 } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { 2041 2039 // 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;2040 std::string typeName = mangleType( ty ); 2041 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; 2044 2042 2045 2043 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized … … 2048 2046 2049 2047 // 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 call2048 knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2051 2049 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2052 2050 … … 2054 2052 if ( n_members == 0 ) { 2055 2053 // all empty structs have the same layout - size 1, align 1 2056 makeVar( size Name, 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 ) ) ) );2054 makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); 2055 makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) ); 2058 2056 // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array 2059 2057 } else { 2060 ObjectDecl *sizeVar = makeVar( size Name, 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 ) );2058 ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); 2059 ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); 2060 ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) ); 2063 2061 2064 2062 // generate call to layout function 2065 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + structTy->get_baseStruct()->get_name() ) );2063 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) ); 2066 2064 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2067 2065 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); … … 2075 2073 } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { 2076 2074 // 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;2075 std::string typeName = mangleType( ty ); 2076 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; 2079 2077 2080 2078 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized … … 2083 2081 2084 2082 // 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 call2083 knownLayouts.insert( typeName ); // done early so as not to interfere with the later addition of parameters to the layout call 2086 2084 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 2087 2085 2088 ObjectDecl *sizeVar = makeVar( size Name, layoutType );2089 ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() );2086 ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); 2087 ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); 2090 2088 2091 2089 // generate call to layout function 2092 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + unionTy->get_baseUnion()->get_name() ) );2090 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) ); 2093 2091 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); 2094 2092 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); … … 2106 2104 Type *ty = sizeofExpr->get_type(); 2107 2105 if ( findGeneric( ty ) ) { 2108 Expression *ret = new NameExpr( sizeofName( ty) );2106 Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) ); 2109 2107 delete sizeofExpr; 2110 2108 return ret; … … 2116 2114 Type *ty = alignofExpr->get_type(); 2117 2115 if ( findGeneric( ty ) ) { 2118 Expression *ret = new NameExpr( alignofName( ty) );2116 Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) ); 2119 2117 delete alignofExpr; 2120 2118 return ret; … … 2154 2152 if ( findGeneric( ty ) ) { 2155 2153 // pull offset back from generated type information 2156 ret = new NameExpr( offsetofName( ty) );2154 ret = new NameExpr( offsetofName( mangleType( ty ) ) ); 2157 2155 } else { 2158 std::string offsetName = offsetofName( ty);2156 std::string offsetName = offsetofName( mangleType( ty ) ); 2159 2157 if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { 2160 2158 // use the already-generated offsets for this type … … 2196 2194 void PolyGenericCalculator::doEndScope() { 2197 2195 knownLayouts.endScope(); 2198 knownOffsets. beginScope();2196 knownOffsets.endScope(); 2199 2197 } 2200 2198 … … 2203 2201 template< typename DeclClass > 2204 2202 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { 2205 TyVarMap oldtyVars = scopeTyVars;2203 scopeTyVars.beginScope(); 2206 2204 makeTyVarMap( type, scopeTyVars ); 2207 2205 … … 2209 2207 ScrubTyVars::scrub( decl, scopeTyVars ); 2210 2208 2211 scopeTyVars = oldtyVars;2209 scopeTyVars.endScope(); 2212 2210 return ret; 2213 2211 } … … 2239 2237 2240 2238 Type * Pass3::mutate( PointerType *pointerType ) { 2241 TyVarMap oldtyVars = scopeTyVars;2239 scopeTyVars.beginScope(); 2242 2240 makeTyVarMap( pointerType, scopeTyVars ); 2243 2241 2244 2242 Type *ret = Mutator::mutate( pointerType ); 2245 2243 2246 scopeTyVars = oldtyVars;2244 scopeTyVars.endScope(); 2247 2245 return ret; 2248 2246 } 2249 2247 2250 2248 Type * Pass3::mutate( FunctionType *functionType ) { 2251 TyVarMap oldtyVars = scopeTyVars;2249 scopeTyVars.beginScope(); 2252 2250 makeTyVarMap( functionType, scopeTyVars ); 2253 2251 2254 2252 Type *ret = Mutator::mutate( functionType ); 2255 2253 2256 scopeTyVars = oldtyVars;2254 scopeTyVars.endScope(); 2257 2255 return ret; 2258 2256 } -
src/GenPoly/FindFunction.cc
re55ca05 rdf2be83 55 55 TyVarMap::iterator var = tyVars.find( (*i)->get_name() ); 56 56 if ( var != tyVars.end() ) { 57 tyVars.erase( var );57 tyVars.erase( var->first ); 58 58 } // if 59 59 } // for … … 61 61 62 62 Type * FindFunction::mutate( FunctionType *functionType ) { 63 TyVarMap oldTyVars = tyVars;63 tyVars.beginScope(); 64 64 handleForall( functionType->get_forall() ); 65 65 mutateAll( functionType->get_returnVals(), *this ); … … 72 72 } // if 73 73 } // if 74 tyVars = oldTyVars;74 tyVars.endScope(); 75 75 return ret; 76 76 } 77 77 78 78 Type * FindFunction::mutate( PointerType *pointerType ) { 79 TyVarMap oldTyVars = tyVars;79 tyVars.beginScope(); 80 80 handleForall( pointerType->get_forall() ); 81 81 Type *ret = Mutator::mutate( pointerType ); 82 tyVars = oldTyVars;82 tyVars.endScope(); 83 83 return ret; 84 84 } -
src/GenPoly/GenPoly.cc
re55ca05 rdf2be83 16 16 #include "GenPoly.h" 17 17 18 #include "SymTab/Mangler.h"19 18 #include "SynTree/Expression.h" 20 19 #include "SynTree/Type.h" … … 38 37 ReferenceToType *isPolyRet( FunctionType *function ) { 39 38 if ( ! function->get_returnVals().empty() ) { 40 TyVarMap forallTypes ;39 TyVarMap forallTypes( (TypeDecl::Kind)-1 ); 41 40 makeTyVarMap( function, forallTypes ); 42 41 return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes ); … … 218 217 } 219 218 220 std::string sizeofName( Type *ty ) {221 return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );222 }223 224 std::string alignofName( Type *ty ) {225 return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );226 }227 228 std::string offsetofName( Type* ty ) {229 return std::string( "_offsetof_" ) + SymTab::Mangler::mangleType( ty );230 }231 232 219 } // namespace GenPoly 233 220 -
src/GenPoly/GenPoly.h
re55ca05 rdf2be83 17 17 #define GENPOLY_H 18 18 19 #include <map>20 19 #include <string> 21 20 #include <iostream> 22 21 #include <utility> 22 23 #include "ErasableScopedMap.h" 24 25 #include "SymTab/Mangler.h" 23 26 24 27 #include "SynTree/Declaration.h" … … 27 30 28 31 namespace GenPoly { 29 typedef std::map< std::string, TypeDecl::Kind > TyVarMap;32 typedef ErasableScopedMap< std::string, TypeDecl::Kind > TyVarMap; 30 33 31 34 /// A function needs an adapter if it returns a polymorphic value or if any of its … … 69 72 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ); 70 73 71 /// Gets the name of the sizeof parameter for the type 72 std::string sizeofName( Type *ty ); 74 /// Gets the mangled name of this type; alias for SymTab::Mangler::mangleType(). 75 inline std::string mangleType( Type *ty ) { return SymTab::Mangler::mangleType( ty ); } 76 77 /// Gets the name of the sizeof parameter for the type, given its mangled name 78 inline std::string sizeofName( const std::string &name ) { return std::string( "_sizeof_" ) + name; } 73 79 74 /// Gets the name of the alignof parameter for the type 75 std::string alignofName( Type *ty );80 /// Gets the name of the alignof parameter for the type, given its mangled name 81 inline std::string alignofName( const std::string &name ) { return std::string( "_alignof_" ) + name; } 76 82 77 /// Gets the name of the offsetof parameter for the type 78 std::string offsetofName( Type *ty ); 83 /// Gets the name of the offsetof parameter for the type, given its mangled name 84 inline std::string offsetofName( const std::string &name ) { return std::string( "_offsetof_" ) + name; } 85 86 /// Gets the name of the layout function for a given aggregate type, given its declaration 87 inline std::string layoutofName( AggregateDecl *decl ) { return std::string( "_layoutof_" ) + decl->get_name(); } 88 79 89 } // namespace GenPoly 80 90 -
src/GenPoly/PolyMutator.cc
re55ca05 rdf2be83 27 27 } 28 28 29 PolyMutator::PolyMutator() : env( 0 ) { 30 } 29 PolyMutator::PolyMutator() : scopeTyVars( (TypeDecl::Kind)-1 ), env( 0 ) {} 31 30 32 31 void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) { -
src/GenPoly/ScopedMap.h
re55ca05 rdf2be83 51 51 typedef typename scope_list::size_type size_type; 52 52 53 /// Checks if this iterator points to a valid item 54 bool is_valid() const { 55 return it != (*scopes)[i].end(); 56 } 57 58 /// Increments on invalid 59 iterator& next_valid() { 60 if ( ! is_valid() ) { ++(*this); } 61 return *this; 62 } 63 64 /// Decrements on invalid 65 iterator& prev_valid() { 66 if ( ! is_valid() ) { --(*this); } 67 return *this; 68 } 69 53 70 iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i) 54 71 : scopes(&_scopes), it(_it), i(_i) {} … … 68 85 --i; 69 86 it = (*scopes)[i].begin(); 70 return *this;71 }72 ++it;73 return *this;87 } else { 88 ++it; 89 } 90 return next_valid(); 74 91 } 75 92 iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } … … 82 99 } 83 100 --it; 84 return *this;101 return prev_valid(); 85 102 } 86 103 iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; } … … 105 122 typedef typename scope_list::size_type size_type; 106 123 124 /// Checks if this iterator points to a valid item 125 bool is_valid() const { 126 return it != (*scopes)[i].end(); 127 } 128 129 /// Increments on invalid 130 const_iterator& next_valid() { 131 if ( ! is_valid() ) { ++(*this); } 132 return *this; 133 } 134 135 /// Decrements on invalid 136 const_iterator& prev_valid() { 137 if ( ! is_valid() ) { --(*this); } 138 return *this; 139 } 140 107 141 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i) 108 142 : scopes(&_scopes), it(_it), i(_i) {} … … 127 161 --i; 128 162 it = (*scopes)[i].begin(); 129 return *this;130 }131 ++it;132 return *this;163 } else { 164 ++it; 165 } 166 return next_valid(); 133 167 } 134 168 const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } … … 141 175 } 142 176 --it; 143 return *this;177 return prev_valid(); 144 178 } 145 179 const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } … … 171 205 ScopedMap() { beginScope(); } 172 206 173 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }174 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }175 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }207 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 208 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 209 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 176 210 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 177 211 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } -
src/GenPoly/ScopedSet.h
re55ca05 rdf2be83 48 48 typedef typename scope_list::size_type size_type; 49 49 50 /// Checks if this iterator points to a valid item 51 bool is_valid() const { 52 return it != (*scopes)[i].end(); 53 } 54 55 /// Increments on invalid 56 iterator& next_valid() { 57 if ( ! is_valid() ) { ++(*this); } 58 return *this; 59 } 60 61 /// Decrements on invalid 62 iterator& prev_valid() { 63 if ( ! is_valid() ) { --(*this); } 64 return *this; 65 } 66 50 67 iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i) 51 68 : scopes(&_scopes), it(_it), i(_i) {} … … 65 82 --i; 66 83 it = (*scopes)[i].begin(); 67 return *this;68 }69 ++it;70 return *this;84 } else { 85 ++it; 86 } 87 return next_valid(); 71 88 } 72 89 iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } … … 79 96 } 80 97 --it; 81 return *this;98 return prev_valid(); 82 99 } 83 100 iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; } … … 102 119 typedef typename scope_list::size_type size_type; 103 120 121 /// Checks if this iterator points to a valid item 122 bool is_valid() const { 123 return it != (*scopes)[i].end(); 124 } 125 126 /// Increments on invalid 127 const_iterator& next_valid() { 128 if ( ! is_valid() ) { ++(*this); } 129 return *this; 130 } 131 132 /// Decrements on invalid 133 const_iterator& prev_valid() { 134 if ( ! is_valid() ) { --(*this); } 135 return *this; 136 } 137 104 138 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i) 105 139 : scopes(&_scopes), it(_it), i(_i) {} … … 124 158 --i; 125 159 it = (*scopes)[i].begin(); 126 return *this;127 }128 ++it;129 return *this;160 } else { 161 ++it; 162 } 163 return next_valid(); 130 164 } 131 165 const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } … … 138 172 } 139 173 --it; 140 return *this;174 return prev_valid(); 141 175 } 142 176 const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } … … 167 201 ScopedSet() { beginScope(); } 168 202 169 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }170 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }171 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) ; }203 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 204 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 205 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); } 172 206 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 173 207 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } -
src/GenPoly/ScrubTyVars.cc
re55ca05 rdf2be83 64 64 // sizeof( T ) => _sizeof_T parameter, which is the size of T 65 65 if ( Type *polyType = isPolyType( szeof->get_type() ) ) { 66 Expression *expr = new NameExpr( sizeofName( polyType) );66 Expression *expr = new NameExpr( sizeofName( mangleType( polyType ) ) ); 67 67 return expr; 68 68 } else { … … 74 74 // alignof( T ) => _alignof_T parameter, which is the alignment of T 75 75 if ( Type *polyType = isPolyType( algnof->get_type() ) ) { 76 Expression *expr = new NameExpr( alignofName( polyType) );76 Expression *expr = new NameExpr( alignofName( mangleType( polyType ) ) ); 77 77 return expr; 78 78 } else {
Note: See TracChangeset
for help on using the changeset viewer.