hydrogen-types.h 2.9 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_CRANKSHAFT_HYDROGEN_TYPES_H_
#define V8_CRANKSHAFT_HYDROGEN_TYPES_H_
7 8

#include <climits>
9
#include <iosfwd>
10

11
#include "src/base/macros.h"
12
#include "src/types.h"
13 14 15 16 17 18

namespace v8 {
namespace internal {

// Forward declarations.
template <typename T> class Handle;
19
class FieldType;
20 21
class Object;

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#define HTYPE_LIST(V)                               \
  V(Any, 0x0)             /* 0000 0000 0000 0000 */ \
  V(Tagged, 0x1)          /* 0000 0000 0000 0001 */ \
  V(TaggedPrimitive, 0x5) /* 0000 0000 0000 0101 */ \
  V(TaggedNumber, 0xd)    /* 0000 0000 0000 1101 */ \
  V(Smi, 0x1d)            /* 0000 0000 0001 1101 */ \
  V(HeapObject, 0x21)     /* 0000 0000 0010 0001 */ \
  V(HeapPrimitive, 0x25)  /* 0000 0000 0010 0101 */ \
  V(Null, 0x27)           /* 0000 0000 0010 0111 */ \
  V(HeapNumber, 0x2d)     /* 0000 0000 0010 1101 */ \
  V(String, 0x65)         /* 0000 0000 0110 0101 */ \
  V(Boolean, 0xa5)        /* 0000 0000 1010 0101 */ \
  V(Undefined, 0x125)     /* 0000 0001 0010 0101 */ \
  V(JSReceiver, 0x221)    /* 0000 0010 0010 0001 */ \
  V(JSObject, 0x621)      /* 0000 0110 0010 0001 */ \
  V(JSArray, 0xe21)       /* 0000 1110 0010 0001 */ \
  V(None, 0xfff)          /* 0000 1111 1111 1111 */
39

40
class HType final {
41 42
 public:
  #define DECLARE_CONSTRUCTOR(Name, mask) \
43
    static HType Name() WARN_UNUSED_RESULT { return HType(k##Name); }
44 45 46 47
  HTYPE_LIST(DECLARE_CONSTRUCTOR)
  #undef DECLARE_CONSTRUCTOR

  // Return the weakest (least precise) common type.
48
  HType Combine(HType other) const WARN_UNUSED_RESULT {
49 50 51
    return HType(static_cast<Kind>(kind_ & other.kind_));
  }

52
  bool Equals(HType other) const WARN_UNUSED_RESULT {
53 54 55
    return kind_ == other.kind_;
  }

56
  bool IsSubtypeOf(HType other) const WARN_UNUSED_RESULT {
57 58 59 60
    return Combine(other).Equals(other);
  }

  #define DECLARE_IS_TYPE(Name, mask)               \
61
    bool Is##Name() const WARN_UNUSED_RESULT {   \
62 63 64 65 66
      return IsSubtypeOf(HType::Name());            \
    }
  HTYPE_LIST(DECLARE_IS_TYPE)
  #undef DECLARE_IS_TYPE

67 68 69
  static HType FromType(Type* type) WARN_UNUSED_RESULT;
  static HType FromFieldType(Handle<FieldType> type,
                             Zone* temp_zone) WARN_UNUSED_RESULT;
70
  static HType FromValue(Handle<Object> value) WARN_UNUSED_RESULT;
71

72
  friend std::ostream& operator<<(std::ostream& os, const HType& t);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

 private:
  enum Kind {
    #define DECLARE_TYPE(Name, mask) k##Name = mask,
    HTYPE_LIST(DECLARE_TYPE)
    #undef DECLARE_TYPE
    LAST_KIND = kNone
  };

  // Make sure type fits in int16.
  STATIC_ASSERT(LAST_KIND < (1 << (CHAR_BIT * sizeof(int16_t))));

  explicit HType(Kind kind) : kind_(kind) { }

  int16_t kind_;
};

90

91
std::ostream& operator<<(std::ostream& os, const HType& t);
92 93
}  // namespace internal
}  // namespace v8
94

95
#endif  // V8_CRANKSHAFT_HYDROGEN_TYPES_H_