blob: d54826e34efc9a1c97d15a5bb3f9fbf89b74f4ae [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() ||
194 map == heap->arguments_marker_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400195 return kInternal & kTaggedPointer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 }
197 case HEAP_NUMBER_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400198 return kNumber & kTaggedPointer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 case SIMD128_VALUE_TYPE:
200 return kSimd;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 case JS_VALUE_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202 case JS_MESSAGE_OBJECT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 case JS_DATE_TYPE:
204 case JS_OBJECT_TYPE:
205 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
206 case JS_GENERATOR_OBJECT_TYPE:
207 case JS_MODULE_TYPE:
208 case JS_GLOBAL_OBJECT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 case JS_GLOBAL_PROXY_TYPE:
210 case JS_ARRAY_BUFFER_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211 case JS_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 case JS_TYPED_ARRAY_TYPE:
213 case JS_DATA_VIEW_TYPE:
214 case JS_SET_TYPE:
215 case JS_MAP_TYPE:
216 case JS_SET_ITERATOR_TYPE:
217 case JS_MAP_ITERATOR_TYPE:
218 case JS_WEAK_MAP_TYPE:
219 case JS_WEAK_SET_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 case JS_PROMISE_TYPE:
221 case JS_BOUND_FUNCTION_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 if (map->is_undetectable()) return kUndetectable;
223 return kOtherObject;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 case JS_FUNCTION_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000225 if (map->is_undetectable()) return kUndetectable;
226 return kFunction;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 case JS_REGEXP_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400228 return kOtherObject; // TODO(rossberg): there should be a RegExp type.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 case JS_PROXY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 return kProxy;
231 case MAP_TYPE:
232 // When compiling stub templates, the meta map is used as a place holder
233 // for the actual map with which the template is later instantiated.
234 // We treat it as a kind of type variable whose upper bound is Any.
235 // TODO(rossberg): for caching of CompareNilIC stubs to work correctly,
236 // we must exclude Undetectable here. This makes no sense, really,
237 // because it means that the template isn't actually parametric.
238 // Also, it doesn't apply elsewhere. 8-(
239 // We ought to find a cleaner solution for compiling stubs parameterised
240 // over type or class variables, esp ones with bounds...
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 return kDetectable & kTaggedPointer;
242 case ALLOCATION_SITE_TYPE:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100243 case ACCESSOR_INFO_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 case SHARED_FUNCTION_INFO_TYPE:
245 case ACCESSOR_PAIR_TYPE:
246 case FIXED_ARRAY_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000247 case FIXED_DOUBLE_ARRAY_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400248 case BYTE_ARRAY_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 case BYTECODE_ARRAY_TYPE:
250 case TRANSITION_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 case FOREIGN_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 case SCRIPT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 case CODE_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 case PROPERTY_CELL_TYPE:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400255 return kInternal & kTaggedPointer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000256
257 // Remaining instance types are unsupported for now. If any of them do
258 // require bit set types, they should get kInternal & kTaggedPointer.
259 case MUTABLE_HEAP_NUMBER_TYPE:
260 case FREE_SPACE_TYPE:
261#define FIXED_TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
262 case FIXED_##TYPE##_ARRAY_TYPE:
263
264 TYPED_ARRAYS(FIXED_TYPED_ARRAY_CASE)
265#undef FIXED_TYPED_ARRAY_CASE
266 case FILLER_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000267 case ACCESS_CHECK_INFO_TYPE:
268 case INTERCEPTOR_INFO_TYPE:
269 case CALL_HANDLER_INFO_TYPE:
270 case FUNCTION_TEMPLATE_INFO_TYPE:
271 case OBJECT_TEMPLATE_INFO_TYPE:
272 case SIGNATURE_INFO_TYPE:
273 case TYPE_SWITCH_INFO_TYPE:
274 case ALLOCATION_MEMENTO_TYPE:
275 case CODE_CACHE_TYPE:
276 case POLYMORPHIC_CODE_CACHE_TYPE:
277 case TYPE_FEEDBACK_INFO_TYPE:
278 case ALIASED_ARGUMENTS_ENTRY_TYPE:
279 case BOX_TYPE:
280 case DEBUG_INFO_TYPE:
281 case BREAK_POINT_INFO_TYPE:
282 case CELL_TYPE:
283 case WEAK_CELL_TYPE:
284 case PROTOTYPE_INFO_TYPE:
285 case SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 UNREACHABLE();
287 return kNone;
288 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 UNREACHABLE();
290 return kNone;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291}
292
Ben Murdoch097c5b22016-05-18 11:27:45 +0100293Type::bitset BitsetType::Lub(i::Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 DisallowHeapAllocation no_allocation;
295 if (value->IsNumber()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400296 return Lub(value->Number()) &
297 (value->IsSmi() ? kTaggedSigned : kTaggedPointer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 }
299 return Lub(i::HeapObject::cast(value)->map());
300}
301
Ben Murdoch097c5b22016-05-18 11:27:45 +0100302Type::bitset BitsetType::Lub(double value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303 DisallowHeapAllocation no_allocation;
304 if (i::IsMinusZero(value)) return kMinusZero;
305 if (std::isnan(value)) return kNaN;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400306 if (IsUint32Double(value) || IsInt32Double(value)) return Lub(value, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307 return kOtherNumber;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308}
309
310
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311// Minimum values of plain numeric bitsets.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100312const BitsetType::Boundary BitsetType::BoundariesArray[] = {
313 {kOtherNumber, kPlainNumber, -V8_INFINITY},
314 {kOtherSigned32, kNegative32, kMinInt},
315 {kNegative31, kNegative31, -0x40000000},
316 {kUnsigned30, kUnsigned30, 0},
317 {kOtherUnsigned31, kUnsigned31, 0x40000000},
318 {kOtherUnsigned32, kUnsigned32, 0x80000000},
319 {kOtherNumber, kPlainNumber, static_cast<double>(kMaxUInt32) + 1}};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320
Ben Murdoch097c5b22016-05-18 11:27:45 +0100321const BitsetType::Boundary* BitsetType::Boundaries() { return BoundariesArray; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322
Ben Murdoch097c5b22016-05-18 11:27:45 +0100323size_t BitsetType::BoundariesSize() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 // Windows doesn't like arraysize here.
325 // return arraysize(BoundariesArray);
326 return 7;
327}
328
Ben Murdoch097c5b22016-05-18 11:27:45 +0100329Type::bitset BitsetType::ExpandInternals(Type::bitset bits) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 DisallowHeapAllocation no_allocation;
331 if (!(bits & SEMANTIC(kPlainNumber))) return bits; // Shortcut.
332 const Boundary* boundaries = Boundaries();
333 for (size_t i = 0; i < BoundariesSize(); ++i) {
334 DCHECK(BitsetType::Is(boundaries[i].internal, boundaries[i].external));
335 if (bits & SEMANTIC(boundaries[i].internal))
336 bits |= SEMANTIC(boundaries[i].external);
337 }
338 return bits;
339}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340
Ben Murdoch097c5b22016-05-18 11:27:45 +0100341Type::bitset BitsetType::Lub(double min, double max) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342 DisallowHeapAllocation no_allocation;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 int lub = kNone;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000344 const Boundary* mins = Boundaries();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000346 for (size_t i = 1; i < BoundariesSize(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 if (min < mins[i].min) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 lub |= mins[i-1].internal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 if (max < mins[i].min) return lub;
350 }
351 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 return lub | mins[BoundariesSize() - 1].internal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353}
354
Ben Murdoch097c5b22016-05-18 11:27:45 +0100355Type::bitset BitsetType::NumberBits(bitset bits) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000356 return SEMANTIC(bits & kPlainNumber);
357}
358
Ben Murdoch097c5b22016-05-18 11:27:45 +0100359Type::bitset BitsetType::Glb(double min, double max) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360 DisallowHeapAllocation no_allocation;
361 int glb = kNone;
362 const Boundary* mins = Boundaries();
363
364 // If the range does not touch 0, the bound is empty.
365 if (max < -1 || min > 0) return glb;
366
367 for (size_t i = 1; i + 1 < BoundariesSize(); ++i) {
368 if (min <= mins[i].min) {
369 if (max + 1 < mins[i + 1].min) break;
370 glb |= mins[i].external;
371 }
372 }
373 // OtherNumber also contains float numbers, so it can never be
374 // in the greatest lower bound.
375 return glb & ~(SEMANTIC(kOtherNumber));
376}
377
Ben Murdoch097c5b22016-05-18 11:27:45 +0100378double BitsetType::Min(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000380 DCHECK(Is(SEMANTIC(bits), kNumber));
381 const Boundary* mins = Boundaries();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 bool mz = SEMANTIC(bits & kMinusZero);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 for (size_t i = 0; i < BoundariesSize(); ++i) {
384 if (Is(SEMANTIC(mins[i].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385 return mz ? std::min(0.0, mins[i].min) : mins[i].min;
386 }
387 }
388 if (mz) return 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390}
391
Ben Murdoch097c5b22016-05-18 11:27:45 +0100392double BitsetType::Max(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000394 DCHECK(Is(SEMANTIC(bits), kNumber));
395 const Boundary* mins = Boundaries();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400396 bool mz = SEMANTIC(bits & kMinusZero);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397 if (BitsetType::Is(SEMANTIC(mins[BoundariesSize() - 1].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 return +V8_INFINITY;
399 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 for (size_t i = BoundariesSize() - 1; i-- > 0;) {
401 if (Is(SEMANTIC(mins[i].internal), bits)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402 return mz ?
403 std::max(0.0, mins[i+1].min - 1) : mins[i+1].min - 1;
404 }
405 }
406 if (mz) return 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408}
409
410
411// -----------------------------------------------------------------------------
412// Predicates.
413
Ben Murdoch097c5b22016-05-18 11:27:45 +0100414bool Type::SimplyEquals(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 DisallowHeapAllocation no_allocation;
416 if (this->IsClass()) {
417 return that->IsClass()
418 && *this->AsClass()->Map() == *that->AsClass()->Map();
419 }
420 if (this->IsConstant()) {
421 return that->IsConstant()
422 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
423 }
424 if (this->IsContext()) {
425 return that->IsContext()
426 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
427 }
428 if (this->IsArray()) {
429 return that->IsArray()
430 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
431 }
432 if (this->IsFunction()) {
433 if (!that->IsFunction()) return false;
434 FunctionType* this_fun = this->AsFunction();
435 FunctionType* that_fun = that->AsFunction();
436 if (this_fun->Arity() != that_fun->Arity() ||
437 !this_fun->Result()->Equals(that_fun->Result()) ||
438 !this_fun->Receiver()->Equals(that_fun->Receiver())) {
439 return false;
440 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400441 for (int i = 0, n = this_fun->Arity(); i < n; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 if (!this_fun->Parameter(i)->Equals(that_fun->Parameter(i))) return false;
443 }
444 return true;
445 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100446 if (this->IsTuple()) {
447 if (!that->IsTuple()) return false;
448 TupleType* this_tuple = this->AsTuple();
449 TupleType* that_tuple = that->AsTuple();
450 if (this_tuple->Arity() != that_tuple->Arity()) {
451 return false;
452 }
453 for (int i = 0, n = this_tuple->Arity(); i < n; ++i) {
454 if (!this_tuple->Element(i)->Equals(that_tuple->Element(i))) return false;
455 }
456 return true;
457 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 UNREACHABLE();
459 return false;
460}
461
Ben Murdoch097c5b22016-05-18 11:27:45 +0100462Type::bitset Type::Representation() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 return REPRESENTATION(this->BitsetLub());
464}
465
466
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467// Check if [this] <= [that].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100468bool Type::SlowIs(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 DisallowHeapAllocation no_allocation;
470
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000471 // Fast bitset cases
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 if (that->IsBitset()) {
473 return BitsetType::Is(this->BitsetLub(), that->AsBitset());
474 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 if (this->IsBitset()) {
477 return BitsetType::Is(this->AsBitset(), that->BitsetGlb());
478 }
479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480 // Check the representations.
481 if (!BitsetType::Is(Representation(), that->Representation())) {
482 return false;
483 }
484
485 // Check the semantic part.
486 return SemanticIs(that);
487}
488
489
490// Check if SEMANTIC([this]) <= SEMANTIC([that]). The result of the method
491// should be independent of the representation axis of the types.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100492bool Type::SemanticIs(Type* that) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000493 DisallowHeapAllocation no_allocation;
494
495 if (this == that) return true;
496
497 if (that->IsBitset()) {
498 return BitsetType::Is(SEMANTIC(this->BitsetLub()), that->AsBitset());
499 }
500 if (this->IsBitset()) {
501 return BitsetType::Is(SEMANTIC(this->AsBitset()), that->BitsetGlb());
502 }
503
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 // (T1 \/ ... \/ Tn) <= T if (T1 <= T) /\ ... /\ (Tn <= T)
505 if (this->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400506 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507 if (!this->AsUnion()->Get(i)->SemanticIs(that)) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 }
509 return true;
510 }
511
512 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn)
513 if (that->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400514 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100515 if (this->SemanticIs(that->AsUnion()->Get(i))) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 if (i > 1 && this->IsRange()) return false; // Shortcut.
517 }
518 return false;
519 }
520
521 if (that->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000522 return (this->IsRange() && Contains(that->AsRange(), this->AsRange())) ||
523 (this->IsConstant() &&
524 Contains(that->AsRange(), this->AsConstant()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 }
526 if (this->IsRange()) return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400527
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 return this->SimplyEquals(that);
529}
530
Ben Murdoch097c5b22016-05-18 11:27:45 +0100531// Most precise _current_ type of a value (usually its class).
532Type* Type::NowOf(i::Object* value, Zone* zone) {
533 if (value->IsSmi() ||
534 i::HeapObject::cast(value)->map()->instance_type() == HEAP_NUMBER_TYPE) {
535 return Of(value, zone);
536 }
537 return Class(i::handle(i::HeapObject::cast(value)->map()), zone);
538}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539
Ben Murdoch097c5b22016-05-18 11:27:45 +0100540bool Type::NowContains(i::Object* value) {
541 DisallowHeapAllocation no_allocation;
542 if (this->IsAny()) return true;
543 if (value->IsHeapObject()) {
544 i::Map* map = i::HeapObject::cast(value)->map();
545 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
546 if (*it.Current() == map) return true;
547 }
548 }
549 return this->Contains(value);
550}
551
552bool Type::NowIs(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 DisallowHeapAllocation no_allocation;
554
555 // TODO(rossberg): this is incorrect for
556 // Union(Constant(V), T)->NowIs(Class(M))
557 // but fuzzing does not cover that!
558 if (this->IsConstant()) {
559 i::Object* object = *this->AsConstant()->Value();
560 if (object->IsHeapObject()) {
561 i::Map* map = i::HeapObject::cast(object)->map();
562 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
563 if (*it.Current() == map) return true;
564 }
565 }
566 }
567 return this->Is(that);
568}
569
570
571// Check if [this] contains only (currently) stable classes.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100572bool Type::NowStable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000573 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000574 return !this->IsClass() || this->AsClass()->Map()->is_stable();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575}
576
577
578// Check if [this] and [that] overlap.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100579bool Type::Maybe(Type* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 DisallowHeapAllocation no_allocation;
581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582 // Take care of the representation part (and also approximate
583 // the semantic part).
584 if (!BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()))
585 return false;
586
587 return SemanticMaybe(that);
588}
589
Ben Murdoch097c5b22016-05-18 11:27:45 +0100590bool Type::SemanticMaybe(Type* that) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000591 DisallowHeapAllocation no_allocation;
592
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 // (T1 \/ ... \/ Tn) overlaps T if (T1 overlaps T) \/ ... \/ (Tn overlaps T)
594 if (this->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400595 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000596 if (this->AsUnion()->Get(i)->SemanticMaybe(that)) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 }
598 return false;
599 }
600
601 // T overlaps (T1 \/ ... \/ Tn) if (T overlaps T1) \/ ... \/ (T overlaps Tn)
602 if (that->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400603 for (int i = 0, n = that->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100604 if (this->SemanticMaybe(that->AsUnion()->Get(i))) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605 }
606 return false;
607 }
608
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609 if (!BitsetType::SemanticIsInhabited(this->BitsetLub() & that->BitsetLub()))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611
612 if (this->IsBitset() && that->IsBitset()) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613
614 if (this->IsClass() != that->IsClass()) return true;
615
616 if (this->IsRange()) {
617 if (that->IsConstant()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000618 return Contains(this->AsRange(), that->AsConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620 if (that->IsRange()) {
621 return Overlap(this->AsRange(), that->AsRange());
622 }
623 if (that->IsBitset()) {
624 bitset number_bits = BitsetType::NumberBits(that->AsBitset());
625 if (number_bits == BitsetType::kNone) {
626 return false;
627 }
628 double min = std::max(BitsetType::Min(number_bits), this->Min());
629 double max = std::min(BitsetType::Max(number_bits), this->Max());
630 return min <= max;
631 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 }
633 if (that->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000634 return that->SemanticMaybe(this); // This case is handled above.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 }
636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000637 if (this->IsBitset() || that->IsBitset()) return true;
638
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639 return this->SimplyEquals(that);
640}
641
642
643// Return the range in [this], or [NULL].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100644Type* Type::GetRange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 DisallowHeapAllocation no_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100646 if (this->IsRange()) return this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 if (this->IsUnion() && this->AsUnion()->Get(1)->IsRange()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100648 return this->AsUnion()->Get(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649 }
650 return NULL;
651}
652
Ben Murdoch097c5b22016-05-18 11:27:45 +0100653bool Type::Contains(i::Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 DisallowHeapAllocation no_allocation;
655 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
656 if (*it.Current() == value) return true;
657 }
658 if (IsInteger(value)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100659 Type* range = this->GetRange();
660 if (range != NULL && Contains(range->AsRange(), value)) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 }
662 return BitsetType::New(BitsetType::Lub(value))->Is(this);
663}
664
Ben Murdoch097c5b22016-05-18 11:27:45 +0100665bool UnionType::Wellformed() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 DisallowHeapAllocation no_allocation;
667 // This checks the invariants of the union representation:
668 // 1. There are at least two elements.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000669 // 2. The first element is a bitset, no other element is a bitset.
670 // 3. At most one element is a range, and it must be the second one.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671 // 4. No element is itself a union.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000672 // 5. No element (except the bitset) is a subtype of any other.
673 // 6. If there is a range, then the bitset type does not contain
674 // plain number bits.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 DCHECK(this->Length() >= 2); // (1)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 DCHECK(this->Get(0)->IsBitset()); // (2a)
677
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 for (int i = 0; i < this->Length(); ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000679 if (i != 0) DCHECK(!this->Get(i)->IsBitset()); // (2b)
680 if (i != 1) DCHECK(!this->Get(i)->IsRange()); // (3)
681 DCHECK(!this->Get(i)->IsUnion()); // (4)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682 for (int j = 0; j < this->Length(); ++j) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000683 if (i != j && i != 0)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100684 DCHECK(!this->Get(i)->SemanticIs(this->Get(j))); // (5)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 }
686 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000687 DCHECK(!this->Get(1)->IsRange() ||
688 (BitsetType::NumberBits(this->Get(0)->AsBitset()) ==
689 BitsetType::kNone)); // (6)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000690 return true;
691}
692
693
694// -----------------------------------------------------------------------------
695// Union and intersection
696
697
698static bool AddIsSafe(int x, int y) {
699 return x >= 0 ?
700 y <= std::numeric_limits<int>::max() - x :
701 y >= std::numeric_limits<int>::min() - x;
702}
703
Ben Murdoch097c5b22016-05-18 11:27:45 +0100704Type* Type::Intersect(Type* type1, Type* type2, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000705 // Fast case: bit sets.
706 if (type1->IsBitset() && type2->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100707 return BitsetType::New(type1->AsBitset() & type2->AsBitset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 }
709
710 // Fast case: top or bottom types.
711 if (type1->IsNone() || type2->IsAny()) return type1; // Shortcut.
712 if (type2->IsNone() || type1->IsAny()) return type2; // Shortcut.
713
714 // Semi-fast case.
715 if (type1->Is(type2)) return type1;
716 if (type2->Is(type1)) return type2;
717
718 // Slow case: create union.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000719
720 // Figure out the representation of the result first.
721 // The rest of the method should not change this representation and
722 // it should not make any decisions based on representations (i.e.,
723 // it should only use the semantic part of types).
724 const bitset representation =
725 type1->Representation() & type2->Representation();
726
727 // Semantic subtyping check - this is needed for consistency with the
728 // semi-fast case above - we should behave the same way regardless of
729 // representations. Intersection with a universal bitset should only update
730 // the representations.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100731 if (type1->SemanticIs(type2)) {
732 type2 = Any();
733 } else if (type2->SemanticIs(type1)) {
734 type1 = Any();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000735 }
736
737 bitset bits =
738 SEMANTIC(type1->BitsetGlb() & type2->BitsetGlb()) | representation;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
740 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100741 if (!AddIsSafe(size1, size2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 int size = size1 + size2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100743 if (!AddIsSafe(size, 2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000744 size += 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100745 Type* result_type = UnionType::New(size, zone);
746 UnionType* result = result_type->AsUnion();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747 size = 0;
748
749 // Deal with bitsets.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100750 result->Set(size++, BitsetType::New(bits));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751
Ben Murdoch097c5b22016-05-18 11:27:45 +0100752 RangeType::Limits lims = RangeType::Limits::Empty();
753 size = IntersectAux(type1, type2, result, size, &lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000755 // If the range is not empty, then insert it into the union and
756 // remove the number bits from the bitset.
757 if (!lims.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100758 size = UpdateRange(RangeType::New(lims, representation, zone), result, size,
759 zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000760
761 // Remove the number bits.
762 bitset number_bits = BitsetType::NumberBits(bits);
763 bits &= ~number_bits;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100764 result->Set(0, BitsetType::New(bits));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000765 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100766 return NormalizeUnion(result_type, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000767}
768
Ben Murdoch097c5b22016-05-18 11:27:45 +0100769int Type::UpdateRange(Type* range, UnionType* result, int size, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000770 if (size == 1) {
771 result->Set(size++, range);
772 } else {
773 // Make space for the range.
774 result->Set(size++, result->Get(1));
775 result->Set(1, range);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777
778 // Remove any components that just got subsumed.
779 for (int i = 2; i < size; ) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100780 if (result->Get(i)->SemanticIs(range)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781 result->Set(i, result->Get(--size));
782 } else {
783 ++i;
784 }
785 }
786 return size;
787}
788
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789RangeType::Limits Type::ToLimits(bitset bits, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000790 bitset number_bits = BitsetType::NumberBits(bits);
791
792 if (number_bits == BitsetType::kNone) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100793 return RangeType::Limits::Empty();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794 }
795
Ben Murdoch097c5b22016-05-18 11:27:45 +0100796 return RangeType::Limits(BitsetType::Min(number_bits),
797 BitsetType::Max(number_bits));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798}
799
Ben Murdoch097c5b22016-05-18 11:27:45 +0100800RangeType::Limits Type::IntersectRangeAndBitset(Type* range, Type* bitset,
801 Zone* zone) {
802 RangeType::Limits range_lims(range->AsRange());
803 RangeType::Limits bitset_lims = ToLimits(bitset->AsBitset(), zone);
804 return RangeType::Limits::Intersect(range_lims, bitset_lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000805}
806
Ben Murdoch097c5b22016-05-18 11:27:45 +0100807int Type::IntersectAux(Type* lhs, Type* rhs, UnionType* result, int size,
808 RangeType::Limits* lims, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 if (lhs->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400810 for (int i = 0, n = lhs->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000811 size =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100812 IntersectAux(lhs->AsUnion()->Get(i), rhs, result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 }
814 return size;
815 }
816 if (rhs->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400817 for (int i = 0, n = rhs->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 size =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100819 IntersectAux(lhs, rhs->AsUnion()->Get(i), result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000820 }
821 return size;
822 }
823
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 if (!BitsetType::SemanticIsInhabited(lhs->BitsetLub() & rhs->BitsetLub())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 return size;
826 }
827
828 if (lhs->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829 if (rhs->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100830 RangeType::Limits lim = IntersectRangeAndBitset(lhs, rhs, zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000831
832 if (!lim.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100833 *lims = RangeType::Limits::Union(lim, *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000834 }
835 return size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000836 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837 if (rhs->IsClass()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100838 *lims =
839 RangeType::Limits::Union(RangeType::Limits(lhs->AsRange()), *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000840 }
841 if (rhs->IsConstant() && Contains(lhs->AsRange(), rhs->AsConstant())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100842 return AddToUnion(rhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000844 if (rhs->IsRange()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100845 RangeType::Limits lim = RangeType::Limits::Intersect(
846 RangeType::Limits(lhs->AsRange()), RangeType::Limits(rhs->AsRange()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000847 if (!lim.IsEmpty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100848 *lims = RangeType::Limits::Union(lim, *lims);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000849 }
850 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000851 return size;
852 }
853 if (rhs->IsRange()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000854 // This case is handled symmetrically above.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100855 return IntersectAux(rhs, lhs, result, size, lims, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000856 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000857 if (lhs->IsBitset() || rhs->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100858 return AddToUnion(lhs->IsBitset() ? rhs : lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 }
860 if (lhs->IsClass() != rhs->IsClass()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100861 return AddToUnion(lhs->IsClass() ? rhs : lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000862 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100863 if (lhs->SimplyEquals(rhs)) {
864 return AddToUnion(lhs, result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000865 }
866 return size;
867}
868
869
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000870// Make sure that we produce a well-formed range and bitset:
871// If the range is non-empty, the number bits in the bitset should be
872// clear. Moreover, if we have a canonical range (such as Signed32),
873// we want to produce a bitset rather than a range.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100874Type* Type::NormalizeRangeAndBitset(Type* range, bitset* bits, Zone* zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000875 // Fast path: If the bitset does not mention numbers, we can just keep the
876 // range.
877 bitset number_bits = BitsetType::NumberBits(*bits);
878 if (number_bits == 0) {
879 return range;
880 }
881
882 // If the range is semantically contained within the bitset, return None and
883 // leave the bitset untouched.
884 bitset range_lub = SEMANTIC(range->BitsetLub());
885 if (BitsetType::Is(range_lub, *bits)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100886 return None();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000887 }
888
889 // Slow path: reconcile the bitset range and the range.
890 double bitset_min = BitsetType::Min(number_bits);
891 double bitset_max = BitsetType::Max(number_bits);
892
893 double range_min = range->Min();
894 double range_max = range->Max();
895
896 // Remove the number bits from the bitset, they would just confuse us now.
897 // NOTE: bits contains OtherNumber iff bits contains PlainNumber, in which
898 // case we already returned after the subtype check above.
899 *bits &= ~number_bits;
900
901 if (range_min <= bitset_min && range_max >= bitset_max) {
902 // Bitset is contained within the range, just return the range.
903 return range;
904 }
905
906 if (bitset_min < range_min) {
907 range_min = bitset_min;
908 }
909 if (bitset_max > range_max) {
910 range_max = bitset_max;
911 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100912 return RangeType::New(range_min, range_max, BitsetType::kNone, zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000913}
914
Ben Murdoch097c5b22016-05-18 11:27:45 +0100915Type* Type::Union(Type* type1, Type* type2, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 // Fast case: bit sets.
917 if (type1->IsBitset() && type2->IsBitset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100918 return BitsetType::New(type1->AsBitset() | type2->AsBitset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000919 }
920
921 // Fast case: top or bottom types.
922 if (type1->IsAny() || type2->IsNone()) return type1;
923 if (type2->IsAny() || type1->IsNone()) return type2;
924
925 // Semi-fast case.
926 if (type1->Is(type2)) return type2;
927 if (type2->Is(type1)) return type1;
928
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000929 // Figure out the representation of the result.
930 // The rest of the method should not change this representation and
931 // it should not make any decisions based on representations (i.e.,
932 // it should only use the semantic part of types).
933 const bitset representation =
934 type1->Representation() | type2->Representation();
935
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 // Slow case: create union.
937 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
938 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100939 if (!AddIsSafe(size1, size2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 int size = size1 + size2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100941 if (!AddIsSafe(size, 2)) return Any();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 size += 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100943 Type* result_type = UnionType::New(size, zone);
944 UnionType* result = result_type->AsUnion();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945 size = 0;
946
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000947 // Compute the new bitset.
948 bitset new_bitset = SEMANTIC(type1->BitsetGlb() | type2->BitsetGlb());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949
950 // Deal with ranges.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100951 Type* range = None();
952 Type* range1 = type1->GetRange();
953 Type* range2 = type2->GetRange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000954 if (range1 != NULL && range2 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100955 RangeType::Limits lims =
956 RangeType::Limits::Union(RangeType::Limits(range1->AsRange()),
957 RangeType::Limits(range2->AsRange()));
958 Type* union_range = RangeType::New(lims, representation, zone);
959 range = NormalizeRangeAndBitset(union_range, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960 } else if (range1 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100961 range = NormalizeRangeAndBitset(range1, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000962 } else if (range2 != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100963 range = NormalizeRangeAndBitset(range2, &new_bitset, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000965 new_bitset = SEMANTIC(new_bitset) | representation;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100966 Type* bits = BitsetType::New(new_bitset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000967 result->Set(size++, bits);
968 if (!range->IsNone()) result->Set(size++, range);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969
Ben Murdoch097c5b22016-05-18 11:27:45 +0100970 size = AddToUnion(type1, result, size, zone);
971 size = AddToUnion(type2, result, size, zone);
972 return NormalizeUnion(result_type, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973}
974
975
976// Add [type] to [result] unless [type] is bitset, range, or already subsumed.
977// Return new size of [result].
Ben Murdoch097c5b22016-05-18 11:27:45 +0100978int Type::AddToUnion(Type* type, UnionType* result, int size, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 if (type->IsBitset() || type->IsRange()) return size;
980 if (type->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400981 for (int i = 0, n = type->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100982 size = AddToUnion(type->AsUnion()->Get(i), result, size, zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000983 }
984 return size;
985 }
986 for (int i = 0; i < size; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100987 if (type->SemanticIs(result->Get(i))) return size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000988 }
989 result->Set(size++, type);
990 return size;
991}
992
Ben Murdoch097c5b22016-05-18 11:27:45 +0100993Type* Type::NormalizeUnion(Type* union_type, int size, Zone* zone) {
994 UnionType* unioned = union_type->AsUnion();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000995 DCHECK(size >= 1);
996 DCHECK(unioned->Get(0)->IsBitset());
997 // If the union has just one element, return it.
998 if (size == 1) {
999 return unioned->Get(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001000 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001001 bitset bits = unioned->Get(0)->AsBitset();
1002 // If the union only consists of a range, we can get rid of the union.
1003 if (size == 2 && SEMANTIC(bits) == BitsetType::kNone) {
1004 bitset representation = REPRESENTATION(bits);
1005 if (representation == unioned->Get(1)->Representation()) {
1006 return unioned->Get(1);
1007 }
1008 if (unioned->Get(1)->IsRange()) {
1009 return RangeType::New(unioned->Get(1)->AsRange()->Min(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01001010 unioned->Get(1)->AsRange()->Max(),
1011 unioned->Get(0)->AsBitset(), zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001012 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001014 unioned->Shrink(size);
1015 SLOW_DCHECK(unioned->Wellformed());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001016 return union_type;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017}
1018
1019
1020// -----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001021// Component extraction
1022
1023// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01001024Type* Type::Representation(Type* t, Zone* zone) {
1025 return BitsetType::New(t->Representation());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026}
1027
1028
1029// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01001030Type* Type::Semantic(Type* t, Zone* zone) {
1031 return Intersect(t, BitsetType::New(BitsetType::kSemantic), zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001032}
1033
1034
1035// -----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036// Iteration.
1037
Ben Murdoch097c5b22016-05-18 11:27:45 +01001038int Type::NumClasses() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001039 DisallowHeapAllocation no_allocation;
1040 if (this->IsClass()) {
1041 return 1;
1042 } else if (this->IsUnion()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043 int result = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001044 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
1045 if (this->AsUnion()->Get(i)->IsClass()) ++result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046 }
1047 return result;
1048 } else {
1049 return 0;
1050 }
1051}
1052
Ben Murdoch097c5b22016-05-18 11:27:45 +01001053int Type::NumConstants() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001054 DisallowHeapAllocation no_allocation;
1055 if (this->IsConstant()) {
1056 return 1;
1057 } else if (this->IsUnion()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001058 int result = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001059 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
1060 if (this->AsUnion()->Get(i)->IsConstant()) ++result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001061 }
1062 return result;
1063 } else {
1064 return 0;
1065 }
1066}
1067
Ben Murdoch097c5b22016-05-18 11:27:45 +01001068template <class T>
1069Type* Type::Iterator<T>::get_type() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070 DCHECK(!Done());
1071 return type_->IsUnion() ? type_->AsUnion()->Get(index_) : type_;
1072}
1073
1074
1075// C++ cannot specialise nested templates, so we have to go through this
1076// contortion with an auxiliary template to simulate it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001077template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078struct TypeImplIteratorAux {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001079 static bool matches(Type* type);
1080 static i::Handle<T> current(Type* type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081};
1082
Ben Murdoch097c5b22016-05-18 11:27:45 +01001083template <>
1084struct TypeImplIteratorAux<i::Map> {
1085 static bool matches(Type* type) { return type->IsClass(); }
1086 static i::Handle<i::Map> current(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001087 return type->AsClass()->Map();
1088 }
1089};
1090
Ben Murdoch097c5b22016-05-18 11:27:45 +01001091template <>
1092struct TypeImplIteratorAux<i::Object> {
1093 static bool matches(Type* type) { return type->IsConstant(); }
1094 static i::Handle<i::Object> current(Type* type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095 return type->AsConstant()->Value();
1096 }
1097};
1098
Ben Murdoch097c5b22016-05-18 11:27:45 +01001099template <class T>
1100bool Type::Iterator<T>::matches(Type* type) {
1101 return TypeImplIteratorAux<T>::matches(type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001102}
1103
Ben Murdoch097c5b22016-05-18 11:27:45 +01001104template <class T>
1105i::Handle<T> Type::Iterator<T>::Current() {
1106 return TypeImplIteratorAux<T>::current(get_type());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107}
1108
Ben Murdoch097c5b22016-05-18 11:27:45 +01001109template <class T>
1110void Type::Iterator<T>::Advance() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111 DisallowHeapAllocation no_allocation;
1112 ++index_;
1113 if (type_->IsUnion()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001114 for (int n = type_->AsUnion()->Length(); index_ < n; ++index_) {
1115 if (matches(type_->AsUnion()->Get(index_))) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001116 }
1117 } else if (index_ == 0 && matches(type_)) {
1118 return;
1119 }
1120 index_ = -1;
1121}
1122
1123
1124// -----------------------------------------------------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125// Printing.
1126
Ben Murdoch097c5b22016-05-18 11:27:45 +01001127const char* BitsetType::Name(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 switch (bits) {
1129 case REPRESENTATION(kAny): return "Any";
1130 #define RETURN_NAMED_REPRESENTATION_TYPE(type, value) \
1131 case REPRESENTATION(k##type): return #type;
1132 REPRESENTATION_BITSET_TYPE_LIST(RETURN_NAMED_REPRESENTATION_TYPE)
1133 #undef RETURN_NAMED_REPRESENTATION_TYPE
1134
1135 #define RETURN_NAMED_SEMANTIC_TYPE(type, value) \
1136 case SEMANTIC(k##type): return #type;
1137 SEMANTIC_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001138 INTERNAL_BITSET_TYPE_LIST(RETURN_NAMED_SEMANTIC_TYPE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001139 #undef RETURN_NAMED_SEMANTIC_TYPE
1140
1141 default:
1142 return NULL;
1143 }
1144}
1145
Ben Murdoch097c5b22016-05-18 11:27:45 +01001146void BitsetType::Print(std::ostream& os, // NOLINT
1147 bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001148 DisallowHeapAllocation no_allocation;
1149 const char* name = Name(bits);
1150 if (name != NULL) {
1151 os << name;
1152 return;
1153 }
1154
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001155 // clang-format off
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001156 static const bitset named_bitsets[] = {
1157#define BITSET_CONSTANT(type, value) REPRESENTATION(k##type),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001158 REPRESENTATION_BITSET_TYPE_LIST(BITSET_CONSTANT)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159#undef BITSET_CONSTANT
1160
1161#define BITSET_CONSTANT(type, value) SEMANTIC(k##type),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001162 INTERNAL_BITSET_TYPE_LIST(BITSET_CONSTANT)
1163 SEMANTIC_BITSET_TYPE_LIST(BITSET_CONSTANT)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164#undef BITSET_CONSTANT
1165 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001166 // clang-format on
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001167
1168 bool is_first = true;
1169 os << "(";
1170 for (int i(arraysize(named_bitsets) - 1); bits != 0 && i >= 0; --i) {
1171 bitset subset = named_bitsets[i];
1172 if ((bits & subset) == subset) {
1173 if (!is_first) os << " | ";
1174 is_first = false;
1175 os << Name(subset);
1176 bits -= subset;
1177 }
1178 }
1179 DCHECK(bits == 0);
1180 os << ")";
1181}
1182
Ben Murdoch097c5b22016-05-18 11:27:45 +01001183void Type::PrintTo(std::ostream& os, PrintDimension dim) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001184 DisallowHeapAllocation no_allocation;
1185 if (dim != REPRESENTATION_DIM) {
1186 if (this->IsBitset()) {
1187 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
1188 } else if (this->IsClass()) {
1189 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
1190 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
1191 os << ")";
1192 } else if (this->IsConstant()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001193 os << "Constant(" << Brief(*this->AsConstant()->Value()) << ")";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194 } else if (this->IsRange()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001195 std::ostream::fmtflags saved_flags = os.setf(std::ios::fixed);
1196 std::streamsize saved_precision = os.precision(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001197 os << "Range(" << this->AsRange()->Min() << ", " << this->AsRange()->Max()
1198 << ")";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001199 os.flags(saved_flags);
1200 os.precision(saved_precision);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001201 } else if (this->IsContext()) {
1202 os << "Context(";
1203 this->AsContext()->Outer()->PrintTo(os, dim);
1204 os << ")";
1205 } else if (this->IsUnion()) {
1206 os << "(";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001207 for (int i = 0, n = this->AsUnion()->Length(); i < n; ++i) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001208 Type* type_i = this->AsUnion()->Get(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209 if (i > 0) os << " | ";
1210 type_i->PrintTo(os, dim);
1211 }
1212 os << ")";
1213 } else if (this->IsArray()) {
1214 os << "Array(";
1215 AsArray()->Element()->PrintTo(os, dim);
1216 os << ")";
1217 } else if (this->IsFunction()) {
1218 if (!this->AsFunction()->Receiver()->IsAny()) {
1219 this->AsFunction()->Receiver()->PrintTo(os, dim);
1220 os << ".";
1221 }
1222 os << "(";
1223 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
1224 if (i > 0) os << ", ";
1225 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
1226 }
1227 os << ")->";
1228 this->AsFunction()->Result()->PrintTo(os, dim);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001229 } else if (this->IsTuple()) {
1230 os << "<";
1231 for (int i = 0, n = this->AsTuple()->Arity(); i < n; ++i) {
1232 Type* type_i = this->AsTuple()->Element(i);
1233 if (i > 0) os << ", ";
1234 type_i->PrintTo(os, dim);
1235 }
1236 os << ">";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001237 } else {
1238 UNREACHABLE();
1239 }
1240 }
1241 if (dim == BOTH_DIMS) os << "/";
1242 if (dim != SEMANTIC_DIM) {
1243 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
1244 }
1245}
1246
1247
1248#ifdef DEBUG
Ben Murdoch097c5b22016-05-18 11:27:45 +01001249void Type::Print() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001250 OFStream os(stdout);
1251 PrintTo(os);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001252 os << std::endl;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001253}
Ben Murdoch097c5b22016-05-18 11:27:45 +01001254void BitsetType::Print(bitset bits) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 OFStream os(stdout);
1256 Print(os, bits);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001257 os << std::endl;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258}
1259#endif
1260
Ben Murdoch097c5b22016-05-18 11:27:45 +01001261BitsetType::bitset BitsetType::SignedSmall() {
1262 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32;
1263}
1264
1265BitsetType::bitset BitsetType::UnsignedSmall() {
1266 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31;
1267}
1268
1269#define CONSTRUCT_SIMD_TYPE(NAME, Name, name, lane_count, lane_type) \
1270 Type* Type::Name(Isolate* isolate, Zone* zone) { \
1271 return Class(i::handle(isolate->heap()->name##_map()), zone); \
1272 }
1273SIMD128_TYPES(CONSTRUCT_SIMD_TYPE)
1274#undef CONSTRUCT_SIMD_TYPE
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001275
1276// -----------------------------------------------------------------------------
1277// Instantiations.
1278
Ben Murdoch097c5b22016-05-18 11:27:45 +01001279template class Type::Iterator<i::Map>;
1280template class Type::Iterator<i::Object>;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001281
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001282} // namespace internal
1283} // namespace v8