Changeset 4cf2472 for src


Ignore:
Timestamp:
Feb 7, 2019, 4:48:10 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
deferred_resn
Children:
1cc4390
Parents:
e1f7eef
Message:

Add sign element to cost tuple

Location:
src/ResolvExpr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    re1f7eef r4cf2472  
    2828
    2929namespace ResolvExpr {
    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::safe =      Cost{  0,  0,  1,  0,  0,  0 };
    35         const Cost Cost::var =       Cost{  0,  0,  0,  1,  0,  0 };
    36         const Cost Cost::spec =      Cost{  0,  0,  0,  0, -1,  0 };
    37         const Cost Cost::reference = Cost{  0,  0,  0,  0,  0,  1 };
     30        const Cost Cost::zero =      Cost{  0,  0,  0,  0,  0,  0,  0 };
     31        const Cost Cost::infinity =  Cost{ -1, -1, -1, -1, -1,  1, -1 };
     32        const Cost Cost::unsafe =    Cost{  1,  0,  0,  0,  0,  0,  0 };
     33        const Cost Cost::poly =      Cost{  0,  1,  0,  0,  0,  0,  0 };
     34        const Cost Cost::safe =      Cost{  0,  0,  1,  0,  0,  0,  0 };
     35        const Cost Cost::sign =      Cost{  0,  0,  0,  1,  0,  0,  0 };
     36        const Cost Cost::var =       Cost{  0,  0,  0,  0,  1,  0,  0 };
     37        const Cost Cost::spec =      Cost{  0,  0,  0,  0,  0, -1,  0 };
     38        const Cost Cost::reference = Cost{  0,  0,  0,  0,  0,  0,  1 };
    3839
    3940#if 0
     
    266267        );
    267268
     269        static const int signMatrix[][ BasicType::NUMBER_OF_BASIC_TYPES ] = {
     270        /* Src \ Dest:  Bool    Char    SChar   UChar   Short   UShort  Int     UInt    Long    ULong   LLong   ULLong  Float   Double  LDbl    FCplex  DCplex  LDCplex FImag   DImag   LDImag  I128,   U128, F80, F128 */
     271                /* Bool */      { 0,    1,              1,              0,              1,              0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     272                /* Char */      { -1,   0,              -1,             1,              0,              1,              0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     273                /* SChar */ { -1,       -1,             0,              1,              0,              1,              0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     274                /* UChar */ { -1,       -1,             -1,             0,              1,              0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     275                /* Short */ { -1,       -1,             -1,             -1,             0,              1,              0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     276                /* UShort */{ -1,       -1,             -1,             -1,             -1,             0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     277                /* Int */       { -1,   -1,             -1,             -1,             -1,             -1,             0,              1,              0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     278                /* UInt */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             0,              1,              0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     279                /* Long */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     280                /* ULong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     281                /* LLong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     282                /* ULLong */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     283
     284                /* Float */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             -1,             -1,       0,    0},
     285                /* Double */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              -1,             0,              0,              -1,             -1,             -1,             -1,             -1,       0,    0},
     286                /* LDbl */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,       -1,   0},
     287                /* FCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
     288                /* DCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
     289                /* LDCplex */{ -1,      -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
     290                /* FImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              0,              0,              0,              -1,             -1,       -1,   -1},
     291                /* DImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              -1,             0,              0,              -1,             -1,       -1,   -1},
     292                /* LDImag */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             0,              -1,             -1,       -1,   -1},
     293
     294                /* I128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             0,              1,        0,    0},
     295                /* U128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              0,              0,              0,              0,              0,              -1,             -1,             -1,             1,              0,        0,    0},
     296
     297                /* F80 */       { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,       0,    0},
     298                /* F128 */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,       -1,   0},
     299        };
     300        static_assert(
     301                sizeof(signMatrix)/sizeof(signMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES,
     302                "Each basic type kind should have a corresponding row in the sign matrix"
     303        );
     304
    268305
    269306        void ConversionCost::postvisit( VoidType * ) {
     
    279316                                cost = Cost::zero;
    280317                                cost.incSafe( tableResult );
     318                                cost.incSign( signMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ] );
    281319                        } // if
    282320                } else if ( dynamic_cast< EnumInstType *>( dest ) ) {
     
    422460                                cost = Cost::zero;
    423461                                cost.incSafe( tableResult + 1 );
     462                                cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] );
    424463                        }
    425464                } else if ( dynamic_cast< PointerType* >( dest ) ) {
     
    439478                                cost = Cost::zero;
    440479                                cost.incSafe( tableResult + 1 );
     480                                cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] );
    441481                        }
    442482                }
  • src/ResolvExpr/Cost.h

    re1f7eef r4cf2472  
    2121        class Cost {
    2222          private:
    23                 Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost,
    24                         int referenceCost );
     23                Cost( int unsafeCost, int polyCost, int safeCost, int signCost,
     24                        int varCost, int specCost, int referenceCost );
    2525
    2626          public:
     
    2828                Cost & incPoly( int inc = 1 );
    2929                Cost & incSafe( int inc = 1 );
     30                Cost & incSign( int inc = 1 );
    3031                Cost & incVar( int inc = 1 );
    3132                Cost & decSpec( int inc = 1 );
     
    3536                int get_polyCost() const { return polyCost; }
    3637                int get_safeCost() const { return safeCost; }
     38                int get_signCost() const { return signCost; }
    3739                int get_varCost() const { return varCost; }
    3840                int get_specCost() const { return specCost; }
     
    4042
    4143                Cost operator+( const Cost &other ) const;
    42                 Cost operator-( const Cost &other ) const;
    4344                Cost &operator+=( const Cost &other );
    4445                bool operator<( const Cost &other ) const;
     
    5556                static const Cost poly;
    5657                static const Cost safe;
     58                static const Cost sign;
    5759                static const Cost var;
    5860                static const Cost spec;
     
    6365                int polyCost;       ///< Count of parameters and return values bound to some poly type
    6466                int safeCost;       ///< Safe (widening) conversions
     67                int signCost;       ///< Count of safe sign conversions
    6568                int varCost;        ///< Count of polymorphic type variables
    6669                int specCost;       ///< Polymorphic type specializations (type assertions), negative cost
     
    6871        };
    6972
    70         inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost,
    71                         int referenceCost )
    72                 : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), varCost( varCost ),
    73                   specCost( specCost ), referenceCost( referenceCost ) {}
     73        inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost,
     74                        int varCost, int specCost, int referenceCost )
     75                : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ),
     76                  varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {}
    7477
    7578        inline Cost & Cost::incUnsafe( int inc ) {
     
    8891                if ( *this == infinity ) return *this;
    8992                safeCost += inc;
     93                return *this;
     94        }
     95
     96        inline Cost & Cost::incSign( int inc ) {
     97                if ( *this == infinity ) return *this;
     98                signCost += inc;
    9099                return *this;
    91100        }
     
    113122                return Cost{
    114123                        unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost,
    115                         varCost + other.varCost, specCost + other.specCost,
     124                        signCost + other.signCost, varCost + other.varCost, specCost + other.specCost,
    116125                        referenceCost + other.referenceCost };
    117         }
    118 
    119         inline Cost Cost::operator-( const Cost &other ) const {
    120                 if ( *this == infinity || other == infinity ) return infinity;
    121                 return Cost{
    122                         unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost,
    123                         varCost - other.varCost, specCost - other.specCost,
    124                         referenceCost - other.referenceCost };
    125126        }
    126127
     
    134135                polyCost += other.polyCost;
    135136                safeCost += other.safeCost;
     137                signCost += other.signCost;
    136138                varCost += other.varCost;
    137139                specCost += other.specCost;
     
    156158                } else if ( safeCost < other.safeCost ) {
    157159                        return true;
     160                } else if ( signCost > other.signCost ) {
     161                        return false;
     162                } else if ( signCost < other.signCost ) {
     163                        return true;
    158164                } else if ( varCost > other.varCost ) {
    159165                        return false;
     
    180186                c = polyCost - other.polyCost; if ( c ) return c;
    181187                c = safeCost - other.safeCost; if ( c ) return c;
     188                c = signCost - other.signCost; if ( c ) return c;
    182189                c = varCost - other.varCost; if ( c ) return c;
    183190                c = specCost - other.specCost; if ( c ) return c;
     
    189196                        && polyCost == other.polyCost
    190197                        && safeCost == other.safeCost
     198                        && signCost == other.signCost
    191199                        && varCost == other.varCost
    192200                        && specCost == other.specCost
     
    200208        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
    201209                return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
    202                           << cost.safeCost << ", " << cost.varCost << ", " << cost.specCost << ", "
     210                          << cost.safeCost << ", " << cost.signCost << ", "
     211                                  << cost.varCost << ", " << cost.specCost << ", "
    203212                          << cost.referenceCost << " )";
    204213        }
Note: See TracChangeset for help on using the changeset viewer.