Changeset d60a4c2 for src/GenPoly


Ignore:
Timestamp:
Jan 11, 2025, 5:48:46 PM (13 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master, stuck-waitfor-destruct
Children:
f886608
Parents:
7d65715f (diff), 32a119e9 (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cpp

    r7d65715f rd60a4c2  
    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 );
    8296                std::string paramName = Mangle::mangleType( &inst );
    83                 params.emplace_back( new ast::ObjectDecl(
     97                auto sizeofParam = new ast::ObjectDecl(
    8498                        sizedParam->location,
    8599                        sizeofName( paramName ),
    86                         makeLayoutCType()
    87                 ) );
    88                 auto alignParam = new ast::ObjectDecl(
     100                        getLayoutCType( transUnit )
     101                );
     102                sizeofParam->attributes.push_back( new ast::Attribute( "unused" ) );
     103                params.emplace_back( sizeofParam );
     104                auto alignofParam = new ast::ObjectDecl(
    89105                        sizedParam->location,
    90106                        alignofName( paramName ),
    91                         makeLayoutCType()
     107                        getLayoutCType( transUnit )
    92108                );
    93                 alignParam->attributes.push_back( new ast::Attribute( "unused" ) );
    94                 params.emplace_back( alignParam );
    95         }
    96 }
    97 
    98 ast::Type * makeLayoutOutType() {
    99         return new ast::PointerType( makeLayoutType() );
     109                alignofParam->attributes.push_back( new ast::Attribute( "unused" ) );
     110                params.emplace_back( alignofParam );
     111        }
     112}
     113
     114ast::Type * getLayoutOutType( const ast::TranslationUnit & transUnit ) {
     115        return new ast::PointerType( getLayoutType( transUnit ) );
    100116}
    101117
     
    110126                CodeLocation const & location, ast::AggregateDecl const * aggr,
    111127                ast::vector<ast::TypeDecl> const & sizedParams,
    112                 bool isInFunction, bool isStruct ) {
     128                bool isInFunction, bool isStruct,
     129                const ast::TranslationUnit & transUnit ) {
    113130        ast::ObjectDecl * sizeParam = new ast::ObjectDecl(
    114131                location,
    115132                sizeofName( aggr->name ),
    116                 makeLayoutOutType()
     133                getLayoutOutType( transUnit )
    117134        );
    118135        ast::ObjectDecl * alignParam = new ast::ObjectDecl(
    119136                location,
    120137                alignofName( aggr->name ),
    121                 makeLayoutOutType()
     138                getLayoutOutType( transUnit )
    122139        );
    123140        ast::ObjectDecl * offsetParam = nullptr;
     
    127144                        location,
    128145                        offsetofName( aggr->name ),
    129                         makeLayoutOutType()
     146                        getLayoutOutType( transUnit )
    130147                );
    131148                params.push_back( offsetParam );
    132149        }
    133         addSTypeParams( params, sizedParams );
     150        addSTypeParams( params, sizedParams, transUnit );
    134151
    135152        // Routines at global scope marked "static" to prevent multiple
     
    218235        // Build layout function signature.
    219236        LayoutData layout = buildLayoutFunction(
    220                 location, decl, sizedParams, isInFunction(), true );
     237                location, decl, sizedParams, isInFunction(), true, transUnit() );
    221238        ast::FunctionDecl * layoutDecl = layout.function;
    222239        // Also return these or extract them from the parameter list?
     
    292309        // Build layout function signature.
    293310        LayoutData layout = buildLayoutFunction(
    294                 location, decl, sizedParams, isInFunction(), false );
     311                location, decl, sizedParams, isInFunction(), false, transUnit() );
    295312        ast::FunctionDecl * layoutDecl = layout.function;
    296313        // Also return these or extract them from the parameter list?
     
    13901407/// * Move polymorphic returns in function types to pointer-type parameters.
    13911408/// * Adds type size and assertion parameters to parameter lists.
    1392 struct DeclAdapter final {
     1409struct DeclAdapter final :
     1410                public ast::WithConstTranslationUnit {
    13931411        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl );
    13941412        ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl );
     
    13981416
    13991417ast::ObjectDecl * makeObj(
    1400                 CodeLocation const & location, std::string const & name ) {
     1418                CodeLocation const & location, std::string const & name,
     1419                const ast::TranslationUnit & transUnit ) {
    14011420        // The size/align parameters may be unused, so add the unused attribute.
    14021421        return new ast::ObjectDecl( location, name,
    1403                 makeLayoutCType(),
     1422                getLayoutCType( transUnit ),
    14041423                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
    14051424                { new ast::Attribute( "unused" ) } );
     
    14411460                        std::string paramName = Mangle::mangleType( &paramType );
    14421461
    1443                         auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) );
     1462                        auto sizeParam = makeObj(
     1463                                typeParam->location, sizeofName( paramName ), transUnit() );
    14441464                        layoutParams.emplace_back( sizeParam );
    14451465
    1446                         auto alignParam = makeObj( typeParam->location, alignofName( paramName ) );
     1466                        auto alignParam = makeObj(
     1467                                typeParam->location, alignofName( paramName ), transUnit() );
    14471468                        layoutParams.emplace_back( alignParam );
    14481469                }
     
    15761597/// * Inserts dynamic calculation of polymorphic type layouts where needed.
    15771598struct PolyGenericCalculator final :
    1578                 public ast::WithConstTypeSubstitution,
     1599                public ast::WithConstTranslationUnit,
     1600                public ast::WithConstTypeSubstitution,
    15791601                public ast::WithDeclsToAdd,
    15801602                public ast::WithGuards,
     
    16931715
    16941716        ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location,
    1695                 sizeofName( typeName ), makeLayoutCType(),
     1717                sizeofName( typeName ), getLayoutCType( transUnit() ),
    16961718                new ast::SingleInit( decl->location,
    16971719                        new ast::SizeofExpr( decl->location, deepCopy( base ) )
     
    16991721        );
    17001722        ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location,
    1701                 alignofName( typeName ), makeLayoutCType(),
     1723                alignofName( typeName ), getLayoutCType( transUnit() ),
    17021724                new ast::SingleInit( decl->location,
    17031725                        new ast::AlignofExpr( decl->location, deepCopy( base ) )
     
    19872009        auto offsetArray = makeVar( expr->location, offsetName,
    19882010                new ast::ArrayType(
    1989                         makeLayoutType(),
     2011                        getLayoutType( transUnit() ),
    19902012                        ast::ConstantExpr::from_ulong( expr->location, inits.size() ),
    19912013                        ast::FixedLen,
     
    20712093                        // All empty structures have the same layout (size 1, align 1).
    20722094                        makeVar( location,
    2073                                 sizeofName( typeName ), makeLayoutType(),
     2095                                sizeofName( typeName ), getLayoutType( transUnit() ),
    20742096                                new ast::SingleInit( location,
    20752097                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
    20762098                        makeVar( location,
    2077                                 alignofName( typeName ), makeLayoutType(),
     2099                                alignofName( typeName ), getLayoutType( transUnit() ),
    20782100                                new ast::SingleInit( location,
    20792101                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
     
    20812103                } else {
    20822104                        ast::ObjectDecl const * sizeofVar = makeVar( location,
    2083                                 sizeofName( typeName ), makeLayoutType(), nullptr );
     2105                                sizeofName( typeName ), getLayoutType( transUnit() ), nullptr );
    20842106                        ast::ObjectDecl const * alignofVar = makeVar( location,
    2085                                 alignofName( typeName ), makeLayoutType(), nullptr );
     2107                                alignofName( typeName ), getLayoutType( transUnit() ), nullptr );
    20862108                        ast::ObjectDecl const * offsetofVar = makeVar( location,
    20872109                                offsetofName( typeName ),
    20882110                                new ast::ArrayType(
    2089                                         makeLayoutType(),
     2111                                        getLayoutType( transUnit() ),
    20902112                                        ast::ConstantExpr::from_int( location, memberCount ),
    20912113                                        ast::FixedLen,
     
    21332155
    21342156                ast::ObjectDecl * sizeofVar = makeVar( location,
    2135                         sizeofName( typeName ), makeLayoutType() );
     2157                        sizeofName( typeName ), getLayoutType( transUnit() ) );
    21362158                ast::ObjectDecl * alignofVar = makeVar( location,
    2137                         alignofName( typeName ), makeLayoutType() );
     2159                        alignofName( typeName ), getLayoutType( transUnit() ) );
    21382160
    21392161                ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location,
Note: See TracChangeset for help on using the changeset viewer.