Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_COMPILER_MACHINE_TYPE_H_ |
| 6 | #define V8_COMPILER_MACHINE_TYPE_H_ |
| 7 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8 | #include <iosfwd> |
| 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/base/bits.h" |
| 11 | #include "src/globals.h" |
| 12 | #include "src/zone.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | namespace compiler { |
| 17 | |
| 18 | // Machine-level types and representations. |
| 19 | // TODO(titzer): Use the real type system instead of MachineType. |
| 20 | enum MachineType { |
| 21 | // Representations. |
| 22 | kRepBit = 1 << 0, |
| 23 | kRepWord8 = 1 << 1, |
| 24 | kRepWord16 = 1 << 2, |
| 25 | kRepWord32 = 1 << 3, |
| 26 | kRepWord64 = 1 << 4, |
| 27 | kRepFloat32 = 1 << 5, |
| 28 | kRepFloat64 = 1 << 6, |
| 29 | kRepTagged = 1 << 7, |
| 30 | |
| 31 | // Types. |
| 32 | kTypeBool = 1 << 8, |
| 33 | kTypeInt32 = 1 << 9, |
| 34 | kTypeUint32 = 1 << 10, |
| 35 | kTypeInt64 = 1 << 11, |
| 36 | kTypeUint64 = 1 << 12, |
| 37 | kTypeNumber = 1 << 13, |
| 38 | kTypeAny = 1 << 14, |
| 39 | |
| 40 | // Machine types. |
| 41 | kMachNone = 0, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 42 | kMachBool = kRepBit | kTypeBool, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 53 | kMachIntPtr = (kPointerSize == 4) ? kMachInt32 : kMachInt64, |
| 54 | kMachUintPtr = (kPointerSize == 4) ? kMachUint32 : kMachUint64, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | kMachPtr = (kPointerSize == 4) ? kRepWord32 : kRepWord64, |
| 56 | kMachAnyTagged = kRepTagged | kTypeAny |
| 57 | }; |
| 58 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | std::ostream& operator<<(std::ostream& os, const MachineType& type); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | |
| 61 | typedef uint16_t MachineTypeUnion; |
| 62 | |
| 63 | // Globally useful machine types and constants. |
| 64 | const MachineTypeUnion kRepMask = kRepBit | kRepWord8 | kRepWord16 | |
| 65 | kRepWord32 | kRepWord64 | kRepFloat32 | |
| 66 | kRepFloat64 | kRepTagged; |
| 67 | const MachineTypeUnion kTypeMask = kTypeBool | kTypeInt32 | kTypeUint32 | |
| 68 | kTypeInt64 | kTypeUint64 | kTypeNumber | |
| 69 | kTypeAny; |
| 70 | |
| 71 | // Gets only the type of the given type. |
| 72 | inline MachineType TypeOf(MachineType machine_type) { |
| 73 | int result = machine_type & kTypeMask; |
| 74 | return static_cast<MachineType>(result); |
| 75 | } |
| 76 | |
| 77 | // Gets only the representation of the given type. |
| 78 | inline MachineType RepresentationOf(MachineType machine_type) { |
| 79 | int result = machine_type & kRepMask; |
| 80 | CHECK(base::bits::IsPowerOfTwo32(result)); |
| 81 | return static_cast<MachineType>(result); |
| 82 | } |
| 83 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 84 | // Gets the log2 of the element size in bytes of the machine type. |
| 85 | inline int ElementSizeLog2Of(MachineType machine_type) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | switch (RepresentationOf(machine_type)) { |
| 87 | case kRepBit: |
| 88 | case kRepWord8: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 89 | return 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | case kRepWord16: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 91 | return 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | case kRepWord32: |
| 93 | case kRepFloat32: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 94 | return 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | case kRepWord64: |
| 96 | case kRepFloat64: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 97 | return 3; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | case kRepTagged: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 99 | return kPointerSizeLog2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | default: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 101 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 103 | UNREACHABLE(); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | // Gets the element size in bytes of the machine type. |
| 108 | inline int ElementSizeOf(MachineType machine_type) { |
| 109 | const int shift = ElementSizeLog2Of(machine_type); |
| 110 | DCHECK_NE(-1, shift); |
| 111 | return 1 << shift; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Describes the inputs and outputs of a function or call. |
| 115 | template <typename T> |
| 116 | class Signature : public ZoneObject { |
| 117 | public: |
| 118 | Signature(size_t return_count, size_t parameter_count, T* reps) |
| 119 | : return_count_(return_count), |
| 120 | parameter_count_(parameter_count), |
| 121 | reps_(reps) {} |
| 122 | |
| 123 | size_t return_count() const { return return_count_; } |
| 124 | size_t parameter_count() const { return parameter_count_; } |
| 125 | |
| 126 | T GetParam(size_t index) const { |
| 127 | DCHECK(index < parameter_count_); |
| 128 | return reps_[return_count_ + index]; |
| 129 | } |
| 130 | |
| 131 | T GetReturn(size_t index = 0) const { |
| 132 | DCHECK(index < return_count_); |
| 133 | return reps_[index]; |
| 134 | } |
| 135 | |
| 136 | // For incrementally building signatures. |
| 137 | class Builder { |
| 138 | public: |
| 139 | Builder(Zone* zone, size_t return_count, size_t parameter_count) |
| 140 | : return_count_(return_count), |
| 141 | parameter_count_(parameter_count), |
| 142 | zone_(zone), |
| 143 | rcursor_(0), |
| 144 | pcursor_(0), |
| 145 | buffer_(zone->NewArray<T>( |
| 146 | static_cast<int>(return_count + parameter_count))) {} |
| 147 | |
| 148 | const size_t return_count_; |
| 149 | const size_t parameter_count_; |
| 150 | |
| 151 | void AddReturn(T val) { |
| 152 | DCHECK(rcursor_ < return_count_); |
| 153 | buffer_[rcursor_++] = val; |
| 154 | } |
| 155 | void AddParam(T val) { |
| 156 | DCHECK(pcursor_ < parameter_count_); |
| 157 | buffer_[return_count_ + pcursor_++] = val; |
| 158 | } |
| 159 | Signature<T>* Build() { |
| 160 | DCHECK(rcursor_ == return_count_); |
| 161 | DCHECK(pcursor_ == parameter_count_); |
| 162 | return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_); |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | Zone* zone_; |
| 167 | size_t rcursor_; |
| 168 | size_t pcursor_; |
| 169 | T* buffer_; |
| 170 | }; |
| 171 | |
| 172 | protected: |
| 173 | size_t return_count_; |
| 174 | size_t parameter_count_; |
| 175 | T* reps_; |
| 176 | }; |
| 177 | |
| 178 | typedef Signature<MachineType> MachineSignature; |
| 179 | } // namespace compiler |
| 180 | } // namespace internal |
| 181 | } // namespace v8 |
| 182 | |
| 183 | #endif // V8_COMPILER_MACHINE_TYPE_H_ |