Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 5 | #include <iomanip> |
| 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/types.h" |
| 8 | |
| 9 | #include "src/ostreams.h" |
| 10 | #include "src/types-inl.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | |
| 16 | // NOTE: If code is marked as being a "shortcut", this means that removing |
| 17 | // the code won't affect the semantics of the surrounding function definition. |
| 18 | |
| 19 | |
| 20 | // ----------------------------------------------------------------------------- |
| 21 | // Range-related helper functions. |
| 22 | |
| 23 | // The result may be invalid (max < min). |
| 24 | template<class Config> |
| 25 | typename TypeImpl<Config>::Limits TypeImpl<Config>::Intersect( |
| 26 | Limits lhs, Limits rhs) { |
| 27 | DisallowHeapAllocation no_allocation; |
| 28 | Limits result(lhs); |
| 29 | if (lhs.min->Number() < rhs.min->Number()) result.min = rhs.min; |
| 30 | if (lhs.max->Number() > rhs.max->Number()) result.max = rhs.max; |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | template<class Config> |
| 36 | typename TypeImpl<Config>::Limits TypeImpl<Config>::Union( |
| 37 | Limits lhs, Limits rhs) { |
| 38 | DisallowHeapAllocation no_allocation; |
| 39 | Limits result(lhs); |
| 40 | if (lhs.min->Number() > rhs.min->Number()) result.min = rhs.min; |
| 41 | if (lhs.max->Number() < rhs.max->Number()) result.max = rhs.max; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | template<class Config> |
| 47 | bool TypeImpl<Config>::Overlap( |
| 48 | typename TypeImpl<Config>::RangeType* lhs, |
| 49 | typename TypeImpl<Config>::RangeType* rhs) { |
| 50 | DisallowHeapAllocation no_allocation; |
| 51 | typename TypeImpl<Config>::Limits lim = Intersect(Limits(lhs), Limits(rhs)); |
| 52 | return lim.min->Number() <= lim.max->Number(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | template<class Config> |
| 57 | bool TypeImpl<Config>::Contains( |
| 58 | typename TypeImpl<Config>::RangeType* lhs, |
| 59 | typename TypeImpl<Config>::RangeType* rhs) { |
| 60 | DisallowHeapAllocation no_allocation; |
| 61 | return lhs->Min()->Number() <= rhs->Min()->Number() |
| 62 | && rhs->Max()->Number() <= lhs->Max()->Number(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | template<class Config> |
| 67 | bool TypeImpl<Config>::Contains( |
| 68 | typename TypeImpl<Config>::RangeType* range, i::Object* val) { |
| 69 | DisallowHeapAllocation no_allocation; |
| 70 | return IsInteger(val) |
| 71 | && range->Min()->Number() <= val->Number() |
| 72 | && val->Number() <= range->Max()->Number(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // ----------------------------------------------------------------------------- |
| 77 | // Min and Max computation. |
| 78 | |
| 79 | template<class Config> |
| 80 | double TypeImpl<Config>::Min() { |
| 81 | DCHECK(this->Is(Number())); |
| 82 | if (this->IsBitset()) return BitsetType::Min(this->AsBitset()); |
| 83 | if (this->IsUnion()) { |
| 84 | double min = +V8_INFINITY; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 85 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | min = std::min(min, this->AsUnion()->Get(i)->Min()); |
| 87 | } |
| 88 | return min; |
| 89 | } |
| 90 | if (this->IsRange()) return this->AsRange()->Min()->Number(); |
| 91 | if (this->IsConstant()) return this->AsConstant()->Value()->Number(); |
| 92 | UNREACHABLE(); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | template<class Config> |
| 98 | double TypeImpl<Config>::Max() { |
| 99 | DCHECK(this->Is(Number())); |
| 100 | if (this->IsBitset()) return BitsetType::Max(this->AsBitset()); |
| 101 | if (this->IsUnion()) { |
| 102 | double max = -V8_INFINITY; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 103 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | max = std::max(max, this->AsUnion()->Get(i)->Max()); |
| 105 | } |
| 106 | return max; |
| 107 | } |
| 108 | if (this->IsRange()) return this->AsRange()->Max()->Number(); |
| 109 | if (this->IsConstant()) return this->AsConstant()->Value()->Number(); |
| 110 | UNREACHABLE(); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // ----------------------------------------------------------------------------- |
| 116 | // Glb and lub computation. |
| 117 | |
| 118 | |
| 119 | // The largest bitset subsumed by this type. |
| 120 | template<class Config> |
| 121 | typename TypeImpl<Config>::bitset |
| 122 | TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) { |
| 123 | DisallowHeapAllocation no_allocation; |
| 124 | if (type->IsBitset()) { |
| 125 | return type->AsBitset(); |
| 126 | } else if (type->IsUnion()) { |
| 127 | SLOW_DCHECK(type->AsUnion()->Wellformed()); |
| 128 | return type->AsUnion()->Get(0)->BitsetGlb(); // Shortcut. |
| 129 | // (The remaining BitsetGlb's are None anyway). |
| 130 | } else { |
| 131 | return kNone; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // The smallest bitset subsuming this type. |
| 137 | template<class Config> |
| 138 | typename TypeImpl<Config>::bitset |
| 139 | TypeImpl<Config>::BitsetType::Lub(TypeImpl* type) { |
| 140 | DisallowHeapAllocation no_allocation; |
| 141 | if (type->IsBitset()) return type->AsBitset(); |
| 142 | if (type->IsUnion()) { |
| 143 | int bitset = kNone; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 144 | for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | bitset |= type->AsUnion()->Get(i)->BitsetLub(); |
| 146 | } |
| 147 | return bitset; |
| 148 | } |
| 149 | if (type->IsClass()) { |
| 150 | // Little hack to avoid the need for a region for handlification here... |
| 151 | return Config::is_class(type) ? Lub(*Config::as_class(type)) : |
| 152 | type->AsClass()->Bound(NULL)->AsBitset(); |
| 153 | } |
| 154 | if (type->IsConstant()) return type->AsConstant()->Bound()->AsBitset(); |
| 155 | if (type->IsRange()) return type->AsRange()->BitsetLub(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 156 | if (type->IsContext()) return kInternal & kTaggedPointer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 157 | if (type->IsArray()) return kArray; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 158 | if (type->IsFunction()) return kOtherObject; // TODO(rossberg): kFunction |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | UNREACHABLE(); |
| 160 | return kNone; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | template<class Config> |
| 165 | typename TypeImpl<Config>::bitset |
| 166 | TypeImpl<Config>::BitsetType::Lub(i::Map* map) { |
| 167 | DisallowHeapAllocation no_allocation; |
| 168 | switch (map->instance_type()) { |
| 169 | case STRING_TYPE: |
| 170 | case ONE_BYTE_STRING_TYPE: |
| 171 | case CONS_STRING_TYPE: |
| 172 | case CONS_ONE_BYTE_STRING_TYPE: |
| 173 | case SLICED_STRING_TYPE: |
| 174 | case SLICED_ONE_BYTE_STRING_TYPE: |
| 175 | case EXTERNAL_STRING_TYPE: |
| 176 | case EXTERNAL_ONE_BYTE_STRING_TYPE: |
| 177 | case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 178 | case SHORT_EXTERNAL_STRING_TYPE: |
| 179 | case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE: |
| 180 | case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 181 | return kOtherString; |
| 182 | case INTERNALIZED_STRING_TYPE: |
| 183 | case ONE_BYTE_INTERNALIZED_STRING_TYPE: |
| 184 | case EXTERNAL_INTERNALIZED_STRING_TYPE: |
| 185 | case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: |
| 186 | case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 187 | case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE: |
| 188 | case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: |
| 189 | case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: |
| 190 | return kInternalizedString; |
| 191 | case SYMBOL_TYPE: |
| 192 | return kSymbol; |
| 193 | case ODDBALL_TYPE: { |
| 194 | Heap* heap = map->GetHeap(); |
| 195 | if (map == heap->undefined_map()) return kUndefined; |
| 196 | if (map == heap->null_map()) return kNull; |
| 197 | if (map == heap->boolean_map()) return kBoolean; |
| 198 | DCHECK(map == heap->the_hole_map() || |
| 199 | map == heap->uninitialized_map() || |
| 200 | map == heap->no_interceptor_result_sentinel_map() || |
| 201 | map == heap->termination_exception_map() || |
| 202 | map == heap->arguments_marker_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 203 | return kInternal & kTaggedPointer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 204 | } |
| 205 | case HEAP_NUMBER_TYPE: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 206 | return kNumber & kTaggedPointer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 207 | case JS_VALUE_TYPE: |
| 208 | case JS_DATE_TYPE: |
| 209 | case JS_OBJECT_TYPE: |
| 210 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
| 211 | case JS_GENERATOR_OBJECT_TYPE: |
| 212 | case JS_MODULE_TYPE: |
| 213 | case JS_GLOBAL_OBJECT_TYPE: |
| 214 | case JS_BUILTINS_OBJECT_TYPE: |
| 215 | case JS_GLOBAL_PROXY_TYPE: |
| 216 | case JS_ARRAY_BUFFER_TYPE: |
| 217 | case JS_TYPED_ARRAY_TYPE: |
| 218 | case JS_DATA_VIEW_TYPE: |
| 219 | case JS_SET_TYPE: |
| 220 | case JS_MAP_TYPE: |
| 221 | case JS_SET_ITERATOR_TYPE: |
| 222 | case JS_MAP_ITERATOR_TYPE: |
| 223 | case JS_WEAK_MAP_TYPE: |
| 224 | case JS_WEAK_SET_TYPE: |
| 225 | if (map->is_undetectable()) return kUndetectable; |
| 226 | return kOtherObject; |
| 227 | case JS_ARRAY_TYPE: |
| 228 | return kArray; |
| 229 | case JS_FUNCTION_TYPE: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 230 | return kOtherObject; // TODO(rossberg): there should be a Function type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 231 | case JS_REGEXP_TYPE: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 232 | return kOtherObject; // TODO(rossberg): there should be a RegExp type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | case JS_PROXY_TYPE: |
| 234 | case JS_FUNCTION_PROXY_TYPE: |
| 235 | return kProxy; |
| 236 | case MAP_TYPE: |
| 237 | // When compiling stub templates, the meta map is used as a place holder |
| 238 | // for the actual map with which the template is later instantiated. |
| 239 | // We treat it as a kind of type variable whose upper bound is Any. |
| 240 | // TODO(rossberg): for caching of CompareNilIC stubs to work correctly, |
| 241 | // we must exclude Undetectable here. This makes no sense, really, |
| 242 | // because it means that the template isn't actually parametric. |
| 243 | // Also, it doesn't apply elsewhere. 8-( |
| 244 | // We ought to find a cleaner solution for compiling stubs parameterised |
| 245 | // over type or class variables, esp ones with bounds... |
| 246 | return kDetectable; |
| 247 | case DECLARED_ACCESSOR_INFO_TYPE: |
| 248 | case EXECUTABLE_ACCESSOR_INFO_TYPE: |
| 249 | case SHARED_FUNCTION_INFO_TYPE: |
| 250 | case ACCESSOR_PAIR_TYPE: |
| 251 | case FIXED_ARRAY_TYPE: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 252 | case BYTE_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 253 | case FOREIGN_TYPE: |
| 254 | case CODE_TYPE: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 255 | return kInternal & kTaggedPointer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | default: |
| 257 | UNREACHABLE(); |
| 258 | return kNone; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
| 263 | template<class Config> |
| 264 | typename TypeImpl<Config>::bitset |
| 265 | TypeImpl<Config>::BitsetType::Lub(i::Object* value) { |
| 266 | DisallowHeapAllocation no_allocation; |
| 267 | if (value->IsNumber()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 268 | return Lub(value->Number()) & |
| 269 | (value->IsSmi() ? kTaggedSigned : kTaggedPointer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 270 | } |
| 271 | return Lub(i::HeapObject::cast(value)->map()); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | template<class Config> |
| 276 | typename TypeImpl<Config>::bitset |
| 277 | TypeImpl<Config>::BitsetType::Lub(double value) { |
| 278 | DisallowHeapAllocation no_allocation; |
| 279 | if (i::IsMinusZero(value)) return kMinusZero; |
| 280 | if (std::isnan(value)) return kNaN; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 281 | if (IsUint32Double(value) || IsInt32Double(value)) return Lub(value, value); |
| 282 | return kPlainNumber; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | // Minimum values of regular numeric bitsets when SmiValuesAre31Bits. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 287 | template <class Config> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 288 | const typename TypeImpl<Config>::BitsetType::BitsetMin |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 289 | TypeImpl<Config>::BitsetType::BitsetMins31[] = { |
| 290 | {kOtherNumber, -V8_INFINITY}, |
| 291 | {kOtherSigned32, kMinInt}, |
| 292 | {kNegativeSignedSmall, -0x40000000}, |
| 293 | {kUnsignedSmall, 0}, |
| 294 | {kOtherUnsigned31, 0x40000000}, |
| 295 | {kOtherUnsigned32, 0x80000000}, |
| 296 | {kOtherNumber, static_cast<double>(kMaxUInt32) + 1}}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | |
| 298 | |
| 299 | // Minimum values of regular numeric bitsets when SmiValuesAre32Bits. |
| 300 | // OtherSigned32 and OtherUnsigned31 are empty (see the diagrams in types.h). |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 301 | template <class Config> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 302 | const typename TypeImpl<Config>::BitsetType::BitsetMin |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 303 | TypeImpl<Config>::BitsetType::BitsetMins32[] = { |
| 304 | {kOtherNumber, -V8_INFINITY}, |
| 305 | {kNegativeSignedSmall, kMinInt}, |
| 306 | {kUnsignedSmall, 0}, |
| 307 | {kOtherUnsigned32, 0x80000000}, |
| 308 | {kOtherNumber, static_cast<double>(kMaxUInt32) + 1}}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | template<class Config> |
| 312 | typename TypeImpl<Config>::bitset |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 313 | TypeImpl<Config>::BitsetType::Lub(double min, double max) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 315 | int lub = kNone; |
| 316 | const BitsetMin* mins = BitsetMins(); |
| 317 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 318 | // Make sure the min-max range touches 0, so we are guaranteed no holes |
| 319 | // in unions of valid bitsets. |
| 320 | if (max < -1) max = -1; |
| 321 | if (min > 0) min = 0; |
| 322 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | for (size_t i = 1; i < BitsetMinsSize(); ++i) { |
| 324 | if (min < mins[i].min) { |
| 325 | lub |= mins[i-1].bits; |
| 326 | if (max < mins[i].min) return lub; |
| 327 | } |
| 328 | } |
| 329 | return lub |= mins[BitsetMinsSize()-1].bits; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | template<class Config> |
| 334 | double TypeImpl<Config>::BitsetType::Min(bitset bits) { |
| 335 | DisallowHeapAllocation no_allocation; |
| 336 | DCHECK(Is(bits, kNumber)); |
| 337 | const BitsetMin* mins = BitsetMins(); |
| 338 | bool mz = SEMANTIC(bits & kMinusZero); |
| 339 | for (size_t i = 0; i < BitsetMinsSize(); ++i) { |
| 340 | if (Is(SEMANTIC(mins[i].bits), bits)) { |
| 341 | return mz ? std::min(0.0, mins[i].min) : mins[i].min; |
| 342 | } |
| 343 | } |
| 344 | if (mz) return 0; |
| 345 | return base::OS::nan_value(); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | template<class Config> |
| 350 | double TypeImpl<Config>::BitsetType::Max(bitset bits) { |
| 351 | DisallowHeapAllocation no_allocation; |
| 352 | DCHECK(Is(bits, kNumber)); |
| 353 | const BitsetMin* mins = BitsetMins(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 354 | bool mz = SEMANTIC(bits & kMinusZero); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 355 | if (BitsetType::Is(mins[BitsetMinsSize()-1].bits, bits)) { |
| 356 | return +V8_INFINITY; |
| 357 | } |
| 358 | for (size_t i = BitsetMinsSize()-1; i-- > 0; ) { |
| 359 | if (Is(SEMANTIC(mins[i].bits), bits)) { |
| 360 | return mz ? |
| 361 | std::max(0.0, mins[i+1].min - 1) : mins[i+1].min - 1; |
| 362 | } |
| 363 | } |
| 364 | if (mz) return 0; |
| 365 | return base::OS::nan_value(); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | // ----------------------------------------------------------------------------- |
| 370 | // Predicates. |
| 371 | |
| 372 | |
| 373 | template<class Config> |
| 374 | bool TypeImpl<Config>::SimplyEquals(TypeImpl* that) { |
| 375 | DisallowHeapAllocation no_allocation; |
| 376 | if (this->IsClass()) { |
| 377 | return that->IsClass() |
| 378 | && *this->AsClass()->Map() == *that->AsClass()->Map(); |
| 379 | } |
| 380 | if (this->IsConstant()) { |
| 381 | return that->IsConstant() |
| 382 | && *this->AsConstant()->Value() == *that->AsConstant()->Value(); |
| 383 | } |
| 384 | if (this->IsContext()) { |
| 385 | return that->IsContext() |
| 386 | && this->AsContext()->Outer()->Equals(that->AsContext()->Outer()); |
| 387 | } |
| 388 | if (this->IsArray()) { |
| 389 | return that->IsArray() |
| 390 | && this->AsArray()->Element()->Equals(that->AsArray()->Element()); |
| 391 | } |
| 392 | if (this->IsFunction()) { |
| 393 | if (!that->IsFunction()) return false; |
| 394 | FunctionType* this_fun = this->AsFunction(); |
| 395 | FunctionType* that_fun = that->AsFunction(); |
| 396 | if (this_fun->Arity() != that_fun->Arity() || |
| 397 | !this_fun->Result()->Equals(that_fun->Result()) || |
| 398 | !this_fun->Receiver()->Equals(that_fun->Receiver())) { |
| 399 | return false; |
| 400 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 401 | for (int i = 0, n = this_fun->Arity(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 402 | if (!this_fun->Parameter(i)->Equals(that_fun->Parameter(i))) return false; |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | UNREACHABLE(); |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | // Check if [this] <= [that]. |
| 412 | template<class Config> |
| 413 | bool TypeImpl<Config>::SlowIs(TypeImpl* that) { |
| 414 | DisallowHeapAllocation no_allocation; |
| 415 | |
| 416 | if (that->IsBitset()) { |
| 417 | return BitsetType::Is(this->BitsetLub(), that->AsBitset()); |
| 418 | } |
| 419 | if (this->IsBitset()) { |
| 420 | return BitsetType::Is(this->AsBitset(), that->BitsetGlb()); |
| 421 | } |
| 422 | |
| 423 | // (T1 \/ ... \/ Tn) <= T if (T1 <= T) /\ ... /\ (Tn <= T) |
| 424 | if (this->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 425 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
| 426 | if (!this->AsUnion()->Get(i)->Is(that)) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn) |
| 432 | if (that->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 433 | for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 434 | if (this->Is(that->AsUnion()->Get(i))) return true; |
| 435 | if (i > 1 && this->IsRange()) return false; // Shortcut. |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | if (that->IsRange()) { |
| 441 | return (this->IsRange() && Contains(that->AsRange(), this->AsRange())) |
| 442 | || (this->IsConstant() && |
| 443 | Contains(that->AsRange(), *this->AsConstant()->Value())); |
| 444 | } |
| 445 | if (this->IsRange()) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 446 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 447 | return this->SimplyEquals(that); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | template<class Config> |
| 452 | bool TypeImpl<Config>::NowIs(TypeImpl* that) { |
| 453 | DisallowHeapAllocation no_allocation; |
| 454 | |
| 455 | // TODO(rossberg): this is incorrect for |
| 456 | // Union(Constant(V), T)->NowIs(Class(M)) |
| 457 | // but fuzzing does not cover that! |
| 458 | if (this->IsConstant()) { |
| 459 | i::Object* object = *this->AsConstant()->Value(); |
| 460 | if (object->IsHeapObject()) { |
| 461 | i::Map* map = i::HeapObject::cast(object)->map(); |
| 462 | for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) { |
| 463 | if (*it.Current() == map) return true; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | return this->Is(that); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | // Check if [this] contains only (currently) stable classes. |
| 472 | template<class Config> |
| 473 | bool TypeImpl<Config>::NowStable() { |
| 474 | DisallowHeapAllocation no_allocation; |
| 475 | for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) { |
| 476 | if (!it.Current()->is_stable()) return false; |
| 477 | } |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | // Check if [this] and [that] overlap. |
| 483 | template<class Config> |
| 484 | bool TypeImpl<Config>::Maybe(TypeImpl* that) { |
| 485 | DisallowHeapAllocation no_allocation; |
| 486 | |
| 487 | // (T1 \/ ... \/ Tn) overlaps T if (T1 overlaps T) \/ ... \/ (Tn overlaps T) |
| 488 | if (this->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 489 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
| 490 | if (this->AsUnion()->Get(i)->Maybe(that)) return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | // T overlaps (T1 \/ ... \/ Tn) if (T overlaps T1) \/ ... \/ (T overlaps Tn) |
| 496 | if (that->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 497 | for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 498 | if (this->Maybe(that->AsUnion()->Get(i))) return true; |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | if (!BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub())) |
| 504 | return false; |
| 505 | if (this->IsBitset() || that->IsBitset()) return true; |
| 506 | |
| 507 | if (this->IsClass() != that->IsClass()) return true; |
| 508 | |
| 509 | if (this->IsRange()) { |
| 510 | if (that->IsConstant()) { |
| 511 | return Contains(this->AsRange(), *that->AsConstant()->Value()); |
| 512 | } |
| 513 | return that->IsRange() && Overlap(this->AsRange(), that->AsRange()); |
| 514 | } |
| 515 | if (that->IsRange()) { |
| 516 | if (this->IsConstant()) { |
| 517 | return Contains(that->AsRange(), *this->AsConstant()->Value()); |
| 518 | } |
| 519 | return this->IsRange() && Overlap(this->AsRange(), that->AsRange()); |
| 520 | } |
| 521 | |
| 522 | return this->SimplyEquals(that); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | // Return the range in [this], or [NULL]. |
| 527 | template<class Config> |
| 528 | typename TypeImpl<Config>::RangeType* TypeImpl<Config>::GetRange() { |
| 529 | DisallowHeapAllocation no_allocation; |
| 530 | if (this->IsRange()) return this->AsRange(); |
| 531 | if (this->IsUnion() && this->AsUnion()->Get(1)->IsRange()) { |
| 532 | return this->AsUnion()->Get(1)->AsRange(); |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | template<class Config> |
| 539 | bool TypeImpl<Config>::Contains(i::Object* value) { |
| 540 | DisallowHeapAllocation no_allocation; |
| 541 | for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) { |
| 542 | if (*it.Current() == value) return true; |
| 543 | } |
| 544 | if (IsInteger(value)) { |
| 545 | RangeType* range = this->GetRange(); |
| 546 | if (range != NULL && Contains(range, value)) return true; |
| 547 | } |
| 548 | return BitsetType::New(BitsetType::Lub(value))->Is(this); |
| 549 | } |
| 550 | |
| 551 | |
| 552 | template<class Config> |
| 553 | bool TypeImpl<Config>::UnionType::Wellformed() { |
| 554 | DisallowHeapAllocation no_allocation; |
| 555 | // This checks the invariants of the union representation: |
| 556 | // 1. There are at least two elements. |
| 557 | // 2. At most one element is a bitset, and it must be the first one. |
| 558 | // 3. At most one element is a range, and it must be the second one |
| 559 | // (even when the first element is not a bitset). |
| 560 | // 4. No element is itself a union. |
| 561 | // 5. No element is a subtype of any other. |
| 562 | DCHECK(this->Length() >= 2); // (1) |
| 563 | for (int i = 0; i < this->Length(); ++i) { |
| 564 | if (i != 0) DCHECK(!this->Get(i)->IsBitset()); // (2) |
| 565 | if (i != 1) DCHECK(!this->Get(i)->IsRange()); // (3) |
| 566 | DCHECK(!this->Get(i)->IsUnion()); // (4) |
| 567 | for (int j = 0; j < this->Length(); ++j) { |
| 568 | if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j))); // (5) |
| 569 | } |
| 570 | } |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | // ----------------------------------------------------------------------------- |
| 576 | // Union and intersection |
| 577 | |
| 578 | |
| 579 | static bool AddIsSafe(int x, int y) { |
| 580 | return x >= 0 ? |
| 581 | y <= std::numeric_limits<int>::max() - x : |
| 582 | y >= std::numeric_limits<int>::min() - x; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | template<class Config> |
| 587 | typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect( |
| 588 | TypeHandle type1, TypeHandle type2, Region* region) { |
| 589 | bitset bits = type1->BitsetGlb() & type2->BitsetGlb(); |
| 590 | if (!BitsetType::IsInhabited(bits)) bits = BitsetType::kNone; |
| 591 | |
| 592 | // Fast case: bit sets. |
| 593 | if (type1->IsBitset() && type2->IsBitset()) { |
| 594 | return BitsetType::New(bits, region); |
| 595 | } |
| 596 | |
| 597 | // Fast case: top or bottom types. |
| 598 | if (type1->IsNone() || type2->IsAny()) return type1; // Shortcut. |
| 599 | if (type2->IsNone() || type1->IsAny()) return type2; // Shortcut. |
| 600 | |
| 601 | // Semi-fast case. |
| 602 | if (type1->Is(type2)) return type1; |
| 603 | if (type2->Is(type1)) return type2; |
| 604 | |
| 605 | // Slow case: create union. |
| 606 | int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1; |
| 607 | int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1; |
| 608 | if (!AddIsSafe(size1, size2)) return Any(region); |
| 609 | int size = size1 + size2; |
| 610 | if (!AddIsSafe(size, 2)) return Any(region); |
| 611 | size += 2; |
| 612 | UnionHandle result = UnionType::New(size, region); |
| 613 | size = 0; |
| 614 | |
| 615 | // Deal with bitsets. |
| 616 | result->Set(size++, BitsetType::New(bits, region)); |
| 617 | |
| 618 | // Deal with ranges. |
| 619 | TypeHandle range = None(region); |
| 620 | RangeType* range1 = type1->GetRange(); |
| 621 | RangeType* range2 = type2->GetRange(); |
| 622 | if (range1 != NULL && range2 != NULL) { |
| 623 | Limits lim = Intersect(Limits(range1), Limits(range2)); |
| 624 | if (lim.min->Number() <= lim.max->Number()) { |
| 625 | range = RangeType::New(lim, region); |
| 626 | } |
| 627 | } |
| 628 | result->Set(size++, range); |
| 629 | |
| 630 | size = IntersectAux(type1, type2, result, size, region); |
| 631 | return NormalizeUnion(result, size); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | template<class Config> |
| 636 | int TypeImpl<Config>::UpdateRange( |
| 637 | RangeHandle range, UnionHandle result, int size, Region* region) { |
| 638 | TypeHandle old_range = result->Get(1); |
| 639 | DCHECK(old_range->IsRange() || old_range->IsNone()); |
| 640 | if (range->Is(old_range)) return size; |
| 641 | if (!old_range->Is(range->unhandle())) { |
| 642 | range = RangeType::New( |
| 643 | Union(Limits(range->AsRange()), Limits(old_range->AsRange())), region); |
| 644 | } |
| 645 | result->Set(1, range); |
| 646 | |
| 647 | // Remove any components that just got subsumed. |
| 648 | for (int i = 2; i < size; ) { |
| 649 | if (result->Get(i)->Is(range->unhandle())) { |
| 650 | result->Set(i, result->Get(--size)); |
| 651 | } else { |
| 652 | ++i; |
| 653 | } |
| 654 | } |
| 655 | return size; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | template<class Config> |
| 660 | int TypeImpl<Config>::IntersectAux( |
| 661 | TypeHandle lhs, TypeHandle rhs, |
| 662 | UnionHandle result, int size, Region* region) { |
| 663 | if (lhs->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 664 | for (int i = 0, n = lhs->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 665 | size = IntersectAux(lhs->AsUnion()->Get(i), rhs, result, size, region); |
| 666 | } |
| 667 | return size; |
| 668 | } |
| 669 | if (rhs->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 670 | for (int i = 0, n = rhs->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 671 | size = IntersectAux(lhs, rhs->AsUnion()->Get(i), result, size, region); |
| 672 | } |
| 673 | return size; |
| 674 | } |
| 675 | |
| 676 | if (!BitsetType::IsInhabited(lhs->BitsetLub() & rhs->BitsetLub())) { |
| 677 | return size; |
| 678 | } |
| 679 | |
| 680 | if (lhs->IsRange()) { |
| 681 | if (rhs->IsBitset() || rhs->IsClass()) { |
| 682 | return UpdateRange( |
| 683 | Config::template cast<RangeType>(lhs), result, size, region); |
| 684 | } |
| 685 | if (rhs->IsConstant() && |
| 686 | Contains(lhs->AsRange(), *rhs->AsConstant()->Value())) { |
| 687 | return AddToUnion(rhs, result, size, region); |
| 688 | } |
| 689 | return size; |
| 690 | } |
| 691 | if (rhs->IsRange()) { |
| 692 | if (lhs->IsBitset() || lhs->IsClass()) { |
| 693 | return UpdateRange( |
| 694 | Config::template cast<RangeType>(rhs), result, size, region); |
| 695 | } |
| 696 | if (lhs->IsConstant() && |
| 697 | Contains(rhs->AsRange(), *lhs->AsConstant()->Value())) { |
| 698 | return AddToUnion(lhs, result, size, region); |
| 699 | } |
| 700 | return size; |
| 701 | } |
| 702 | |
| 703 | if (lhs->IsBitset() || rhs->IsBitset()) { |
| 704 | return AddToUnion(lhs->IsBitset() ? rhs : lhs, result, size, region); |
| 705 | } |
| 706 | if (lhs->IsClass() != rhs->IsClass()) { |
| 707 | return AddToUnion(lhs->IsClass() ? rhs : lhs, result, size, region); |
| 708 | } |
| 709 | if (lhs->SimplyEquals(rhs->unhandle())) { |
| 710 | return AddToUnion(lhs, result, size, region); |
| 711 | } |
| 712 | return size; |
| 713 | } |
| 714 | |
| 715 | |
| 716 | template<class Config> |
| 717 | typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union( |
| 718 | TypeHandle type1, TypeHandle type2, Region* region) { |
| 719 | |
| 720 | // Fast case: bit sets. |
| 721 | if (type1->IsBitset() && type2->IsBitset()) { |
| 722 | return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region); |
| 723 | } |
| 724 | |
| 725 | // Fast case: top or bottom types. |
| 726 | if (type1->IsAny() || type2->IsNone()) return type1; |
| 727 | if (type2->IsAny() || type1->IsNone()) return type2; |
| 728 | |
| 729 | // Semi-fast case. |
| 730 | if (type1->Is(type2)) return type2; |
| 731 | if (type2->Is(type1)) return type1; |
| 732 | |
| 733 | // Slow case: create union. |
| 734 | int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1; |
| 735 | int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1; |
| 736 | if (!AddIsSafe(size1, size2)) return Any(region); |
| 737 | int size = size1 + size2; |
| 738 | if (!AddIsSafe(size, 2)) return Any(region); |
| 739 | size += 2; |
| 740 | UnionHandle result = UnionType::New(size, region); |
| 741 | size = 0; |
| 742 | |
| 743 | // Deal with bitsets. |
| 744 | TypeHandle bits = BitsetType::New( |
| 745 | type1->BitsetGlb() | type2->BitsetGlb(), region); |
| 746 | result->Set(size++, bits); |
| 747 | |
| 748 | // Deal with ranges. |
| 749 | TypeHandle range = None(region); |
| 750 | RangeType* range1 = type1->GetRange(); |
| 751 | RangeType* range2 = type2->GetRange(); |
| 752 | if (range1 != NULL && range2 != NULL) { |
| 753 | range = RangeType::New(Union(Limits(range1), Limits(range2)), region); |
| 754 | } else if (range1 != NULL) { |
| 755 | range = handle(range1); |
| 756 | } else if (range2 != NULL) { |
| 757 | range = handle(range2); |
| 758 | } |
| 759 | result->Set(size++, range); |
| 760 | |
| 761 | size = AddToUnion(type1, result, size, region); |
| 762 | size = AddToUnion(type2, result, size, region); |
| 763 | return NormalizeUnion(result, size); |
| 764 | } |
| 765 | |
| 766 | |
| 767 | // Add [type] to [result] unless [type] is bitset, range, or already subsumed. |
| 768 | // Return new size of [result]. |
| 769 | template<class Config> |
| 770 | int TypeImpl<Config>::AddToUnion( |
| 771 | TypeHandle type, UnionHandle result, int size, Region* region) { |
| 772 | if (type->IsBitset() || type->IsRange()) return size; |
| 773 | if (type->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 774 | for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 775 | size = AddToUnion(type->AsUnion()->Get(i), result, size, region); |
| 776 | } |
| 777 | return size; |
| 778 | } |
| 779 | for (int i = 0; i < size; ++i) { |
| 780 | if (type->Is(result->Get(i))) return size; |
| 781 | } |
| 782 | result->Set(size++, type); |
| 783 | return size; |
| 784 | } |
| 785 | |
| 786 | |
| 787 | template<class Config> |
| 788 | typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::NormalizeUnion( |
| 789 | UnionHandle unioned, int size) { |
| 790 | DCHECK(size >= 2); |
| 791 | // If range is subsumed by bitset, use its place for a different type. |
| 792 | if (unioned->Get(1)->Is(unioned->Get(0))) { |
| 793 | unioned->Set(1, unioned->Get(--size)); |
| 794 | } |
| 795 | // If bitset is None, use its place for a different type. |
| 796 | if (size >= 2 && unioned->Get(0)->IsNone()) { |
| 797 | unioned->Set(0, unioned->Get(--size)); |
| 798 | } |
| 799 | if (size == 1) return unioned->Get(0); |
| 800 | unioned->Shrink(size); |
| 801 | SLOW_DCHECK(unioned->Wellformed()); |
| 802 | return unioned; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | // ----------------------------------------------------------------------------- |
| 807 | // Iteration. |
| 808 | |
| 809 | template<class Config> |
| 810 | int TypeImpl<Config>::NumClasses() { |
| 811 | DisallowHeapAllocation no_allocation; |
| 812 | if (this->IsClass()) { |
| 813 | return 1; |
| 814 | } else if (this->IsUnion()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 815 | int result = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 816 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
| 817 | if (this->AsUnion()->Get(i)->IsClass()) ++result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 818 | } |
| 819 | return result; |
| 820 | } else { |
| 821 | return 0; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | |
| 826 | template<class Config> |
| 827 | int TypeImpl<Config>::NumConstants() { |
| 828 | DisallowHeapAllocation no_allocation; |
| 829 | if (this->IsConstant()) { |
| 830 | return 1; |
| 831 | } else if (this->IsUnion()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 832 | int result = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 833 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
| 834 | if (this->AsUnion()->Get(i)->IsConstant()) ++result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 835 | } |
| 836 | return result; |
| 837 | } else { |
| 838 | return 0; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | |
| 843 | template<class Config> template<class T> |
| 844 | typename TypeImpl<Config>::TypeHandle |
| 845 | TypeImpl<Config>::Iterator<T>::get_type() { |
| 846 | DCHECK(!Done()); |
| 847 | return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | // C++ cannot specialise nested templates, so we have to go through this |
| 852 | // contortion with an auxiliary template to simulate it. |
| 853 | template<class Config, class T> |
| 854 | struct TypeImplIteratorAux { |
| 855 | static bool matches(typename TypeImpl<Config>::TypeHandle type); |
| 856 | static i::Handle<T> current(typename TypeImpl<Config>::TypeHandle type); |
| 857 | }; |
| 858 | |
| 859 | template<class Config> |
| 860 | struct TypeImplIteratorAux<Config, i::Map> { |
| 861 | static bool matches(typename TypeImpl<Config>::TypeHandle type) { |
| 862 | return type->IsClass(); |
| 863 | } |
| 864 | static i::Handle<i::Map> current(typename TypeImpl<Config>::TypeHandle type) { |
| 865 | return type->AsClass()->Map(); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | template<class Config> |
| 870 | struct TypeImplIteratorAux<Config, i::Object> { |
| 871 | static bool matches(typename TypeImpl<Config>::TypeHandle type) { |
| 872 | return type->IsConstant(); |
| 873 | } |
| 874 | static i::Handle<i::Object> current( |
| 875 | typename TypeImpl<Config>::TypeHandle type) { |
| 876 | return type->AsConstant()->Value(); |
| 877 | } |
| 878 | }; |
| 879 | |
| 880 | template<class Config> template<class T> |
| 881 | bool TypeImpl<Config>::Iterator<T>::matches(TypeHandle type) { |
| 882 | return TypeImplIteratorAux<Config, T>::matches(type); |
| 883 | } |
| 884 | |
| 885 | template<class Config> template<class T> |
| 886 | i::Handle<T> TypeImpl<Config>::Iterator<T>::Current() { |
| 887 | return TypeImplIteratorAux<Config, T>::current(get_type()); |
| 888 | } |
| 889 | |
| 890 | |
| 891 | template<class Config> template<class T> |
| 892 | void TypeImpl<Config>::Iterator<T>::Advance() { |
| 893 | DisallowHeapAllocation no_allocation; |
| 894 | ++index_; |
| 895 | if (type_->IsUnion()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 896 | for (int n = type_->AsUnion()->Length(); index_ < n; ++index_) { |
| 897 | if (matches(type_->AsUnion()->Get(index_))) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 898 | } |
| 899 | } else if (index_ == 0 && matches(type_)) { |
| 900 | return; |
| 901 | } |
| 902 | index_ = -1; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | // ----------------------------------------------------------------------------- |
| 907 | // Conversion between low-level representations. |
| 908 | |
| 909 | template<class Config> |
| 910 | template<class OtherType> |
| 911 | typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert( |
| 912 | typename OtherType::TypeHandle type, Region* region) { |
| 913 | if (type->IsBitset()) { |
| 914 | return BitsetType::New(type->AsBitset(), region); |
| 915 | } else if (type->IsClass()) { |
| 916 | return ClassType::New(type->AsClass()->Map(), region); |
| 917 | } else if (type->IsConstant()) { |
| 918 | return ConstantType::New(type->AsConstant()->Value(), region); |
| 919 | } else if (type->IsRange()) { |
| 920 | return RangeType::New( |
| 921 | type->AsRange()->Min(), type->AsRange()->Max(), region); |
| 922 | } else if (type->IsContext()) { |
| 923 | TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region); |
| 924 | return ContextType::New(outer, region); |
| 925 | } else if (type->IsUnion()) { |
| 926 | int length = type->AsUnion()->Length(); |
| 927 | UnionHandle unioned = UnionType::New(length, region); |
| 928 | for (int i = 0; i < length; ++i) { |
| 929 | TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region); |
| 930 | unioned->Set(i, t); |
| 931 | } |
| 932 | return unioned; |
| 933 | } else if (type->IsArray()) { |
| 934 | TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region); |
| 935 | return ArrayType::New(element, region); |
| 936 | } else if (type->IsFunction()) { |
| 937 | TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region); |
| 938 | TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region); |
| 939 | FunctionHandle function = FunctionType::New( |
| 940 | res, rcv, type->AsFunction()->Arity(), region); |
| 941 | for (int i = 0; i < function->Arity(); ++i) { |
| 942 | TypeHandle param = Convert<OtherType>( |
| 943 | type->AsFunction()->Parameter(i), region); |
| 944 | function->InitParameter(i, param); |
| 945 | } |
| 946 | return function; |
| 947 | } else { |
| 948 | UNREACHABLE(); |
| 949 | return None(region); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | |
| 954 | // ----------------------------------------------------------------------------- |
| 955 | // Printing. |
| 956 | |
| 957 | template<class Config> |
| 958 | const char* TypeImpl<Config>::BitsetType::Name(bitset bits) { |
| 959 | switch (bits) { |
| 960 | case REPRESENTATION(kAny): return "Any"; |
| 961 | #define RETURN_NAMED_REPRESENTATION_TYPE(type, value) \ |
| 962 | case REPRESENTATION(k##type): return #type; |
| 963 | REPRESENTATION_BITSET_TYPE_LIST(RETURN_NAMED_REPRESENTATION_TYPE) |
| 964 | #undef RETURN_NAMED_REPRESENTATION_TYPE |
| 965 | |
| 966 | #define RETURN_NAMED_SEMANTIC_TYPE(type, value) \ |
| 967 | case SEMANTIC(k##type): return #type; |
| 968 | SEMANTIC_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE) |
| 969 | #undef RETURN_NAMED_SEMANTIC_TYPE |
| 970 | |
| 971 | default: |
| 972 | return NULL; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | |
| 977 | template <class Config> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 978 | void TypeImpl<Config>::BitsetType::Print(std::ostream& os, // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 979 | bitset bits) { |
| 980 | DisallowHeapAllocation no_allocation; |
| 981 | const char* name = Name(bits); |
| 982 | if (name != NULL) { |
| 983 | os << name; |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | static const bitset named_bitsets[] = { |
| 988 | #define BITSET_CONSTANT(type, value) REPRESENTATION(k##type), |
| 989 | REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT) |
| 990 | #undef BITSET_CONSTANT |
| 991 | |
| 992 | #define BITSET_CONSTANT(type, value) SEMANTIC(k##type), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 993 | INTERNAL_BITSET_TYPE_LIST(BITSET_CONSTANT) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 994 | SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT) |
| 995 | #undef BITSET_CONSTANT |
| 996 | }; |
| 997 | |
| 998 | bool is_first = true; |
| 999 | os << "("; |
| 1000 | for (int i(arraysize(named_bitsets) - 1); bits != 0 && i >= 0; --i) { |
| 1001 | bitset subset = named_bitsets[i]; |
| 1002 | if ((bits & subset) == subset) { |
| 1003 | if (!is_first) os << " | "; |
| 1004 | is_first = false; |
| 1005 | os << Name(subset); |
| 1006 | bits -= subset; |
| 1007 | } |
| 1008 | } |
| 1009 | DCHECK(bits == 0); |
| 1010 | os << ")"; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | template <class Config> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1015 | void TypeImpl<Config>::PrintTo(std::ostream& os, PrintDimension dim) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1016 | DisallowHeapAllocation no_allocation; |
| 1017 | if (dim != REPRESENTATION_DIM) { |
| 1018 | if (this->IsBitset()) { |
| 1019 | BitsetType::Print(os, SEMANTIC(this->AsBitset())); |
| 1020 | } else if (this->IsClass()) { |
| 1021 | os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < "; |
| 1022 | BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim); |
| 1023 | os << ")"; |
| 1024 | } else if (this->IsConstant()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1025 | os << "Constant(" << Brief(*this->AsConstant()->Value()) << ")"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1026 | } else if (this->IsRange()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1027 | std::ostream::fmtflags saved_flags = os.setf(std::ios::fixed); |
| 1028 | std::streamsize saved_precision = os.precision(0); |
| 1029 | os << "Range(" << this->AsRange()->Min()->Number() << ", " |
| 1030 | << this->AsRange()->Max()->Number() << ")"; |
| 1031 | os.flags(saved_flags); |
| 1032 | os.precision(saved_precision); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1033 | } else if (this->IsContext()) { |
| 1034 | os << "Context("; |
| 1035 | this->AsContext()->Outer()->PrintTo(os, dim); |
| 1036 | os << ")"; |
| 1037 | } else if (this->IsUnion()) { |
| 1038 | os << "("; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1039 | for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) { |
| 1040 | TypeHandle type_i = this->AsUnion()->Get(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1041 | if (i > 0) os << " | "; |
| 1042 | type_i->PrintTo(os, dim); |
| 1043 | } |
| 1044 | os << ")"; |
| 1045 | } else if (this->IsArray()) { |
| 1046 | os << "Array("; |
| 1047 | AsArray()->Element()->PrintTo(os, dim); |
| 1048 | os << ")"; |
| 1049 | } else if (this->IsFunction()) { |
| 1050 | if (!this->AsFunction()->Receiver()->IsAny()) { |
| 1051 | this->AsFunction()->Receiver()->PrintTo(os, dim); |
| 1052 | os << "."; |
| 1053 | } |
| 1054 | os << "("; |
| 1055 | for (int i = 0; i < this->AsFunction()->Arity(); ++i) { |
| 1056 | if (i > 0) os << ", "; |
| 1057 | this->AsFunction()->Parameter(i)->PrintTo(os, dim); |
| 1058 | } |
| 1059 | os << ")->"; |
| 1060 | this->AsFunction()->Result()->PrintTo(os, dim); |
| 1061 | } else { |
| 1062 | UNREACHABLE(); |
| 1063 | } |
| 1064 | } |
| 1065 | if (dim == BOTH_DIMS) os << "/"; |
| 1066 | if (dim != SEMANTIC_DIM) { |
| 1067 | BitsetType::Print(os, REPRESENTATION(this->BitsetLub())); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | #ifdef DEBUG |
| 1073 | template <class Config> |
| 1074 | void TypeImpl<Config>::Print() { |
| 1075 | OFStream os(stdout); |
| 1076 | PrintTo(os); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1077 | os << std::endl; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1078 | } |
| 1079 | template <class Config> |
| 1080 | void TypeImpl<Config>::BitsetType::Print(bitset bits) { |
| 1081 | OFStream os(stdout); |
| 1082 | Print(os, bits); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1083 | os << std::endl; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1084 | } |
| 1085 | #endif |
| 1086 | |
| 1087 | |
| 1088 | // ----------------------------------------------------------------------------- |
| 1089 | // Instantiations. |
| 1090 | |
| 1091 | template class TypeImpl<ZoneTypeConfig>; |
| 1092 | template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>; |
| 1093 | template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>; |
| 1094 | |
| 1095 | template class TypeImpl<HeapTypeConfig>; |
| 1096 | template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>; |
| 1097 | template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; |
| 1098 | |
| 1099 | template TypeImpl<ZoneTypeConfig>::TypeHandle |
| 1100 | TypeImpl<ZoneTypeConfig>::Convert<HeapType>( |
| 1101 | TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*); |
| 1102 | template TypeImpl<HeapTypeConfig>::TypeHandle |
| 1103 | TypeImpl<HeapTypeConfig>::Convert<Type>( |
| 1104 | TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*); |
| 1105 | |
| 1106 | } } // namespace v8::internal |