diff --git a/src/wasm/value-type.h b/src/wasm/value-type.h index 49f348b714..845678ffc5 100644 --- a/src/wasm/value-type.h +++ b/src/wasm/value-type.h @@ -16,6 +16,9 @@ class Signature; namespace wasm { +// Type for holding simd values, defined in wasm-value.h. +class Simd128; + // Type lattice: For any two types connected by a line, the type at the bottom // is a subtype of the other type. // @@ -40,6 +43,13 @@ enum ValueType : uint8_t { kWasmBottom, }; +#define FOREACH_WASMVALUE_CTYPES(V) \ + V(kWasmI32, int32_t) \ + V(kWasmI64, int64_t) \ + V(kWasmF32, float) \ + V(kWasmF64, double) \ + V(kWasmS128, Simd128) + using FunctionSig = Signature; inline size_t hash_value(ValueType type) { return static_cast(type); } diff --git a/src/wasm/wasm-debug.cc b/src/wasm/wasm-debug.cc index bb81f3c73c..3cac5eb2fe 100644 --- a/src/wasm/wasm-debug.cc +++ b/src/wasm/wasm-debug.cc @@ -380,11 +380,8 @@ class InterpreterHandle { .Assert(); } - DCHECK_EQ(1, interpreter()->GetThreadCount()); - WasmInterpreter::Thread* thread = interpreter()->GetThread(0); - - uint32_t global_count = thread->GetGlobalCount(); - if (global_count > 0) { + auto& globals = module()->globals; + if (!globals.empty()) { Handle globals_obj = isolate_->factory()->NewJSObjectWithNullProto(); Handle globals_name = @@ -393,10 +390,11 @@ class InterpreterHandle { globals_name, globals_obj, NONE) .Assert(); - for (uint32_t i = 0; i < global_count; ++i) { + for (uint32_t i = 0; i < globals.size(); ++i) { const char* label = "global#%d"; Handle name = PrintFToOneByteString(isolate_, label, i); - WasmValue value = thread->GetGlobalValue(i); + WasmValue value = + WasmInstanceObject::GetGlobalValue(instance, globals[i]); Handle value_obj = WasmValueToValueObject(isolate_, value); JSObject::SetOwnPropertyIgnoreAttributes(globals_obj, name, value_obj, NONE) diff --git a/src/wasm/wasm-interpreter.cc b/src/wasm/wasm-interpreter.cc index dbb26da347..7e2c09a83f 100644 --- a/src/wasm/wasm-interpreter.cc +++ b/src/wasm/wasm-interpreter.cc @@ -51,9 +51,6 @@ using base::WriteUnalignedValue; #define FOREACH_INTERNAL_OPCODE(V) V(Breakpoint, 0xFF) -#define WASM_CTYPES(V) \ - V(I32, int32_t) V(I64, int64_t) V(F32, float) V(F64, double) V(S128, Simd128) - #define FOREACH_SIMPLE_BINOP(V) \ V(I32Add, uint32_t, +) \ V(I32Sub, uint32_t, -) \ @@ -1295,40 +1292,6 @@ class ThreadImpl { return WasmInterpreter::Thread::HANDLED; } - uint32_t GetGlobalCount() { - return static_cast(module()->globals.size()); - } - - WasmValue GetGlobalValue(uint32_t index) { - 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(reinterpret_cast
(ptr))); \ - break; \ - } - WASM_CTYPES(CASE_TYPE) -#undef CASE_TYPE - case kWasmAnyRef: - case kWasmFuncRef: - case kWasmExnRef: { - HandleScope handle_scope(isolate_); // Avoid leaking handles. - Handle global_buffer; // The buffer of the global. - uint32_t global_index; // The index into the buffer. - std::tie(global_buffer, global_index) = - WasmInstanceObject::GetGlobalBufferAndIndex(instance_object_, - global); - Handle value(global_buffer->get(global_index), isolate_); - return WasmValue(handle_scope.CloseAndEscape(value)); - } - default: - UNREACHABLE(); - } - } - private: // Handle a thrown exception. Returns whether the exception was handled inside // the current activation. Unwinds the interpreted stack accordingly. @@ -1490,11 +1453,11 @@ class ThreadImpl { for (auto p : code->locals.type_list) { WasmValue val; switch (p) { -#define CASE_TYPE(wasm, ctype) \ - case kWasm##wasm: \ - val = WasmValue(ctype{}); \ +#define CASE_TYPE(valuetype, ctype) \ + case valuetype: \ + val = WasmValue(ctype{}); \ break; - WASM_CTYPES(CASE_TYPE) + FOREACH_WASMVALUE_CTYPES(CASE_TYPE) #undef CASE_TYPE case kWasmAnyRef: case kWasmFuncRef: @@ -3311,7 +3274,8 @@ class ThreadImpl { GlobalIndexImmediate imm(&decoder, code->at(pc)); HandleScope handle_scope(isolate_); - Push(GetGlobalValue(imm.index)); + Push(WasmInstanceObject::GetGlobalValue( + instance_object_, module()->globals[imm.index])); len = 1 + imm.length; break; } @@ -3320,15 +3284,15 @@ class ThreadImpl { code->at(pc)); auto& global = module()->globals[imm.index]; switch (global.type) { -#define CASE_TYPE(wasm, ctype) \ - case kWasm##wasm: { \ - byte* ptr = \ +#define CASE_TYPE(valuetype, ctype) \ + case valuetype: { \ + uint8_t* ptr = \ WasmInstanceObject::GetGlobalStorage(instance_object_, global); \ WriteLittleEndianValue(reinterpret_cast
(ptr), \ Pop().to()); \ break; \ } - WASM_CTYPES(CASE_TYPE) + FOREACH_WASMVALUE_CTYPES(CASE_TYPE) #undef CASE_TYPE case kWasmAnyRef: case kWasmFuncRef: @@ -4069,12 +4033,6 @@ WasmValue WasmInterpreter::Thread::GetReturnValue(int index) { TrapReason WasmInterpreter::Thread::GetTrapReason() { return ToImpl(this)->GetTrapReason(); } -uint32_t WasmInterpreter::Thread::GetGlobalCount() { - return ToImpl(this)->GetGlobalCount(); -} -WasmValue WasmInterpreter::Thread::GetGlobalValue(uint32_t index) { - return ToImpl(this)->GetGlobalValue(index); -} bool WasmInterpreter::Thread::PossibleNondeterminism() { return ToImpl(this)->PossibleNondeterminism(); } @@ -4259,7 +4217,6 @@ void InterpretedFrameDeleter::operator()(InterpretedFrame* ptr) { #undef TRACE #undef LANE #undef FOREACH_INTERNAL_OPCODE -#undef WASM_CTYPES #undef FOREACH_SIMPLE_BINOP #undef FOREACH_OTHER_BINOP #undef FOREACH_I32CONV_FLOATOP diff --git a/src/wasm/wasm-interpreter.h b/src/wasm/wasm-interpreter.h index a6ffd4e670..2fe79effdd 100644 --- a/src/wasm/wasm-interpreter.h +++ b/src/wasm/wasm-interpreter.h @@ -139,9 +139,6 @@ class V8_EXPORT_PRIVATE WasmInterpreter { WasmValue GetReturnValue(int index = 0); TrapReason GetTrapReason(); - uint32_t GetGlobalCount(); - WasmValue GetGlobalValue(uint32_t index); - // Returns true if the thread executed an instruction which may produce // nondeterministic results, e.g. float div, float sqrt, and float mul, // where the sign bit of a NaN is nondeterministic. diff --git a/src/wasm/wasm-objects.cc b/src/wasm/wasm-objects.cc index d0b3e44193..90dc0dd515 100644 --- a/src/wasm/wasm-objects.cc +++ b/src/wasm/wasm-objects.cc @@ -27,6 +27,7 @@ #include "src/wasm/wasm-limits.h" #include "src/wasm/wasm-module.h" #include "src/wasm/wasm-objects-inl.h" +#include "src/wasm/wasm-value.h" #define TRACE_IFT(...) \ do { \ @@ -1626,6 +1627,30 @@ WasmInstanceObject::GetGlobalBufferAndIndex(Handle instance, return {handle(instance->tagged_globals_buffer(), isolate), global.offset}; } +// static +wasm::WasmValue WasmInstanceObject::GetGlobalValue( + Handle instance, const wasm::WasmGlobal& global) { + Isolate* isolate = instance->GetIsolate(); + if (wasm::ValueTypes::IsReferenceType(global.type)) { + Handle global_buffer; // The buffer of the global. + uint32_t global_index = 0; // The index into the buffer. + std::tie(global_buffer, global_index) = + GetGlobalBufferAndIndex(instance, global); + return wasm::WasmValue(handle(global_buffer->get(global_index), isolate)); + } + Address ptr = reinterpret_cast
(GetGlobalStorage(instance, global)); + using wasm::Simd128; + switch (global.type) { +#define CASE_TYPE(valuetype, ctype) \ + case wasm::valuetype: \ + return wasm::WasmValue(base::ReadLittleEndianValue(ptr)); + FOREACH_WASMVALUE_CTYPES(CASE_TYPE) +#undef CASE_TYPE + default: + UNREACHABLE(); + } +} + // static Handle WasmExceptionObject::New( Isolate* isolate, const wasm::FunctionSig* sig, diff --git a/src/wasm/wasm-objects.h b/src/wasm/wasm-objects.h index 2ea61535b3..d91e2e2392 100644 --- a/src/wasm/wasm-objects.h +++ b/src/wasm/wasm-objects.h @@ -569,6 +569,10 @@ class WasmInstanceObject : public JSObject { static std::pair, uint32_t> GetGlobalBufferAndIndex( Handle, const wasm::WasmGlobal&); + // Get the value of a global in the given instance. + static wasm::WasmValue GetGlobalValue(Handle, + const wasm::WasmGlobal&); + OBJECT_CONSTRUCTORS(WasmInstanceObject, JSObject); private: diff --git a/src/wasm/wasm-value.h b/src/wasm/wasm-value.h index 8de53b96cf..9a6f0ca726 100644 --- a/src/wasm/wasm-value.h +++ b/src/wasm/wasm-value.h @@ -32,11 +32,8 @@ FOREACH_SIMD_TYPE(DEFINE_SIMD_TYPE) class Simd128 { public: - Simd128() : val_() { - for (size_t i = 0; i < 16; i++) { - val_[i] = 0; - } - } + Simd128() = default; + #define DEFINE_SIMD_TYPE_SPECIFIC_METHODS(cType, sType, name, size) \ explicit Simd128(sType val) { \ base::WriteUnalignedValue(reinterpret_cast
(val_), val); \ @@ -48,7 +45,7 @@ class Simd128 { #undef DEFINE_SIMD_TYPE_SPECIFIC_METHODS private: - uint8_t val_[16]; + uint8_t val_[16] = {0}; }; // Macro for defining WasmValue methods for different types.