// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_TYPES_INL_H_ #define V8_TYPES_INL_H_ #include "src/types.h" #include "src/factory.h" #include "src/handles-inl.h" namespace v8 { namespace internal { // ----------------------------------------------------------------------------- // TypeImpl template TypeImpl* TypeImpl::cast(typename Config::Base* object) { TypeImpl* t = static_cast(object); ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsRange() || t->IsUnion() || t->IsArray() || t->IsFunction() || t->IsContext()); return t; } // Most precise _current_ type of a value (usually its class). template typename TypeImpl::TypeHandle TypeImpl::NowOf( i::Object* value, Region* region) { if (value->IsSmi() || i::HeapObject::cast(value)->map()->instance_type() == HEAP_NUMBER_TYPE) { return Of(value, region); } return Class(i::handle(i::HeapObject::cast(value)->map()), region); } template bool TypeImpl::NowContains(i::Object* value) { DisallowHeapAllocation no_allocation; if (this->IsAny()) return true; if (value->IsHeapObject()) { i::Map* map = i::HeapObject::cast(value)->map(); for (Iterator it = this->Classes(); !it.Done(); it.Advance()) { if (*it.Current() == map) return true; } } return this->Contains(value); } // ----------------------------------------------------------------------------- // ZoneTypeConfig // static template T* ZoneTypeConfig::handle(T* type) { return type; } // static template T* ZoneTypeConfig::cast(Type* type) { return static_cast(type); } // static bool ZoneTypeConfig::is_bitset(Type* type) { return reinterpret_cast(type) & 1; } // static bool ZoneTypeConfig::is_struct(Type* type, int tag) { return !is_bitset(type) && struct_tag(as_struct(type)) == tag; } // static bool ZoneTypeConfig::is_class(Type* type) { return false; } // static int ZoneTypeConfig::as_bitset(Type* type) { ASSERT(is_bitset(type)); return static_cast(reinterpret_cast(type) >> 1); } // static ZoneTypeConfig::Struct* ZoneTypeConfig::as_struct(Type* type) { ASSERT(!is_bitset(type)); return reinterpret_cast(type); } // static i::Handle ZoneTypeConfig::as_class(Type* type) { UNREACHABLE(); return i::Handle(); } // static ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset(int bitset) { return reinterpret_cast((bitset << 1) | 1); } // static ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset(int bitset, Zone* Zone) { return from_bitset(bitset); } // static ZoneTypeConfig::Type* ZoneTypeConfig::from_struct(Struct* structure) { return reinterpret_cast(structure); } // static ZoneTypeConfig::Type* ZoneTypeConfig::from_class( i::Handle map, Zone* zone) { return from_bitset(0); } // static ZoneTypeConfig::Struct* ZoneTypeConfig::struct_create( int tag, int length, Zone* zone) { Struct* structure = reinterpret_cast( zone->New(sizeof(void*) * (length + 2))); // NOLINT structure[0] = reinterpret_cast(tag); structure[1] = reinterpret_cast(length); return structure; } // static void ZoneTypeConfig::struct_shrink(Struct* structure, int length) { ASSERT(0 <= length && length <= struct_length(structure)); structure[1] = reinterpret_cast(length); } // static int ZoneTypeConfig::struct_tag(Struct* structure) { return static_cast(reinterpret_cast(structure[0])); } // static int ZoneTypeConfig::struct_length(Struct* structure) { return static_cast(reinterpret_cast(structure[1])); } // static Type* ZoneTypeConfig::struct_get(Struct* structure, int i) { ASSERT(0 <= i && i <= struct_length(structure)); return static_cast(structure[2 + i]); } // static void ZoneTypeConfig::struct_set(Struct* structure, int i, Type* x) { ASSERT(0 <= i && i <= struct_length(structure)); structure[2 + i] = x; } // static template i::Handle ZoneTypeConfig::struct_get_value(Struct* structure, int i) { ASSERT(0 <= i && i <= struct_length(structure)); return i::Handle(static_cast(structure[2 + i])); } // static template void ZoneTypeConfig::struct_set_value( Struct* structure, int i, i::Handle x) { ASSERT(0 <= i && i <= struct_length(structure)); structure[2 + i] = x.location(); } // ----------------------------------------------------------------------------- // HeapTypeConfig // static template i::Handle HeapTypeConfig::handle(T* type) { return i::handle(type, i::HeapObject::cast(type)->GetIsolate()); } // static template i::Handle HeapTypeConfig::cast(i::Handle type) { return i::Handle::cast(type); } // static bool HeapTypeConfig::is_bitset(Type* type) { return type->IsSmi(); } // static bool HeapTypeConfig::is_class(Type* type) { return type->IsMap(); } // static bool HeapTypeConfig::is_struct(Type* type, int tag) { return type->IsFixedArray() && struct_tag(as_struct(type)) == tag; } // static int HeapTypeConfig::as_bitset(Type* type) { return i::Smi::cast(type)->value(); } // static i::Handle HeapTypeConfig::as_class(Type* type) { return i::handle(i::Map::cast(type)); } // static i::Handle HeapTypeConfig::as_struct(Type* type) { return i::handle(Struct::cast(type)); } // static HeapTypeConfig::Type* HeapTypeConfig::from_bitset(int bitset) { return Type::cast(i::Smi::FromInt(bitset)); } // static i::Handle HeapTypeConfig::from_bitset( int bitset, Isolate* isolate) { return i::handle(from_bitset(bitset), isolate); } // static i::Handle HeapTypeConfig::from_class( i::Handle map, Isolate* isolate) { return i::Handle::cast(i::Handle::cast(map)); } // static i::Handle HeapTypeConfig::from_struct( i::Handle structure) { return i::Handle::cast(i::Handle::cast(structure)); } // static i::Handle HeapTypeConfig::struct_create( int tag, int length, Isolate* isolate) { i::Handle structure = isolate->factory()->NewFixedArray(length + 1); structure->set(0, i::Smi::FromInt(tag)); return structure; } // static void HeapTypeConfig::struct_shrink(i::Handle structure, int length) { structure->Shrink(length + 1); } // static int HeapTypeConfig::struct_tag(i::Handle structure) { return static_cast(structure->get(0))->value(); } // static int HeapTypeConfig::struct_length(i::Handle structure) { return structure->length() - 1; } // static i::Handle HeapTypeConfig::struct_get( i::Handle structure, int i) { Type* type = static_cast(structure->get(i + 1)); return i::handle(type, structure->GetIsolate()); } // static void HeapTypeConfig::struct_set( i::Handle structure, int i, i::Handle type) { structure->set(i + 1, *type); } // static template i::Handle HeapTypeConfig::struct_get_value( i::Handle structure, int i) { V* x = static_cast(structure->get(i + 1)); return i::handle(x, structure->GetIsolate()); } // static template void HeapTypeConfig::struct_set_value( i::Handle structure, int i, i::Handle x) { structure->set(i + 1, *x); } } } // namespace v8::internal #endif // V8_TYPES_INL_H_