Revision: 18813
Author:   [email protected]
Date:     Fri Jan 24 11:47:47 2014 UTC
Log:      Type representation converter

[email protected]
BUG=

Review URL: https://codereview.chromium.org/145083007
http://code.google.com/p/v8/source/detail?r=18813

Modified:
 /branches/bleeding_edge/src/types.cc
 /branches/bleeding_edge/src/types.h
 /branches/bleeding_edge/test/cctest/test-types.cc

=======================================
--- /branches/bleeding_edge/src/types.cc        Tue Jan 21 16:22:52 2014 UTC
+++ /branches/bleeding_edge/src/types.cc        Fri Jan 24 11:47:47 2014 UTC
@@ -538,6 +538,30 @@
     return Config::from_union(unioned);
   }
 }
+
+
+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.
@@ -620,5 +644,12 @@
 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
 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
=======================================
--- /branches/bleeding_edge/src/types.h Tue Jan 21 16:22:52 2014 UTC
+++ /branches/bleeding_edge/src/types.h Fri Jan 24 11:47:47 2014 UTC
@@ -242,6 +242,10 @@
ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion());
     return t;
   }
+
+  template<class OtherTypeImpl>
+  static TypeHandle Convert(
+      typename OtherTypeImpl::TypeHandle type, Region* region);

 #ifdef OBJECT_PRINT
   void TypePrint();
@@ -250,6 +254,7 @@

  private:
   template<class> friend class Iterator;
+  template<class> friend class TypeImpl;

   // A union is a fixed array containing types. Invariants:
   // - its length is at least 2
@@ -271,6 +276,13 @@
   bool IsUnion() { return Config::is_union(this); }
   int AsBitset() { return Config::as_bitset(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);

=======================================
--- /branches/bleeding_edge/test/cctest/test-types.cc Wed Jan 22 15:06:36 2014 UTC +++ /branches/bleeding_edge/test/cctest/test-types.cc Fri Jan 24 11:47:47 2014 UTC
@@ -118,6 +118,54 @@
   TypeHandle Intersect(TypeHandle t1, TypeHandle t2) {
     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:
   Region* region_;
@@ -756,6 +804,16 @@
             T.Union(T.ObjectConstant1, T.ArrayConstant2)),
         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;
@@ -809,3 +867,10 @@
   ZoneTests().Intersect();
   HeapTests().Intersect();
 }
+
+
+TEST(Convert) {
+  CcTest::InitializeVM();
+  ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>();
+  HeapTests().Convert<Type, Type*, Zone, ZoneRep>();
+}

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to