blob: 4c51a9ffe5ebd45a89fa3a24723307f6690f13f2 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Bernierd0a1eb72015-03-24 16:35:39 -04008#include <iosfwd>
9
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/base/bits.h"
11#include "src/globals.h"
12#include "src/zone.h"
13
14namespace v8 {
15namespace internal {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016namespace compiler {
17
18// Machine-level types and representations.
19// TODO(titzer): Use the real type system instead of MachineType.
20enum 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 Bernierd0a1eb72015-03-24 16:35:39 -040042 kMachBool = kRepBit | kTypeBool,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 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 Bernierd0a1eb72015-03-24 16:35:39 -040053 kMachIntPtr = (kPointerSize == 4) ? kMachInt32 : kMachInt64,
54 kMachUintPtr = (kPointerSize == 4) ? kMachUint32 : kMachUint64,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 kMachPtr = (kPointerSize == 4) ? kRepWord32 : kRepWord64,
56 kMachAnyTagged = kRepTagged | kTypeAny
57};
58
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059std::ostream& operator<<(std::ostream& os, const MachineType& type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
61typedef uint16_t MachineTypeUnion;
62
63// Globally useful machine types and constants.
64const MachineTypeUnion kRepMask = kRepBit | kRepWord8 | kRepWord16 |
65 kRepWord32 | kRepWord64 | kRepFloat32 |
66 kRepFloat64 | kRepTagged;
67const MachineTypeUnion kTypeMask = kTypeBool | kTypeInt32 | kTypeUint32 |
68 kTypeInt64 | kTypeUint64 | kTypeNumber |
69 kTypeAny;
70
71// Gets only the type of the given type.
72inline 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.
78inline 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 Bernierd0a1eb72015-03-24 16:35:39 -040084// Gets the log2 of the element size in bytes of the machine type.
85inline int ElementSizeLog2Of(MachineType machine_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 switch (RepresentationOf(machine_type)) {
87 case kRepBit:
88 case kRepWord8:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 return 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 case kRepWord16:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091 return 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 case kRepWord32:
93 case kRepFloat32:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040094 return 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 case kRepWord64:
96 case kRepFloat64:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040097 return 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 case kRepTagged:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040099 return kPointerSizeLog2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 default:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400101 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400103 UNREACHABLE();
104 return -1;
105}
106
107// Gets the element size in bytes of the machine type.
108inline int ElementSizeOf(MachineType machine_type) {
109 const int shift = ElementSizeLog2Of(machine_type);
110 DCHECK_NE(-1, shift);
111 return 1 << shift;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112}
113
114// Describes the inputs and outputs of a function or call.
115template <typename T>
116class 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
178typedef Signature<MachineType> MachineSignature;
179} // namespace compiler
180} // namespace internal
181} // namespace v8
182
183#endif // V8_COMPILER_MACHINE_TYPE_H_