blob: a48736bb992cf172c0ce30a5093e0d63ad0f0cfe [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04005#include <iomanip>
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/types.h"
8
Ben Murdoch097c5b22016-05-18 11:27:45 +01009#include "src/handles-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/ostreams.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
12namespace v8 {
13namespace 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
Ben Murdoch097c5b22016-05-18 11:27:45 +010019// static
20bool Type::IsInteger(i::Object* x) {
21 return x->IsNumber() && Type::IsInteger(x->Number());
22}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023
24// -----------------------------------------------------------------------------
25// Range-related helper functions.
26
Ben Murdoch097c5b22016-05-18 11:27:45 +010027bool RangeType::Limits::IsEmpty() { return this->min > this->max; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
Ben Murdoch097c5b22016-05-18 11:27:45 +010029RangeType::Limits RangeType::Limits::Intersect(Limits lhs, Limits rhs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 DisallowHeapAllocation no_allocation;
31 Limits result(lhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 if (lhs.min < rhs.min) result.min = rhs.min;
33 if (lhs.max > rhs.max) result.max = rhs.max;
34 return result;
35}
36
Ben Murdoch097c5b22016-05-18 11:27:45 +010037RangeType::Limits RangeType::Limits::Union(Limits lhs, Limits rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 DisallowHeapAllocation no_allocation;
39 if (lhs.IsEmpty()) return rhs;
40 if (rhs.IsEmpty()) return lhs;
41 Limits result(lhs);
42 if (lhs.min > rhs.min) result.min = rhs.min;
43 if (lhs.max < rhs.max) result.max = rhs.max;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 return result;
45}
46
Ben Murdoch097c5b22016-05-18 11:27:45 +010047bool Type::Overlap(RangeType* lhs, RangeType* rhs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 DisallowHeapAllocation no_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +010049 return !RangeType::Limits::Intersect(RangeType::Limits(lhs),
50 RangeType::Limits(rhs))
51 .IsEmpty();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052}
53
Ben Murdoch097c5b22016-05-18 11:27:45 +010054bool Type::Contains(RangeType* lhs, RangeType* rhs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 return lhs->Min() <= rhs->Min() && rhs->Max() <= lhs->Max();
57}
58
Ben Murdoch097c5b22016-05-18 11:27:45 +010059bool Type::Contains(RangeType* lhs, ConstantType* rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 DisallowHeapAllocation no_allocation;
61 return IsInteger(*rhs->Value()) &&
62 lhs->Min() <= rhs->Value()->Number() &&
63 rhs->Value()->Number() <= lhs->Max();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064}
65
Ben Murdoch097c5b22016-05-18 11:27:45 +010066bool Type::Contains(RangeType* range, i::Object* val) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 return IsInteger(val) &&
69 range->Min() <= val->Number() && val->Number() <= range->Max();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070}
71
72
73// -----------------------------------------------------------------------------
74// Min and Max computation.
75
Ben Murdoch097c5b22016-05-18 11:27:45 +010076double Type::Min() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 DCHECK(this->SemanticIs(Number()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 if (this->IsBitset()) return BitsetType::Min(this->AsBitset());
79 if (this->IsUnion()) {
80 double min = +V8_INFINITY;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 min = std::min(min, this->AsUnion()->Get(i)->Min());
83 }
84 return min;
85 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 if (this->IsRange()) return this->AsRange()->Min();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 if (this->IsConstant()) return this->AsConstant()->Value()->Number();
88 UNREACHABLE();
89 return 0;
90}
91
Ben Murdoch097c5b22016-05-18 11:27:45 +010092double Type::Max() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 DCHECK(this->SemanticIs(Number()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 if (this->IsBitset()) return BitsetType::Max(this->AsBitset());
95 if (this->IsUnion()) {
96 double max = -V8_INFINITY;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040097 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 max = std::max(max, this->AsUnion()->Get(i)->Max());
99 }
100 return max;
101 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 if (this->IsRange()) return this->AsRange()->Max();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 if (this->IsConstant()) return this->AsConstant()->Value()->Number();
104 UNREACHABLE();
105 return 0;
106}
107
108
109// -----------------------------------------------------------------------------
110// Glb and lub computation.
111
112
113// The largest bitset subsumed by this type.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100114Type::bitset BitsetType::Glb(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 // Fast case.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100117 if (IsBitset(type)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 return type->AsBitset();
119 } else if (type->IsUnion()) {
120 SLOW_DCHECK(type->AsUnion()->Wellformed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 return type->AsUnion()->Get(0)->BitsetGlb() |
122 SEMANTIC(type->AsUnion()->Get(1)->BitsetGlb()); // Shortcut.
123 } else if (type->IsRange()) {
124 bitset glb = SEMANTIC(
125 BitsetType::Glb(type->AsRange()->Min(), type->AsRange()->Max()));
126 return glb | REPRESENTATION(type->BitsetLub());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 return type->Representation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 }
130}
131
132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133// The smallest bitset subsuming this type, possibly not a proper one.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100134Type::bitset BitsetType::Lub(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 DisallowHeapAllocation no_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100136 if (IsBitset(type)) return type->AsBitset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 if (type->IsUnion()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 // Take the representation from the first element, which is always
139 // a bitset.
140 int bitset = type->AsUnion()->Get(0)->BitsetLub();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400141 for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 // Other elements only contribute their semantic part.
143 bitset |= SEMANTIC(type->AsUnion()->Get(i)->BitsetLub());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 }
145 return bitset;
146 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 if (type->IsClass()) return type->AsClass()->Lub();
148 if (type->IsConstant()) return type->AsConstant()->Lub();
149 if (type->IsRange()) return type->AsRange()->Lub();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400150 if (type->IsContext()) return kInternal & kTaggedPointer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 if (type->IsArray()) return kOtherObject;
152 if (type->IsFunction()) return kFunction;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100153 if (type->IsTuple()) return kInternal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 UNREACHABLE();
155 return kNone;
156}
157
Ben Murdoch097c5b22016-05-18 11:27:45 +0100158Type::bitset BitsetType::Lub(i::Map* map) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 DisallowHeapAllocation no_allocation;
160 switch (map->instance_type()) {
161 case STRING_TYPE:
162 case ONE_BYTE_STRING_TYPE:
163 case CONS_STRING_TYPE:
164 case CONS_ONE_BYTE_STRING_TYPE:
165 case SLICED_STRING_TYPE:
166 case SLICED_ONE_BYTE_STRING_TYPE:
167 case EXTERNAL_STRING_TYPE:
168 case EXTERNAL_ONE_BYTE_STRING_TYPE:
169 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
170 case SHORT_EXTERNAL_STRING_TYPE:
171 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
172 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
173 return kOtherString;
174 case INTERNALIZED_STRING_TYPE:
175 case ONE_BYTE_INTERNALIZED_STRING_TYPE:
176 case EXTERNAL_INTERNALIZED_STRING_TYPE:
177 case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
178 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
179 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
180 case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
181 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
182 return kInternalizedString;
183 case SYMBOL_TYPE:
184 return kSymbol;
185 case ODDBALL_TYPE: {
186 Heap* heap = map->GetHeap();
187 if (map == heap->undefined_map()) return kUndefined;
188 if (map == heap->null_map()) return kNull;
189 if (map == heap->boolean_map()) return kBoolean;
190 DCHECK(map == heap->the_hole_map() ||
191 map == heap->uninitialized_map() ||
192 map == heap->no_interceptor_result_sentinel_map() ||
193 map == heap->termination_exception_map() ||
Ben Murdochda12d292016-06-02 14:46:10 +0100194 map == heap->arguments_marker_map() ||
Ben Murdochc5610432016-08-08 18:44:38 +0100195 map == heap->optimized_out_map() ||
196 map == heap->stale_register_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400197 return kInternal & kTaggedPointer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 }
199 case HEAP_NUMBER_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400200 return kNumber & kTaggedPointer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 case SIMD128_VALUE_TYPE:
202 return kSimd;
Ben Murdochda12d292016-06-02 14:46:10 +0100203 case JS_OBJECT_TYPE:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100204 case JS_ARGUMENTS_TYPE:
205 case JS_ERROR_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100206 case JS_GLOBAL_OBJECT_TYPE:
207 case JS_GLOBAL_PROXY_TYPE:
Ben Murdochc5610432016-08-08 18:44:38 +0100208 case JS_API_OBJECT_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100209 case JS_SPECIAL_API_OBJECT_TYPE:
210 if (map->is_undetectable()) return kOtherUndetectable;
211 return kOtherObject;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 case JS_VALUE_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 case JS_MESSAGE_OBJECT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 case JS_DATE_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
216 case JS_GENERATOR_OBJECT_TYPE:
217 case JS_MODULE_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 case JS_ARRAY_BUFFER_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 case JS_ARRAY_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100220 case JS_REGEXP_TYPE: // TODO(rossberg): there should be a RegExp type.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 case JS_TYPED_ARRAY_TYPE:
222 case JS_DATA_VIEW_TYPE:
223 case JS_SET_TYPE:
224 case JS_MAP_TYPE:
225 case JS_SET_ITERATOR_TYPE:
226 case JS_MAP_ITERATOR_TYPE:
227 case JS_WEAK_MAP_TYPE:
228 case JS_WEAK_SET_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000229 case JS_PROMISE_TYPE:
230 case JS_BOUND_FUNCTION_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100231 DCHECK(!map->is_undetectable());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 return kOtherObject;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 case JS_FUNCTION_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100234 DCHECK(!map->is_undetectable());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235 return kFunction;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 case JS_PROXY_TYPE:
Ben Murdochda12d292016-06-02 14:46:10 +0100237 DCHECK(!map->is_undetectable());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 return kProxy;
239 case MAP_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000240 case ALLOCATION_SITE_TYPE:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100241 case ACCESSOR_INFO_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 case SHARED_FUNCTION_INFO_TYPE:
243 case ACCESSOR_PAIR_TYPE:
244 case FIXED_ARRAY_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000245 case FIXED_DOUBLE_ARRAY_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400246 case BYTE_ARRAY_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000247 case BYTECODE_ARRAY_TYPE:
248 case TRANSITION_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 case FOREIGN_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 case SCRIPT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 case CODE_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 case PROPERTY_CELL_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400253 return kInternal & kTaggedPointer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254
255 // Remaining instance types are unsupported for now. If any of them do
256 // require bit set types, they should get kInternal & kTaggedPointer.
257 case MUTABLE_HEAP_NUMBER_TYPE:
258 case FREE_SPACE_TYPE:
259#define FIXED_TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
260 case FIXED_##TYPE##_ARRAY_TYPE:
261
262 TYPED_ARRAYS(FIXED_TYPED_ARRAY_CASE)
263#undef FIXED_TYPED_ARRAY_CASE
264 case FILLER_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 case ACCESS_CHECK_INFO_TYPE:
266 case INTERCEPTOR_INFO_TYPE:
267 case CALL_HANDLER_INFO_TYPE:
268 case FUNCTION_TEMPLATE_INFO_TYPE:
269 case OBJECT_TEMPLATE_INFO_TYPE:
270 case SIGNATURE_INFO_TYPE:
271 case TYPE_SWITCH_INFO_TYPE:
272 case ALLOCATION_MEMENTO_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273 case TYPE_FEEDBACK_INFO_TYPE:
274 case ALIASED_ARGUMENTS_ENTRY_TYPE:
275 case BOX_TYPE:
276 case DEBUG_INFO_TYPE:
277 case BREAK_POINT_INFO_TYPE:
278 case CELL_TYPE:
279 case WEAK_CELL_TYPE:
280 case PROTOTYPE_INFO_TYPE:
281 case SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 UNREACHABLE();
283 return kNone;
284 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000285 UNREACHABLE();
286 return kNone;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287}
288
Ben Murdoch097c5b22016-05-18 11:27:45 +0100289Type::bitset BitsetType::Lub(i::Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 DisallowHeapAllocation no_allocation;
291 if (value->IsNumber()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400292 return Lub(value->Number()) &
293 (value->IsSmi() ? kTaggedSigned : kTaggedPointer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 }
295 return Lub(i::HeapObject::cast(value)->map());
296}
297
Ben Murdoch097c5b22016-05-18 11:27:45 +0100298Type::bitset BitsetType::Lub(double value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 DisallowHeapAllocation no_allocation;
300 if (i::IsMinusZero(value)) return kMinusZero;
301 if (std::isnan(value)) return kNaN;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400302 if (IsUint32Double(value) || IsInt32Double(value)) return Lub(value, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 return kOtherNumber;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304}
305
306
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307// Minimum values of plain numeric bitsets.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100308const BitsetType::Boundary BitsetType::BoundariesArray[] = {
309 {kOtherNumber, kPlainNumber, -V8_INFINITY},
310 {kOtherSigned32, kNegative32, kMinInt},
311 {kNegative31, kNegative31, -0x40000000},
312 {kUnsigned30, kUnsigned30, 0},
313 {kOtherUnsigned31, kUnsigned31, 0x40000000},
314 {kOtherUnsigned32, kUnsigned32, 0x80000000},
315 {kOtherNumber, kPlainNumber, static_cast<double>(kMaxUInt32) + 1}};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316
Ben Murdoch097c5b22016-05-18 11:27:45 +0100317const BitsetType::Boundary* BitsetType::Boundaries() { return BoundariesArray; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318
Ben Murdoch097c5b22016-05-18 11:27:45 +0100319size_t BitsetType::BoundariesSize() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 // Windows doesn't like arraysize here.
321 // return arraysize(BoundariesArray);
322 return 7;
323}
324
Ben Murdoch097c5b22016-05-18 11:27:45 +0100325Type::bitset BitsetType::ExpandInternals(Type::bitset bits) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326 DisallowHeapAllocation no_allocation;
327 if (!(bits & SEMANTIC(kPlainNumber))) return bits; // Shortcut.
328 const Boundary* boundaries = Boundaries();
329 for (size_t i = 0; i < BoundariesSize(); ++i) {
330 DCHECK(BitsetType::Is(boundaries[i].internal, boundaries[i].external));
331 if (bits & SEMANTIC(boundaries[i].internal))
332 bits |= SEMANTIC(boundaries[i].external);
333 }
334 return bits;
335}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336
Ben Murdoch097c5b22016-05-18 11:27:45 +0100337Type::bitset BitsetType::Lub(double min, double max) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338 DisallowHeapAllocation no_allocation;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 int lub = kNone;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340 const Boundary* mins = Boundaries();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342 for (size_t i = 1; i < BoundariesSize(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 if (min < mins[i].min) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000344 lub |= mins[i-1].internal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 if (max < mins[i].min) return lub;
346 }
347 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 return lub | mins[BoundariesSize() - 1].internal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349}
350
Ben Murdoch097c5b22016-05-18 11:27:45 +0100351Type::bitset BitsetType::NumberBits(bitset bits) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 return SEMANTIC(bits & kPlainNumber);
353}
354
Ben Murdoch097c5b22016-05-18 11:27:45 +0100355Type::bitset BitsetType::Glb(double min, double max) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000356 DisallowHeapAllocation no_allocation;
357 int glb = kNone;
358 const Boundary* mins = Boundaries();
359
360 // If the range does not touch 0, the bound is empty.
361 if (max < -1 || min > 0) return glb;
362
363 for (size_t i = 1; i + 1 < BoundariesSize(); ++i) {
364 if (min <= mins[i].min) {
365 if (max + 1 < mins[i + 1].min) break;
366 glb |= mins[i].external;
367 }
368 }
369 // OtherNumber also contains float numbers, so it can never be
370 // in the greatest lower bound.
371 return glb & ~(SEMANTIC(kOtherNumber));
372}
373
Ben Murdoch097c5b22016-05-18 11:27:45 +0100374double BitsetType::Min(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000376 DCHECK(Is(SEMANTIC(bits), kNumber));
377 const Boundary* mins = Boundaries();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378 bool mz = SEMANTIC(bits & kMinusZero);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 for (size_t i = 0; i < BoundariesSize(); ++i) {
380 if (Is(SEMANTIC(mins[i].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 return mz ? std::min(0.0, mins[i].min) : mins[i].min;
382 }
383 }
384 if (mz) return 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386}
387
Ben Murdoch097c5b22016-05-18 11:27:45 +0100388double BitsetType::Max(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 DCHECK(Is(SEMANTIC(bits), kNumber));
391 const Boundary* mins = Boundaries();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400392 bool mz = SEMANTIC(bits & kMinusZero);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 if (BitsetType::Is(SEMANTIC(mins[BoundariesSize() - 1].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 return +V8_INFINITY;
395 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396 for (size_t i = BoundariesSize() - 1; i-- > 0;) {
397 if (Is(SEMANTIC(mins[i].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 return mz ?
399 std::max(0.0, mins[i+1].min - 1) : mins[i+1].min - 1;
400 }
401 }
402 if (mz) return 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404}
405
406
407// -----------------------------------------------------------------------------
408// Predicates.
409
Ben Murdoch097c5b22016-05-18 11:27:45 +0100410bool Type::SimplyEquals(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 DisallowHeapAllocation no_allocation;
412 if (this->IsClass()) {
413 return that->IsClass()
414 && *this->AsClass()->Map() == *that->AsClass()->Map();
415 }
416 if (this->IsConstant()) {
417 return that->IsConstant()
418 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
419 }
420 if (this->IsContext()) {
421 return that->IsContext()
422 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
423 }
424 if (this->IsArray()) {
425 return that->IsArray()
426 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
427 }
428 if (this->IsFunction()) {
429 if (!that->IsFunction()) return false;
430 FunctionType* this_fun = this->AsFunction();
431 FunctionType* that_fun = that->AsFunction();
432 if (this_fun->Arity() != that_fun->Arity() ||
433 !this_fun->Result()->Equals(that_fun->Result()) ||
434 !this_fun->Receiver()->Equals(that_fun->Receiver())) {
435 return false;
436 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400437 for (int i = 0, n = this_fun->Arity(); i < n; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 if (!this_fun->Parameter(i)->Equals(that_fun->Parameter(i))) return false;
439 }
440 return true;
441 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100442 if (this->IsTuple()) {
443 if (!that->IsTuple()) return false;
444 TupleType* this_tuple = this->AsTuple();
445 TupleType* that_tuple = that->AsTuple();
446 if (this_tuple->Arity() != that_tuple->Arity()) {
447 return false;
448 }
449 for (int i = 0, n = this_tuple->Arity(); i < n; ++i) {
450 if (!this_tuple->Element(i)->Equals(that_tuple->Element(i))) return false;
451 }
452 return true;
453 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 UNREACHABLE();
455 return false;
456}
457
Ben Murdoch097c5b22016-05-18 11:27:45 +0100458Type::bitset Type::Representation() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000459 return REPRESENTATION(this->BitsetLub());
460}
461
462
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463// Check if [this] <= [that].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100464bool Type::SlowIs(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 DisallowHeapAllocation no_allocation;
466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467 // Fast bitset cases
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 if (that->IsBitset()) {
469 return BitsetType::Is(this->BitsetLub(), that->AsBitset());
470 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000471
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 if (this->IsBitset()) {
473 return BitsetType::Is(this->AsBitset(), that->BitsetGlb());
474 }
475
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000476 // Check the representations.
477 if (!BitsetType::Is(Representation(), that->Representation())) {
478 return false;
479 }
480
481 // Check the semantic part.
482 return SemanticIs(that);
483}
484
485
486// Check if SEMANTIC([this]) <= SEMANTIC([that]). The result of the method
487// should be independent of the representation axis of the types.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100488bool Type::SemanticIs(Type* that) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 DisallowHeapAllocation no_allocation;
490
491 if (this == that) return true;
492
493 if (that->IsBitset()) {
494 return BitsetType::Is(SEMANTIC(this->BitsetLub()), that->AsBitset());
495 }
496 if (this->IsBitset()) {
497 return BitsetType::Is(SEMANTIC(this->AsBitset()), that->BitsetGlb());
498 }
499
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 // (T1 \/ ... \/ Tn) <= T if (T1 <= T) /\ ... /\ (Tn <= T)
501 if (this->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400502 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000503 if (!this->AsUnion()->Get(i)->SemanticIs(that)) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 }
505 return true;
506 }
507
508 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn)
509 if (that->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400510 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100511 if (this->SemanticIs(that->AsUnion()->Get(i))) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 if (i > 1 && this->IsRange()) return false; // Shortcut.
513 }
514 return false;
515 }
516
517 if (that->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000518 return (this->IsRange() && Contains(that->AsRange(), this->AsRange())) ||
519 (this->IsConstant() &&
520 Contains(that->AsRange(), this->AsConstant()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000521 }
522 if (this->IsRange()) return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400523
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 return this->SimplyEquals(that);
525}
526
Ben Murdoch097c5b22016-05-18 11:27:45 +0100527// Most precise _current_ type of a value (usually its class).
528Type* Type::NowOf(i::Object* value, Zone* zone) {
529 if (value->IsSmi() ||
530 i::HeapObject::cast(value)->map()->instance_type() == HEAP_NUMBER_TYPE) {
531 return Of(value, zone);
532 }
533 return Class(i::handle(i::HeapObject::cast(value)->map()), zone);
534}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535
Ben Murdoch097c5b22016-05-18 11:27:45 +0100536bool Type::NowContains(i::Object* value) {
537 DisallowHeapAllocation no_allocation;
538 if (this->IsAny()) return true;
539 if (value->IsHeapObject()) {
540 i::Map* map = i::HeapObject::cast(value)->map();
541 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
542 if (*it.Current() == map) return true;
543 }
544 }
545 return this->Contains(value);
546}
547
548bool Type::NowIs(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 DisallowHeapAllocation no_allocation;
550
551 // TODO(rossberg): this is incorrect for
552 // Union(Constant(V), T)->NowIs(Class(M))
553 // but fuzzing does not cover that!
554 if (this->IsConstant()) {
555 i::Object* object = *this->AsConstant()->Value();
556 if (object->IsHeapObject()) {
557 i::Map* map = i::HeapObject::cast(object)->map();
558 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
559 if (*it.Current() == map) return true;
560 }
561 }
562 }
563 return this->Is(that);
564}
565
566
567// Check if [this] contains only (currently) stable classes.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100568bool Type::NowStable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000570 return !this->IsClass() || this->AsClass()->Map()->is_stable();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571}
572
573
574// Check if [this] and [that] overlap.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100575bool Type::Maybe(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 DisallowHeapAllocation no_allocation;
577
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000578 // Take care of the representation part (and also approximate
579 // the semantic part).
580 if (!BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()))
581 return false;
582
583 return SemanticMaybe(that);
584}
585
Ben Murdoch097c5b22016-05-18 11:27:45 +0100586bool Type::SemanticMaybe(Type* that) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000587 DisallowHeapAllocation no_allocation;
588
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 // (T1 \/ ... \/ Tn) overlaps T if (T1 overlaps T) \/ ... \/ (Tn overlaps T)
590 if (this->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400591 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 if (this->AsUnion()->Get(i)->SemanticMaybe(that)) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 }
594 return false;
595 }
596
597 // T overlaps (T1 \/ ... \/ Tn) if (T overlaps T1) \/ ... \/ (T overlaps Tn)
598 if (that->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400599 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100600 if (this->SemanticMaybe(that->AsUnion()->Get(i))) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 }
602 return false;
603 }
604
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000605 if (!BitsetType::SemanticIsInhabited(this->BitsetLub() & that->BitsetLub()))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607
608 if (this->IsBitset() && that->IsBitset()) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609
610 if (this->IsClass() != that->IsClass()) return true;
611
612 if (this->IsRange()) {
613 if (that->IsConstant()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 return Contains(this->AsRange(), that->AsConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616 if (that->IsRange()) {
617 return Overlap(this->AsRange(), that->AsRange());
618 }
619 if (that->IsBitset()) {
620 bitset number_bits = BitsetType::NumberBits(that->AsBitset());
621 if (number_bits == BitsetType::kNone) {
622 return false;
623 }
624 double min = std::max(BitsetType::Min(number_bits), this->Min());
625 double max = std::min(BitsetType::Max(number_bits), this->Max());
626 return min <= max;
627 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 }
629 if (that->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000630 return that->SemanticMaybe(this); // This case is handled above.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 }
632
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000633 if (this->IsBitset() || that->IsBitset()) return true;
634
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 return this->SimplyEquals(that);
636}
637
638
639// Return the range in [this], or [NULL].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100640Type* Type::GetRange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 DisallowHeapAllocation no_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100642 if (this->IsRange()) return this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 if (this->IsUnion() && this->AsUnion()->Get(1)->IsRange()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100644 return this->AsUnion()->Get(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 }
646 return NULL;
647}
648
Ben Murdoch097c5b22016-05-18 11:27:45 +0100649bool Type::Contains(i::Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 DisallowHeapAllocation no_allocation;
651 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
652 if (*it.Current() == value) return true;
653 }
654 if (IsInteger(value)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100655 Type* range = this->GetRange();
656 if (range != NULL && Contains(range->AsRange(), value)) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657 }
658 return BitsetType::New(BitsetType::Lub(value))->Is(this);
659}
660
Ben Murdoch097c5b22016-05-18 11:27:45 +0100661bool UnionType::Wellformed() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 DisallowHeapAllocation no_allocation;
663 // This checks the invariants of the union representation:
664 // 1. There are at least two elements.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000665 // 2. The first element is a bitset, no other element is a bitset.
666 // 3. At most one element is a range, and it must be the second one.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667 // 4. No element is itself a union.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000668 // 5. No element (except the bitset) is a subtype of any other.
669 // 6. If there is a range, then the bitset type does not contain
670 // plain number bits.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671 DCHECK(this->Length() >= 2); // (1)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000672 DCHECK(this->Get(0)->IsBitset()); // (2a)
673
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 for (int i = 0; i < this->Length(); ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000675 if (i != 0) DCHECK(!this->Get(i)->IsBitset()); // (2b)
676 if (i != 1) DCHECK(!this->Get(i)->IsRange()); // (3)
677 DCHECK(!this->Get(i)->IsUnion()); // (4)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 for (int j = 0; j < this->Length(); ++j) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000679 if (i != j && i != 0)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100680 DCHECK(!this->Get(i)->SemanticIs(this->Get(j))); // (5)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681 }
682 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000683 DCHECK(!this->Get(1)->IsRange() ||
684 (BitsetType::NumberBits(this->Get(0)->AsBitset()) ==
685 BitsetType::kNone)); // (6)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000686 return true;
687}
688
689
690// -----------------------------------------------------------------------------
691// Union and intersection
692
693
694static bool AddIsSafe(int x, int y) {
695 return x >= 0 ?
696 y <= std::numeric_limits<int>::max() - x :
697 y >= std::numeric_limits<int>::min() - x;
698}
699
Ben Murdoch097c5b22016-05-18 11:27:45 +0100700Type* Type::Intersect(Type* type1, Type* type2, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701 // Fast case: bit sets.
702 if (type1->IsBitset() && type2->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100703 return BitsetType::New(type1->AsBitset() & type2->AsBitset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 }
705
706 // Fast case: top or bottom types.
707 if (type1->IsNone() || type2->IsAny()) return type1; // Shortcut.
708 if (type2->IsNone() || type1->IsAny()) return type2; // Shortcut.
709
710 // Semi-fast case.
711 if (type1->Is(type2)) return type1;
712 if (type2->Is(type1)) return type2;
713
714 // Slow case: create union.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000715
716 // Figure out the representation of the result first.
717 // The rest of the method should not change this representation and
718 // it should not make any decisions based on representations (i.e.,
719 // it should only use the semantic part of types).
720 const bitset representation =
721 type1->Representation() & type2->Representation();
722
723 // Semantic subtyping check - this is needed for consistency with the
724 // semi-fast case above - we should behave the same way regardless of
725 // representations. Intersection with a universal bitset should only update
726 // the representations.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100727 if (type1->SemanticIs(type2)) {
728 type2 = Any();
729 } else if (type2->SemanticIs(type1)) {
730 type1 = Any();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000731 }
732
733 bitset bits =
734 SEMANTIC(type1->BitsetGlb() & type2->BitsetGlb()) | representation;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
736 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100737 if (!AddIsSafe(size1, size2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 int size = size1 + size2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100739 if (!AddIsSafe(size, 2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 size += 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100741 Type* result_type = UnionType::New(size, zone);
742 UnionType* result = result_type->AsUnion();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743 size = 0;
744
745 // Deal with bitsets.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100746 result->Set(size++, BitsetType::New(bits));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747
Ben Murdoch097c5b22016-05-18 11:27:45 +0100748 RangeType::Limits lims = RangeType::Limits::Empty();
749 size = IntersectAux(type1, type2, result, size, &lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000751 // If the range is not empty, then insert it into the union and
752 // remove the number bits from the bitset.
753 if (!lims.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100754 size = UpdateRange(RangeType::New(lims, representation, zone), result, size,
755 zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000756
757 // Remove the number bits.
758 bitset number_bits = BitsetType::NumberBits(bits);
759 bits &= ~number_bits;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100760 result->Set(0, BitsetType::New(bits));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000761 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100762 return NormalizeUnion(result_type, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763}
764
Ben Murdoch097c5b22016-05-18 11:27:45 +0100765int Type::UpdateRange(Type* range, UnionType* result, int size, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000766 if (size == 1) {
767 result->Set(size++, range);
768 } else {
769 // Make space for the range.
770 result->Set(size++, result->Get(1));
771 result->Set(1, range);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000773
774 // Remove any components that just got subsumed.
775 for (int i = 2; i < size; ) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100776 if (result->Get(i)->SemanticIs(range)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 result->Set(i, result->Get(--size));
778 } else {
779 ++i;
780 }
781 }
782 return size;
783}
784
Ben Murdoch097c5b22016-05-18 11:27:45 +0100785RangeType::Limits Type::ToLimits(bitset bits, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000786 bitset number_bits = BitsetType::NumberBits(bits);
787
788 if (number_bits == BitsetType::kNone) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 return RangeType::Limits::Empty();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000790 }
791
Ben Murdoch097c5b22016-05-18 11:27:45 +0100792 return RangeType::Limits(BitsetType::Min(number_bits),
793 BitsetType::Max(number_bits));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794}
795
Ben Murdoch097c5b22016-05-18 11:27:45 +0100796RangeType::Limits Type::IntersectRangeAndBitset(Type* range, Type* bitset,
797 Zone* zone) {
798 RangeType::Limits range_lims(range->AsRange());
799 RangeType::Limits bitset_lims = ToLimits(bitset->AsBitset(), zone);
800 return RangeType::Limits::Intersect(range_lims, bitset_lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000801}
802
Ben Murdoch097c5b22016-05-18 11:27:45 +0100803int Type::IntersectAux(Type* lhs, Type* rhs, UnionType* result, int size,
804 RangeType::Limits* lims, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 if (lhs->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400806 for (int i = 0, n = lhs->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000807 size =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100808 IntersectAux(lhs->AsUnion()->Get(i), rhs, result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 }
810 return size;
811 }
812 if (rhs->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400813 for (int i = 0, n = rhs->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000814 size =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100815 IntersectAux(lhs, rhs->AsUnion()->Get(i), result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 }
817 return size;
818 }
819
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000820 if (!BitsetType::SemanticIsInhabited(lhs->BitsetLub() & rhs->BitsetLub())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 return size;
822 }
823
824 if (lhs->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000825 if (rhs->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100826 RangeType::Limits lim = IntersectRangeAndBitset(lhs, rhs, zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827
828 if (!lim.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100829 *lims = RangeType::Limits::Union(lim, *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000830 }
831 return size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000832 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000833 if (rhs->IsClass()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100834 *lims =
835 RangeType::Limits::Union(RangeType::Limits(lhs->AsRange()), *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000836 }
837 if (rhs->IsConstant() && Contains(lhs->AsRange(), rhs->AsConstant())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100838 return AddToUnion(rhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000840 if (rhs->IsRange()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100841 RangeType::Limits lim = RangeType::Limits::Intersect(
842 RangeType::Limits(lhs->AsRange()), RangeType::Limits(rhs->AsRange()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000843 if (!lim.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100844 *lims = RangeType::Limits::Union(lim, *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000845 }
846 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000847 return size;
848 }
849 if (rhs->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000850 // This case is handled symmetrically above.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100851 return IntersectAux(rhs, lhs, result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 if (lhs->IsBitset() || rhs->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100854 return AddToUnion(lhs->IsBitset() ? rhs : lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 }
856 if (lhs->IsClass() != rhs->IsClass()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100857 return AddToUnion(lhs->IsClass() ? rhs : lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100859 if (lhs->SimplyEquals(rhs)) {
860 return AddToUnion(lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861 }
862 return size;
863}
864
865
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000866// Make sure that we produce a well-formed range and bitset:
867// If the range is non-empty, the number bits in the bitset should be
868// clear. Moreover, if we have a canonical range (such as Signed32),
869// we want to produce a bitset rather than a range.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100870Type* Type::NormalizeRangeAndBitset(Type* range, bitset* bits, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000871 // Fast path: If the bitset does not mention numbers, we can just keep the
872 // range.
873 bitset number_bits = BitsetType::NumberBits(*bits);
874 if (number_bits == 0) {
875 return range;
876 }
877
878 // If the range is semantically contained within the bitset, return None and
879 // leave the bitset untouched.
880 bitset range_lub = SEMANTIC(range->BitsetLub());
881 if (BitsetType::Is(range_lub, *bits)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100882 return None();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000883 }
884
885 // Slow path: reconcile the bitset range and the range.
886 double bitset_min = BitsetType::Min(number_bits);
887 double bitset_max = BitsetType::Max(number_bits);
888
889 double range_min = range->Min();
890 double range_max = range->Max();
891
892 // Remove the number bits from the bitset, they would just confuse us now.
893 // NOTE: bits contains OtherNumber iff bits contains PlainNumber, in which
894 // case we already returned after the subtype check above.
895 *bits &= ~number_bits;
896
897 if (range_min <= bitset_min && range_max >= bitset_max) {
898 // Bitset is contained within the range, just return the range.
899 return range;
900 }
901
902 if (bitset_min < range_min) {
903 range_min = bitset_min;
904 }
905 if (bitset_max > range_max) {
906 range_max = bitset_max;
907 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100908 return RangeType::New(range_min, range_max, BitsetType::kNone, zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000909}
910
Ben Murdoch097c5b22016-05-18 11:27:45 +0100911Type* Type::Union(Type* type1, Type* type2, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 // Fast case: bit sets.
913 if (type1->IsBitset() && type2->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100914 return BitsetType::New(type1->AsBitset() | type2->AsBitset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 }
916
917 // Fast case: top or bottom types.
918 if (type1->IsAny() || type2->IsNone()) return type1;
919 if (type2->IsAny() || type1->IsNone()) return type2;
920
921 // Semi-fast case.
922 if (type1->Is(type2)) return type2;
923 if (type2->Is(type1)) return type1;
924
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000925 // Figure out the representation of the result.
926 // The rest of the method should not change this representation and
927 // it should not make any decisions based on representations (i.e.,
928 // it should only use the semantic part of types).
929 const bitset representation =
930 type1->Representation() | type2->Representation();
931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 // Slow case: create union.
933 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
934 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100935 if (!AddIsSafe(size1, size2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 int size = size1 + size2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100937 if (!AddIsSafe(size, 2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 size += 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100939 Type* result_type = UnionType::New(size, zone);
940 UnionType* result = result_type->AsUnion();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 size = 0;
942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000943 // Compute the new bitset.
944 bitset new_bitset = SEMANTIC(type1->BitsetGlb() | type2->BitsetGlb());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945
946 // Deal with ranges.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100947 Type* range = None();
948 Type* range1 = type1->GetRange();
949 Type* range2 = type2->GetRange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950 if (range1 != NULL && range2 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100951 RangeType::Limits lims =
952 RangeType::Limits::Union(RangeType::Limits(range1->AsRange()),
953 RangeType::Limits(range2->AsRange()));
954 Type* union_range = RangeType::New(lims, representation, zone);
955 range = NormalizeRangeAndBitset(union_range, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956 } else if (range1 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100957 range = NormalizeRangeAndBitset(range1, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000958 } else if (range2 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100959 range = NormalizeRangeAndBitset(range2, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000961 new_bitset = SEMANTIC(new_bitset) | representation;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100962 Type* bits = BitsetType::New(new_bitset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000963 result->Set(size++, bits);
964 if (!range->IsNone()) result->Set(size++, range);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000965
Ben Murdoch097c5b22016-05-18 11:27:45 +0100966 size = AddToUnion(type1, result, size, zone);
967 size = AddToUnion(type2, result, size, zone);
968 return NormalizeUnion(result_type, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969}
970
971
972// Add [type] to [result] unless [type] is bitset, range, or already subsumed.
973// Return new size of [result].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100974int Type::AddToUnion(Type* type, UnionType* result, int size, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000975 if (type->IsBitset() || type->IsRange()) return size;
976 if (type->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400977 for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100978 size = AddToUnion(type->AsUnion()->Get(i), result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 }
980 return size;
981 }
982 for (int i = 0; i < size; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100983 if (type->SemanticIs(result->Get(i))) return size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984 }
985 result->Set(size++, type);
986 return size;
987}
988
Ben Murdoch097c5b22016-05-18 11:27:45 +0100989Type* Type::NormalizeUnion(Type* union_type, int size, Zone* zone) {
990 UnionType* unioned = union_type->AsUnion();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000991 DCHECK(size >= 1);
992 DCHECK(unioned->Get(0)->IsBitset());
993 // If the union has just one element, return it.
994 if (size == 1) {
995 return unioned->Get(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000997 bitset bits = unioned->Get(0)->AsBitset();
998 // If the union only consists of a range, we can get rid of the union.
999 if (size == 2 && SEMANTIC(bits) == BitsetType::kNone) {
1000 bitset representation = REPRESENTATION(bits);
1001 if (representation == unioned->Get(1)->Representation()) {
1002 return unioned->Get(1);
1003 }
1004 if (unioned->Get(1)->IsRange()) {
1005 return RangeType::New(unioned->Get(1)->AsRange()->Min(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01001006 unioned->Get(1)->AsRange()->Max(),
1007 unioned->Get(0)->AsBitset(), zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001008 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001009 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 unioned->Shrink(size);
1011 SLOW_DCHECK(unioned->Wellformed());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001012 return union_type;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013}
1014
1015
1016// -----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001017// Component extraction
1018
1019// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01001020Type* Type::Representation(Type* t, Zone* zone) {
1021 return BitsetType::New(t->Representation());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022}
1023
1024
1025// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01001026Type* Type::Semantic(Type* t, Zone* zone) {
1027 return Intersect(t, BitsetType::New(BitsetType::kSemantic), zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028}
1029
1030
1031// -----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032// Iteration.
1033
Ben Murdoch097c5b22016-05-18 11:27:45 +01001034int Type::NumClasses() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 DisallowHeapAllocation no_allocation;
1036 if (this->IsClass()) {
1037 return 1;
1038 } else if (this->IsUnion()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001039 int result = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001040 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
1041 if (this->AsUnion()->Get(i)->IsClass()) ++result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001042 }
1043 return result;
1044 } else {
1045 return 0;
1046 }
1047}
1048
Ben Murdoch097c5b22016-05-18 11:27:45 +01001049int Type::NumConstants() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 DisallowHeapAllocation no_allocation;
1051 if (this->IsConstant()) {
1052 return 1;
1053 } else if (this->IsUnion()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001054 int result = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001055 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
1056 if (this->AsUnion()->Get(i)->IsConstant()) ++result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 }
1058 return result;
1059 } else {
1060 return 0;
1061 }
1062}
1063
Ben Murdoch097c5b22016-05-18 11:27:45 +01001064template <class T>
1065Type* Type::Iterator<T>::get_type() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001066 DCHECK(!Done());
1067 return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_;
1068}
1069
1070
1071// C++ cannot specialise nested templates, so we have to go through this
1072// contortion with an auxiliary template to simulate it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001073template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001074struct TypeImplIteratorAux {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001075 static bool matches(Type* type);
1076 static i::Handle<T> current(Type* type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001077};
1078
Ben Murdoch097c5b22016-05-18 11:27:45 +01001079template <>
1080struct TypeImplIteratorAux<i::Map> {
1081 static bool matches(Type* type) { return type->IsClass(); }
1082 static i::Handle<i::Map> current(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 return type->AsClass()->Map();
1084 }
1085};
1086
Ben Murdoch097c5b22016-05-18 11:27:45 +01001087template <>
1088struct TypeImplIteratorAux<i::Object> {
1089 static bool matches(Type* type) { return type->IsConstant(); }
1090 static i::Handle<i::Object> current(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001091 return type->AsConstant()->Value();
1092 }
1093};
1094
Ben Murdoch097c5b22016-05-18 11:27:45 +01001095template <class T>
1096bool Type::Iterator<T>::matches(Type* type) {
1097 return TypeImplIteratorAux<T>::matches(type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001098}
1099
Ben Murdoch097c5b22016-05-18 11:27:45 +01001100template <class T>
1101i::Handle<T> Type::Iterator<T>::Current() {
1102 return TypeImplIteratorAux<T>::current(get_type());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103}
1104
Ben Murdoch097c5b22016-05-18 11:27:45 +01001105template <class T>
1106void Type::Iterator<T>::Advance() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107 DisallowHeapAllocation no_allocation;
1108 ++index_;
1109 if (type_->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001110 for (int n = type_->AsUnion()->Length(); index_ < n; ++index_) {
1111 if (matches(type_->AsUnion()->Get(index_))) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001112 }
1113 } else if (index_ == 0 && matches(type_)) {
1114 return;
1115 }
1116 index_ = -1;
1117}
1118
1119
1120// -----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121// Printing.
1122
Ben Murdoch097c5b22016-05-18 11:27:45 +01001123const char* BitsetType::Name(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124 switch (bits) {
1125 case REPRESENTATION(kAny): return "Any";
1126 #define RETURN_NAMED_REPRESENTATION_TYPE(type, value) \
1127 case REPRESENTATION(k##type): return #type;
1128 REPRESENTATION_BITSET_TYPE_LIST(RETURN_NAMED_REPRESENTATION_TYPE)
1129 #undef RETURN_NAMED_REPRESENTATION_TYPE
1130
1131 #define RETURN_NAMED_SEMANTIC_TYPE(type, value) \
1132 case SEMANTIC(k##type): return #type;
1133 SEMANTIC_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001134 INTERNAL_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001135 #undef RETURN_NAMED_SEMANTIC_TYPE
1136
1137 default:
1138 return NULL;
1139 }
1140}
1141
Ben Murdoch097c5b22016-05-18 11:27:45 +01001142void BitsetType::Print(std::ostream& os, // NOLINT
1143 bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001144 DisallowHeapAllocation no_allocation;
1145 const char* name = Name(bits);
1146 if (name != NULL) {
1147 os << name;
1148 return;
1149 }
1150
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001151 // clang-format off
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001152 static const bitset named_bitsets[] = {
1153#define BITSET_CONSTANT(type, value) REPRESENTATION(k##type),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154 REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001155#undef BITSET_CONSTANT
1156
1157#define BITSET_CONSTANT(type, value) SEMANTIC(k##type),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001158 INTERNAL_BITSET_TYPE_LIST(BITSET_CONSTANT)
1159 SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160#undef BITSET_CONSTANT
1161 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001162 // clang-format on
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001163
1164 bool is_first = true;
1165 os << "(";
1166 for (int i(arraysize(named_bitsets) - 1); bits != 0 && i >= 0; --i) {
1167 bitset subset = named_bitsets[i];
1168 if ((bits & subset) == subset) {
1169 if (!is_first) os << " | ";
1170 is_first = false;
1171 os << Name(subset);
1172 bits -= subset;
1173 }
1174 }
1175 DCHECK(bits == 0);
1176 os << ")";
1177}
1178
Ben Murdoch097c5b22016-05-18 11:27:45 +01001179void Type::PrintTo(std::ostream& os, PrintDimension dim) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001180 DisallowHeapAllocation no_allocation;
1181 if (dim != REPRESENTATION_DIM) {
1182 if (this->IsBitset()) {
1183 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
1184 } else if (this->IsClass()) {
1185 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
1186 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
1187 os << ")";
1188 } else if (this->IsConstant()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001189 os << "Constant(" << Brief(*this->AsConstant()->Value()) << ")";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190 } else if (this->IsRange()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001191 std::ostream::fmtflags saved_flags = os.setf(std::ios::fixed);
1192 std::streamsize saved_precision = os.precision(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001193 os << "Range(" << this->AsRange()->Min() << ", " << this->AsRange()->Max()
1194 << ")";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001195 os.flags(saved_flags);
1196 os.precision(saved_precision);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001197 } else if (this->IsContext()) {
1198 os << "Context(";
1199 this->AsContext()->Outer()->PrintTo(os, dim);
1200 os << ")";
1201 } else if (this->IsUnion()) {
1202 os << "(";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001203 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001204 Type* type_i = this->AsUnion()->Get(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205 if (i > 0) os << " | ";
1206 type_i->PrintTo(os, dim);
1207 }
1208 os << ")";
1209 } else if (this->IsArray()) {
1210 os << "Array(";
1211 AsArray()->Element()->PrintTo(os, dim);
1212 os << ")";
1213 } else if (this->IsFunction()) {
1214 if (!this->AsFunction()->Receiver()->IsAny()) {
1215 this->AsFunction()->Receiver()->PrintTo(os, dim);
1216 os << ".";
1217 }
1218 os << "(";
1219 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
1220 if (i > 0) os << ", ";
1221 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
1222 }
1223 os << ")->";
1224 this->AsFunction()->Result()->PrintTo(os, dim);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001225 } else if (this->IsTuple()) {
1226 os << "<";
1227 for (int i = 0, n = this->AsTuple()->Arity(); i < n; ++i) {
1228 Type* type_i = this->AsTuple()->Element(i);
1229 if (i > 0) os << ", ";
1230 type_i->PrintTo(os, dim);
1231 }
1232 os << ">";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 } else {
1234 UNREACHABLE();
1235 }
1236 }
1237 if (dim == BOTH_DIMS) os << "/";
1238 if (dim != SEMANTIC_DIM) {
1239 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
1240 }
1241}
1242
1243
1244#ifdef DEBUG
Ben Murdoch097c5b22016-05-18 11:27:45 +01001245void Type::Print() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 OFStream os(stdout);
1247 PrintTo(os);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001248 os << std::endl;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249}
Ben Murdoch097c5b22016-05-18 11:27:45 +01001250void BitsetType::Print(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001251 OFStream os(stdout);
1252 Print(os, bits);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001253 os << std::endl;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001254}
1255#endif
1256
Ben Murdoch097c5b22016-05-18 11:27:45 +01001257BitsetType::bitset BitsetType::SignedSmall() {
1258 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32;
1259}
1260
1261BitsetType::bitset BitsetType::UnsignedSmall() {
1262 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31;
1263}
1264
1265#define CONSTRUCT_SIMD_TYPE(NAME, Name, name, lane_count, lane_type) \
1266 Type* Type::Name(Isolate* isolate, Zone* zone) { \
1267 return Class(i::handle(isolate->heap()->name##_map()), zone); \
1268 }
1269SIMD128_TYPES(CONSTRUCT_SIMD_TYPE)
1270#undef CONSTRUCT_SIMD_TYPE
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271
1272// -----------------------------------------------------------------------------
1273// Instantiations.
1274
Ben Murdoch097c5b22016-05-18 11:27:45 +01001275template class Type::Iterator<i::Map>;
1276template class Type::Iterator<i::Object>;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001277
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001278} // namespace internal
1279} // namespace v8