Commit 3a79d5bc authored by Ben L. Titzer's avatar Ben L. Titzer Committed by Commit Bot

[wasm] Move (almost all) constants to wasm-constants.h

This CL centralizes constants related to decoding from several places
into one place and makes it no longer necessary to include
wasm-opcodes.h for some simple constants.

R=clemensh@chromium.org

Bug: 
Change-Id: I53aa81e34167df467bc7455b717bf67083033943
Reviewed-on: https://chromium-review.googlesource.com/859764
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50503}
parent 4ff98cb1
......@@ -2133,6 +2133,7 @@ v8_source_set("v8_base") {
"src/wasm/wasm-code-specialization.h",
"src/wasm/wasm-code-wrapper.cc",
"src/wasm/wasm-code-wrapper.h",
"src/wasm/wasm-constants.h",
"src/wasm/wasm-debug.cc",
"src/wasm/wasm-engine.h",
"src/wasm/wasm-external-refs.cc",
......
......@@ -3416,7 +3416,7 @@ Node* WasmGraphBuilder::CurrentMemoryPages() {
}
return graph()->NewNode(
jsgraph()->machine()->Word32Shr(), mem_size,
jsgraph()->Int32Constant(WhichPowerOf2(wasm::WasmModule::kPageSize)));
jsgraph()->Int32Constant(WhichPowerOf2(wasm::kWasmPageSize)));
}
void WasmGraphBuilder::EnsureFunctionTableNodes() {
......@@ -3547,11 +3547,11 @@ Node* WasmGraphBuilder::BoundsCheckMem(uint8_t access_size, Node* index,
return Uint32ToUintptr(index);
}
uint32_t min_size = env_->module->initial_pages * wasm::WasmModule::kPageSize;
uint32_t min_size = env_->module->initial_pages * wasm::kWasmPageSize;
uint32_t max_size =
(env_->module->has_maximum_pages ? env_->module->maximum_pages
: wasm::kV8MaxWasmMemoryPages) *
wasm::WasmModule::kPageSize;
wasm::kWasmPageSize;
if (access_size > max_size || offset > max_size - access_size) {
// The access will be out of bounds, even for the largest memory.
......
......@@ -17,9 +17,9 @@
#include "src/v8memory.h"
#include "src/wasm/module-compiler.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-constants.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-opcodes.h"
namespace v8 {
namespace internal {
......@@ -171,7 +171,7 @@ RUNTIME_FUNCTION(Runtime_WasmGetExceptionRuntimeId) {
}
}
}
return Smi::FromInt(wasm::WasmModule::kInvalidExceptionTag);
return Smi::FromInt(wasm::kInvalidExceptionTag);
}
RUNTIME_FUNCTION(Runtime_WasmExceptionGetElement) {
......
......@@ -1484,6 +1484,7 @@
'wasm/wasm-code-specialization.h',
'wasm/wasm-code-wrapper.cc',
'wasm/wasm-code-wrapper.h',
'wasm/wasm-constants.h',
'wasm/wasm-debug.cc',
'wasm/wasm-engine.h',
'wasm/wasm-external-refs.cc',
......
......@@ -113,11 +113,11 @@ class LiftoffCompiler {
: asm_(liftoff_asm),
call_desc_(call_desc),
env_(env),
min_size_(env_->module->initial_pages * wasm::WasmModule::kPageSize),
min_size_(env_->module->initial_pages * wasm::kWasmPageSize),
max_size_((env_->module->has_maximum_pages
? env_->module->maximum_pages
: wasm::kV8MaxWasmMemoryPages) *
wasm::WasmModule::kPageSize),
wasm::kWasmPageSize),
runtime_exception_support_(runtime_exception_support),
source_position_table_builder_(source_position_table_builder),
protected_instructions_(protected_instructions),
......@@ -128,7 +128,7 @@ class LiftoffCompiler {
DCHECK_EQ(max_size_, uint64_t{env_->module->has_maximum_pages
? env_->module->maximum_pages
: wasm::kV8MaxWasmMemoryPages} *
wasm::WasmModule::kPageSize);
wasm::kWasmPageSize);
}
bool ok() const { return ok_; }
......
......@@ -3043,7 +3043,7 @@ int InstanceBuilder::ProcessImports(Handle<FixedArray> code_table,
Handle<JSArrayBuffer> buffer(memory->array_buffer(), isolate_);
memory_ = buffer;
uint32_t imported_cur_pages = static_cast<uint32_t>(
buffer->byte_length()->Number() / WasmModule::kPageSize);
buffer->byte_length()->Number() / kWasmPageSize);
if (imported_cur_pages < module_->initial_pages) {
thrower_->LinkError(
"memory import %d is smaller than initial %u, got %u", index,
......@@ -3183,7 +3183,7 @@ Handle<JSArrayBuffer> InstanceBuilder::AllocateMemory(uint32_t num_pages) {
const bool is_shared_memory =
module_->has_shared_memory && i::FLAG_experimental_wasm_threads;
Handle<JSArrayBuffer> mem_buffer = NewArrayBuffer(
isolate_, num_pages * WasmModule::kPageSize, enable_guard_regions,
isolate_, num_pages * kWasmPageSize, enable_guard_regions,
is_shared_memory ? i::SharedFlag::kShared : i::SharedFlag::kNotShared);
if (mem_buffer.is_null()) {
......
......@@ -37,6 +37,20 @@ constexpr size_t num_chars(const char (&)[N]) {
return N - 1; // remove null character at end.
}
const char* ExternalKindName(ImportExportKindCode kind) {
switch (kind) {
case kExternalFunction:
return "function";
case kExternalTable:
return "table";
case kExternalMemory:
return "memory";
case kExternalGlobal:
return "global";
}
return "unknown";
}
} // namespace
const char* SectionName(SectionCode code) {
......@@ -443,7 +457,8 @@ class ModuleDecoderImpl : public Decoder {
const byte* pos = pc_;
import->module_name = consume_string(true, "module name");
import->field_name = consume_string(true, "field name");
import->kind = static_cast<WasmExternalKind>(consume_u8("import kind"));
import->kind =
static_cast<ImportExportKindCode>(consume_u8("import kind"));
switch (import->kind) {
case kExternalFunction: {
// ===== Imported function =======================================
......@@ -469,7 +484,7 @@ class ModuleDecoderImpl : public Decoder {
module_->function_tables.emplace_back();
WasmIndirectFunctionTable* table = &module_->function_tables.back();
table->imported = true;
expect_u8("element type", kWasmAnyFunctionTypeForm);
expect_u8("element type", kWasmAnyFunctionTypeCode);
consume_resizable_limits(
"element count", "elements", FLAG_wasm_max_table_size,
&table->initial_size, &table->has_maximum_size,
......@@ -535,7 +550,7 @@ class ModuleDecoderImpl : public Decoder {
if (!AddTable(module_.get())) break;
module_->function_tables.emplace_back();
WasmIndirectFunctionTable* table = &module_->function_tables.back();
expect_u8("table type", kWasmAnyFunctionTypeForm);
expect_u8("table type", kWasmAnyFunctionTypeCode);
consume_resizable_limits("table elements", "elements",
FLAG_wasm_max_table_size, &table->initial_size,
&table->has_maximum_size,
......@@ -588,7 +603,7 @@ class ModuleDecoderImpl : public Decoder {
exp->name = consume_string(true, "field name");
const byte* pos = pc();
exp->kind = static_cast<WasmExternalKind>(consume_u8("export kind"));
exp->kind = static_cast<ImportExportKindCode>(consume_u8("export kind"));
switch (exp->kind) {
case kExternalFunction: {
WasmFunction* func = nullptr;
......@@ -781,12 +796,12 @@ class ModuleDecoderImpl : public Decoder {
// Decode function names, ignore the rest.
// Local names will be decoded when needed.
switch (name_type) {
case NameSectionType::kModule: {
case NameSectionKindCode::kModule: {
WireBytesRef name = wasm::consume_string(inner, false, "module name");
if (inner.ok() && validate_utf8(&inner, name)) module_->name = name;
break;
}
case NameSectionType::kFunction: {
case NameSectionKindCode::kFunction: {
uint32_t functions_count = inner.consume_u32v("functions count");
for (; inner.ok() && functions_count > 0; --functions_count) {
......@@ -1289,7 +1304,7 @@ class ModuleDecoderImpl : public Decoder {
private:
FunctionSig* consume_sig_internal(Zone* zone, bool has_return_values) {
if (has_return_values && !expect_u8("type form", kWasmFunctionTypeForm))
if (has_return_values && !expect_u8("type form", kWasmFunctionTypeCode))
return nullptr;
// parse parameter types
uint32_t param_count =
......@@ -1583,7 +1598,7 @@ void DecodeLocalNames(const byte* module_start, const byte* module_end,
uint32_t name_payload_len = decoder.consume_u32v("name payload length");
if (!decoder.checkAvailable(name_payload_len)) break;
if (name_type != NameSectionType::kLocal) {
if (name_type != NameSectionKindCode::kLocal) {
decoder.consume_bytes(name_payload_len, "name subsection payload");
continue;
}
......
......@@ -7,6 +7,7 @@
#include "src/globals.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/wasm-constants.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-result.h"
......@@ -19,41 +20,6 @@ struct ModuleEnv;
namespace wasm {
const uint8_t kWasmFunctionTypeForm = 0x60;
const uint8_t kWasmAnyFunctionTypeForm = 0x70;
const uint8_t kHasMaximumFlag = 1;
const uint8_t kNoMaximumFlag = 0;
enum MemoryFlags : uint8_t {
kNoMaximum = 0,
kMaximum = 1,
kSharedNoMaximum = 2,
kSharedAndMaximum = 3
};
enum SectionCode : int8_t {
kUnknownSectionCode = 0, // code for unknown sections
kTypeSectionCode = 1, // Function signature declarations
kImportSectionCode = 2, // Import declarations
kFunctionSectionCode = 3, // Function declarations
kTableSectionCode = 4, // Indirect function table and other tables
kMemorySectionCode = 5, // Memory attributes
kGlobalSectionCode = 6, // Global declarations
kExportSectionCode = 7, // Exports
kStartSectionCode = 8, // Start function declaration
kElementSectionCode = 9, // Elements section
kCodeSectionCode = 10, // Function code
kDataSectionCode = 11, // Data segments
kNameSectionCode = 12, // Name section (encoded as a string)
kExceptionSectionCode = 13, // Exception section
// Helper values
kFirstSectionInModule = kTypeSectionCode,
kLastKnownModuleSection = kExceptionSectionCode,
};
enum NameSectionType : uint8_t { kModule = 0, kFunction = 1, kLocal = 2 };
inline bool IsValidSectionCode(uint8_t byte) {
return kTypeSectionCode <= byte && byte <= kLastKnownModuleSection;
}
......
......@@ -12,7 +12,6 @@
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects-inl.h"
#include "src/wasm/wasm-opcodes.h"
namespace v8 {
namespace internal {
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_WASM_CONSTANTS_H_
#define V8_WASM_CONSTANTS_H_
namespace v8 {
namespace internal {
namespace wasm {
// Binary encoding of the module header.
constexpr uint32_t kWasmMagic = 0x6d736100;
constexpr uint32_t kWasmVersion = 0x01;
// Binary encoding of local types.
enum ValueTypeCode : uint8_t {
kLocalVoid = 0x40,
kLocalI32 = 0x7f,
kLocalI64 = 0x7e,
kLocalF32 = 0x7d,
kLocalF64 = 0x7c,
kLocalS128 = 0x7b
};
// Binary encoding of other types.
constexpr uint8_t kWasmFunctionTypeCode = 0x60;
constexpr uint8_t kWasmAnyFunctionTypeCode = 0x70;
// Binary encoding of import/export kinds.
enum ImportExportKindCode : uint8_t {
kExternalFunction = 0,
kExternalTable = 1,
kExternalMemory = 2,
kExternalGlobal = 3
};
// Binary encoding of maximum and shared flags for memories.
enum MaximumFlag : uint8_t { kNoMaximumFlag = 0, kHasMaximumFlag = 1 };
enum MemoryFlags : uint8_t {
kNoMaximum = 0,
kMaximum = 1,
kSharedNoMaximum = 2,
kSharedAndMaximum = 3
};
// Binary encoding of sections identifiers.
enum SectionCode : int8_t {
kUnknownSectionCode = 0, // code for unknown sections
kTypeSectionCode = 1, // Function signature declarations
kImportSectionCode = 2, // Import declarations
kFunctionSectionCode = 3, // Function declarations
kTableSectionCode = 4, // Indirect function table and other tables
kMemorySectionCode = 5, // Memory attributes
kGlobalSectionCode = 6, // Global declarations
kExportSectionCode = 7, // Exports
kStartSectionCode = 8, // Start function declaration
kElementSectionCode = 9, // Elements section
kCodeSectionCode = 10, // Function code
kDataSectionCode = 11, // Data segments
kNameSectionCode = 12, // Name section (encoded as a string)
kExceptionSectionCode = 13, // Exception section
// Helper values
kFirstSectionInModule = kTypeSectionCode,
kLastKnownModuleSection = kExceptionSectionCode,
};
// Binary encoding of name section kinds.
enum NameSectionKindCode : uint8_t { kModule = 0, kFunction = 1, kLocal = 2 };
constexpr uint32_t kWasmPageSize = 0x10000;
constexpr int kInvalidExceptionTag = -1;
// TODO(wasm): Wrap WasmCodePosition in a struct.
using WasmCodePosition = int;
constexpr WasmCodePosition kNoCodePosition = -1;
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_WASM_CONSTANTS_H_
......@@ -2089,8 +2089,8 @@ class ThreadImpl {
case kExprMemorySize: {
MemoryIndexOperand<Decoder::kNoValidate> operand(&decoder,
code->at(pc));
Push(WasmValue(static_cast<uint32_t>(wasm_context_->mem_size /
WasmModule::kPageSize)));
Push(WasmValue(
static_cast<uint32_t>(wasm_context_->mem_size / kWasmPageSize)));
len = 1 + operand.length;
break;
}
......
......@@ -630,7 +630,7 @@ void WebAssemblyMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
}
size_t size = static_cast<size_t>(i::wasm::WasmModule::kPageSize) *
size_t size = static_cast<size_t>(i::wasm::kWasmPageSize) *
static_cast<size_t>(initial);
const bool enable_guard_regions =
internal::trap_handler::IsTrapHandlerEnabled();
......
......@@ -9,6 +9,8 @@
#include <cstdint>
#include <limits>
#include "src/wasm/wasm-constants.h"
namespace v8 {
namespace internal {
namespace wasm {
......@@ -45,8 +47,7 @@ static_assert(kV8MaxWasmMemoryPages <= kSpecMaxWasmMemoryPages,
"v8 should not be more permissive than the spec");
constexpr size_t kSpecMaxWasmTableSize = 0xFFFFFFFFu;
// TODO(titzer): move WASM page size constant here.
constexpr size_t kV8MaxWasmMemoryBytes = kV8MaxWasmMemoryPages * 65536;
constexpr size_t kV8MaxWasmMemoryBytes = kV8MaxWasmMemoryPages * kWasmPageSize;
constexpr uint64_t kWasmMaxHeapOffset =
static_cast<uint64_t>(
......
......@@ -124,8 +124,7 @@ Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
// Check against kMaxInt, since the byte length is stored as int in the
// JSArrayBuffer. Note that wasm_max_mem_pages can be raised from the command
// line, and we don't want to fail a CHECK then.
if (size > FLAG_wasm_max_mem_pages * WasmModule::kPageSize ||
size > kMaxInt) {
if (size > FLAG_wasm_max_mem_pages * kWasmPageSize || size > kMaxInt) {
// TODO(titzer): lift restriction on maximum memory allocated here.
return Handle<JSArrayBuffer>::null();
}
......
......@@ -11,7 +11,7 @@
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/leb-helper.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-constants.h"
#include "src/wasm/wasm-module-builder.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h"
......@@ -343,7 +343,7 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
buffer.write_size(signatures_.size());
for (FunctionSig* sig : signatures_) {
buffer.write_u8(kWasmFunctionTypeForm);
buffer.write_u8(kWasmFunctionTypeCode);
buffer.write_size(sig->parameter_count());
for (auto param : sig->parameters()) {
buffer.write_u8(WasmOpcodes::ValueTypeCodeFor(param));
......@@ -392,7 +392,7 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
if (indirect_functions_.size() > 0) {
size_t start = EmitSection(kTableSectionCode, buffer);
buffer.write_u8(1); // table count
buffer.write_u8(kWasmAnyFunctionTypeForm);
buffer.write_u8(kWasmAnyFunctionTypeCode);
buffer.write_u8(kHasMaximumFlag);
buffer.write_size(indirect_functions_.size());
buffer.write_size(indirect_functions_.size());
......@@ -554,7 +554,7 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
buffer.write_size(4);
buffer.write(reinterpret_cast<const byte*>("name"), 4);
// Emit a subsection for the function names.
buffer.write_u8(NameSectionType::kFunction);
buffer.write_u8(NameSectionKindCode::kFunction);
// Emit a placeholder for the subsection length.
size_t functions_start = buffer.reserve_u32v();
// Emit the function names.
......
......@@ -450,21 +450,6 @@ Handle<FixedArray> DecodeLocalNames(Isolate* isolate,
}
return locals_names;
}
const char* ExternalKindName(WasmExternalKind kind) {
switch (kind) {
case kExternalFunction:
return "function";
case kExternalTable:
return "table";
case kExternalMemory:
return "memory";
case kExternalGlobal:
return "global";
}
return "unknown";
}
} // namespace wasm
} // namespace internal
} // namespace v8
......@@ -15,7 +15,7 @@
#include "src/wasm/decoder.h"
#include "src/wasm/signature-map.h"
#include "src/wasm/wasm-opcodes.h"
#include "src/wasm/wasm-constants.h"
namespace v8 {
namespace internal {
......@@ -36,13 +36,6 @@ namespace wasm {
class ErrorThrower;
class NativeModule;
enum WasmExternalKind {
kExternalFunction = 0,
kExternalTable = 1,
kExternalMemory = 2,
kExternalGlobal = 3
};
// Static representation of a wasm function.
struct WasmFunction {
FunctionSig* sig; // signature of the function.
......@@ -118,14 +111,14 @@ struct WasmTableInit {
struct WasmImport {
WireBytesRef module_name; // module name.
WireBytesRef field_name; // import name.
WasmExternalKind kind; // kind of the import.
ImportExportKindCode kind; // kind of the import.
uint32_t index; // index into the respective space.
};
// Static representation of a wasm export.
struct WasmExport {
WireBytesRef name; // exported name.
WasmExternalKind kind; // kind of the export.
ImportExportKindCode kind; // kind of the export.
uint32_t index; // index into the respective space.
};
......@@ -137,11 +130,6 @@ struct ModuleWireBytes;
struct V8_EXPORT_PRIVATE WasmModule {
MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(WasmModule);
static const uint32_t kPageSize = 0x10000; // Page size, 64kb.
static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb
static constexpr int kInvalidExceptionTag = -1;
std::unique_ptr<Zone> signature_zone;
uint32_t initial_pages = 0; // initial size of the memory in 64k pages
uint32_t maximum_pages = 0; // maximum size of the memory in 64k pages
......@@ -299,8 +287,6 @@ void UnpackAndRegisterProtectedInstructionsGC(Isolate* isolate,
void UnpackAndRegisterProtectedInstructions(
Isolate* isolate, const wasm::NativeModule* native_module);
const char* ExternalKindName(WasmExternalKind);
// TruncatedUserString makes it easy to output names up to a certain length, and
// output a truncation followed by '...' if they exceed a limit.
// Use like this:
......
......@@ -362,18 +362,18 @@ Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
old_mem_start = static_cast<Address>(old_buffer->backing_store());
CHECK(old_buffer->byte_length()->ToUint32(&old_size));
}
DCHECK_EQ(0, old_size % WasmModule::kPageSize);
uint32_t old_pages = old_size / WasmModule::kPageSize;
DCHECK_EQ(0, old_size % wasm::kWasmPageSize);
uint32_t old_pages = old_size / wasm::kWasmPageSize;
DCHECK_GE(std::numeric_limits<uint32_t>::max(),
old_size + pages * WasmModule::kPageSize);
old_size + pages * wasm::kWasmPageSize);
if (old_pages > maximum_pages || pages > maximum_pages - old_pages) {
return Handle<JSArrayBuffer>::null();
}
const bool enable_guard_regions =
old_buffer.is_null() ? use_trap_handler : old_buffer->has_guard_region();
size_t new_size =
static_cast<size_t>(old_pages + pages) * WasmModule::kPageSize;
if (new_size > FLAG_wasm_max_mem_pages * WasmModule::kPageSize ||
static_cast<size_t>(old_pages + pages) * wasm::kWasmPageSize;
if (new_size > FLAG_wasm_max_mem_pages * wasm::kWasmPageSize ||
new_size > kMaxInt) {
return Handle<JSArrayBuffer>::null();
}
......@@ -384,8 +384,7 @@ Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
old_mem_start, new_size,
v8::ArrayBuffer::Allocator::Protection::kReadWrite);
reinterpret_cast<v8::Isolate*>(isolate)
->AdjustAmountOfExternalAllocatedMemory(pages *
WasmModule::kPageSize);
->AdjustAmountOfExternalAllocatedMemory(pages * wasm::kWasmPageSize);
}
// NOTE: We must allocate a new array buffer here because the spec
// assumes that ArrayBuffers do not change size.
......@@ -424,7 +423,7 @@ void SetInstanceMemory(Isolate* isolate, Handle<WasmInstanceObject> instance,
// To flush out bugs earlier, in DEBUG mode, check that all pages of the
// memory are accessible by reading and writing one byte on each page.
for (uint32_t offset = 0; offset < wasm_context->mem_size;
offset += WasmModule::kPageSize) {
offset += wasm::kWasmPageSize) {
byte val = wasm_context->mem_start[offset];
wasm_context->mem_start[offset] = val;
}
......@@ -468,7 +467,7 @@ Handle<WasmMemoryObject> WasmMemoryObject::New(
uint32_t WasmMemoryObject::current_pages() {
uint32_t byte_length;
CHECK(array_buffer()->byte_length()->ToUint32(&byte_length));
return byte_length / WasmModule::kPageSize;
return byte_length / wasm::kWasmPageSize;
}
void WasmMemoryObject::AddInstance(Isolate* isolate,
......@@ -501,7 +500,7 @@ int32_t WasmMemoryObject::Grow(Isolate* isolate,
if (!old_buffer->is_growable()) return -1;
uint32_t old_size = 0;
CHECK(old_buffer->byte_length()->ToUint32(&old_size));
DCHECK_EQ(0, old_size % WasmModule::kPageSize);
DCHECK_EQ(0, old_size % wasm::kWasmPageSize);
Handle<JSArrayBuffer> new_buffer;
uint32_t maximum_pages = FLAG_wasm_max_mem_pages;
......@@ -526,7 +525,7 @@ int32_t WasmMemoryObject::Grow(Isolate* isolate,
}
}
memory_object->set_array_buffer(*new_buffer);
return old_size / WasmModule::kPageSize;
return old_size / wasm::kWasmPageSize;
}
WasmModuleObject* WasmInstanceObject::module_object() {
......@@ -567,8 +566,8 @@ Handle<WasmInstanceObject> WasmInstanceObject::New(
int32_t WasmInstanceObject::GetMemorySize() {
if (!has_memory_object()) return 0;
uint32_t bytes = memory_object()->array_buffer()->byte_length()->Number();
DCHECK_EQ(0, bytes % WasmModule::kPageSize);
return bytes / WasmModule::kPageSize;
DCHECK_EQ(0, bytes % wasm::kWasmPageSize);
return bytes / wasm::kWasmPageSize;
}
int32_t WasmInstanceObject::GrowMemory(Isolate* isolate,
......@@ -1717,7 +1716,7 @@ void WasmCompiledModule::ReinitializeAfterDeserialization(
}
uint32_t WasmCompiledModule::default_mem_size() const {
return initial_pages() * WasmModule::kPageSize;
return initial_pages() * wasm::kWasmPageSize;
}
MaybeHandle<String> WasmSharedModuleData::GetModuleNameOrNull(
......
......@@ -69,8 +69,7 @@ struct WasmContext {
byte* globals_start = nullptr;
inline void SetRawMemory(void* mem_start, size_t mem_size) {
DCHECK_LE(mem_size,
wasm::kV8MaxWasmMemoryPages * wasm::WasmModule::kPageSize);
DCHECK_LE(mem_size, wasm::kV8MaxWasmMemoryPages * wasm::kWasmPageSize);
this->mem_start = static_cast<byte*>(mem_start);
this->mem_size = static_cast<uint32_t>(mem_size);
this->mem_mask = base::bits::RoundUpToPowerOfTwo32(this->mem_size) - 1;
......
......@@ -9,25 +9,12 @@
#include "src/machine-type.h"
#include "src/runtime/runtime.h"
#include "src/signature.h"
#include "src/wasm/wasm-constants.h"
namespace v8 {
namespace internal {
namespace wasm {
// Binary encoding of the module header.
const uint32_t kWasmMagic = 0x6d736100;
const uint32_t kWasmVersion = 0x01;
// Binary encoding of local types.
enum ValueTypeCode : uint8_t {
kLocalVoid = 0x40,
kLocalI32 = 0x7f,
kLocalI64 = 0x7e,
kLocalF32 = 0x7d,
kLocalF64 = 0x7c,
kLocalS128 = 0x7b
};
// We reuse the internal machine type to represent WebAssembly types.
// A typedef improves readability without adding a whole new type system.
using ValueType = MachineRepresentation;
......@@ -45,9 +32,6 @@ bool IsJSCompatibleSignature(const FunctionSig* sig);
using WasmName = Vector<const char>;
using WasmCodePosition = int;
constexpr WasmCodePosition kNoCodePosition = -1;
// Control expressions and blocks.
#define FOREACH_CONTROL_OPCODE(V) \
V(Unreachable, 0x00, _) \
......
......@@ -294,14 +294,14 @@ TEST(Breakpoint_I32And_disable) {
TEST(GrowMemory) {
{
WasmRunner<int32_t, uint32_t> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
r.builder().SetMaxMemPages(10);
BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
CHECK_EQ(1, r.Call(1));
}
{
WasmRunner<int32_t, uint32_t> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
r.builder().SetMaxMemPages(10);
BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
CHECK_EQ(-1, r.Call(11));
......@@ -312,7 +312,7 @@ TEST(GrowMemoryPreservesData) {
int32_t index = 16;
int32_t value = 2335;
WasmRunner<int32_t, uint32_t> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index),
WASM_I32V(value)),
WASM_GROW_MEMORY(WASM_GET_LOCAL(0)), WASM_DROP,
......@@ -323,7 +323,7 @@ TEST(GrowMemoryPreservesData) {
TEST(GrowMemoryInvalidSize) {
// Grow memory by an invalid amount without initial memory.
WasmRunner<int32_t, uint32_t> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
CHECK_EQ(-1, r.Call(1048575));
}
......@@ -364,7 +364,7 @@ TEST(TestPossibleNondeterminism) {
{
int32_t index = 16;
WasmRunner<int32_t, float> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
BUILD(r, WASM_STORE_MEM(MachineType::Float32(), WASM_I32V(index),
WASM_GET_LOCAL(0)),
WASM_I32V(index));
......@@ -376,7 +376,7 @@ TEST(TestPossibleNondeterminism) {
{
int32_t index = 16;
WasmRunner<int32_t, double> r(kExecuteInterpreter);
r.builder().AddMemory(WasmModule::kPageSize);
r.builder().AddMemory(kWasmPageSize);
BUILD(r, WASM_STORE_MEM(MachineType::Float64(), WASM_I32V(index),
WASM_GET_LOCAL(0)),
WASM_I32V(index));
......
......@@ -1131,10 +1131,10 @@ TEST(Run_WasmModule_Buffer_Externalized_GrowMemMemSize) {
Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate);
void* backing_store =
isolate->array_buffer_allocator()->Allocate(16 * WasmModule::kPageSize);
Handle<JSArrayBuffer> buffer = wasm::SetupArrayBuffer(
isolate, backing_store, 16 * WasmModule::kPageSize, backing_store,
16 * WasmModule::kPageSize, false, false);
isolate->array_buffer_allocator()->Allocate(16 * kWasmPageSize);
Handle<JSArrayBuffer> buffer =
wasm::SetupArrayBuffer(isolate, backing_store, 16 * kWasmPageSize,
backing_store, 16 * kWasmPageSize, false, false);
Handle<WasmMemoryObject> mem_obj =
WasmMemoryObject::New(isolate, buffer, 100);
v8::Utils::ToLocal(buffer)->Externalize();
......@@ -1142,8 +1142,7 @@ TEST(Run_WasmModule_Buffer_Externalized_GrowMemMemSize) {
wasm::DetachMemoryBuffer(isolate, buffer, false);
CHECK_EQ(16, result);
isolate->array_buffer_allocator()->Free(backing_store,
16 * WasmModule::kPageSize);
isolate->array_buffer_allocator()->Free(backing_store, 16 * kWasmPageSize);
}
Cleanup();
}
......@@ -1155,14 +1154,13 @@ TEST(Run_WasmModule_Buffer_Externalized_Detach) {
Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate);
void* backing_store =
isolate->array_buffer_allocator()->Allocate(16 * WasmModule::kPageSize);
Handle<JSArrayBuffer> buffer = wasm::SetupArrayBuffer(
isolate, backing_store, 16 * WasmModule::kPageSize, backing_store,
16 * WasmModule::kPageSize, false, false);
isolate->array_buffer_allocator()->Allocate(16 * kWasmPageSize);
Handle<JSArrayBuffer> buffer =
wasm::SetupArrayBuffer(isolate, backing_store, 16 * kWasmPageSize,
backing_store, 16 * kWasmPageSize, false, false);
v8::Utils::ToLocal(buffer)->Externalize();
wasm::DetachMemoryBuffer(isolate, buffer, true);
isolate->array_buffer_allocator()->Free(backing_store,
16 * WasmModule::kPageSize);
isolate->array_buffer_allocator()->Free(backing_store, 16 * kWasmPageSize);
}
Cleanup();
}
......
......@@ -568,20 +568,20 @@ inline WasmOpcode LoadStoreOpcodeOf(MachineType type, bool store) {
#define WASM_GROW_MEMORY(x) x, kExprGrowMemory, 0
#define WASM_MEMORY_SIZE kExprMemorySize, 0
#define SIG_ENTRY_v_v kWasmFunctionTypeForm, 0, 0
#define SIG_ENTRY_v_v kWasmFunctionTypeCode, 0, 0
#define SIZEOF_SIG_ENTRY_v_v 3
#define SIG_ENTRY_v_x(a) kWasmFunctionTypeForm, 1, a, 0
#define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeForm, 2, a, b, 0
#define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 0
#define SIG_ENTRY_v_x(a) kWasmFunctionTypeCode, 1, a, 0
#define SIG_ENTRY_v_xx(a, b) kWasmFunctionTypeCode, 2, a, b, 0
#define SIG_ENTRY_v_xxx(a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 0
#define SIZEOF_SIG_ENTRY_v_x 4
#define SIZEOF_SIG_ENTRY_v_xx 5
#define SIZEOF_SIG_ENTRY_v_xxx 6
#define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r
#define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r
#define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r
#define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r
#define SIG_ENTRY_x(r) kWasmFunctionTypeCode, 0, 1, r
#define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeCode, 1, a, 1, r
#define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeCode, 2, a, b, 1, r
#define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeCode, 3, a, b, c, 1, r
#define SIZEOF_SIG_ENTRY_x 4
#define SIZEOF_SIG_ENTRY_x_x 5
#define SIZEOF_SIG_ENTRY_x_xx 6
......
......@@ -23,7 +23,7 @@ namespace wasm {
namespace testing {
uint32_t GetInitialMemSize(const WasmModule* module) {
return WasmModule::kPageSize * module->initial_pages;
return kWasmPageSize * module->initial_pages;
}
std::unique_ptr<WasmModule> DecodeWasmModuleForTesting(
......
......@@ -731,7 +731,7 @@ TEST_F(WasmModuleVerifyTest, OneIndirectFunction) {
// funcs ---------------------------------------------------------------
ONE_EMPTY_FUNCTION,
// table declaration ---------------------------------------------------
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1};
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeCode, 0, 1};
ModuleResult result = DecodeModule(data, data + sizeof(data));
EXPECT_OK(result);
......@@ -746,7 +746,7 @@ TEST_F(WasmModuleVerifyTest, OneIndirectFunction) {
TEST_F(WasmModuleVerifyTest, ElementSectionWithInternalTable) {
static const byte data[] = {
// table ---------------------------------------------------------------
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1,
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeCode, 0, 1,
// elements ------------------------------------------------------------
SECTION(Element, 1),
0 // entry count
......@@ -764,7 +764,7 @@ TEST_F(WasmModuleVerifyTest, ElementSectionWithImportedTable) {
NAME_LENGTH(1), // --
't', // table name
kExternalTable, // import kind
kWasmAnyFunctionTypeForm, // elem_type
kWasmAnyFunctionTypeCode, // elem_type
0, // no maximum field
1, // initial size
// elements ------------------------------------------------------------
......@@ -797,7 +797,7 @@ TEST_F(WasmModuleVerifyTest, Regression_735887) {
// funcs ---------------------------------------------------------------
ONE_EMPTY_FUNCTION,
// table declaration ---------------------------------------------------
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1,
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeCode, 0, 1,
// elements ------------------------------------------------------------
SECTION(Element, 7),
1, // entry count
......@@ -816,7 +816,7 @@ TEST_F(WasmModuleVerifyTest, OneIndirectFunction_one_entry) {
// funcs ---------------------------------------------------------------
ONE_EMPTY_FUNCTION,
// table declaration ---------------------------------------------------
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 1,
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeCode, 0, 1,
// elements ------------------------------------------------------------
SECTION(Element, 7),
1, // entry count
......@@ -844,7 +844,7 @@ TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) {
// funcs ------------------------------------------------------
FOUR_EMPTY_FUNCTIONS,
// table declaration -------------------------------------------
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeForm, 0, 8,
SECTION(Table, 4), ENTRY_COUNT(1), kWasmAnyFunctionTypeCode, 0, 8,
// table elements ----------------------------------------------
SECTION(Element, 14),
1, // entry count
......@@ -974,7 +974,7 @@ TEST_F(WasmSignatureDecodeTest, Ok_i_tt) {
}
TEST_F(WasmSignatureDecodeTest, TooManyParams) {
static const byte data[] = {kWasmFunctionTypeForm,
static const byte data[] = {kWasmFunctionTypeCode,
WASM_I32V_3(kV8MaxWasmFunctionParams + 1),
kLocalI32, 0};
FunctionSig* sig =
......@@ -988,7 +988,7 @@ TEST_F(WasmSignatureDecodeTest, TooManyReturns) {
const int max_return_count = static_cast<int>(
FLAG_experimental_wasm_mv ? kV8MaxWasmFunctionMultiReturns
: kV8MaxWasmFunctionReturns);
byte data[] = {kWasmFunctionTypeForm, 0, WASM_I32V_3(max_return_count + 1),
byte data[] = {kWasmFunctionTypeCode, 0, WASM_I32V_3(max_return_count + 1),
kLocalI32};
FunctionSig* sig =
DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment