Changeset 2f8d351 for src/GenPoly


Ignore:
Timestamp:
Dec 4, 2023, 10:22:47 AM (7 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
8a4e472
Parents:
4dc3b8c (diff), c4b9fa9 (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

    r4dc3b8c r2f8d351  
    4040namespace {
    4141
     42/// The layout type is used to represent sizes, alignments and offsets.
     43ast::BasicType * makeLayoutType() {
     44        return new ast::BasicType( ast::BasicType::LongUnsignedInt );
     45}
     46
     47/// Fixed version of layout type (just adding a 'C' in C++ style).
     48ast::BasicType * makeLayoutCType() {
     49        return new ast::BasicType( ast::BasicType::LongUnsignedInt,
     50                ast::CV::Qualifiers( ast::CV::Const ) );
     51}
     52
    4253// --------------------------------------------------------------------------
    4354/// Adds layout-generation functions to polymorphic types.
     
    6071        }
    6172        return sizedParams;
    62 }
    63 
    64 ast::BasicType * makeSizeAlignType() {
    65         return new ast::BasicType( ast::BasicType::LongUnsignedInt );
    6673}
    6774
     
    7683                        sizedParam->location,
    7784                        sizeofName( paramName ),
    78                         makeSizeAlignType()
     85                        makeLayoutCType()
    7986                ) );
    8087                params.emplace_back( new ast::ObjectDecl(
    8188                        sizedParam->location,
    8289                        alignofName( paramName ),
    83                         makeSizeAlignType()
     90                        makeLayoutCType()
    8491                ) );
    8592        }
    8693}
    8794
    88 ast::Type * makeSizeAlignOutType() {
    89         return new ast::PointerType( makeSizeAlignType() );
     95ast::Type * makeLayoutOutType() {
     96        return new ast::PointerType( makeLayoutType() );
    9097}
    9198
     
    104111                location,
    105112                sizeofName( aggr->name ),
    106                 makeSizeAlignOutType()
     113                makeLayoutOutType()
    107114        );
    108115        ast::ObjectDecl * alignParam = new ast::ObjectDecl(
    109116                location,
    110117                alignofName( aggr->name ),
    111                 makeSizeAlignOutType()
     118                makeLayoutOutType()
    112119        );
    113120        ast::ObjectDecl * offsetParam = nullptr;
     
    117124                        location,
    118125                        offsetofName( aggr->name ),
    119                         makeSizeAlignOutType()
     126                        makeLayoutOutType()
    120127                );
    121128                params.push_back( offsetParam );
     
    13721379};
    13731380
    1374 // size/align/offset parameters may not be used, so add the unused attribute.
    13751381ast::ObjectDecl * makeObj(
    13761382                CodeLocation const & location, std::string const & name ) {
     1383        // The size/align parameters may be unused, so add the unused attribute.
    13771384        return new ast::ObjectDecl( location, name,
    1378                 makeSizeAlignType(),
     1385                makeLayoutCType(),
    13791386                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
    13801387                { new ast::Attribute( "unused" ) } );
     1388}
     1389
     1390/// A modified and specialized version of ast::add_qualifiers.
     1391ast::Type const * addConst( ast::Type const * type ) {
     1392        ast::CV::Qualifiers cvq = { ast::CV::Const };
     1393        if ( ( type->qualifiers & cvq ) != 0 ) return type;
     1394        auto mutType = ast::mutate( type );
     1395        mutType->qualifiers |= cvq;
     1396        return mutType;
    13811397}
    13821398
     
    14181434        }
    14191435        for ( ast::ptr<ast::DeclWithType> & assert : mutDecl->assertions ) {
     1436                ast::DeclWithType * mutAssert = ast::mutate( assert.get() );
    14201437                // Assertion parameters may not be used in body,
    14211438                // pass along with unused attribute.
    1422                 assert.get_and_mutate()->attributes.push_back(
    1423                         new ast::Attribute( "unused" ) );
    1424                 inferredParams.push_back( assert );
     1439                mutAssert->attributes.push_back( new ast::Attribute( "unused" ) );
     1440                mutAssert->set_type( addConst( mutAssert->get_type() ) );
     1441                inferredParams.emplace_back( mutAssert );
    14251442        }
    14261443        mutDecl->assertions.clear();
     
    16091626
    16101627/// Converts polymorphic type into a suitable monomorphic representation.
    1611 /// Currently: __attribute__((aligned(8) )) char[size_T];
     1628/// Currently: __attribute__(( aligned(8) )) char[size_T];
    16121629ast::Type * polyToMonoType( CodeLocation const & location,
    16131630                ast::Type const * declType ) {
     
    16151632        auto size = new ast::NameExpr( location,
    16161633                sizeofName( Mangle::mangleType( declType ) ) );
    1617         auto aligned = new ast::Attribute( "aligned",
    1618                 { ast::ConstantExpr::from_int( location, 8 ) } );
    16191634        auto ret = new ast::ArrayType( charType, size,
    16201635                ast::VariableLen, ast::DynamicDim, ast::CV::Qualifiers() );
    1621         ret->attributes.push_back( aligned );
     1636        ret->attributes.emplace_back( new ast::Attribute( "aligned",
     1637                { ast::ConstantExpr::from_int( location, 8 ) } ) );
    16221638        return ret;
    16231639}
     
    16451661        ast::TypeInstType inst( decl->name, decl );
    16461662        std::string typeName = Mangle::mangleType( &inst );
    1647         ast::Type * layoutType = new ast::BasicType(
    1648                 ast::BasicType::LongUnsignedInt );
    16491663
    16501664        ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location,
    1651                 sizeofName( typeName ), layoutType,
     1665                sizeofName( typeName ), makeLayoutCType(),
    16521666                new ast::SingleInit( decl->location,
    16531667                        new ast::SizeofExpr( decl->location, deepCopy( base ) )
     
    16551669        );
    16561670        ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location,
    1657                 alignofName( typeName ), layoutType,
     1671                alignofName( typeName ), makeLayoutCType(),
    16581672                new ast::SingleInit( decl->location,
    16591673                        new ast::AlignofExpr( decl->location, deepCopy( base ) )
     
    17521766long findMember( ast::DeclWithType const * memberDecl,
    17531767                const ast::vector<ast::Decl> & baseDecls ) {
    1754         for ( auto pair : enumerate( baseDecls ) ) {
    1755                 if ( isMember( memberDecl, pair.val.get() ) ) {
    1756                         return pair.idx;
     1768        for ( auto const & [index, value] : enumerate( baseDecls ) ) {
     1769                if ( isMember( memberDecl, value.get() ) ) {
     1770                        return index;
    17571771                }
    17581772        }
     
    19121926        knownOffsets.insert( offsetName );
    19131927
    1914         auto baseMembers = type->base->members;
    1915         ast::Type const * offsetType = new ast::BasicType(
    1916                 ast::BasicType::LongUnsignedInt );
    1917 
    19181928        // Build initializer list for offset array.
    19191929        ast::vector<ast::Init> inits;
    1920         for ( ast::ptr<ast::Decl> & member : baseMembers ) {
     1930        for ( ast::ptr<ast::Decl> const & member : type->base->members ) {
    19211931                auto memberDecl = member.as<ast::DeclWithType>();
    19221932                assertf( memberDecl, "Requesting offset of non-DWT member: %s",
     
    19321942        auto offsetArray = makeVar( expr->location, offsetName,
    19331943                new ast::ArrayType(
    1934                         offsetType,
    1935                         ast::ConstantExpr::from_ulong( expr->location, baseMembers.size() ),
     1944                        makeLayoutType(),
     1945                        ast::ConstantExpr::from_ulong( expr->location, inits.size() ),
    19361946                        ast::FixedLen,
    19371947                        ast::DynamicDim
     
    20122022                // parameters to the layout call.
    20132023                knownLayouts.insert( typeName );
    2014                 ast::Type const * layoutType = makeSizeAlignType();
    20152024
    20162025                int memberCount = inst->base->members.size();
     
    20182027                        // All empty structures have the same layout (size 1, align 1).
    20192028                        makeVar( location,
    2020                                 sizeofName( typeName ), layoutType,
     2029                                sizeofName( typeName ), makeLayoutType(),
    20212030                                new ast::SingleInit( location,
    20222031                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
    20232032                        makeVar( location,
    2024                                 alignofName( typeName ), ast::deepCopy( layoutType ),
     2033                                alignofName( typeName ), makeLayoutType(),
    20252034                                new ast::SingleInit( location,
    20262035                                                ast::ConstantExpr::from_ulong( location, 1 ) ) );
     
    20282037                } else {
    20292038                        ast::ObjectDecl const * sizeofVar = makeVar( location,
    2030                                 sizeofName( typeName ), deepCopy( layoutType ), nullptr );
     2039                                sizeofName( typeName ), makeLayoutType(), nullptr );
    20312040                        ast::ObjectDecl const * alignofVar = makeVar( location,
    2032                                 alignofName( typeName ), deepCopy( layoutType ), nullptr );
     2041                                alignofName( typeName ), makeLayoutType(), nullptr );
    20332042                        ast::ObjectDecl const * offsetofVar = makeVar( location,
    20342043                                offsetofName( typeName ),
    20352044                                new ast::ArrayType(
    2036                                         layoutType,
     2045                                        makeLayoutType(),
    20372046                                        ast::ConstantExpr::from_int( location, memberCount ),
    20382047                                        ast::FixedLen,
     
    20782087                // parameters to the layout call.
    20792088                knownLayouts.insert( typeName );
    2080                 ast::Type const * layoutType = makeSizeAlignType();
    20812089
    20822090                ast::ObjectDecl * sizeofVar = makeVar( location,
    2083                         sizeofName( typeName ), layoutType );
     2091                        sizeofName( typeName ), makeLayoutType() );
    20842092                ast::ObjectDecl * alignofVar = makeVar( location,
    2085                         alignofName( typeName ), ast::deepCopy( layoutType ) );
     2093                        alignofName( typeName ), makeLayoutType() );
    20862094
    20872095                ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location,
Note: See TracChangeset for help on using the changeset viewer.