Changeset 1dd1bd2


Ignore:
Timestamp:
Oct 3, 2018, 2:22:44 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
588c2b0
Parents:
04bdc26
Message:

Add var count and type specialization costs to resolver model

Location:
src
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • src/Makefile.am

    r04bdc26 r1dd1bd2  
    128128  ResolvExpr/Resolver.cc \
    129129  ResolvExpr/ResolveTypeof.cc \
     130  ResolvExpr/SpecCost.cc \
    130131  ResolvExpr/TypeEnvironment.cc \
    131132  ResolvExpr/Unify.cc \
  • src/Makefile.in

    r04bdc26 r1dd1bd2  
    205205        ResolvExpr/RenameVars.$(OBJEXT) ResolvExpr/Resolver.$(OBJEXT) \
    206206        ResolvExpr/ResolveTypeof.$(OBJEXT) \
     207        ResolvExpr/SpecCost.$(OBJEXT) \
    207208        ResolvExpr/TypeEnvironment.$(OBJEXT) \
    208209        ResolvExpr/Unify.$(OBJEXT) SymTab/Autogen.$(OBJEXT) \
     
    259260        ResolvExpr/TypeEnvironment.$(OBJEXT) \
    260261        ResolvExpr/CurrentObject.$(OBJEXT) \
    261         ResolvExpr/ExplodedActual.$(OBJEXT) SymTab/Indexer.$(OBJEXT) \
     262        ResolvExpr/ExplodedActual.$(OBJEXT) \
     263        ResolvExpr/SpecCost.$(OBJEXT) SymTab/Indexer.$(OBJEXT) \
    262264        SymTab/Mangler.$(OBJEXT) SymTab/ManglerCommon.$(OBJEXT) \
    263265        SymTab/Validate.$(OBJEXT) SymTab/FixFunction.$(OBJEXT) \
     
    547549        ResolvExpr/Occurs.cc ResolvExpr/TypeEnvironment.cc \
    548550        ResolvExpr/CurrentObject.cc ResolvExpr/ExplodedActual.cc \
    549         SymTab/Indexer.cc SymTab/Mangler.cc SymTab/ManglerCommon.cc \
    550         SymTab/Validate.cc SymTab/FixFunction.cc SymTab/Autogen.cc \
    551         SynTree/Type.cc SynTree/VoidType.cc SynTree/BasicType.cc \
     551        ResolvExpr/SpecCost.cc SymTab/Indexer.cc SymTab/Mangler.cc \
     552        SymTab/ManglerCommon.cc SymTab/Validate.cc \
     553        SymTab/FixFunction.cc SymTab/Autogen.cc SynTree/Type.cc \
     554        SynTree/VoidType.cc SynTree/BasicType.cc \
    552555        SynTree/PointerType.cc SynTree/ArrayType.cc \
    553556        SynTree/ReferenceType.cc SynTree/FunctionType.cc \
     
    656659  ResolvExpr/Resolver.cc \
    657660  ResolvExpr/ResolveTypeof.cc \
     661  ResolvExpr/SpecCost.cc \
    658662  ResolvExpr/TypeEnvironment.cc \
    659663  ResolvExpr/Unify.cc \
     
    908912ResolvExpr/ResolveTypeof.$(OBJEXT): ResolvExpr/$(am__dirstamp) \
    909913        ResolvExpr/$(DEPDIR)/$(am__dirstamp)
     914ResolvExpr/SpecCost.$(OBJEXT): ResolvExpr/$(am__dirstamp) \
     915        ResolvExpr/$(DEPDIR)/$(am__dirstamp)
    910916ResolvExpr/TypeEnvironment.$(OBJEXT): ResolvExpr/$(am__dirstamp) \
    911917        ResolvExpr/$(DEPDIR)/$(am__dirstamp)
     
    11601166@AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/ResolveTypeof.Po@am__quote@
    11611167@AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/Resolver.Po@am__quote@
     1168@AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/SpecCost.Po@am__quote@
    11621169@AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/TypeEnvironment.Po@am__quote@
    11631170@AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/Unify.Po@am__quote@
  • src/ResolvExpr/AlternativeFinder.cc

    r04bdc26 r1dd1bd2  
    410410        Cost computeApplicationConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) {
    411411                ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( alt.expr );
    412                 PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
    413                 FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() );
     412                PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->function->result );
     413                FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->base );
    414414
    415415                Cost convCost = Cost::zero;
    416                 std::list< DeclarationWithType* >& formals = function->get_parameters();
     416                std::list< DeclarationWithType* >& formals = function->parameters;
    417417                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    418                 std::list< Expression* >& actuals = appExpr->get_args();
    419 
    420                 for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    421                         Type * actualType = (*actualExpr)->get_result();
     418                std::list< Expression* >& actuals = appExpr->args;
     419
     420                for ( Expression*& actualExpr : actuals ) {
     421                        Type * actualType = actualExpr->result;
    422422                        PRINT(
    423423                                std::cerr << "actual expression:" << std::endl;
    424                                 (*actualExpr)->print( std::cerr, 8 );
     424                                actualExpr->print( std::cerr, 8 );
    425425                                std::cerr << "--- results are" << std::endl;
    426426                                actualType->print( std::cerr, 8 );
    427427                        )
    428428                        if ( formal == formals.end() ) {
    429                                 if ( function->get_isVarArgs() ) {
     429                                if ( function->isVarArgs ) {
    430430                                        convCost.incUnsafe();
    431431                                        PRINT( std::cerr << "end of formals with varargs function: inc unsafe: " << convCost << std::endl; ; )
    432432                                        // convert reference-typed expressions to value-typed expressions
    433                                         referenceToRvalueConversion( *actualExpr, convCost );
     433                                        referenceToRvalueConversion( actualExpr, convCost );
    434434                                        continue;
    435435                                } else {
     
    437437                                }
    438438                        }
    439                         if ( DefaultArgExpr * def = dynamic_cast< DefaultArgExpr * >( *actualExpr ) ) {
     439                        if ( DefaultArgExpr * def = dynamic_cast< DefaultArgExpr * >( actualExpr ) ) {
    440440                                // default arguments should be free - don't include conversion cost.
    441441                                // Unwrap them here because they are not relevant to the rest of the system.
    442                                 *actualExpr = def->expr;
     442                                actualExpr = def->expr;
    443443                                ++formal;
    444444                                continue;
    445445                        }
     446                        // mark conversion cost to formal and also specialization cost of formal type
    446447                        Type * formalType = (*formal)->get_type();
    447                         convCost += computeExpressionConversionCost( *actualExpr, formalType, indexer, alt.env );
     448                        convCost += computeExpressionConversionCost( actualExpr, formalType, indexer, alt.env );
     449                        convCost.decSpec( specCost( formalType ) );
    448450                        ++formal; // can't be in for-loop update because of the continue
    449451                }
     
    452454                }
    453455
     456                // mark specialization cost of return types
     457                for ( DeclarationWithType* returnVal : function->returnVals ) {
     458                        convCost.decSpec( specCost( returnVal->get_type() ) );
     459                }
     460
     461                // mark type variable and specialization cost of forall clause
     462                convCost.incVar( function->forall.size() );
     463                for ( TypeDecl* td : function->forall ) {
     464                        convCost.decSpec( td->assertions.size() );
     465                }
     466
     467                // xxx -- replace with new costs in resolver
    454468                for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) {
    455469                        convCost += computeConversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env );
  • src/ResolvExpr/ConversionCost.cc

    r04bdc26 r1dd1bd2  
    2828
    2929namespace ResolvExpr {
    30         const Cost Cost::zero =      Cost(  0,  0,  0,  0 );
    31         const Cost Cost::infinity =  Cost( -1, -1, -1, -1 );
    32         const Cost Cost::unsafe =    Cost(  1,  0,  0,  0 );
    33         const Cost Cost::poly =      Cost(  0,  1,  0,  0 );
    34         const Cost Cost::safe =      Cost(  0,  0,  1,  0 );
    35         const Cost Cost::reference = Cost(  0,  0,  0,  1 );
     30        const Cost Cost::zero =      Cost{  0,  0,  0,  0,  0,  0 };
     31        const Cost Cost::infinity =  Cost{ -1, -1, -1,  1, -1, -1 };
     32        const Cost Cost::unsafe =    Cost{  1,  0,  0,  0,  0,  0 };
     33        const Cost Cost::poly =      Cost{  0,  1,  0,  0,  0,  0 };
     34        const Cost Cost::var =       Cost{  0,  0,  1,  0,  0,  0 };
     35        const Cost Cost::spec =      Cost{  0,  0,  0, -1,  0,  0 };
     36        const Cost Cost::safe =      Cost{  0,  0,  0,  0,  1,  0 };
     37        const Cost Cost::reference = Cost{  0,  0,  0,  0,  0,  1 };
    3638
    3739#if 0
  • src/ResolvExpr/Cost.h

    r04bdc26 r1dd1bd2  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 09:39:50 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 22 09:35:55 2017
    13 // Update Count     : 5
     11// Last Modified By : Aaron B. Moss
     12// Last Modified On : Tue Oct 02 14:40:00 2018
     13// Update Count     : 6
    1414//
    1515
     
    2121        class Cost {
    2222          private:
    23                 Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost );
     23                Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
     24                      int referenceCost );
    2425
    2526          public:
    2627                Cost & incUnsafe( int inc = 1 );
    2728                Cost & incPoly( int inc = 1 );
     29                Cost & incVar( int inc = 1 );
     30                Cost & decSpec( int inc = 1 );
    2831                Cost & incSafe( int inc = 1 );
    2932                Cost & incReference( int inc = 1 );
     
    3134                int get_unsafeCost() const { return unsafeCost; }
    3235                int get_polyCost() const { return polyCost; }
     36                int get_varCost() const { return varCost; }
     37                int get_specCost() const { return specCost; }
    3338                int get_safeCost() const { return safeCost; }
    3439                int get_referenceCost() const { return referenceCost; }
     
    4752                static const Cost unsafe;
    4853                static const Cost poly;
     54                static const Cost var;
     55                static const Cost spec;
    4956                static const Cost safe;
    5057                static const Cost reference;
     
    5259                int compare( const Cost &other ) const;
    5360
    54                 int unsafeCost;
    55                 int polyCost;
    56                 int safeCost;
    57                 int referenceCost;
     61                int unsafeCost;     ///< Unsafe (narrowing) conversions
     62                int polyCost;       ///< Count of parameters and return values bound to some poly type
     63                int varCost;        ///< Count of polymorphic type variables
     64                int specCost;       ///< Polymorphic type specializations (type assertions), negative cost
     65                int safeCost;       ///< Safe (widening) conversions
     66                int referenceCost;  ///< reference conversions
    5867        };
    5968
    60         inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), referenceCost( referenceCost ) {}
     69        inline Cost::Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
     70                        int referenceCost )
     71                : unsafeCost( unsafeCost ), polyCost( polyCost ), varCost( varCost ), specCost( specCost ),
     72                  safeCost( safeCost ), referenceCost( referenceCost ) {}
    6173
    6274        inline Cost & Cost::incUnsafe( int inc ) {
     
    6981                if ( *this == infinity ) return *this;
    7082                polyCost += inc;
     83                return *this;
     84        }
     85
     86        inline Cost & Cost::incVar( int inc ) {
     87                if ( *this == infinity ) return *this;
     88                varCost += inc;
     89                return *this;
     90        }
     91
     92        inline Cost& Cost::decSpec( int dec ) {
     93                if ( *this == infinity ) return *this;
     94                specCost -= dec;
    7195                return *this;
    7296        }
     
    86110        inline Cost Cost::operator+( const Cost &other ) const {
    87111                if ( *this == infinity || other == infinity ) return infinity;
    88                 return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, referenceCost + other.referenceCost );
     112                return Cost{
     113                        unsafeCost + other.unsafeCost, polyCost + other.polyCost,
     114                        varCost + other.varCost, specCost + other.specCost,
     115                        safeCost + other.safeCost, referenceCost + other.referenceCost };
    89116        }
    90117
    91118        inline Cost Cost::operator-( const Cost &other ) const {
    92119                if ( *this == infinity || other == infinity ) return infinity;
    93                 return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, referenceCost - other.referenceCost );
     120                return Cost{
     121                        unsafeCost - other.unsafeCost, polyCost - other.polyCost,
     122                        varCost - other.varCost, specCost - other.specCost,
     123                        safeCost - other.safeCost, referenceCost - other.referenceCost };
    94124        }
    95125
     
    102132                unsafeCost += other.unsafeCost;
    103133                polyCost += other.polyCost;
     134                varCost += other.varCost;
     135                specCost += other.specCost;
    104136                safeCost += other.safeCost;
    105137                referenceCost += other.referenceCost;
     
    119151                } else if ( polyCost < other.polyCost ) {
    120152                        return true;
     153                } else if ( varCost > other.varCost ) {
     154                        return false;
     155                } else if ( varCost < other.varCost ) {
     156                        return true;
     157                } else if ( specCost > other.specCost ) {
     158                        return false;
     159                } else if ( specCost > other.specCost ) {
     160                        return true;
    121161                } else if ( safeCost > other.safeCost ) {
    122162                        return false;
     
    135175                return unsafeCost == other.unsafeCost
    136176                        && polyCost == other.polyCost
     177                        && varCost == other.varCost
     178                        && specCost == other.specCost
    137179                        && safeCost == other.safeCost
    138180                        && referenceCost == other.referenceCost;
     
    144186
    145187        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
    146                 os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << ", " << cost.referenceCost << " )";
    147                 return os;
     188                return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
     189                          << cost.varCost << ", " << cost.specCost << ", "
     190                          << cost.safeCost << ", " << cost.referenceCost << " )";
    148191        }
    149192} // namespace ResolvExpr
  • src/ResolvExpr/module.mk

    r04bdc26 r1dd1bd2  
    3333       ResolvExpr/TypeEnvironment.cc \
    3434       ResolvExpr/CurrentObject.cc \
    35        ResolvExpr/ExplodedActual.cc
     35       ResolvExpr/ExplodedActual.cc \
     36       ResolvExpr/SpecCost.cc
  • src/ResolvExpr/typeops.h

    r04bdc26 r1dd1bd2  
    102102        int polyCost( Type *type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
    103103
     104        // in SpecCost.cc
     105        int specCost( Type *type );
     106
    104107        // in Occurs.cc
    105108        bool occurs( Type *type, std::string varName, const TypeEnvironment &env );
Note: See TracChangeset for help on using the changeset viewer.