v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
asm-types.cc
Go to the documentation of this file.
1// Copyright 2016 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
6
7#include <cinttypes>
8
9namespace v8 {
10namespace internal {
11namespace wasm {
12
14 if (AsValueType() != nullptr) {
15 return nullptr;
16 }
17
18 return reinterpret_cast<AsmCallableType*>(this);
19}
20
21std::string AsmType::Name() {
22 AsmValueType* avt = this->AsValueType();
23 if (avt != nullptr) {
24 switch (avt->Bitset()) {
25#define RETURN_TYPE_NAME(CamelName, string_name, number, parent_types) \
26 case AsmValueType::kAsm##CamelName: \
27 return string_name;
29#undef RETURN_TYPE_NAME
30 default:
32 }
33 }
34
35 return this->AsCallableType()->Name();
36}
37
39 // TODO(jpp): maybe this can become x == y.
40 if (x == nullptr) return y == nullptr;
41 AsmValueType* avt = x->AsValueType();
42 if (avt != nullptr) {
43 AsmValueType* tavt = y->AsValueType();
44 if (tavt == nullptr) {
45 return false;
46 }
47 return avt->Bitset() == tavt->Bitset();
48 }
49
50 // TODO(jpp): is it useful to allow non-value types to be tested with
51 // IsExactly?
52 return x == y;
53}
54
55bool AsmType::IsA(AsmType* that) {
56 // IsA is used for querying inheritance relationships. Therefore it is only
57 // meaningful for basic types.
58 if (auto* avt = this->AsValueType()) {
59 if (auto* tavt = that->AsValueType()) {
60 return (avt->Bitset() & tavt->Bitset()) == tavt->Bitset();
61 }
62 return false;
63 }
64
65 if (auto* as_callable = this->AsCallableType()) {
66 return as_callable->IsA(that);
67 }
68
70}
71
73 auto* value = AsValueType();
74 if (value == nullptr) {
76 }
77 switch (value->Bitset()) {
78 case AsmValueType::kAsmInt8Array:
79 case AsmValueType::kAsmUint8Array:
80 return 1;
81 case AsmValueType::kAsmInt16Array:
82 case AsmValueType::kAsmUint16Array:
83 return 2;
84 case AsmValueType::kAsmInt32Array:
85 case AsmValueType::kAsmUint32Array:
86 case AsmValueType::kAsmFloat32Array:
87 return 4;
88 case AsmValueType::kAsmFloat64Array:
89 return 8;
90 default:
92 }
93}
94
96 auto* value = AsValueType();
97 if (value == nullptr) {
98 return AsmType::None();
99 }
100 switch (value->Bitset()) {
101 case AsmValueType::kAsmInt8Array:
102 case AsmValueType::kAsmUint8Array:
103 case AsmValueType::kAsmInt16Array:
104 case AsmValueType::kAsmUint16Array:
105 case AsmValueType::kAsmInt32Array:
106 case AsmValueType::kAsmUint32Array:
107 return AsmType::Intish();
108 case AsmValueType::kAsmFloat32Array:
109 return AsmType::FloatQ();
110 case AsmValueType::kAsmFloat64Array:
111 return AsmType::DoubleQ();
112 default:
113 return AsmType::None();
114 }
115}
116
118 auto* value = AsValueType();
119 if (value == nullptr) {
120 return AsmType::None();
121 }
122 switch (value->Bitset()) {
123 case AsmValueType::kAsmInt8Array:
124 case AsmValueType::kAsmUint8Array:
125 case AsmValueType::kAsmInt16Array:
126 case AsmValueType::kAsmUint16Array:
127 case AsmValueType::kAsmInt32Array:
128 case AsmValueType::kAsmUint32Array:
129 return AsmType::Intish();
130 case AsmValueType::kAsmFloat32Array:
131 return AsmType::FloatishDoubleQ();
132 case AsmValueType::kAsmFloat64Array:
133 return AsmType::FloatQDoubleQ();
134 default:
135 return AsmType::None();
136 }
137}
138
140 return other->AsCallableType() == this;
141}
142
144 std::string ret;
145 ret += "(";
146 for (size_t ii = 0; ii < args_.size(); ++ii) {
147 ret += args_[ii]->Name();
148 if (ii != args_.size() - 1) {
149 ret += ", ";
150 }
151 }
152 ret += ") -> ";
153 ret += return_type_->Name();
154 return ret;
155}
156
157namespace {
158class AsmFroundType final : public AsmCallableType {
159 public:
160 friend AsmType;
161
162 AsmFroundType() : AsmCallableType() {}
163
164 bool CanBeInvokedWith(AsmType* return_type,
165 const ZoneVector<AsmType*>& args) override;
166
167 std::string Name() override { return "fround"; }
168};
169} // namespace
170
172 auto* Fround = zone->New<AsmFroundType>();
173 return reinterpret_cast<AsmType*>(Fround);
174}
175
176bool AsmFroundType::CanBeInvokedWith(AsmType* return_type,
177 const ZoneVector<AsmType*>& args) {
178 if (args.size() != 1) {
179 return false;
180 }
181
182 auto* arg = args[0];
183 if (!arg->IsA(AsmType::Floatish()) && !arg->IsA(AsmType::DoubleQ()) &&
184 !arg->IsA(AsmType::Signed()) && !arg->IsA(AsmType::Unsigned())) {
185 return false;
186 }
187
188 return true;
189}
190
191namespace {
192class AsmMinMaxType final : public AsmCallableType {
193 private:
194 friend AsmType;
195 friend Zone;
196
197 AsmMinMaxType(AsmType* dest, AsmType* src)
198 : AsmCallableType(), return_type_(dest), arg_(src) {}
199
200 bool CanBeInvokedWith(AsmType* return_type,
201 const ZoneVector<AsmType*>& args) override {
202 if (!AsmType::IsExactly(return_type_, return_type)) {
203 return false;
204 }
205
206 if (args.size() < 2) {
207 return false;
208 }
209
210 for (size_t ii = 0; ii < args.size(); ++ii) {
211 if (!args[ii]->IsA(arg_)) {
212 return false;
213 }
214 }
215
216 return true;
217 }
218
219 std::string Name() override {
220 return "(" + arg_->Name() + ", " + arg_->Name() + "...) -> " +
221 return_type_->Name();
222 }
223
226};
227} // namespace
228
231 DCHECK_NOT_NULL(src->AsValueType());
232 auto* MinMax = zone->New<AsmMinMaxType>(dest, src);
233 return reinterpret_cast<AsmType*>(MinMax);
234}
235
237 auto* that = other->AsFunctionType();
238 if (that == nullptr) {
239 return false;
240 }
241 if (!AsmType::IsExactly(return_type_, that->return_type_)) {
242 return false;
243 }
244
245 if (args_.size() != that->args_.size()) {
246 return false;
247 }
248
249 for (size_t ii = 0; ii < args_.size(); ++ii) {
250 if (!AsmType::IsExactly(args_[ii], that->args_[ii])) {
251 return false;
252 }
253 }
254
255 return true;
256}
257
259 const ZoneVector<AsmType*>& args) {
260 if (!AsmType::IsExactly(return_type_, return_type)) {
261 return false;
262 }
263
264 if (args_.size() != args.size()) {
265 return false;
266 }
267
268 for (size_t ii = 0; ii < args_.size(); ++ii) {
269 if (!args[ii]->IsA(args_[ii])) {
270 return false;
271 }
272 }
273
274 return true;
275}
276
278 std::string ret;
279
280 for (size_t ii = 0; ii < overloads_.size(); ++ii) {
281 if (ii != 0) {
282 ret += " /\\ ";
283 }
284 ret += overloads_[ii]->Name();
285 }
286
287 return ret;
288}
289
291 AsmType* return_type, const ZoneVector<AsmType*>& args) {
292 for (size_t ii = 0; ii < overloads_.size(); ++ii) {
293 if (overloads_[ii]->AsCallableType()->CanBeInvokedWith(return_type, args)) {
294 return true;
295 }
296 }
297
298 return false;
299}
300
302 DCHECK_NOT_NULL(overload->AsCallableType());
303 overloads_.push_back(overload);
304}
305
306} // namespace wasm
307} // namespace internal
308} // namespace v8
#define RETURN_TYPE_NAME(CamelName, string_name, number, parent_types)
friend Zone
Definition asm-types.cc:195
AsmType * return_type_
Definition asm-types.cc:224
AsmType * arg_
Definition asm-types.cc:225
friend AsmType
Definition asm-types.cc:160
#define FOR_EACH_ASM_VALUE_TYPE_LIST(V)
Definition asm-types.h:24
T * New(Args &&... args)
Definition zone.h:114
virtual std::string Name()=0
virtual bool IsA(AsmType *other)
Definition asm-types.cc:139
ZoneVector< AsmType * > args_
Definition asm-types.h:151
bool CanBeInvokedWith(AsmType *return_type, const ZoneVector< AsmType * > &args) override
Definition asm-types.cc:258
bool IsA(AsmType *other) override
Definition asm-types.cc:236
std::string Name() override
Definition asm-types.cc:143
bool CanBeInvokedWith(AsmType *return_type, const ZoneVector< AsmType * > &args) override
Definition asm-types.cc:290
static AsmType * FroundType(Zone *zone)
Definition asm-types.cc:171
AsmValueType * AsValueType()
Definition asm-types.h:196
bool IsA(AsmType *that)
Definition asm-types.cc:55
static AsmType * MinMaxType(Zone *zone, AsmType *dest, AsmType *src)
Definition asm-types.cc:229
AsmCallableType * AsCallableType()
Definition asm-types.cc:13
static bool IsExactly(AsmType *x, AsmType *y)
Definition asm-types.cc:38
static AsmValueType * AsValueType(AsmType *type)
Definition asm-types.h:76
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
int x
Definition c-api.cc:87
#define DCHECK_NOT_NULL(val)
Definition logging.h:492