Changeset a0ad7dc for src


Ignore:
Timestamp:
Apr 7, 2017, 6:12:50 PM (8 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
d919f47
Parents:
3a48e283
git-author:
Aaron Moss <a3moss@…> (04/07/17 18:12:21)
git-committer:
Aaron Moss <a3moss@…> (04/07/17 18:12:50)
Message:

Switch alloca's to VLA's for safe in-loop use

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r3a48e283 ra0ad7dc  
    3434#include "Parser/ParseNode.h"
    3535
     36#include "SynTree/Attribute.h"
    3637#include "SynTree/Constant.h"
    3738#include "SynTree/Declaration.h"
     
    165166                        using Parent::mutate;
    166167
     168                        PolyGenericCalculator();
     169
    167170                        template< typename DeclClass >
    168171                        DeclClass *handleDecl( DeclClass *decl, Type *type );
     
    198201                        ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
    199202                        ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName
     203                        UniqueName bufNamer;                           ///< Namer for VLA buffers
    200204                };
    201205
     
    14521456////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
    14531457
     1458                PolyGenericCalculator::PolyGenericCalculator()
     1459                        : Parent(), knownLayouts(), knownOffsets(), bufNamer( "_buf" ) {}
     1460
    14541461                void PolyGenericCalculator::beginTypeScope( Type *ty ) {
    14551462                        scopeTyVars.beginScope();
     
    15281535                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    15291536                                if ( findGeneric( objectDecl->get_type() ) ) {
    1530                                         // change initialization of a polymorphic value object
    1531                                         // to allocate storage with alloca
     1537                                        // change initialization of a polymorphic value object to allocate via a VLA
     1538                                        // (alloca was previously used, but can't be safely used in loops)
    15321539                                        Type *declType = objectDecl->get_type();
    1533                                         UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    1534                                         alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) );
     1540                                        std::string bufName = bufNamer.newName();
     1541                                        ObjectDecl *newBuf = new ObjectDecl( bufName, Type::StorageClasses(), LinkageSpec::C, 0,
     1542                                                new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Kind::Char), new NameExpr( sizeofName( mangleType(declType) ) ),
     1543                                                true, false, std::list<Attribute*>{ new Attribute( std::string{"aligned"}, std::list<Expression*>{ new ConstantExpr( Constant::from_int(8) ) } ) } ), 0 );
     1544                                        stmtsToAdd.push_back( new DeclStmt( noLabels, newBuf ) );
    15351545
    15361546                                        delete objectDecl->get_init();
    15371547
    1538                                         std::list<Expression*> designators;
    1539                                         objectDecl->set_init( new SingleInit( alloc, designators, false ) ); // not constructed
     1548                                        objectDecl->set_init( new SingleInit( new NameExpr( bufName ) ) );
    15401549                                }
    15411550                        }
Note: See TracChangeset for help on using the changeset viewer.