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.
5 #ifndef V8_COMPILER_MACHINE_TYPE_H_
6 #define V8_COMPILER_MACHINE_TYPE_H_
10 #include "src/base/bits.h"
11 #include "src/globals.h"
18 // Machine-level types and representations.
19 // TODO(titzer): Use the real type system instead of MachineType.
34 kTypeUint32 = 1 << 10,
36 kTypeUint64 = 1 << 12,
37 kTypeNumber = 1 << 13,
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
59 std::ostream& operator<<(std::ostream& os, const MachineType& type);
61 typedef uint16_t MachineTypeUnion;
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 |
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);
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);
84 // Gets the log2 of the element size in bytes of the machine type.
85 inline int ElementSizeLog2Of(MachineType machine_type) {
86 switch (RepresentationOf(machine_type)) {
99 return kPointerSizeLog2;
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);
114 // Describes the inputs and outputs of a function or call.
115 template <typename T>
116 class Signature : public ZoneObject {
118 Signature(size_t return_count, size_t parameter_count, T* reps)
119 : return_count_(return_count),
120 parameter_count_(parameter_count),
123 size_t return_count() const { return return_count_; }
124 size_t parameter_count() const { return parameter_count_; }
126 T GetParam(size_t index) const {
127 DCHECK(index < parameter_count_);
128 return reps_[return_count_ + index];
131 T GetReturn(size_t index = 0) const {
132 DCHECK(index < return_count_);
136 // For incrementally building signatures.
139 Builder(Zone* zone, size_t return_count, size_t parameter_count)
140 : return_count_(return_count),
141 parameter_count_(parameter_count),
145 buffer_(zone->NewArray<T>(
146 static_cast<int>(return_count + parameter_count))) {}
148 const size_t return_count_;
149 const size_t parameter_count_;
151 void AddReturn(T val) {
152 DCHECK(rcursor_ < return_count_);
153 buffer_[rcursor_++] = val;
155 void AddParam(T val) {
156 DCHECK(pcursor_ < parameter_count_);
157 buffer_[return_count_ + pcursor_++] = val;
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_);
173 size_t return_count_;
174 size_t parameter_count_;
178 typedef Signature<MachineType> MachineSignature;
179 } // namespace compiler
180 } // namespace internal
183 #endif // V8_COMPILER_MACHINE_TYPE_H_