Changeset 4cf2472
- Timestamp:
- Feb 7, 2019, 4:48:10 PM (6 years ago)
- Branches:
- deferred_resn
- Children:
- 1cc4390
- Parents:
- e1f7eef
- Location:
- src/ResolvExpr
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ConversionCost.cc
re1f7eef r4cf2472 28 28 29 29 namespace 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 }; 38 39 39 40 #if 0 … … 266 267 ); 267 268 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 268 305 269 306 void ConversionCost::postvisit( VoidType * ) { … … 279 316 cost = Cost::zero; 280 317 cost.incSafe( tableResult ); 318 cost.incSign( signMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ] ); 281 319 } // if 282 320 } else if ( dynamic_cast< EnumInstType *>( dest ) ) { … … 422 460 cost = Cost::zero; 423 461 cost.incSafe( tableResult + 1 ); 462 cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] ); 424 463 } 425 464 } else if ( dynamic_cast< PointerType* >( dest ) ) { … … 439 478 cost = Cost::zero; 440 479 cost.incSafe( tableResult + 1 ); 480 cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] ); 441 481 } 442 482 } -
src/ResolvExpr/Cost.h
re1f7eef r4cf2472 21 21 class Cost { 22 22 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 ); 25 25 26 26 public: … … 28 28 Cost & incPoly( int inc = 1 ); 29 29 Cost & incSafe( int inc = 1 ); 30 Cost & incSign( int inc = 1 ); 30 31 Cost & incVar( int inc = 1 ); 31 32 Cost & decSpec( int inc = 1 ); … … 35 36 int get_polyCost() const { return polyCost; } 36 37 int get_safeCost() const { return safeCost; } 38 int get_signCost() const { return signCost; } 37 39 int get_varCost() const { return varCost; } 38 40 int get_specCost() const { return specCost; } … … 40 42 41 43 Cost operator+( const Cost &other ) const; 42 Cost operator-( const Cost &other ) const;43 44 Cost &operator+=( const Cost &other ); 44 45 bool operator<( const Cost &other ) const; … … 55 56 static const Cost poly; 56 57 static const Cost safe; 58 static const Cost sign; 57 59 static const Cost var; 58 60 static const Cost spec; … … 63 65 int polyCost; ///< Count of parameters and return values bound to some poly type 64 66 int safeCost; ///< Safe (widening) conversions 67 int signCost; ///< Count of safe sign conversions 65 68 int varCost; ///< Count of polymorphic type variables 66 69 int specCost; ///< Polymorphic type specializations (type assertions), negative cost … … 68 71 }; 69 72 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 ) {} 74 77 75 78 inline Cost & Cost::incUnsafe( int inc ) { … … 88 91 if ( *this == infinity ) return *this; 89 92 safeCost += inc; 93 return *this; 94 } 95 96 inline Cost & Cost::incSign( int inc ) { 97 if ( *this == infinity ) return *this; 98 signCost += inc; 90 99 return *this; 91 100 } … … 113 122 return Cost{ 114 123 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, 116 125 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 };125 126 } 126 127 … … 134 135 polyCost += other.polyCost; 135 136 safeCost += other.safeCost; 137 signCost += other.signCost; 136 138 varCost += other.varCost; 137 139 specCost += other.specCost; … … 156 158 } else if ( safeCost < other.safeCost ) { 157 159 return true; 160 } else if ( signCost > other.signCost ) { 161 return false; 162 } else if ( signCost < other.signCost ) { 163 return true; 158 164 } else if ( varCost > other.varCost ) { 159 165 return false; … … 180 186 c = polyCost - other.polyCost; if ( c ) return c; 181 187 c = safeCost - other.safeCost; if ( c ) return c; 188 c = signCost - other.signCost; if ( c ) return c; 182 189 c = varCost - other.varCost; if ( c ) return c; 183 190 c = specCost - other.specCost; if ( c ) return c; … … 189 196 && polyCost == other.polyCost 190 197 && safeCost == other.safeCost 198 && signCost == other.signCost 191 199 && varCost == other.varCost 192 200 && specCost == other.specCost … … 200 208 inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { 201 209 return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " 202 << cost.safeCost << ", " << cost.varCost << ", " << cost.specCost << ", " 210 << cost.safeCost << ", " << cost.signCost << ", " 211 << cost.varCost << ", " << cost.specCost << ", " 203 212 << cost.referenceCost << " )"; 204 213 }
Note: See TracChangeset
for help on using the changeset viewer.