1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*
* Copyright (C) 2015-2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(WEBASSEMBLY)
#include "CodeLocation.h"
#include "Identifier.h"
#include "MacroAssemblerCodeRef.h"
#include "RegisterAtOffsetList.h"
#include "WasmMemoryInformation.h"
#include "WasmName.h"
#include "WasmNameSection.h"
#include "WasmOps.h"
#include "WasmPageCount.h"
#include "WasmSignature.h"
#include <cstdint>
#include <limits>
#include <memory>
#include <wtf/Vector.h>
namespace JSC {
class Compilation;
namespace Wasm {
struct CompilationContext;
struct ModuleInformation;
using BlockSignature = const Signature*;
enum class TableElementType : uint8_t {
Externref,
Funcref
};
inline bool isValueType(Type type)
{
switch (type.kind) {
case TypeKind::I32:
case TypeKind::I64:
case TypeKind::F32:
case TypeKind::F64:
case TypeKind::Externref:
case TypeKind::Funcref:
return true;
case TypeKind::TypeIdx:
return Options::useWebAssemblyTypedFunctionReferences();
default:
break;
}
return false;
}
inline bool isSubtype(Type sub, Type parent)
{
if (sub.isNullable() && !parent.isNullable())
return false;
if (sub.isTypeIdx() && parent.isFuncref())
return true;
return sub == parent;
}
inline bool isRefType(Type type)
{
return type.isFuncref() || type.isExternref() || type.isTypeIdx();
}
inline bool isValidHeapTypeKind(TypeKind kind)
{
switch (kind) {
case TypeKind::Funcref:
case TypeKind::Externref:
return true;
default:
break;
}
return false;
}
inline bool isDefaultableType(Type type)
{
return !isRefType(type) || type.isNullable();
}
enum class ExternalKind : uint8_t {
// FIXME auto-generate this. https://bugs.webkit.org/show_bug.cgi?id=165231
Function = 0,
Table = 1,
Memory = 2,
Global = 3,
};
template<typename Int>
inline bool isValidExternalKind(Int val)
{
switch (val) {
case static_cast<Int>(ExternalKind::Function):
case static_cast<Int>(ExternalKind::Table):
case static_cast<Int>(ExternalKind::Memory):
case static_cast<Int>(ExternalKind::Global):
return true;
}
return false;
}
static_assert(static_cast<int>(ExternalKind::Function) == 0, "Wasm needs Function to have the value 0");
static_assert(static_cast<int>(ExternalKind::Table) == 1, "Wasm needs Table to have the value 1");
static_assert(static_cast<int>(ExternalKind::Memory) == 2, "Wasm needs Memory to have the value 2");
static_assert(static_cast<int>(ExternalKind::Global) == 3, "Wasm needs Global to have the value 3");
inline const char* makeString(ExternalKind kind)
{
switch (kind) {
case ExternalKind::Function: return "function";
case ExternalKind::Table: return "table";
case ExternalKind::Memory: return "memory";
case ExternalKind::Global: return "global";
}
RELEASE_ASSERT_NOT_REACHED();
return "?";
}
struct Import {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
const Name module;
const Name field;
ExternalKind kind;
unsigned kindIndex; // Index in the vector of the corresponding kind.
};
struct Export {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
const Name field;
ExternalKind kind;
unsigned kindIndex; // Index in the vector of the corresponding kind.
};
String makeString(const Name& characters);
struct GlobalInformation {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
enum Mutability : uint8_t {
// FIXME auto-generate this. https://bugs.webkit.org/show_bug.cgi?id=165231
Mutable = 1,
Immutable = 0
};
enum InitializationType : uint8_t {
IsImport,
FromGlobalImport,
FromRefFunc,
FromExpression
};
enum class BindingMode : uint8_t {
EmbeddedInInstance = 0,
Portable,
};
Mutability mutability;
Type type;
InitializationType initializationType { IsImport };
BindingMode bindingMode { BindingMode::EmbeddedInInstance };
uint64_t initialBitsOrImportNumber { 0 };
};
struct FunctionData {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
size_t start;
size_t end;
Vector<uint8_t> data;
};
class I32InitExpr {
WTF_MAKE_FAST_ALLOCATED;
enum Type : uint8_t {
Global,
Const
};
I32InitExpr(Type type, uint32_t bits)
: m_bits(bits)
, m_type(type)
{ }
public:
I32InitExpr() = delete;
static I32InitExpr globalImport(uint32_t globalImportNumber) { return I32InitExpr(Global, globalImportNumber); }
static I32InitExpr constValue(uint32_t constValue) { return I32InitExpr(Const, constValue); }
bool isConst() const { return m_type == Const; }
bool isGlobalImport() const { return m_type == Global; }
uint32_t constValue() const
{
RELEASE_ASSERT(isConst());
return m_bits;
}
uint32_t globalImportIndex() const
{
RELEASE_ASSERT(isGlobalImport());
return m_bits;
}
private:
uint32_t m_bits;
Type m_type;
};
struct Segment {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
enum class Kind : uint8_t {
Active,
Passive,
};
Kind kind;
uint32_t sizeInBytes;
std::optional<I32InitExpr> offsetIfActive;
// Bytes are allocated at the end.
uint8_t& byte(uint32_t pos)
{
ASSERT(pos < sizeInBytes);
return *reinterpret_cast<uint8_t*>(reinterpret_cast<char*>(this) + sizeof(Segment) + pos);
}
static void destroy(Segment*);
typedef std::unique_ptr<Segment, decltype(&Segment::destroy)> Ptr;
static Segment::Ptr create(std::optional<I32InitExpr>, uint32_t, Kind);
bool isActive() const { return kind == Kind::Active; }
bool isPassive() const { return kind == Kind::Passive; }
};
struct Element {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
// nullFuncIndex represents the case when an element segment (of type funcref)
// contains a null element.
constexpr static uint32_t nullFuncIndex = UINT32_MAX;
enum class Kind : uint8_t {
Active,
Passive,
Declared,
};
Element(Element::Kind kind, TableElementType elementType, std::optional<uint32_t> tableIndex, std::optional<I32InitExpr> initExpr)
: kind(kind)
, elementType(elementType)
, tableIndexIfActive(WTFMove(tableIndex))
, offsetIfActive(WTFMove(initExpr))
{ }
Element(Element::Kind kind, TableElementType elemType)
: Element(kind, elemType, std::nullopt, std::nullopt)
{ }
uint32_t length() const { return functionIndices.size(); }
bool isActive() const { return kind == Kind::Active; }
bool isPassive() const { return kind == Kind::Passive; }
static bool isNullFuncIndex(uint32_t idx) { return idx == nullFuncIndex; }
Kind kind;
TableElementType elementType;
std::optional<uint32_t> tableIndexIfActive;
std::optional<I32InitExpr> offsetIfActive;
// Index may be nullFuncIndex.
Vector<uint32_t> functionIndices;
};
class TableInformation {
WTF_MAKE_FAST_ALLOCATED;
public:
TableInformation()
{
ASSERT(!*this);
}
TableInformation(uint32_t initial, std::optional<uint32_t> maximum, bool isImport, TableElementType type)
: m_initial(initial)
, m_maximum(maximum)
, m_isImport(isImport)
, m_isValid(true)
, m_type(type)
{
ASSERT(*this);
}
explicit operator bool() const { return m_isValid; }
bool isImport() const { return m_isImport; }
uint32_t initial() const { return m_initial; }
std::optional<uint32_t> maximum() const { return m_maximum; }
TableElementType type() const { return m_type; }
Type wasmType() const { return m_type == TableElementType::Funcref ? Types::Funcref : Types::Externref; }
private:
uint32_t m_initial;
std::optional<uint32_t> m_maximum;
bool m_isImport { false };
bool m_isValid { false };
TableElementType m_type;
};
struct CustomSection {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
Name name;
Vector<uint8_t> payload;
};
enum class NameType : uint8_t {
Module = 0,
Function = 1,
Local = 2,
};
template<typename Int>
inline bool isValidNameType(Int val)
{
switch (val) {
case static_cast<Int>(NameType::Module):
case static_cast<Int>(NameType::Function):
case static_cast<Int>(NameType::Local):
return true;
}
return false;
}
struct UnlinkedWasmToWasmCall {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
CodeLocationNearCall<WasmEntryPtrTag> callLocation;
size_t functionIndexSpace;
};
struct Entrypoint {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
std::unique_ptr<Compilation> compilation;
RegisterAtOffsetList calleeSaveRegisters;
};
struct InternalFunction {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
CodeLocationDataLabelPtr<WasmEntryPtrTag> calleeMoveLocation;
Entrypoint entrypoint;
};
// WebAssembly direct calls and call_indirect use indices into "function index space". This space starts
// with all imports, and then all internal functions. WasmToWasmImportableFunction and FunctionIndexSpace are only
// meant as fast lookup tables for these opcodes and do not own code.
struct WasmToWasmImportableFunction {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
using LoadLocation = MacroAssemblerCodePtr<WasmEntryPtrTag>*;
static ptrdiff_t offsetOfSignatureIndex() { return OBJECT_OFFSETOF(WasmToWasmImportableFunction, signatureIndex); }
static ptrdiff_t offsetOfEntrypointLoadLocation() { return OBJECT_OFFSETOF(WasmToWasmImportableFunction, entrypointLoadLocation); }
// FIXME: Pack signature index and code pointer into one 64-bit value. See <https://bugs.webkit.org/show_bug.cgi?id=165511>.
SignatureIndex signatureIndex { Signature::invalidIndex };
LoadLocation entrypointLoadLocation;
};
using FunctionIndexSpace = Vector<WasmToWasmImportableFunction>;
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
|