Commit 2311b678 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Type representation converter

R=danno@chromium.org
BUG=

Review URL: https://codereview.chromium.org/145083007

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18813 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent cde3ed1f
...@@ -540,6 +540,30 @@ typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect( ...@@ -540,6 +540,30 @@ typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
} }
template<class Config>
template<class OtherType>
typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
typename OtherType::TypeHandle type, Region* region) {
if (type->IsBitset()) {
return Config::from_bitset(type->AsBitset(), region);
} else if (type->IsClass()) {
return Config::from_class(type->AsClass(), region);
} else if (type->IsConstant()) {
return Config::from_constant(type->AsConstant(), region);
} else {
ASSERT(type->IsUnion());
typename OtherType::UnionedHandle unioned = type->AsUnion();
int length = OtherType::UnionLength(unioned);
UnionedHandle new_unioned = Config::union_create(length, region);
for (int i = 0; i < length; ++i) {
Config::union_set(new_unioned, i,
Convert<OtherType>(OtherType::UnionGet(unioned, i), region));
}
return Config::from_union(new_unioned);
}
}
// TODO(rossberg): this does not belong here. // TODO(rossberg): this does not belong here.
Representation Representation::FromType(Type* type) { Representation Representation::FromType(Type* type) {
if (type->Is(Type::None())) return Representation::None(); if (type->Is(Type::None())) return Representation::None();
...@@ -620,5 +644,12 @@ template class TypeImpl<HeapTypeConfig>; ...@@ -620,5 +644,12 @@ template class TypeImpl<HeapTypeConfig>;
template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>; template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
template TypeImpl<ZoneTypeConfig>::TypeHandle
TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
template TypeImpl<HeapTypeConfig>::TypeHandle
TypeImpl<HeapTypeConfig>::Convert<Type>(
TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
} } // namespace v8::internal } } // namespace v8::internal
...@@ -243,6 +243,10 @@ class TypeImpl : public Config::Base { ...@@ -243,6 +243,10 @@ class TypeImpl : public Config::Base {
return t; return t;
} }
template<class OtherTypeImpl>
static TypeHandle Convert(
typename OtherTypeImpl::TypeHandle type, Region* region);
#ifdef OBJECT_PRINT #ifdef OBJECT_PRINT
void TypePrint(); void TypePrint();
void TypePrint(FILE* out); void TypePrint(FILE* out);
...@@ -250,6 +254,7 @@ class TypeImpl : public Config::Base { ...@@ -250,6 +254,7 @@ class TypeImpl : public Config::Base {
private: private:
template<class> friend class Iterator; template<class> friend class Iterator;
template<class> friend class TypeImpl;
// A union is a fixed array containing types. Invariants: // A union is a fixed array containing types. Invariants:
// - its length is at least 2 // - its length is at least 2
...@@ -272,6 +277,13 @@ class TypeImpl : public Config::Base { ...@@ -272,6 +277,13 @@ class TypeImpl : public Config::Base {
int AsBitset() { return Config::as_bitset(this); } int AsBitset() { return Config::as_bitset(this); }
UnionedHandle AsUnion() { return Config::as_union(this); } UnionedHandle AsUnion() { return Config::as_union(this); }
static int UnionLength(UnionedHandle unioned) {
return Config::union_length(unioned);
}
static TypeHandle UnionGet(UnionedHandle unioned, int i) {
return Config::union_get(unioned, i);
}
bool SlowIs(TypeImpl* that); bool SlowIs(TypeImpl* that);
int LubBitset(); // least upper bound that's a bitset int LubBitset(); // least upper bound that's a bitset
......
...@@ -119,6 +119,54 @@ class Types { ...@@ -119,6 +119,54 @@ class Types {
return Type::Intersect(t1, t2, region_); return Type::Intersect(t1, t2, region_);
} }
template<class Type2, class TypeHandle2>
TypeHandle Convert(TypeHandle2 t) {
return Type::template Convert<Type2>(t, region_);
}
TypeHandle Fuzz(int depth = 5) {
switch (random() % (depth == 0 ? 3 : 20)) {
case 0: { // bitset
int n = 0
#define COUNT_BITSET_TYPES(type, value) + 1
BITSET_TYPE_LIST(COUNT_BITSET_TYPES)
#undef COUNT_BITSET_TYPES
;
int i = random() % n;
#define PICK_BITSET_TYPE(type, value) \
if (i-- == 0) return Type::type(region_);
BITSET_TYPE_LIST(PICK_BITSET_TYPE)
#undef PICK_BITSET_TYPE
UNREACHABLE();
}
case 1: // class
switch (random() % 2) {
case 0: return ObjectClass;
case 1: return ArrayClass;
}
UNREACHABLE();
case 2: // constant
switch (random() % 6) {
case 0: return SmiConstant;
case 1: return Signed32Constant;
case 2: return ObjectConstant1;
case 3: return ObjectConstant2;
case 4: return ArrayConstant1;
case 5: return ArrayConstant2;
}
UNREACHABLE();
default: { // union
int n = random() % 10;
TypeHandle type = None;
for (int i = 0; i < n; ++i) {
type = Type::Union(type, Fuzz(depth - 1), region_);
}
return type;
}
}
UNREACHABLE();
}
private: private:
Region* region_; Region* region_;
}; };
...@@ -756,6 +804,16 @@ struct Tests : Rep { ...@@ -756,6 +804,16 @@ struct Tests : Rep {
T.Union(T.ObjectConstant1, T.ArrayConstant2)), T.Union(T.ObjectConstant1, T.ArrayConstant2)),
T.ArrayConstant1); T.ArrayConstant1);
} }
template<class Type2, class TypeHandle2, class Region2, class Rep2>
void Convert() {
Types<Type2, TypeHandle2, Region2> T2(
Rep2::ToRegion(&zone, isolate), isolate);
for (int i = 0; i < 100; ++i) {
TypeHandle type = T.Fuzz();
CheckEqual(type, T.Convert<Type2>(T2.Convert<Type>(type)));
}
}
}; };
typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests; typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests;
...@@ -809,3 +867,10 @@ TEST(Intersect) { ...@@ -809,3 +867,10 @@ TEST(Intersect) {
ZoneTests().Intersect(); ZoneTests().Intersect();
HeapTests().Intersect(); HeapTests().Intersect();
} }
TEST(Convert) {
CcTest::InitializeVM();
ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>();
HeapTests().Convert<Type, Type*, Zone, ZoneRep>();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment