• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   AsmCallableType(const AsmCallableType&) = delete;
105   AsmCallableType& operator=(const AsmCallableType&) = delete;
106 
107   virtual std::string Name() = 0;
108 
109   virtual bool CanBeInvokedWith(AsmType* return_type,
110                                 const ZoneVector<AsmType*>& args) = 0;
111 
112 #define DECLARE_CAST(CamelName) \
113   virtual Asm##CamelName* As##CamelName() { return nullptr; }
114   FOR_EACH_ASM_CALLABLE_TYPE_LIST(DECLARE_CAST)
115 #undef DECLARE_CAST
116 
117  protected:
118   AsmCallableType() = default;
119   virtual ~AsmCallableType() = default;
120   virtual bool IsA(AsmType* other);
121 
122  private:
123   friend class AsmType;
124 };
125 
126 class V8_EXPORT_PRIVATE AsmFunctionType final : public AsmCallableType {
127  public:
128   AsmFunctionType(const AsmFunctionType&) = delete;
129   AsmFunctionType& operator=(const AsmFunctionType&) = delete;
130 
AsFunctionType()131   AsmFunctionType* AsFunctionType() final { return this; }
132 
AddArgument(AsmType * type)133   void AddArgument(AsmType* type) { args_.push_back(type); }
Arguments()134   const ZoneVector<AsmType*> Arguments() const { return args_; }
ReturnType()135   AsmType* ReturnType() const { return return_type_; }
136 
137   bool CanBeInvokedWith(AsmType* return_type,
138                         const ZoneVector<AsmType*>& args) override;
139 
140  protected:
AsmFunctionType(Zone * zone,AsmType * return_type)141   AsmFunctionType(Zone* zone, AsmType* return_type)
142       : return_type_(return_type), args_(zone) {}
143 
144  private:
145   friend AsmType;
146   friend Zone;
147 
148   std::string Name() override;
149   bool IsA(AsmType* other) override;
150 
151   AsmType* return_type_;
152   ZoneVector<AsmType*> args_;
153 };
154 
155 class V8_EXPORT_PRIVATE AsmOverloadedFunctionType final
156     : public AsmCallableType {
157  public:
AsOverloadedFunctionType()158   AsmOverloadedFunctionType* AsOverloadedFunctionType() override {
159     return this;
160   }
161 
162   void AddOverload(AsmType* overload);
163 
164  private:
165   friend AsmType;
166   friend Zone;
167 
AsmOverloadedFunctionType(Zone * zone)168   explicit AsmOverloadedFunctionType(Zone* zone) : overloads_(zone) {}
169 
170   std::string Name() override;
171   bool CanBeInvokedWith(AsmType* return_type,
172                         const ZoneVector<AsmType*>& args) override;
173 
174   ZoneVector<AsmType*> overloads_;
175 
176   DISALLOW_IMPLICIT_CONSTRUCTORS(AsmOverloadedFunctionType);
177 };
178 
179 class V8_EXPORT_PRIVATE AsmType {
180  public:
181 #define DEFINE_CONSTRUCTOR(CamelName, string_name, number, parent_types) \
182   static AsmType* CamelName() {                                          \
183     return AsmValueType::New(AsmValueType::kAsm##CamelName);             \
184   }
FOR_EACH_ASM_VALUE_TYPE_LIST(DEFINE_CONSTRUCTOR)185   FOR_EACH_ASM_VALUE_TYPE_LIST(DEFINE_CONSTRUCTOR)
186 #undef DEFINE_CONSTRUCTOR
187 
188 #define DEFINE_CAST(CamelCase)                                        \
189   Asm##CamelCase* As##CamelCase() {                                   \
190     if (AsValueType() != nullptr) {                                   \
191       return nullptr;                                                 \
192     }                                                                 \
193     return reinterpret_cast<AsmCallableType*>(this)->As##CamelCase(); \
194   }
195   FOR_EACH_ASM_CALLABLE_TYPE_LIST(DEFINE_CAST)
196 #undef DEFINE_CAST
197   AsmValueType* AsValueType() { return AsmValueType::AsValueType(this); }
198   AsmCallableType* AsCallableType();
199 
200   // A function returning ret. Callers still need to invoke AddArgument with the
201   // returned type to fully create this type.
Function(Zone * zone,AsmType * ret)202   static AsmType* Function(Zone* zone, AsmType* ret) {
203     AsmFunctionType* f = zone->New<AsmFunctionType>(zone, ret);
204     return reinterpret_cast<AsmType*>(f);
205   }
206 
207   // Overloaded function types. Not creatable by asm source, but useful to
208   // represent the overloaded stdlib functions.
OverloadedFunction(Zone * zone)209   static AsmType* OverloadedFunction(Zone* zone) {
210     auto* f = zone->New<AsmOverloadedFunctionType>(zone);
211     return reinterpret_cast<AsmType*>(f);
212   }
213 
214   // The type for fround(src).
215   static AsmType* FroundType(Zone* zone);
216 
217   // The (variadic) type for min and max.
218   static AsmType* MinMaxType(Zone* zone, AsmType* dest, AsmType* src);
219 
220   std::string Name();
221   // IsExactly returns true if x is the exact same type as y. For
222   // non-value types (e.g., callables), this returns x == y.
223   static bool IsExactly(AsmType* x, AsmType* y);
224   // IsA is used to query whether this is an instance of that (i.e., if this is
225   // a type derived from that.) For non-value types (e.g., callables), this
226   // returns this == that.
227   bool IsA(AsmType* that);
228 
229   // The following methods are meant to be used for inspecting the traits of
230   // element types for the heap view types.
231   enum : int32_t { kNotHeapType = -1 };
232 
233   // Returns the element size if this is a heap type. Otherwise returns
234   // kNotHeapType.
235   int32_t ElementSizeInBytes();
236   // Returns the load type if this is a heap type. AsmType::None is returned if
237   // this is not a heap type.
238   AsmType* LoadType();
239   // Returns the store type if this is a heap type. AsmType::None is returned if
240   // this is not a heap type.
241   AsmType* StoreType();
242 };
243 
244 }  // namespace wasm
245 }  // namespace internal
246 }  // namespace v8
247 
248 #endif  // V8_ASMJS_ASM_TYPES_H_
249