Commit f30a92e6 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[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/+/1875097Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64511}
parent 23bd7353
...@@ -1300,14 +1300,15 @@ class ThreadImpl { ...@@ -1300,14 +1300,15 @@ class ThreadImpl {
} }
WasmValue GetGlobalValue(uint32_t index) { WasmValue GetGlobalValue(uint32_t index) {
const WasmGlobal* global = &module()->globals[index]; auto& global = module()->globals[index];
switch (global->type) { switch (global.type) {
#define CASE_TYPE(wasm, ctype) \ #define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \ case kWasm##wasm: { \
byte* ptr = GetGlobalPtr(global); \ byte* ptr = \
return WasmValue( \ WasmInstanceObject::GetGlobalStorage(instance_object_, global); \
ReadLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr))); \ return WasmValue( \
break; \ ReadLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr))); \
break; \
} }
WASM_CTYPES(CASE_TYPE) WASM_CTYPES(CASE_TYPE)
#undef CASE_TYPE #undef CASE_TYPE
...@@ -1316,8 +1317,10 @@ class ThreadImpl { ...@@ -1316,8 +1317,10 @@ class ThreadImpl {
case kWasmExnRef: { case kWasmExnRef: {
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<FixedArray> global_buffer; // The buffer of the global. Handle<FixedArray> global_buffer; // The buffer of the global.
uint32_t global_index = 0; // The index into the buffer. uint32_t global_index; // The index into the buffer.
GetGlobalBufferAndIndex(global, &global_buffer, &global_index); std::tie(global_buffer, global_index) =
WasmInstanceObject::GetGlobalBufferAndIndex(instance_object_,
global);
Handle<Object> value(global_buffer->get(global_index), isolate_); Handle<Object> value(global_buffer->get(global_index), isolate_);
return WasmValue(handle_scope.CloseAndEscape(value)); return WasmValue(handle_scope.CloseAndEscape(value));
} }
...@@ -2204,34 +2207,6 @@ class ThreadImpl { ...@@ -2204,34 +2207,6 @@ class ThreadImpl {
return true; 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, bool ExecuteSimdOp(WasmOpcode opcode, Decoder* decoder, InterpreterCode* code,
pc_t pc, int* const len) { pc_t pc, int* const len) {
switch (opcode) { switch (opcode) {
...@@ -3343,14 +3318,15 @@ class ThreadImpl { ...@@ -3343,14 +3318,15 @@ class ThreadImpl {
case kExprGlobalSet: { case kExprGlobalSet: {
GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder, GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc));
const WasmGlobal* global = &module()->globals[imm.index]; auto& global = module()->globals[imm.index];
switch (global->type) { switch (global.type) {
#define CASE_TYPE(wasm, ctype) \ #define CASE_TYPE(wasm, ctype) \
case kWasm##wasm: { \ case kWasm##wasm: { \
byte* ptr = GetGlobalPtr(global); \ byte* ptr = \
WriteLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr), \ WasmInstanceObject::GetGlobalStorage(instance_object_, global); \
Pop().to<ctype>()); \ WriteLittleEndianValue<ctype>(reinterpret_cast<Address>(ptr), \
break; \ Pop().to<ctype>()); \
break; \
} }
WASM_CTYPES(CASE_TYPE) WASM_CTYPES(CASE_TYPE)
#undef CASE_TYPE #undef CASE_TYPE
...@@ -3359,8 +3335,10 @@ class ThreadImpl { ...@@ -3359,8 +3335,10 @@ class ThreadImpl {
case kWasmExnRef: { case kWasmExnRef: {
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<FixedArray> global_buffer; // The buffer of the global. Handle<FixedArray> global_buffer; // The buffer of the global.
uint32_t global_index = 0; // The index into the buffer. uint32_t global_index; // The index into the buffer.
GetGlobalBufferAndIndex(global, &global_buffer, &global_index); std::tie(global_buffer, global_index) =
WasmInstanceObject::GetGlobalBufferAndIndex(instance_object_,
global);
global_buffer->set(global_index, *Pop().to_anyref()); global_buffer->set(global_index, *Pop().to_anyref());
break; break;
} }
......
...@@ -1591,6 +1591,36 @@ void WasmInstanceObject::ImportWasmJSFunctionIntoTable( ...@@ -1591,6 +1591,36 @@ void WasmInstanceObject::ImportWasmJSFunctionIntoTable(
.Set(sig_id, call_target, *tuple); .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 // static
Handle<WasmExceptionObject> WasmExceptionObject::New( Handle<WasmExceptionObject> WasmExceptionObject::New(
Isolate* isolate, const wasm::FunctionSig* sig, Isolate* isolate, const wasm::FunctionSig* sig,
......
...@@ -28,6 +28,7 @@ class SignatureMap; ...@@ -28,6 +28,7 @@ class SignatureMap;
class WasmCode; class WasmCode;
struct WasmException; struct WasmException;
struct WasmFeatures; struct WasmFeatures;
struct WasmGlobal;
class WasmInterpreter; class WasmInterpreter;
struct WasmModule; struct WasmModule;
class WasmValue; class WasmValue;
...@@ -558,6 +559,16 @@ class WasmInstanceObject : public JSObject { ...@@ -558,6 +559,16 @@ class WasmInstanceObject : public JSObject {
int table_index, int entry_index, int table_index, int entry_index,
Handle<WasmJSFunction> js_function); 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); OBJECT_CONSTRUCTORS(WasmInstanceObject, JSObject);
private: private:
......
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