OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_MACHINE_TYPE_H_ | 5 #ifndef V8_MACHINE_TYPE_H_ |
6 #define V8_MACHINE_TYPE_H_ | 6 #define V8_MACHINE_TYPE_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 | 9 |
10 #include "src/base/bits.h" | 10 #include "src/base/bits.h" |
11 #include "src/globals.h" | 11 #include "src/globals.h" |
12 #include "src/signature.h" | 12 #include "src/signature.h" |
13 #include "src/zone.h" | 13 #include "src/zone.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 // Machine-level types and representations. | 18 enum class MachineRepresentation : uint8_t { |
19 // TODO(titzer): Use the real type system instead of MachineType. | 19 kNone, |
20 enum MachineType : uint16_t { | 20 kBit, |
21 // Representations. | 21 kWord8, |
22 kRepBit = 1u << 0, | 22 kWord16, |
23 kRepWord8 = 1u << 1, | 23 kWord32, |
24 kRepWord16 = 1u << 2, | 24 kWord64, |
25 kRepWord32 = 1u << 3, | 25 kFloat32, |
26 kRepWord64 = 1u << 4, | 26 kFloat64, |
27 kRepFloat32 = 1u << 5, | 27 kTagged |
28 kRepFloat64 = 1u << 6, | |
29 kRepTagged = 1u << 7, | |
30 | |
31 // Types. | |
32 kTypeBool = 1u << 8, | |
33 kTypeInt32 = 1u << 9, | |
34 kTypeUint32 = 1u << 10, | |
35 kTypeInt64 = 1u << 11, | |
36 kTypeUint64 = 1u << 12, | |
37 kTypeNumber = 1u << 13, | |
38 kTypeAny = 1u << 14, | |
39 | |
40 // Machine types. | |
41 kMachNone = 0u, | |
42 kMachBool = kRepBit | kTypeBool, | |
43 kMachFloat32 = kRepFloat32 | kTypeNumber, | |
44 kMachFloat64 = kRepFloat64 | kTypeNumber, | |
45 kMachInt8 = kRepWord8 | kTypeInt32, | |
46 kMachUint8 = kRepWord8 | kTypeUint32, | |
47 kMachInt16 = kRepWord16 | kTypeInt32, | |
48 kMachUint16 = kRepWord16 | kTypeUint32, | |
49 kMachInt32 = kRepWord32 | kTypeInt32, | |
50 kMachUint32 = kRepWord32 | kTypeUint32, | |
51 kMachInt64 = kRepWord64 | kTypeInt64, | |
52 kMachUint64 = kRepWord64 | kTypeUint64, | |
53 kMachIntPtr = (kPointerSize == 4) ? kMachInt32 : kMachInt64, | |
54 kMachUintPtr = (kPointerSize == 4) ? kMachUint32 : kMachUint64, | |
55 kMachPtr = (kPointerSize == 4) ? kRepWord32 : kRepWord64, | |
56 kMachAnyTagged = kRepTagged | kTypeAny | |
57 }; | 28 }; |
58 | 29 |
59 V8_INLINE size_t hash_value(MachineType type) { | 30 enum class MachineSemantic : uint8_t { |
60 return static_cast<size_t>(type); | 31 kNone, |
| 32 kBool, |
| 33 kInt32, |
| 34 kUint32, |
| 35 kInt64, |
| 36 kUint64, |
| 37 kNumber, |
| 38 kAny |
| 39 }; |
| 40 |
| 41 class MachineType { |
| 42 public: |
| 43 MachineType() |
| 44 : representation_(MachineRepresentation::kNone), |
| 45 semantic_(MachineSemantic::kNone) {} |
| 46 MachineType(MachineRepresentation representation, MachineSemantic semantic) |
| 47 : representation_(representation), semantic_(semantic) {} |
| 48 |
| 49 bool operator==(MachineType other) const { |
| 50 return representation() == other.representation() && |
| 51 semantic() == other.semantic(); |
| 52 } |
| 53 |
| 54 bool operator!=(MachineType other) const { return !(*this == other); } |
| 55 |
| 56 |
| 57 MachineRepresentation representation() const { return representation_; } |
| 58 MachineSemantic semantic() const { return semantic_; } |
| 59 |
| 60 bool IsSigned() { |
| 61 return semantic() == MachineSemantic::kInt32 || |
| 62 semantic() == MachineSemantic::kInt64; |
| 63 } |
| 64 bool IsUnsigned() { |
| 65 return semantic() == MachineSemantic::kUint32 || |
| 66 semantic() == MachineSemantic::kUint64; |
| 67 } |
| 68 |
| 69 static MachineRepresentation PointerRepresentation() { |
| 70 return (kPointerSize == 4) ? MachineRepresentation::kWord32 |
| 71 : MachineRepresentation::kWord64; |
| 72 } |
| 73 static MachineType Pointer() { |
| 74 return MachineType(PointerRepresentation(), MachineSemantic::kNone); |
| 75 } |
| 76 static MachineType IntPtr() { |
| 77 return (kPointerSize == 4) ? Int32() : Int64(); |
| 78 } |
| 79 static MachineType Float32() { |
| 80 return MachineType(MachineRepresentation::kFloat32, |
| 81 MachineSemantic::kNumber); |
| 82 } |
| 83 static MachineType Float64() { |
| 84 return MachineType(MachineRepresentation::kFloat64, |
| 85 MachineSemantic::kNumber); |
| 86 } |
| 87 static MachineType Int8() { |
| 88 return MachineType(MachineRepresentation::kWord8, MachineSemantic::kInt32); |
| 89 } |
| 90 static MachineType Uint8() { |
| 91 return MachineType(MachineRepresentation::kWord8, MachineSemantic::kUint32); |
| 92 } |
| 93 static MachineType Int16() { |
| 94 return MachineType(MachineRepresentation::kWord16, MachineSemantic::kInt32); |
| 95 } |
| 96 static MachineType Uint16() { |
| 97 return MachineType(MachineRepresentation::kWord16, |
| 98 MachineSemantic::kUint32); |
| 99 } |
| 100 static MachineType Int32() { |
| 101 return MachineType(MachineRepresentation::kWord32, MachineSemantic::kInt32); |
| 102 } |
| 103 static MachineType Uint32() { |
| 104 return MachineType(MachineRepresentation::kWord32, |
| 105 MachineSemantic::kUint32); |
| 106 } |
| 107 static MachineType Int64() { |
| 108 return MachineType(MachineRepresentation::kWord64, MachineSemantic::kInt64); |
| 109 } |
| 110 static MachineType Uint64() { |
| 111 return MachineType(MachineRepresentation::kWord64, |
| 112 MachineSemantic::kUint64); |
| 113 } |
| 114 static MachineType AnyTagged() { |
| 115 return MachineType(MachineRepresentation::kTagged, MachineSemantic::kAny); |
| 116 } |
| 117 static MachineType Bool() { |
| 118 return MachineType(MachineRepresentation::kBit, MachineSemantic::kBool); |
| 119 } |
| 120 static MachineType TaggedBool() { |
| 121 return MachineType(MachineRepresentation::kTagged, MachineSemantic::kBool); |
| 122 } |
| 123 static MachineType None() { |
| 124 return MachineType(MachineRepresentation::kNone, MachineSemantic::kNone); |
| 125 } |
| 126 |
| 127 // These naked representations should eventually go away. |
| 128 static MachineType RepWord8() { |
| 129 return MachineType(MachineRepresentation::kWord8, MachineSemantic::kNone); |
| 130 } |
| 131 static MachineType RepWord16() { |
| 132 return MachineType(MachineRepresentation::kWord16, MachineSemantic::kNone); |
| 133 } |
| 134 static MachineType RepWord32() { |
| 135 return MachineType(MachineRepresentation::kWord32, MachineSemantic::kNone); |
| 136 } |
| 137 static MachineType RepWord64() { |
| 138 return MachineType(MachineRepresentation::kWord64, MachineSemantic::kNone); |
| 139 } |
| 140 static MachineType RepFloat32() { |
| 141 return MachineType(MachineRepresentation::kFloat32, MachineSemantic::kNone); |
| 142 } |
| 143 static MachineType RepFloat64() { |
| 144 return MachineType(MachineRepresentation::kFloat64, MachineSemantic::kNone); |
| 145 } |
| 146 static MachineType RepTagged() { |
| 147 return MachineType(MachineRepresentation::kTagged, MachineSemantic::kNone); |
| 148 } |
| 149 static MachineType RepBit() { |
| 150 return MachineType(MachineRepresentation::kBit, MachineSemantic::kNone); |
| 151 } |
| 152 |
| 153 private: |
| 154 MachineRepresentation representation_; |
| 155 MachineSemantic semantic_; |
| 156 }; |
| 157 |
| 158 V8_INLINE size_t hash_value(MachineRepresentation rep) { |
| 159 return static_cast<size_t>(rep); |
61 } | 160 } |
62 | 161 |
63 std::ostream& operator<<(std::ostream& os, const MachineType& type); | 162 V8_INLINE size_t hash_value(MachineType type) { |
64 | 163 return static_cast<size_t>(type.representation()) + |
65 typedef uint16_t MachineTypeUnion; | 164 static_cast<size_t>(type.semantic()) * 16; |
66 | |
67 // Globally useful machine types and constants. | |
68 const MachineTypeUnion kRepMask = kRepBit | kRepWord8 | kRepWord16 | | |
69 kRepWord32 | kRepWord64 | kRepFloat32 | | |
70 kRepFloat64 | kRepTagged; | |
71 const MachineTypeUnion kTypeMask = kTypeBool | kTypeInt32 | kTypeUint32 | | |
72 kTypeInt64 | kTypeUint64 | kTypeNumber | | |
73 kTypeAny; | |
74 | |
75 // Gets only the type of the given type. | |
76 inline MachineType TypeOf(MachineType machine_type) { | |
77 int result = machine_type & kTypeMask; | |
78 return static_cast<MachineType>(result); | |
79 } | 165 } |
80 | 166 |
81 // Gets only the representation of the given type. | 167 std::ostream& operator<<(std::ostream& os, MachineRepresentation rep); |
82 inline MachineType RepresentationOf(MachineType machine_type) { | 168 std::ostream& operator<<(std::ostream& os, MachineSemantic type); |
83 int result = machine_type & kRepMask; | 169 std::ostream& operator<<(std::ostream& os, MachineType type); |
84 CHECK(base::bits::IsPowerOfTwo32(result)); | 170 |
85 return static_cast<MachineType>(result); | 171 inline bool IsFloatingPoint(MachineRepresentation rep) { |
| 172 return rep == MachineRepresentation::kFloat32 || |
| 173 rep == MachineRepresentation::kFloat64; |
86 } | 174 } |
87 | 175 |
88 // Gets the log2 of the element size in bytes of the machine type. | 176 // Gets the log2 of the element size in bytes of the machine type. |
89 inline int ElementSizeLog2Of(MachineType machine_type) { | 177 inline int ElementSizeLog2Of(MachineRepresentation rep) { |
90 switch (RepresentationOf(machine_type)) { | 178 switch (rep) { |
91 case kRepBit: | 179 case MachineRepresentation::kBit: |
92 case kRepWord8: | 180 case MachineRepresentation::kWord8: |
93 return 0; | 181 return 0; |
94 case kRepWord16: | 182 case MachineRepresentation::kWord16: |
95 return 1; | 183 return 1; |
96 case kRepWord32: | 184 case MachineRepresentation::kWord32: |
97 case kRepFloat32: | 185 case MachineRepresentation::kFloat32: |
98 return 2; | 186 return 2; |
99 case kRepWord64: | 187 case MachineRepresentation::kWord64: |
100 case kRepFloat64: | 188 case MachineRepresentation::kFloat64: |
101 return 3; | 189 return 3; |
102 case kRepTagged: | 190 case MachineRepresentation::kTagged: |
103 return kPointerSizeLog2; | 191 return kPointerSizeLog2; |
104 default: | 192 default: |
105 break; | 193 break; |
106 } | 194 } |
107 UNREACHABLE(); | 195 UNREACHABLE(); |
108 return -1; | 196 return -1; |
109 } | 197 } |
110 | 198 |
111 // Gets the element size in bytes of the machine type. | |
112 inline int ElementSizeOf(MachineType machine_type) { | |
113 const int shift = ElementSizeLog2Of(machine_type); | |
114 DCHECK_NE(-1, shift); | |
115 return 1 << shift; | |
116 } | |
117 | |
118 inline bool IsFloatingPoint(MachineType type) { | |
119 MachineType rep = RepresentationOf(type); | |
120 return rep == kRepFloat32 || rep == kRepFloat64; | |
121 } | |
122 | |
123 typedef Signature<MachineType> MachineSignature; | 199 typedef Signature<MachineType> MachineSignature; |
124 | 200 |
125 } // namespace internal | 201 } // namespace internal |
126 } // namespace v8 | 202 } // namespace v8 |
127 | 203 |
128 #endif // V8_MACHINE_TYPE_H_ | 204 #endif // V8_MACHINE_TYPE_H_ |
OLD | NEW |