1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_TYPES_INL_H_
6 #define V8_TYPES_INL_H_
10 #include "src/factory.h"
11 #include "src/handles-inl.h"
16 // -----------------------------------------------------------------------------
19 template<class Config>
20 typename TypeImpl<Config>::bitset TypeImpl<Config>::BitsetType::SignedSmall() {
21 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32;
25 template<class Config>
26 typename TypeImpl<Config>::bitset
27 TypeImpl<Config>::BitsetType::UnsignedSmall() {
28 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31;
32 #define CONSTRUCT_SIMD_TYPE(NAME, Name, name, lane_count, lane_type) \
33 template<class Config> \
34 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Name( \
35 Isolate* isolate, Region* region) { \
36 return Class(i::handle(isolate->heap()->name##_map()), region); \
38 SIMD128_TYPES(CONSTRUCT_SIMD_TYPE)
39 #undef CONSTRUCT_SIMD_TYPE
42 template<class Config>
43 TypeImpl<Config>* TypeImpl<Config>::cast(typename Config::Base* object) {
44 TypeImpl* t = static_cast<TypeImpl*>(object);
45 DCHECK(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsRange() ||
46 t->IsUnion() || t->IsArray() || t->IsFunction() || t->IsContext());
51 // Most precise _current_ type of a value (usually its class).
52 template<class Config>
53 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::NowOf(
54 i::Object* value, Region* region) {
56 i::HeapObject::cast(value)->map()->instance_type() == HEAP_NUMBER_TYPE) {
57 return Of(value, region);
59 return Class(i::handle(i::HeapObject::cast(value)->map()), region);
63 template<class Config>
64 bool TypeImpl<Config>::NowContains(i::Object* value) {
65 DisallowHeapAllocation no_allocation;
66 if (this->IsAny()) return true;
67 if (value->IsHeapObject()) {
68 i::Map* map = i::HeapObject::cast(value)->map();
69 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
70 if (*it.Current() == map) return true;
73 return this->Contains(value);
77 // -----------------------------------------------------------------------------
82 T* ZoneTypeConfig::handle(T* type) {
89 T* ZoneTypeConfig::cast(Type* type) {
90 return static_cast<T*>(type);
95 bool ZoneTypeConfig::is_bitset(Type* type) {
96 return reinterpret_cast<uintptr_t>(type) & 1;
101 bool ZoneTypeConfig::is_struct(Type* type, int tag) {
102 DCHECK(tag != kRangeStructTag);
103 if (is_bitset(type)) return false;
104 int type_tag = struct_tag(as_struct(type));
105 return type_tag == tag;
110 bool ZoneTypeConfig::is_range(Type* type) {
111 if (is_bitset(type)) return false;
112 int type_tag = struct_tag(as_struct(type));
113 return type_tag == kRangeStructTag;
118 bool ZoneTypeConfig::is_class(Type* type) {
124 ZoneTypeConfig::Type::bitset ZoneTypeConfig::as_bitset(Type* type) {
125 DCHECK(is_bitset(type));
126 return static_cast<Type::bitset>(reinterpret_cast<uintptr_t>(type) ^ 1u);
131 ZoneTypeConfig::Struct* ZoneTypeConfig::as_struct(Type* type) {
132 DCHECK(!is_bitset(type));
133 return reinterpret_cast<Struct*>(type);
138 ZoneTypeConfig::Range* ZoneTypeConfig::as_range(Type* type) {
139 DCHECK(!is_bitset(type));
140 return reinterpret_cast<Range*>(type);
145 i::Handle<i::Map> ZoneTypeConfig::as_class(Type* type) {
147 return i::Handle<i::Map>();
152 ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset(Type::bitset bitset) {
153 return reinterpret_cast<Type*>(static_cast<uintptr_t>(bitset | 1u));
158 ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset(
159 Type::bitset bitset, Zone* Zone) {
160 return from_bitset(bitset);
165 ZoneTypeConfig::Type* ZoneTypeConfig::from_struct(Struct* structure) {
166 return reinterpret_cast<Type*>(structure);
171 ZoneTypeConfig::Type* ZoneTypeConfig::from_range(Range* range) {
172 return reinterpret_cast<Type*>(range);
177 ZoneTypeConfig::Type* ZoneTypeConfig::from_class(
178 i::Handle<i::Map> map, Zone* zone) {
179 return from_bitset(0);
184 ZoneTypeConfig::Struct* ZoneTypeConfig::struct_create(
185 int tag, int length, Zone* zone) {
186 DCHECK(tag != kRangeStructTag);
187 Struct* structure = reinterpret_cast<Struct*>(
188 zone->New(sizeof(void*) * (length + 2))); // NOLINT
189 structure[0] = reinterpret_cast<void*>(tag);
190 structure[1] = reinterpret_cast<void*>(length);
196 void ZoneTypeConfig::struct_shrink(Struct* structure, int length) {
197 DCHECK(0 <= length && length <= struct_length(structure));
198 structure[1] = reinterpret_cast<void*>(length);
203 int ZoneTypeConfig::struct_tag(Struct* structure) {
204 return static_cast<int>(reinterpret_cast<intptr_t>(structure[0]));
209 int ZoneTypeConfig::struct_length(Struct* structure) {
210 return static_cast<int>(reinterpret_cast<intptr_t>(structure[1]));
215 Type* ZoneTypeConfig::struct_get(Struct* structure, int i) {
216 DCHECK(0 <= i && i <= struct_length(structure));
217 return static_cast<Type*>(structure[2 + i]);
222 void ZoneTypeConfig::struct_set(Struct* structure, int i, Type* x) {
223 DCHECK(0 <= i && i <= struct_length(structure));
224 structure[2 + i] = x;
230 i::Handle<V> ZoneTypeConfig::struct_get_value(Struct* structure, int i) {
231 DCHECK(0 <= i && i <= struct_length(structure));
232 return i::Handle<V>(static_cast<V**>(structure[2 + i]));
238 void ZoneTypeConfig::struct_set_value(
239 Struct* structure, int i, i::Handle<V> x) {
240 DCHECK(0 <= i && i <= struct_length(structure));
241 structure[2 + i] = x.location();
246 ZoneTypeConfig::Range* ZoneTypeConfig::range_create(Zone* zone) {
247 Range* range = reinterpret_cast<Range*>(zone->New(sizeof(Range))); // NOLINT
248 range->tag = reinterpret_cast<void*>(kRangeStructTag);
250 range->limits[0] = 1;
251 range->limits[1] = 0;
257 int ZoneTypeConfig::range_get_bitset(ZoneTypeConfig::Range* range) {
258 return range->bitset;
263 void ZoneTypeConfig::range_set_bitset(ZoneTypeConfig::Range* range, int value) {
264 range->bitset = value;
269 double ZoneTypeConfig::range_get_double(ZoneTypeConfig::Range* range,
271 DCHECK(index >= 0 && index < 2);
272 return range->limits[index];
277 void ZoneTypeConfig::range_set_double(ZoneTypeConfig::Range* range, int index,
278 double value, Zone*) {
279 DCHECK(index >= 0 && index < 2);
280 range->limits[index] = value;
284 // -----------------------------------------------------------------------------
289 i::Handle<T> HeapTypeConfig::handle(T* type) {
290 return i::handle(type, i::HeapObject::cast(type)->GetIsolate());
296 i::Handle<T> HeapTypeConfig::cast(i::Handle<Type> type) {
297 return i::Handle<T>::cast(type);
302 bool HeapTypeConfig::is_bitset(Type* type) {
303 return type->IsSmi();
308 bool HeapTypeConfig::is_class(Type* type) {
309 return type->IsMap();
314 bool HeapTypeConfig::is_struct(Type* type, int tag) {
315 DCHECK(tag != kRangeStructTag);
316 return type->IsFixedArray() && struct_tag(as_struct(type)) == tag;
321 bool HeapTypeConfig::is_range(Type* type) {
322 return type->IsFixedArray() && struct_tag(as_struct(type)) == kRangeStructTag;
327 HeapTypeConfig::Type::bitset HeapTypeConfig::as_bitset(Type* type) {
328 // TODO(rossberg): Breaks the Smi abstraction. Fix once there is a better way.
329 return static_cast<Type::bitset>(reinterpret_cast<uintptr_t>(type));
334 i::Handle<i::Map> HeapTypeConfig::as_class(Type* type) {
335 return i::handle(i::Map::cast(type));
340 i::Handle<HeapTypeConfig::Struct> HeapTypeConfig::as_struct(Type* type) {
341 return i::handle(Struct::cast(type));
346 i::Handle<HeapTypeConfig::Range> HeapTypeConfig::as_range(Type* type) {
347 return i::handle(Range::cast(type));
352 HeapTypeConfig::Type* HeapTypeConfig::from_bitset(Type::bitset bitset) {
353 // TODO(rossberg): Breaks the Smi abstraction. Fix once there is a better way.
354 return reinterpret_cast<Type*>(static_cast<uintptr_t>(bitset));
359 i::Handle<HeapTypeConfig::Type> HeapTypeConfig::from_bitset(
360 Type::bitset bitset, Isolate* isolate) {
361 return i::handle(from_bitset(bitset), isolate);
366 i::Handle<HeapTypeConfig::Type> HeapTypeConfig::from_class(
367 i::Handle<i::Map> map, Isolate* isolate) {
368 return i::Handle<Type>::cast(i::Handle<Object>::cast(map));
373 i::Handle<HeapTypeConfig::Type> HeapTypeConfig::from_struct(
374 i::Handle<Struct> structure) {
375 return i::Handle<Type>::cast(i::Handle<Object>::cast(structure));
380 i::Handle<HeapTypeConfig::Type> HeapTypeConfig::from_range(
381 i::Handle<Range> range) {
382 return i::Handle<Type>::cast(i::Handle<Object>::cast(range));
387 i::Handle<HeapTypeConfig::Struct> HeapTypeConfig::struct_create(
388 int tag, int length, Isolate* isolate) {
389 i::Handle<Struct> structure = isolate->factory()->NewFixedArray(length + 1);
390 structure->set(0, i::Smi::FromInt(tag));
396 void HeapTypeConfig::struct_shrink(i::Handle<Struct> structure, int length) {
397 structure->Shrink(length + 1);
402 int HeapTypeConfig::struct_tag(i::Handle<Struct> structure) {
403 return static_cast<i::Smi*>(structure->get(0))->value();
408 int HeapTypeConfig::struct_length(i::Handle<Struct> structure) {
409 return structure->length() - 1;
414 i::Handle<HeapTypeConfig::Type> HeapTypeConfig::struct_get(
415 i::Handle<Struct> structure, int i) {
416 Type* type = static_cast<Type*>(structure->get(i + 1));
417 return i::handle(type, structure->GetIsolate());
422 void HeapTypeConfig::struct_set(
423 i::Handle<Struct> structure, int i, i::Handle<Type> type) {
424 structure->set(i + 1, *type);
430 i::Handle<V> HeapTypeConfig::struct_get_value(
431 i::Handle<Struct> structure, int i) {
432 V* x = static_cast<V*>(structure->get(i + 1));
433 return i::handle(x, structure->GetIsolate());
439 void HeapTypeConfig::struct_set_value(
440 i::Handle<Struct> structure, int i, i::Handle<V> x) {
441 structure->set(i + 1, *x);
446 i::Handle<HeapTypeConfig::Range> HeapTypeConfig::range_create(
448 i::Handle<Range> range = isolate->factory()->NewFixedArray(4);
449 range->set(0, i::Smi::FromInt(kRangeStructTag));
455 int HeapTypeConfig::range_get_bitset(i::Handle<HeapTypeConfig::Range> range) {
456 Type* v = static_cast<Type*>(range->get(1));
462 void HeapTypeConfig::range_set_bitset(i::Handle<HeapTypeConfig::Range> range,
464 range->set(1, from_bitset(value));
469 double HeapTypeConfig::range_get_double(i::Handle<HeapTypeConfig::Range> range,
471 DCHECK(index >= 0 && index < 2);
472 return range->get(index + 2)->Number();
477 void HeapTypeConfig::range_set_double(i::Handle<HeapTypeConfig::Range> range,
478 int index, double value,
480 DCHECK(index >= 0 && index < 2);
481 i::Handle<Object> number = isolate->factory()->NewNumber(value);
482 range->set(index + 2, *number);
484 } // namespace internal
487 #endif // V8_TYPES_INL_H_