Home | History | Annotate | Download | only in crankshaft
      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.
      4 
      5 #ifndef V8_CRANKSHAFT_HYDROGEN_TYPES_H_
      6 #define V8_CRANKSHAFT_HYDROGEN_TYPES_H_
      7 
      8 #include <climits>
      9 #include <iosfwd>
     10 
     11 #include "src/base/macros.h"
     12 
     13 namespace v8 {
     14 namespace internal {
     15 
     16 // Forward declarations.
     17 template <typename T> class Handle;
     18 class Object;
     19 
     20 #define HTYPE_LIST(V)                               \
     21   V(Any, 0x0)             /* 0000 0000 0000 0000 */ \
     22   V(Tagged, 0x1)          /* 0000 0000 0000 0001 */ \
     23   V(TaggedPrimitive, 0x5) /* 0000 0000 0000 0101 */ \
     24   V(TaggedNumber, 0xd)    /* 0000 0000 0000 1101 */ \
     25   V(Smi, 0x1d)            /* 0000 0000 0001 1101 */ \
     26   V(HeapObject, 0x21)     /* 0000 0000 0010 0001 */ \
     27   V(HeapPrimitive, 0x25)  /* 0000 0000 0010 0101 */ \
     28   V(Null, 0x27)           /* 0000 0000 0010 0111 */ \
     29   V(HeapNumber, 0x2d)     /* 0000 0000 0010 1101 */ \
     30   V(String, 0x65)         /* 0000 0000 0110 0101 */ \
     31   V(Boolean, 0xa5)        /* 0000 0000 1010 0101 */ \
     32   V(Undefined, 0x125)     /* 0000 0001 0010 0101 */ \
     33   V(JSReceiver, 0x221)    /* 0000 0010 0010 0001 */ \
     34   V(JSObject, 0x621)      /* 0000 0110 0010 0001 */ \
     35   V(JSArray, 0xe21)       /* 0000 1110 0010 0001 */ \
     36   V(None, 0xfff)          /* 0000 1111 1111 1111 */
     37 
     38 class HType final {
     39  public:
     40   #define DECLARE_CONSTRUCTOR(Name, mask) \
     41     static HType Name() WARN_UNUSED_RESULT { return HType(k##Name); }
     42   HTYPE_LIST(DECLARE_CONSTRUCTOR)
     43   #undef DECLARE_CONSTRUCTOR
     44 
     45   // Return the weakest (least precise) common type.
     46   HType Combine(HType other) const WARN_UNUSED_RESULT {
     47     return HType(static_cast<Kind>(kind_ & other.kind_));
     48   }
     49 
     50   bool Equals(HType other) const WARN_UNUSED_RESULT {
     51     return kind_ == other.kind_;
     52   }
     53 
     54   bool IsSubtypeOf(HType other) const WARN_UNUSED_RESULT {
     55     return Combine(other).Equals(other);
     56   }
     57 
     58   #define DECLARE_IS_TYPE(Name, mask)               \
     59     bool Is##Name() const WARN_UNUSED_RESULT {   \
     60       return IsSubtypeOf(HType::Name());            \
     61     }
     62   HTYPE_LIST(DECLARE_IS_TYPE)
     63   #undef DECLARE_IS_TYPE
     64 
     65   template <class T>
     66   static HType FromType(typename T::TypeHandle type) WARN_UNUSED_RESULT;
     67   static HType FromValue(Handle<Object> value) WARN_UNUSED_RESULT;
     68 
     69   friend std::ostream& operator<<(std::ostream& os, const HType& t);
     70 
     71  private:
     72   enum Kind {
     73     #define DECLARE_TYPE(Name, mask) k##Name = mask,
     74     HTYPE_LIST(DECLARE_TYPE)
     75     #undef DECLARE_TYPE
     76     LAST_KIND = kNone
     77   };
     78 
     79   // Make sure type fits in int16.
     80   STATIC_ASSERT(LAST_KIND < (1 << (CHAR_BIT * sizeof(int16_t))));
     81 
     82   explicit HType(Kind kind) : kind_(kind) { }
     83 
     84   int16_t kind_;
     85 };
     86 
     87 
     88 std::ostream& operator<<(std::ostream& os, const HType& t);
     89 }  // namespace internal
     90 }  // namespace v8
     91 
     92 #endif  // V8_CRANKSHAFT_HYDROGEN_TYPES_H_
     93