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