v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
asm-types.h
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
5#ifndef V8_ASMJS_ASM_TYPES_H_
6#define V8_ASMJS_ASM_TYPES_H_
7
8#include <string>
9
11#include "src/base/macros.h"
13#include "src/zone/zone.h"
14
15namespace v8 {
16namespace internal {
17namespace wasm {
18
19class AsmType;
20class AsmFunctionType;
21class AsmOverloadedFunctionType;
22
23// List of V(CamelName, string_name, number, parent_types)
24#define FOR_EACH_ASM_VALUE_TYPE_LIST(V) \
25 /* These tags are not types that are expressable in the asm source. They */ \
26 /* are used to express semantic information about the types they tag. */ \
27 V(Heap, "[]", 1, 0) \
28 V(FloatishDoubleQ, "floatish|double?", 2, 0) \
29 V(FloatQDoubleQ, "float?|double?", 3, 0) \
30 /* The following are actual types that appear in the asm source. */ \
31 V(Void, "void", 4, 0) \
32 V(Extern, "extern", 5, 0) \
33 V(DoubleQ, "double?", 6, kAsmFloatishDoubleQ | kAsmFloatQDoubleQ) \
34 V(Double, "double", 7, kAsmDoubleQ | kAsmExtern) \
35 V(Intish, "intish", 8, 0) \
36 V(Int, "int", 9, kAsmIntish) \
37 V(Signed, "signed", 10, kAsmInt | kAsmExtern) \
38 V(Unsigned, "unsigned", 11, kAsmInt) \
39 V(FixNum, "fixnum", 12, kAsmSigned | kAsmUnsigned) \
40 V(Floatish, "floatish", 13, kAsmFloatishDoubleQ) \
41 V(FloatQ, "float?", 14, kAsmFloatQDoubleQ | kAsmFloatish) \
42 V(Float, "float", 15, kAsmFloatQ) \
43 /* Types used for expressing the Heap accesses. */ \
44 V(Uint8Array, "Uint8Array", 16, kAsmHeap) \
45 V(Int8Array, "Int8Array", 17, kAsmHeap) \
46 V(Uint16Array, "Uint16Array", 18, kAsmHeap) \
47 V(Int16Array, "Int16Array", 19, kAsmHeap) \
48 V(Uint32Array, "Uint32Array", 20, kAsmHeap) \
49 V(Int32Array, "Int32Array", 21, kAsmHeap) \
50 V(Float32Array, "Float32Array", 22, kAsmHeap) \
51 V(Float64Array, "Float64Array", 23, kAsmHeap) \
52 /* None is used to represent errors in the type checker. */ \
53 V(None, "<none>", 31, 0)
54
55// List of V(CamelName)
56#define FOR_EACH_ASM_CALLABLE_TYPE_LIST(V) \
57 V(FunctionType) \
58 V(OverloadedFunctionType)
59
61 public:
62 using bitset_t = uint32_t;
63
64 enum : uint32_t {
65#define DEFINE_TAG(CamelName, string_name, number, parent_types) \
66 kAsm##CamelName = ((1u << (number)) | (parent_types)),
68#undef DEFINE_TAG
71 };
72
73 private:
74 friend class AsmType;
75
77 if ((reinterpret_cast<uintptr_t>(type) & kAsmValueTypeTag) ==
79 return reinterpret_cast<AsmValueType*>(type);
80 }
81 return nullptr;
82 }
83
84 bitset_t Bitset() const {
85 DCHECK_EQ(reinterpret_cast<uintptr_t>(this) & kAsmValueTypeTag,
87 return static_cast<bitset_t>(reinterpret_cast<uintptr_t>(this) &
88 ~kAsmValueTypeTag);
89 }
90
91 static AsmType* New(bitset_t bits) {
92 DCHECK_EQ((bits & kAsmValueTypeTag), 0u);
93 return reinterpret_cast<AsmType*>(
94 static_cast<uintptr_t>(bits | kAsmValueTypeTag));
95 }
96
97 // AsmValueTypes can't be created except through AsmValueType::New.
99};
100
102 public:
105
106 virtual std::string Name() = 0;
107
108 virtual bool CanBeInvokedWith(AsmType* return_type,
109 const ZoneVector<AsmType*>& args) = 0;
110
111#define DECLARE_CAST(CamelName) \
112 virtual Asm##CamelName* As##CamelName() { return nullptr; }
114#undef DECLARE_CAST
115
116 protected:
117 AsmCallableType() = default;
118 virtual ~AsmCallableType() = default;
119 virtual bool IsA(AsmType* other);
120
121 private:
122 friend class AsmType;
123};
124
126 public:
129
130 AsmFunctionType* AsFunctionType() final { return this; }
131
132 void AddArgument(AsmType* type) { args_.push_back(type); }
133 const ZoneVector<AsmType*>& Arguments() const { return args_; }
134 AsmType* ReturnType() const { return return_type_; }
135
136 bool CanBeInvokedWith(AsmType* return_type,
137 const ZoneVector<AsmType*>& args) override;
138
139 protected:
140 AsmFunctionType(Zone* zone, AsmType* return_type)
141 : return_type_(return_type), args_(zone) {}
142
143 private:
144 friend AsmType;
145 friend Zone;
146
147 std::string Name() override;
148 bool IsA(AsmType* other) override;
149
152};
153
155 : public AsmCallableType {
156 public:
158 return this;
159 }
160
161 void AddOverload(AsmType* overload);
162
163 private:
164 friend AsmType;
165 friend Zone;
166
167 explicit AsmOverloadedFunctionType(Zone* zone) : overloads_(zone) {}
168
169 std::string Name() override;
170 bool CanBeInvokedWith(AsmType* return_type,
171 const ZoneVector<AsmType*>& args) override;
172
174
176};
177
179 public:
180#define DEFINE_CONSTRUCTOR(CamelName, string_name, number, parent_types) \
181 static AsmType* CamelName() { \
182 return AsmValueType::New(AsmValueType::kAsm##CamelName); \
183 }
185#undef DEFINE_CONSTRUCTOR
186
187#define DEFINE_CAST(CamelCase) \
188 Asm##CamelCase* As##CamelCase() { \
189 if (AsValueType() != nullptr) { \
190 return nullptr; \
191 } \
192 return reinterpret_cast<AsmCallableType*>(this)->As##CamelCase(); \
193 }
195#undef DEFINE_CAST
196 AsmValueType* AsValueType() { return AsmValueType::AsValueType(this); }
197 AsmCallableType* AsCallableType();
198
199 // A function returning ret. Callers still need to invoke AddArgument with the
200 // returned type to fully create this type.
201 static AsmType* Function(Zone* zone, AsmType* ret) {
202 AsmFunctionType* f = zone->New<AsmFunctionType>(zone, ret);
203 return reinterpret_cast<AsmType*>(f);
204 }
205
206 // Overloaded function types. Not creatable by asm source, but useful to
207 // represent the overloaded stdlib functions.
209 auto* f = zone->New<AsmOverloadedFunctionType>(zone);
210 return reinterpret_cast<AsmType*>(f);
211 }
212
213 // The type for fround(src).
214 static AsmType* FroundType(Zone* zone);
215
216 // The (variadic) type for min and max.
217 static AsmType* MinMaxType(Zone* zone, AsmType* dest, AsmType* src);
218
219 std::string Name();
220 // IsExactly returns true if x is the exact same type as y. For
221 // non-value types (e.g., callables), this returns x == y.
222 static bool IsExactly(AsmType* x, AsmType* y);
223 // IsA is used to query whether this is an instance of that (i.e., if this is
224 // a type derived from that.) For non-value types (e.g., callables), this
225 // returns this == that.
226 bool IsA(AsmType* that);
227
228 // The following methods are meant to be used for inspecting the traits of
229 // element types for the heap view types.
230 enum : int32_t { kNotHeapType = -1 };
231
232 // Returns the element size if this is a heap type. Otherwise returns
233 // kNotHeapType.
234 int32_t ElementSizeInBytes();
235 // Returns the load type if this is a heap type. AsmType::None is returned if
236 // this is not a heap type.
237 AsmType* LoadType();
238 // Returns the store type if this is a heap type. AsmType::None is returned if
239 // this is not a heap type.
241};
242
243} // namespace wasm
244} // namespace internal
245} // namespace v8
246
247#endif // V8_ASMJS_ASM_TYPES_H_
AsmType * return_type_
Definition asm-types.cc:224
friend AsmType
Definition asm-types.cc:160
#define DEFINE_TAG(CamelName, string_name, number, parent_types)
Definition asm-types.h:65
#define DEFINE_CONSTRUCTOR(CamelName, string_name, number, parent_types)
Definition asm-types.h:180
#define DECLARE_CAST(CamelName)
Definition asm-types.h:111
#define DEFINE_CAST(CamelCase)
Definition asm-types.h:187
#define FOR_EACH_ASM_CALLABLE_TYPE_LIST(V)
Definition asm-types.h:56
#define FOR_EACH_ASM_VALUE_TYPE_LIST(V)
Definition asm-types.h:24
T * New(Args &&... args)
Definition zone.h:114
AsmCallableType & operator=(const AsmCallableType &)=delete
virtual std::string Name()=0
virtual bool CanBeInvokedWith(AsmType *return_type, const ZoneVector< AsmType * > &args)=0
AsmCallableType(const AsmCallableType &)=delete
AsmFunctionType * AsFunctionType() final
Definition asm-types.h:130
AsmFunctionType & operator=(const AsmFunctionType &)=delete
ZoneVector< AsmType * > args_
Definition asm-types.h:151
AsmFunctionType(const AsmFunctionType &)=delete
AsmFunctionType(Zone *zone, AsmType *return_type)
Definition asm-types.h:140
const ZoneVector< AsmType * > & Arguments() const
Definition asm-types.h:133
void AddArgument(AsmType *type)
Definition asm-types.h:132
AsmOverloadedFunctionType * AsOverloadedFunctionType() override
Definition asm-types.h:157
DISALLOW_IMPLICIT_CONSTRUCTORS(AsmOverloadedFunctionType)
AsmValueType * AsValueType()
Definition asm-types.h:196
static AsmType * OverloadedFunction(Zone *zone)
Definition asm-types.h:208
static AsmType * Function(Zone *zone, AsmType *ret)
Definition asm-types.h:201
DISALLOW_IMPLICIT_CONSTRUCTORS(AsmValueType)
static AsmValueType * AsValueType(AsmType *type)
Definition asm-types.h:76
static AsmType * New(bitset_t bits)
Definition asm-types.h:91
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
int y
int x
V8_EXPORT_PRIVATE constexpr int ElementSizeInBytes(MachineRepresentation)
Definition c-api.cc:87
#define NON_EXPORTED_BASE(code)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
wasm::ValueType type