[wasm] Move global storage accessors out of interpreter

These accessors do not make any use of the interpreter, hence we can
define them on the WasmInstanceObject alone. This will allow to reuse
them for other (non-interpreted) frames.

R=mstarzinger@chromium.org

Bug: v8:9676
Change-Id: Iff8b665a4c25581b934c25b66a13cebe044cb02c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1875097
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64511}
This commit is contained in:
Clemens Backes 2019-10-23 13:17:14 +02:00 committed by Commit Bot
parent 23bd735394
commit f30a92e6f2
3 changed files with 67 additions and 48 deletions

View File

@ -1300,14 +1300,15 @@ class ThreadImpl {
}
WasmValue GetGlobalValue(uint32_t index) {
const WasmGlobal* global = &module()->globals[index];
switch (global->type) {
#define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \
byte* ptr = GetGlobalPtr(global); \
return WasmValue( \
ReadLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr))); \
break; \
auto& global = module()->globals[index];
switch (global.type) {
#define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \
byte* ptr = \
WasmInstanceObject::GetGlobalStorage(instance_object_, global); \
return WasmValue( \
ReadLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr))); \
break; \
}
WASM_CTYPES(CASE_TYPE)
#undef CASE_TYPE
@ -1316,8 +1317,10 @@ class ThreadImpl {
case kWasmExnRef: {
HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<FixedArray> global_buffer; // The buffer of the global.
uint32_t global_index = 0; // The index into the buffer.
GetGlobalBufferAndIndex(global, &global_buffer, &global_index);
uint32_t global_index; // The index into the buffer.
std::tie(global_buffer, global_index) =
WasmInstanceObject::GetGlobalBufferAndIndex(instance_object_,
global);
Handle<Object> value(global_buffer->get(global_index), isolate_);
return WasmValue(handle_scope.CloseAndEscape(value));
}
@ -2204,34 +2207,6 @@ class ThreadImpl {
return true;
}
byte* GetGlobalPtr(const WasmGlobal* global) {
DCHECK(!ValueTypes::IsReferenceType(global->type));
if (global->mutability && global->imported) {
return reinterpret_cast<byte*>(
instance_object_->imported_mutable_globals()[global->index]);
} else {
return instance_object_->globals_start() + global->offset;
}
}
void GetGlobalBufferAndIndex(const WasmGlobal* global,
Handle<FixedArray>* buffer, uint32_t* index) {
DCHECK(ValueTypes::IsReferenceType(global->type));
if (global->mutability && global->imported) {
*buffer =
handle(FixedArray::cast(
instance_object_->imported_mutable_globals_buffers().get(
global->index)),
isolate_);
Address idx = instance_object_->imported_mutable_globals()[global->index];
DCHECK_LE(idx, std::numeric_limits<uint32_t>::max());
*index = static_cast<uint32_t>(idx);
} else {
*buffer = handle(instance_object_->tagged_globals_buffer(), isolate_);
*index = global->offset;
}
}
bool ExecuteSimdOp(WasmOpcode opcode, Decoder* decoder, InterpreterCode* code,
pc_t pc, int* const len) {
switch (opcode) {
@ -3343,14 +3318,15 @@ class ThreadImpl {
case kExprGlobalSet: {
GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc));
const WasmGlobal* global = &module()->globals[imm.index];
switch (global->type) {
#define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \
byte* ptr = GetGlobalPtr(global); \
WriteLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr), \
Pop().to<ctype>()); \
break; \
auto& global = module()->globals[imm.index];
switch (global.type) {
#define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \
byte* ptr = \
WasmInstanceObject::GetGlobalStorage(instance_object_, global); \
WriteLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr), \
Pop().to<ctype>()); \
break; \
}
WASM_CTYPES(CASE_TYPE)
#undef CASE_TYPE
@ -3359,8 +3335,10 @@ class ThreadImpl {
case kWasmExnRef: {
HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<FixedArray> global_buffer; // The buffer of the global.
uint32_t global_index = 0; // The index into the buffer.
GetGlobalBufferAndIndex(global, &global_buffer, &global_index);
uint32_t global_index; // The index into the buffer.
std::tie(global_buffer, global_index) =
WasmInstanceObject::GetGlobalBufferAndIndex(instance_object_,
global);
global_buffer->set(global_index, *Pop().to_anyref());
break;
}

View File

@ -1591,6 +1591,36 @@ void WasmInstanceObject::ImportWasmJSFunctionIntoTable(
.Set(sig_id, call_target, *tuple);
}
// static
uint8_t* WasmInstanceObject::GetGlobalStorage(
Handle<WasmInstanceObject> instance, const wasm::WasmGlobal& global) {
DCHECK(!wasm::ValueTypes::IsReferenceType(global.type));
if (global.mutability && global.imported) {
return reinterpret_cast<byte*>(
instance->imported_mutable_globals()[global.index]);
} else {
return instance->globals_start() + global.offset;
}
}
// static
std::pair<Handle<FixedArray>, uint32_t>
WasmInstanceObject::GetGlobalBufferAndIndex(Handle<WasmInstanceObject> instance,
const wasm::WasmGlobal& global) {
DCHECK(wasm::ValueTypes::IsReferenceType(global.type));
Isolate* isolate = instance->GetIsolate();
if (global.mutability && global.imported) {
Handle<FixedArray> buffer(
FixedArray::cast(
instance->imported_mutable_globals_buffers().get(global.index)),
isolate);
Address idx = instance->imported_mutable_globals()[global.index];
DCHECK_LE(idx, std::numeric_limits<uint32_t>::max());
return {buffer, static_cast<uint32_t>(idx)};
}
return {handle(instance->tagged_globals_buffer(), isolate), global.offset};
}
// static
Handle<WasmExceptionObject> WasmExceptionObject::New(
Isolate* isolate, const wasm::FunctionSig* sig,

View File

@ -28,6 +28,7 @@ class SignatureMap;
class WasmCode;
struct WasmException;
struct WasmFeatures;
struct WasmGlobal;
class WasmInterpreter;
struct WasmModule;
class WasmValue;
@ -558,6 +559,16 @@ class WasmInstanceObject : public JSObject {
int table_index, int entry_index,
Handle<WasmJSFunction> js_function);
// Get a raw pointer to the location where the given global is stored.
// {global} must not be a reference type.
static uint8_t* GetGlobalStorage(Handle<WasmInstanceObject>,
const wasm::WasmGlobal&);
// Get the FixedArray and the index in that FixedArray for the given global,
// which must be a reference type.
static std::pair<Handle<FixedArray>, uint32_t> GetGlobalBufferAndIndex(
Handle<WasmInstanceObject>, const wasm::WasmGlobal&);
OBJECT_CONSTRUCTORS(WasmInstanceObject, JSObject);
private: