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_
8 #include "src/base/bits.h"
9 #include "src/globals.h"
19 // Machine-level types and representations.
20 // TODO(titzer): Use the real type system instead of MachineType.
35 kTypeUint32 = 1 << 10,
37 kTypeUint64 = 1 << 12,
38 kTypeNumber = 1 << 13,
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 kMachPtr = (kPointerSize == 4) ? kRepWord32 : kRepWord64,
54 kMachAnyTagged = kRepTagged | kTypeAny
57 OStream& operator<<(OStream& os, const MachineType& type);
59 typedef uint16_t MachineTypeUnion;
61 // Globally useful machine types and constants.
62 const MachineTypeUnion kRepMask = kRepBit | kRepWord8 | kRepWord16 |
63 kRepWord32 | kRepWord64 | kRepFloat32 |
64 kRepFloat64 | kRepTagged;
65 const MachineTypeUnion kTypeMask = kTypeBool | kTypeInt32 | kTypeUint32 |
66 kTypeInt64 | kTypeUint64 | kTypeNumber |
69 // Gets only the type of the given type.
70 inline MachineType TypeOf(MachineType machine_type) {
71 int result = machine_type & kTypeMask;
72 return static_cast<MachineType>(result);
75 // Gets only the representation of the given type.
76 inline MachineType RepresentationOf(MachineType machine_type) {
77 int result = machine_type & kRepMask;
78 CHECK(base::bits::IsPowerOfTwo32(result));
79 return static_cast<MachineType>(result);
82 // Gets the element size in bytes of the machine type.
83 inline int ElementSizeOf(MachineType machine_type) {
84 switch (RepresentationOf(machine_type)) {
104 // Describes the inputs and outputs of a function or call.
105 template <typename T>
106 class Signature : public ZoneObject {
108 Signature(size_t return_count, size_t parameter_count, T* reps)
109 : return_count_(return_count),
110 parameter_count_(parameter_count),
113 size_t return_count() const { return return_count_; }
114 size_t parameter_count() const { return parameter_count_; }
116 T GetParam(size_t index) const {
117 DCHECK(index < parameter_count_);
118 return reps_[return_count_ + index];
121 T GetReturn(size_t index = 0) const {
122 DCHECK(index < return_count_);
126 // For incrementally building signatures.
129 Builder(Zone* zone, size_t return_count, size_t parameter_count)
130 : return_count_(return_count),
131 parameter_count_(parameter_count),
135 buffer_(zone->NewArray<T>(
136 static_cast<int>(return_count + parameter_count))) {}
138 const size_t return_count_;
139 const size_t parameter_count_;
141 void AddReturn(T val) {
142 DCHECK(rcursor_ < return_count_);
143 buffer_[rcursor_++] = val;
145 void AddParam(T val) {
146 DCHECK(pcursor_ < parameter_count_);
147 buffer_[return_count_ + pcursor_++] = val;
149 Signature<T>* Build() {
150 DCHECK(rcursor_ == return_count_);
151 DCHECK(pcursor_ == parameter_count_);
152 return new (zone_) Signature<T>(return_count_, parameter_count_, buffer_);
163 size_t return_count_;
164 size_t parameter_count_;
168 typedef Signature<MachineType> MachineSignature;
169 } // namespace compiler
170 } // namespace internal
173 #endif // V8_COMPILER_MACHINE_TYPE_H_