Changeset 58eb9250


Ignore:
Timestamp:
Jan 6, 2025, 12:34:42 PM (2 weeks ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
1f6623c, 66e7cc1
Parents:
8893ad4
Message:

Partly fix #269 and try to fix nightly build. Switch to correct type for polymorphic sizeof(-) on 32-bit.

Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cpp

    r8893ad4 r58eb9250  
    4242
    4343/// The layout type is used to represent sizes, alignments and offsets.
    44 ast::BasicType * makeLayoutType() {
    45         return new ast::BasicType( ast::BasicKind::LongUnsignedInt );
     44const ast::Type * getLayoutType( const ast::TranslationUnit & transUnit ) {
     45        assert( transUnit.global.sizeType.get() );
     46        return transUnit.global.sizeType;
    4647}
    4748
    4849/// Fixed version of layout type (just adding a 'C' in C++ style).
    49 ast::BasicType * makeLayoutCType() {
    50         return new ast::BasicType( ast::BasicKind::LongUnsignedInt,
    51                 ast::CV::Qualifiers( ast::CV::Const ) );
     50const ast::Type * getLayoutCType( const ast::TranslationUnit & transUnit ) {
     51        // Hack for optimization: don't want to clone every time, but don't want
     52        // to hardcode a global-translation-unit assumption here either.  So
     53        // cache it: will be fast if there is a single translation unit, but
     54        // still correct otherwise.
     55        static ast::ptr<ast::Type> lastLayoutType = nullptr;
     56        static ast::ptr<ast::Type> lastLayoutCType = nullptr;
     57        const ast::Type * curLayoutType = getLayoutType( transUnit );
     58        if (lastLayoutType != curLayoutType ) {
     59                lastLayoutCType = ast::deepCopy( curLayoutType );
     60                add_qualifiers(
     61                        lastLayoutCType, ast::CV::Qualifiers{ ast::CV::Const } );
     62        }
     63        return lastLayoutCType;
    5264}
    5365
     
    5567/// Adds layout-generation functions to polymorphic types.
    5668struct LayoutFunctionBuilder final :
     69                public ast::WithConstTranslationUnit,
    5770                public ast::WithDeclsToAdd,
    5871                public ast::WithShortCircuiting,
     
    7790void addSTypeParams(
    7891                ast::vector<ast::DeclWithType> & params,
    79                 ast::vector<ast::TypeDecl> const & sizedParams ) {
     92                ast::vector<ast::TypeDecl> const & sizedParams,
     93                const ast::TranslationUnit & transUnit ) {
    8094        for ( ast::ptr<ast::TypeDecl> const & sizedParam : sizedParams ) {
    8195                ast::TypeInstType inst( sizedParam );
     
    8498                        sizedParam->location,
    8599                        sizeofName( paramName ),
    86                         makeLayoutCType()
     100                        getLayoutCType( transUnit )
    87101                ) );
    88102                auto alignParam = new ast::ObjectDecl(
    89103                        sizedParam->location,
    90104                        alignofName( paramName ),
    91                         makeLayoutCType()
     105                        getLayoutCType( transUnit )
    92106                );
    93107                alignParam->attributes.push_back( new ast::Attribute( "unused" ) );
     
    96110}
    97111
    98 ast::Type * makeLayoutOutType() {
    99         return new ast::PointerType( makeLayoutType() );
     112ast::Type * getLayoutOutType( const ast::TranslationUnit & transUnit ) {
     113        return new ast::PointerType( getLayoutType( transUnit ) );
    100114}
    101115
     
    110124                CodeLocation const & location, ast::AggregateDecl const * aggr,
    111125                ast::vector<ast::TypeDecl> const & sizedParams,
    112                 bool isInFunction, bool isStruct ) {
     126                bool isInFunction, bool isStruct,
     127                const ast::TranslationUnit & transUnit ) {
    113128        ast::ObjectDecl * sizeParam = new ast::ObjectDecl(
    114129                location,
    115130                sizeofName( aggr->name ),
    116                 makeLayoutOutType()
     131                getLayoutOutType( transUnit )
    117132        );
    118133        ast::ObjectDecl * alignParam = new ast::ObjectDecl(
    119134                location,
    120135                alignofName( aggr->name ),
    121                 makeLayoutOutType()
     136                getLayoutOutType( transUnit )
    122137        );
    123138        ast::ObjectDecl * offsetParam = nullptr;
     
    127142                        location,
    128143                        offsetofName( aggr->name ),
    129                         makeLayoutOutType()
     144                        getLayoutOutType( transUnit )
    130145                );
    131146                params.push_back( offsetParam );
    132147        }
    133         addSTypeParams( params, sizedParams );
     148        addSTypeParams( params, sizedParams, transUnit );
    134149
    135150        // Routines at global scope marked "static" to prevent multiple
     
    218233        // Build layout function signature.
    219234        LayoutData layout = buildLayoutFunction(
    220                 location, decl, sizedParams, isInFunction(), true );
     235                location, decl, sizedParams, isInFunction(), true, transUnit() );
    221236        ast::FunctionDecl * layoutDecl = layout.function;
    222237        // Also return these or extract them from the parameter list?
     
    292307        // Build layout function signature.
    293308        LayoutData layout = buildLayoutFunction(
    294                 location, decl, sizedParams, isInFunction(), false );
     309                location, decl, sizedParams, isInFunction(), false, transUnit() );
    295310        ast::FunctionDecl * layoutDecl = layout.function;
    296311        // Also return these or extract them from the parameter list?
     
    13901405/// * Move polymorphic returns in function types to pointer-type parameters.
    13911406/// * Adds type size and assertion parameters to parameter lists.
    1392 struct DeclAdapter final {
     1407struct DeclAdapter final :
     1408                public ast::WithConstTranslationUnit {
    13931409        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl );
    13941410        ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl );
     
    13981414
    13991415ast::ObjectDecl * makeObj(
    1400                 CodeLocation const & location, std::string const & name ) {
     1416                CodeLocation const & location, std::string const & name,
     1417                const ast::TranslationUnit & transUnit ) {
    14011418        // The size/align parameters may be unused, so add the unused attribute.
    14021419        return new ast::ObjectDecl( location, name,
    1403                 makeLayoutCType(),
     1420                getLayoutCType( transUnit ),
    14041421                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
    14051422                { new ast::Attribute( "unused" ) } );
     
    14411458                        std::string paramName = Mangle::mangleType( &paramType );
    14421459
    1443                         auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) );
     1460                        auto sizeParam = makeObj(
     1461                                typeParam->location, sizeofName( paramName ), transUnit() );
    14441462                        layoutParams.emplace_back( sizeParam );
    14451463
    1446                         auto alignParam = makeObj( typeParam->location, alignofName( paramName ) );
     1464                        auto alignParam = makeObj(
     1465                                typeParam->location, alignofName( paramName ), transUnit() );
    14471466                        layoutParams.emplace_back( alignParam );
    14481467                }
     
    15761595/// * Inserts dynamic calculation of polymorphic type layouts where needed.
    15771596struct PolyGenericCalculator final :
    1578                 public ast::WithConstTypeSubstitution,
     1597                public ast::WithConstTranslationUnit,
     1598                public ast::WithConstTypeSubstitution,
    15791599                public ast::WithDeclsToAdd,
    15801600                public ast::WithGuards,
     
    16931713
    16941714        ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location,
    1695                 sizeofName( typeName ), makeLayoutCType(),
     1715                sizeofName( typeName ), getLayoutCType( transUnit() ),
    16961716                new ast::SingleInit( decl->location,
    16971717                        new ast::SizeofExpr( decl->location, deepCopy( base ) )
     
    16991719        );
    17001720        ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location,
    1701                 alignofName( typeName ), makeLayoutCType(),
     1721                alignofName( typeName ), getLayoutCType( transUnit() ),
    17021722                new ast::SingleInit( decl->location,
    17031723                        new ast::AlignofExpr( decl->location, deepCopy( base ) )
     
    19872007        auto offsetArray = makeVar( expr->location, offsetName,
    19882008                new ast::ArrayType(
    1989                         makeLayoutType(),
     2009                        getLayoutType( transUnit() ),
    19902010                        ast::ConstantExpr::from_ulong( expr->location, inits.size() ),
    19912011                        ast::FixedLen,
     
    20712091                        // All empty structures have the same layout (size 1, align 1).
    20722092                        makeVar( location,
    2073                                 sizeofName( typeName ), makeLayoutType(),
     2093                                sizeofName( typeName ), getLayoutType( transUnit() ),
    20742094                                new ast::SingleInit( location,
    20752095                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
    20762096                        makeVar( location,
    2077                                 alignofName( typeName ), makeLayoutType(),
     2097                                alignofName( typeName ), getLayoutType( transUnit() ),
    20782098                                new ast::SingleInit( location,
    20792099                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
     
    20812101                } else {
    20822102                        ast::ObjectDecl const * sizeofVar = makeVar( location,
    2083                                 sizeofName( typeName ), makeLayoutType(), nullptr );
     2103                                sizeofName( typeName ), getLayoutType( transUnit() ), nullptr );
    20842104                        ast::ObjectDecl const * alignofVar = makeVar( location,
    2085                                 alignofName( typeName ), makeLayoutType(), nullptr );
     2105                                alignofName( typeName ), getLayoutType( transUnit() ), nullptr );
    20862106                        ast::ObjectDecl const * offsetofVar = makeVar( location,
    20872107                                offsetofName( typeName ),
    20882108                                new ast::ArrayType(
    2089                                         makeLayoutType(),
     2109                                        getLayoutType( transUnit() ),
    20902110                                        ast::ConstantExpr::from_int( location, memberCount ),
    20912111                                        ast::FixedLen,
     
    21332153
    21342154                ast::ObjectDecl * sizeofVar = makeVar( location,
    2135                         sizeofName( typeName ), makeLayoutType() );
     2155                        sizeofName( typeName ), getLayoutType( transUnit() ) );
    21362156                ast::ObjectDecl * alignofVar = makeVar( location,
    2137                         alignofName( typeName ), makeLayoutType() );
     2157                        alignofName( typeName ), getLayoutType( transUnit() ) );
    21382158
    21392159                ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location,
  • tests/.expect/functions.x86.txt

    r8893ad4 r58eb9250  
    123123struct _tuple2_ {
    124124};
    125 static inline void _layoutof__tuple2_(unsigned long int *_sizeof__tuple2_, unsigned long int *_alignof__tuple2_, unsigned long int *_offsetof__tuple2_, const unsigned long int _sizeof_Y15tuple_param_2_0, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_2_0, const unsigned long int _sizeof_Y15tuple_param_2_1, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_2_1){
     125static inline void _layoutof__tuple2_(unsigned int *_sizeof__tuple2_, unsigned int *_alignof__tuple2_, unsigned int *_offsetof__tuple2_, const unsigned int _sizeof_Y15tuple_param_2_0, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_2_0, const unsigned int _sizeof_Y15tuple_param_2_1, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_2_1){
    126126    ((void)((*_sizeof__tuple2_)=0));
    127127    ((void)((*_alignof__tuple2_)=1));
     
    160160struct _tuple3_ {
    161161};
    162 static inline void _layoutof__tuple3_(unsigned long int *_sizeof__tuple3_, unsigned long int *_alignof__tuple3_, unsigned long int *_offsetof__tuple3_, const unsigned long int _sizeof_Y15tuple_param_3_0, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_3_0, const unsigned long int _sizeof_Y15tuple_param_3_1, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_3_1, const unsigned long int _sizeof_Y15tuple_param_3_2, __attribute__ ((unused)) const unsigned long int _alignof_Y15tuple_param_3_2){
     162static inline void _layoutof__tuple3_(unsigned int *_sizeof__tuple3_, unsigned int *_alignof__tuple3_, unsigned int *_offsetof__tuple3_, const unsigned int _sizeof_Y15tuple_param_3_0, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_0, const unsigned int _sizeof_Y15tuple_param_3_1, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_1, const unsigned int _sizeof_Y15tuple_param_3_2, __attribute__ ((unused)) const unsigned int _alignof_Y15tuple_param_3_2){
    163163    ((void)((*_sizeof__tuple3_)=0));
    164164    ((void)((*_alignof__tuple3_)=1));
  • tests/nowarn/zero-thunk.cfa

    r8893ad4 r58eb9250  
    66forall( T )
    77T g( zero_t ) {
    8     printf( "%ld\n", sizeof(T) );
     8    printf( "%zd\n", sizeof(T) );
    99    return (T){};
    1010}
Note: See TracChangeset for help on using the changeset viewer.