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