Changeset cdec5af for src/GenPoly/Box.cc


Ignore:
Timestamp:
Dec 14, 2015, 1:54:46 PM (9 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
56fcd77
Parents:
63db3d76
Message:

Added LayoutStructDecls? to box pass

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r63db3d76 rcdec5af  
    1414//
    1515
     16#include <map>
    1617#include <set>
     18#include <sstream>
    1719#include <stack>
    1820#include <string>
     
    2022#include <algorithm>
    2123#include <cassert>
     24#include <utility>
    2225
    2326#include "Box.h"
     
    2831#include "Parser/ParseNode.h"
    2932
     33#include "SynTree/Constant.h"
    3034#include "SynTree/Type.h"
    3135#include "SynTree/Expression.h"
     
    5054                FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
    5155
     56                /// Cache for layout struct decls
     57                class LayoutStructDecls {
     58                public:
     59                        /// Gets the layout struct declaration for a given number of members (0 for non-generic); creates declarations as needed.
     60                        /// Declarations are allocated with "new", not freed on LayoutStructDecls destruction
     61                        StructDecl* get( int members );
     62                        /// adds all struct declarations to the translation unit
     63                        void addToTranslationUnit( std::list< Declaration* >& translationUnit );
     64                private:
     65                        /// Layout struct declarations, indexed by number of members in the generic struct (0 for non-generic)
     66                        std::map< int, StructDecl* > decls;
     67                };
     68
    5269                /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call
    5370                class Pass1 : public PolyMutator {
    5471                  public:
    55                         Pass1();
     72                        Pass1( LayoutStructDecls &layoutDecls );
    5673                        virtual Expression *mutate( ApplicationExpr *appExpr );
    5774                        virtual Expression *mutate( AddressExpr *addrExpr );
     
    87104                        bool useRetval;
    88105                        UniqueName tempNamer;
     106                        LayoutStructDecls &layoutDecls;
    89107                };
    90108
     
    92110                class Pass2 : public PolyMutator {
    93111                  public:
    94                         Pass2();
     112                        Pass2( LayoutStructDecls &layoutDecls );
    95113                        template< typename DeclClass >
    96114                        DeclClass *handleDecl( DeclClass *decl, Type *type );
     
    105123 
    106124                        std::map< UniqueId, std::string > adapterName;
     125                        LayoutStructDecls &layoutDecls;
    107126                };
    108127
     
    110129                class Pass3 : public PolyMutator {
    111130                  public:
     131                        Pass3( LayoutStructDecls &layoutDecls );
    112132                        template< typename DeclClass >
    113133                        DeclClass *handleDecl( DeclClass *decl, Type *type );
     
    120140                        virtual Type *mutate( FunctionType *funcType );
    121141                  private:
     142                        LayoutStructDecls &layoutDecls;
    122143                };
    123144
     
    134155
    135156        void box( std::list< Declaration *>& translationUnit ) {
    136                 Pass1 pass1;
    137                 Pass2 pass2;
    138                 Pass3 pass3;
     157                LayoutStructDecls layoutStructs;
     158                Pass1 pass1( layoutStructs );
     159                Pass2 pass2( layoutStructs );
     160                Pass3 pass3( layoutStructs );
    139161                mutateAll( translationUnit, pass1 );
    140162                mutateAll( translationUnit, pass2 );
    141163                mutateAll( translationUnit, pass3 );
     164                layoutStructs.addToTranslationUnit( translationUnit );
     165        }
     166
     167        //////////////////////////////////// LayoutStructDecls //////////////////////////////////////////////
     168
     169        namespace {
     170                StructDecl* LayoutStructDecls::get( int members ) {
     171                        // Attempt to read declaration already in map
     172                        std::map< int, StructDecl* >::iterator decl = decls.find( members );
     173                        if ( decl != decls.end() ) return decl->second;
     174
     175                        // Insert if not found
     176                        std::string member_str;
     177                        if ( members > 0 ) {
     178                                std::stringstream ss;
     179                                ss << members;
     180                                member_str = ss.str();
     181                        }
     182                        StructDecl *newDecl = new StructDecl( std::string("_layout") + member_str );
     183
     184                        newDecl->get_members().push_back(
     185                                        new ObjectDecl( "size", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) );
     186                        newDecl->get_members().push_back(
     187                                        new ObjectDecl( "align", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) );
     188                        if ( members > 0 ) {
     189                                ArrayType *aType =
     190                                                new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ),
     191                                                                           new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), member_str ) ), false, false );
     192                                newDecl->get_members().push_back(
     193                                        new ObjectDecl( "offsets", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, aType, 0 ) );
     194                        }
     195
     196                        decls.insert( std::make_pair( members, newDecl ) );
     197
     198                        return newDecl;
     199                }
     200
     201                void LayoutStructDecls::addToTranslationUnit( std::list< Declaration* >& translationUnit ) {
     202                        for ( std::map< int, StructDecl* >::reverse_iterator decl = decls.rbegin(); decl != decls.rend(); ++decl ) {
     203                                translationUnit.push_front( decl->second );
     204                        }
     205                }
    142206        }
    143207
     
    181245                }
    182246
    183                 Pass1::Pass1()
    184                         : useRetval( false ), tempNamer( "_temp" ) {
     247                Pass1::Pass1( LayoutStructDecls &layoutDecls )
     248                        : useRetval( false ), tempNamer( "_temp" ), layoutDecls( layoutDecls ) {
    185249                        adapters.push(AdapterMap());
    186250                }
     
    9361000////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
    9371001
    938                 Pass2::Pass2() {}
     1002                Pass2::Pass2( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {}
    9391003
    9401004                void Pass2::addAdapters( FunctionType *functionType ) {
     
    10401104////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
    10411105
     1106                Pass3::Pass3( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {}
     1107
    10421108                template< typename DeclClass >
    10431109                DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
Note: See TracChangeset for help on using the changeset viewer.