1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +00:00

LibWasm: Don't put values and labels in OwnPtrs

Doing that was causing a lot of malloc/free traffic, but since there's
no need to have a stable pointer to them, we can just store them by
value.
This makes execution significantly faster :^)
This commit is contained in:
Ali Mohammad Pur 2021-05-23 21:21:17 +04:30 committed by Ali Mohammad Pur
parent c7bc1f59d8
commit 73eb0785e0
5 changed files with 83 additions and 86 deletions

View file

@ -388,7 +388,7 @@ public:
private: private:
size_t m_arity { 0 }; size_t m_arity { 0 };
InstructionPointer m_continuation; InstructionPointer m_continuation { 0 };
}; };
class Frame { class Frame {
@ -418,7 +418,7 @@ private:
class Stack { class Stack {
public: public:
using EntryType = Variant<NonnullOwnPtr<Value>, NonnullOwnPtr<Label>, NonnullOwnPtr<Frame>>; using EntryType = Variant<Value, Label, NonnullOwnPtr<Frame>>;
Stack() = default; Stack() = default;
[[nodiscard]] bool is_empty() const { return m_data.is_empty(); } [[nodiscard]] bool is_empty() const { return m_data.is_empty(); }

View file

@ -13,9 +13,9 @@ Optional<Label> Configuration::nth_label(size_t i)
{ {
for (size_t index = m_stack.size(); index > 0; --index) { for (size_t index = m_stack.size(); index > 0; --index) {
auto& entry = m_stack.entries()[index - 1]; auto& entry = m_stack.entries()[index - 1];
if (auto ptr = entry.get_pointer<NonnullOwnPtr<Label>>()) { if (auto ptr = entry.get_pointer<Label>()) {
if (i == 0) if (i == 0)
return **ptr; return *ptr;
--i; --i;
} }
} }
@ -60,26 +60,23 @@ Result Configuration::execute()
if (interpreter.did_trap()) if (interpreter.did_trap())
return Trap {}; return Trap {};
Vector<NonnullOwnPtr<Value>> results; Vector<Value> results;
results.ensure_capacity(m_current_frame->arity());
for (size_t i = 0; i < m_current_frame->arity(); ++i) for (size_t i = 0; i < m_current_frame->arity(); ++i)
results.append(move(stack().pop().get<NonnullOwnPtr<Value>>())); results.append(move(stack().pop().get<Value>()));
auto label = stack().pop(); auto label = stack().pop();
// ASSERT: label == current frame // ASSERT: label == current frame
if (!label.has<NonnullOwnPtr<Label>>()) if (!label.has<Label>())
return Trap {}; return Trap {};
Vector<Value> results_moved; return Result { move(results) };
results_moved.ensure_capacity(results.size());
for (auto& entry : results)
results_moved.unchecked_append(move(*entry));
return Result { move(results_moved) };
} }
void Configuration::dump_stack() void Configuration::dump_stack()
{ {
for (const auto& entry : stack().entries()) { for (const auto& entry : stack().entries()) {
entry.visit( entry.visit(
[](const NonnullOwnPtr<Value>& v) { [](const Value& v) {
v->value().visit([]<typename T>(const T& v) { v.value().visit([]<typename T>(const T& v) {
if constexpr (IsIntegral<T> || IsFloatingPoint<T>) if constexpr (IsIntegral<T> || IsFloatingPoint<T>)
dbgln(" {}", v); dbgln(" {}", v);
else else
@ -97,8 +94,8 @@ void Configuration::dump_stack()
}); });
} }
}, },
[](const NonnullOwnPtr<Label>& l) { [](const Label& l) {
dbgln(" label({}) -> {}", l->arity(), l->continuation()); dbgln(" label({}) -> {}", l.arity(), l.continuation());
}); });
} }
} }

View file

@ -22,7 +22,7 @@ public:
{ {
m_current_frame = frame.ptr(); m_current_frame = frame.ptr();
m_stack.push(move(frame)); m_stack.push(move(frame));
m_stack.push(make<Label>(m_current_frame->arity(), m_current_frame->expression().instructions().size())); m_stack.push(Label(m_current_frame->arity(), m_current_frame->expression().instructions().size()));
} }
auto& frame() const { return m_current_frame; } auto& frame() const { return m_current_frame; }
auto& frame() { return m_current_frame; } auto& frame() { return m_current_frame; }

View file

@ -58,7 +58,7 @@ void Interpreter::branch_to_label(Configuration& configuration, LabelIndex index
for (; !configuration.stack().is_empty();) { for (; !configuration.stack().is_empty();) {
auto& entry = configuration.stack().peek(); auto& entry = configuration.stack().peek();
if (entry.has<NonnullOwnPtr<Label>>()) { if (entry.has<Label>()) {
if (--drop_count == 0) if (--drop_count == 0)
break; break;
} }
@ -80,7 +80,7 @@ ReadonlyBytes Interpreter::load_from_memory(Configuration& configuration, const
return {}; return {};
} }
auto& arg = instruction.arguments().get<Instruction::MemoryArgument>(); auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
auto base = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>(); auto base = configuration.stack().pop().get<Value>().to<i32>();
if (!base.has_value()) { if (!base.has_value()) {
m_do_trap = true; m_do_trap = true;
return {}; return {};
@ -101,7 +101,7 @@ void Interpreter::store_to_memory(Configuration& configuration, const Instructio
auto memory = configuration.store().get(address); auto memory = configuration.store().get(address);
TRAP_IF_NOT(memory); TRAP_IF_NOT(memory);
auto& arg = instruction.arguments().get<Instruction::MemoryArgument>(); auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
auto base = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>(); auto base = configuration.stack().pop().get<Value>().to<i32>();
TRAP_IF_NOT(base.has_value()); TRAP_IF_NOT(base.has_value());
auto instance_address = base.value() + static_cast<i64>(arg.offset); auto instance_address = base.value() + static_cast<i64>(arg.offset);
if (instance_address < 0 || static_cast<u64>(instance_address + data.size()) > memory->size()) { if (instance_address < 0 || static_cast<u64>(instance_address + data.size()) > memory->size()) {
@ -123,7 +123,7 @@ void Interpreter::call_address(Configuration& configuration, FunctionAddress add
Vector<Value> args; Vector<Value> args;
args.ensure_capacity(type->parameters().size()); args.ensure_capacity(type->parameters().size());
for (size_t i = 0; i < type->parameters().size(); ++i) { for (size_t i = 0; i < type->parameters().size(); ++i) {
args.prepend(move(*configuration.stack().pop().get<NonnullOwnPtr<Value>>())); args.prepend(move(configuration.stack().pop().get<Value>()));
} }
Configuration function_configuration { configuration.store() }; Configuration function_configuration { configuration.store() };
function_configuration.pre_interpret_hook = pre_interpret_hook; function_configuration.pre_interpret_hook = pre_interpret_hook;
@ -135,26 +135,26 @@ void Interpreter::call_address(Configuration& configuration, FunctionAddress add
return; return;
} }
for (auto& entry : result.values()) for (auto& entry : result.values())
configuration.stack().push(make<Value>(move(entry))); configuration.stack().push(Value(move(entry)));
} }
#define BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \ #define BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \
do { \ do { \
auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto rhs = configuration.stack().pop().get<Value>().to<type>(); \
auto lhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto lhs = configuration.stack().pop().get<Value>().to<type>(); \
TRAP_IF_NOT(lhs.has_value()); \ TRAP_IF_NOT(lhs.has_value()); \
TRAP_IF_NOT(rhs.has_value()); \ TRAP_IF_NOT(rhs.has_value()); \
__VA_ARGS__; \ __VA_ARGS__; \
auto result = lhs.value() operator rhs.value(); \ auto result = lhs.value() operator rhs.value(); \
dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), #operator, rhs.value(), result); \ dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), #operator, rhs.value(), result); \
configuration.stack().push(make<Value>(cast(result))); \ configuration.stack().push(Value(cast(result))); \
return; \ return; \
} while (false) } while (false)
#define OVF_CHECKED_BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \ #define OVF_CHECKED_BINARY_NUMERIC_OPERATION(type, operator, cast, ...) \
do { \ do { \
auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto rhs = configuration.stack().pop().get<Value>().to<type>(); \
auto ulhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto ulhs = configuration.stack().pop().get<Value>().to<type>(); \
TRAP_IF_NOT(ulhs.has_value()); \ TRAP_IF_NOT(ulhs.has_value()); \
TRAP_IF_NOT(rhs.has_value()); \ TRAP_IF_NOT(rhs.has_value()); \
dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = ??", ulhs.value(), #operator, rhs.value()); \ dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = ??", ulhs.value(), #operator, rhs.value()); \
@ -164,52 +164,52 @@ void Interpreter::call_address(Configuration& configuration, FunctionAddress add
TRAP_IF_NOT(!lhs.has_overflow()); \ TRAP_IF_NOT(!lhs.has_overflow()); \
auto result = lhs.value(); \ auto result = lhs.value(); \
dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", ulhs.value(), #operator, rhs.value(), result); \ dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", ulhs.value(), #operator, rhs.value(), result); \
configuration.stack().push(make<Value>(cast(result))); \ configuration.stack().push(Value(cast(result))); \
return; \ return; \
} while (false) } while (false)
#define BINARY_PREFIX_NUMERIC_OPERATION(type, operation, cast, ...) \ #define BINARY_PREFIX_NUMERIC_OPERATION(type, operation, cast, ...) \
do { \ do { \
auto rhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto rhs = configuration.stack().pop().get<Value>().to<type>(); \
auto lhs = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<type>(); \ auto lhs = configuration.stack().pop().get<Value>().to<type>(); \
TRAP_IF_NOT(lhs.has_value()); \ TRAP_IF_NOT(lhs.has_value()); \
TRAP_IF_NOT(rhs.has_value()); \ TRAP_IF_NOT(rhs.has_value()); \
auto result = operation(lhs.value(), rhs.value()); \ auto result = operation(lhs.value(), rhs.value()); \
dbgln_if(WASM_TRACE_DEBUG, "{}({} {}) = {}", #operation, lhs.value(), rhs.value(), result); \ dbgln_if(WASM_TRACE_DEBUG, "{}({} {}) = {}", #operation, lhs.value(), rhs.value(), result); \
configuration.stack().push(make<Value>(cast(result))); \ configuration.stack().push(Value(cast(result))); \
return; \ return; \
} while (false) } while (false)
#define UNARY_MAP(pop_type, operation, ...) \ #define UNARY_MAP(pop_type, operation, ...) \
do { \ do { \
auto value = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<pop_type>(); \ auto value = configuration.stack().pop().get<Value>().to<pop_type>(); \
TRAP_IF_NOT(value.has_value()); \ TRAP_IF_NOT(value.has_value()); \
auto result = operation(value.value()); \ auto result = operation(value.value()); \
dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", #operation, value.value(), result); \ dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", #operation, value.value(), result); \
configuration.stack().push(make<Value>(__VA_ARGS__(result))); \ configuration.stack().push(Value(__VA_ARGS__(result))); \
return; \ return; \
} while (false) } while (false)
#define UNARY_NUMERIC_OPERATION(type, operation) \ #define UNARY_NUMERIC_OPERATION(type, operation) \
UNARY_MAP(type, operation, type) UNARY_MAP(type, operation, type)
#define LOAD_AND_PUSH(read_type, push_type) \ #define LOAD_AND_PUSH(read_type, push_type) \
do { \ do { \
auto slice = load_from_memory(configuration, instruction, sizeof(read_type)); \ auto slice = load_from_memory(configuration, instruction, sizeof(read_type)); \
TRAP_IF_NOT(slice.size() == sizeof(read_type)); \ TRAP_IF_NOT(slice.size() == sizeof(read_type)); \
if constexpr (sizeof(read_type) == 1) \ if constexpr (sizeof(read_type) == 1) \
configuration.stack().push(make<Value>(static_cast<push_type>(slice[0]))); \ configuration.stack().push(Value(static_cast<push_type>(slice[0]))); \
else \ else \
configuration.stack().push(make<Value>(read_value<push_type>(slice))); \ configuration.stack().push(Value(read_value<push_type>(slice))); \
return; \ return; \
} while (false) } while (false)
#define POP_AND_STORE(pop_type, store_type) \ #define POP_AND_STORE(pop_type, store_type) \
do { \ do { \
auto value = ConvertToRaw<pop_type> {}(*configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<pop_type>()); \ auto value = ConvertToRaw<pop_type> {}(*configuration.stack().pop().get<Value>().to<pop_type>()); \
dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \ dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \
store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \ store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \
return; \ return; \
} while (false) } while (false)
template<typename T> template<typename T>
@ -324,12 +324,12 @@ MakeUnsigned<T> Interpreter::checked_unsigned_truncate(V value)
return true; return true;
} }
Vector<NonnullOwnPtr<Value>> Interpreter::pop_values(Configuration& configuration, size_t count) Vector<Value> Interpreter::pop_values(Configuration& configuration, size_t count)
{ {
Vector<NonnullOwnPtr<Value>> results; Vector<Value> results;
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
auto top_of_stack = configuration.stack().pop(); auto top_of_stack = configuration.stack().pop();
if (auto value = top_of_stack.get_pointer<NonnullOwnPtr<Value>>()) if (auto value = top_of_stack.get_pointer<Value>())
results.prepend(move(*value)); results.prepend(move(*value));
else else
TRAP_IF_NOT_NORETURN(value); TRAP_IF_NOT_NORETURN(value);
@ -366,31 +366,31 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
case Instructions::nop.value(): case Instructions::nop.value():
return; return;
case Instructions::local_get.value(): case Instructions::local_get.value():
configuration.stack().push(make<Value>(configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()])); configuration.stack().push(Value(configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()]));
return; return;
case Instructions::local_set.value(): { case Instructions::local_set.value(): {
auto entry = configuration.stack().pop(); auto entry = configuration.stack().pop();
configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()] = move(*entry.get<NonnullOwnPtr<Value>>()); configuration.frame()->locals()[instruction.arguments().get<LocalIndex>().value()] = move(entry.get<Value>());
return; return;
} }
case Instructions::i32_const.value(): case Instructions::i32_const.value():
configuration.stack().push(make<Value>(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>()))); configuration.stack().push(Value(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
return; return;
case Instructions::i64_const.value(): case Instructions::i64_const.value():
configuration.stack().push(make<Value>(ValueType { ValueType::I64 }, instruction.arguments().get<i64>())); configuration.stack().push(Value(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
return; return;
case Instructions::f32_const.value(): case Instructions::f32_const.value():
configuration.stack().push(make<Value>(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>()))); configuration.stack().push(Value(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>())));
return; return;
case Instructions::f64_const.value(): case Instructions::f64_const.value():
configuration.stack().push(make<Value>(ValueType { ValueType::F64 }, instruction.arguments().get<double>())); configuration.stack().push(Value(ValueType { ValueType::F64 }, instruction.arguments().get<double>()));
return; return;
case Instructions::block.value(): { case Instructions::block.value(): {
size_t arity = 0; size_t arity = 0;
auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>(); auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
if (args.block_type.kind() != BlockType::Empty) if (args.block_type.kind() != BlockType::Empty)
arity = 1; arity = 1;
configuration.stack().push(make<Label>(arity, args.end_ip)); configuration.stack().push(Label(arity, args.end_ip));
return; return;
} }
case Instructions::loop.value(): { case Instructions::loop.value(): {
@ -398,7 +398,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>(); auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
if (args.block_type.kind() != BlockType::Empty) if (args.block_type.kind() != BlockType::Empty)
arity = 1; arity = 1;
configuration.stack().push(make<Label>(arity, ip.value() + 1)); configuration.stack().push(Label(arity, ip.value() + 1));
return; return;
} }
case Instructions::if_.value(): { case Instructions::if_.value(): {
@ -408,9 +408,9 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
arity = 1; arity = 1;
auto entry = configuration.stack().pop(); auto entry = configuration.stack().pop();
auto value = entry.get<NonnullOwnPtr<Value>>()->to<i32>(); auto value = entry.get<Value>().to<i32>();
TRAP_IF_NOT(value.has_value()); TRAP_IF_NOT(value.has_value());
auto end_label = make<Label>(arity, args.end_ip.value()); auto end_label = Label(arity, args.end_ip.value());
if (value.value() == 0) { if (value.value() == 0) {
if (args.else_ip.has_value()) { if (args.else_ip.has_value()) {
configuration.ip() = args.else_ip.value(); configuration.ip() = args.else_ip.value();
@ -432,7 +432,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
// drop all locals // drop all locals
for (; !configuration.stack().is_empty();) { for (; !configuration.stack().is_empty();) {
auto entry = configuration.stack().pop(); auto entry = configuration.stack().pop();
if (entry.has<NonnullOwnPtr<Label>>()) if (entry.has<Label>())
break; break;
} }
@ -453,19 +453,19 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
for (size_t i = 0; i < frame.arity(); ++i) for (size_t i = 0; i < frame.arity(); ++i)
results.prepend(configuration.stack().pop()); results.prepend(configuration.stack().pop());
// drop all locals // drop all locals
OwnPtr<Label> last_label; Optional<Label> last_label;
for (; !configuration.stack().is_empty();) { for (; !configuration.stack().is_empty();) {
auto entry = configuration.stack().pop(); auto entry = configuration.stack().pop();
if (entry.has<NonnullOwnPtr<Label>>()) { if (entry.has<Label>()) {
last_label = move(entry.get<NonnullOwnPtr<Label>>()); last_label = entry.get<Label>();
continue; continue;
} }
if (entry.has<NonnullOwnPtr<Frame>>()) { if (entry.has<NonnullOwnPtr<Frame>>()) {
// Push the frame back // Push the frame back
configuration.stack().push(move(entry)); configuration.stack().push(move(entry));
// Push its label back (if there is one) // Push its label back (if there is one)
if (last_label) if (last_label.has_value())
configuration.stack().push(last_label.release_nonnull()); configuration.stack().push(last_label.release_value());
break; break;
} }
last_label.clear(); last_label.clear();
@ -481,7 +481,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
case Instructions::br.value(): case Instructions::br.value():
return branch_to_label(configuration, instruction.arguments().get<LabelIndex>()); return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
case Instructions::br_if.value(): { case Instructions::br_if.value(): {
if (configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>().value_or(0) == 0) if (configuration.stack().pop().get<Value>().to<i32>().value_or(0) == 0)
return; return;
return branch_to_label(configuration, instruction.arguments().get<LabelIndex>()); return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
} }
@ -498,7 +498,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>(); auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
auto table_address = configuration.frame()->module().tables()[args.table.value()]; auto table_address = configuration.frame()->module().tables()[args.table.value()];
auto table_instance = configuration.store().get(table_address); auto table_instance = configuration.store().get(table_address);
auto index = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>(); auto index = configuration.stack().pop().get<Value>().to<i32>();
TRAP_IF_NOT(index.has_value()); TRAP_IF_NOT(index.has_value());
if (index.value() < 0 || static_cast<size_t>(index.value()) >= table_instance->elements().size()) { if (index.value() < 0 || static_cast<size_t>(index.value()) >= table_instance->elements().size()) {
dbgln("LibWasm: Element access out of bounds, expected {0} > 0 and {0} < {1}", index.value(), table_instance->elements().size()); dbgln("LibWasm: Element access out of bounds, expected {0} > 0 and {0} < {1}", index.value(), table_instance->elements().size());
@ -563,7 +563,7 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
case Instructions::i64_store32.value(): case Instructions::i64_store32.value():
POP_AND_STORE(i64, i32); POP_AND_STORE(i64, i32);
case Instructions::local_tee.value(): { case Instructions::local_tee.value(): {
auto value = *configuration.stack().peek().get<NonnullOwnPtr<Value>>(); auto value = configuration.stack().peek().get<Value>();
auto local_index = instruction.arguments().get<LocalIndex>(); auto local_index = instruction.arguments().get<LocalIndex>();
TRAP_IF_NOT(configuration.frame()->locals().size() > local_index.value()); TRAP_IF_NOT(configuration.frame()->locals().size() > local_index.value());
dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value()); dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
@ -576,14 +576,14 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
auto address = configuration.frame()->module().globals()[global_index.value()]; auto address = configuration.frame()->module().globals()[global_index.value()];
dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value()); dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
auto global = configuration.store().get(address); auto global = configuration.store().get(address);
configuration.stack().push(make<Value>(global->value())); configuration.stack().push(Value(global->value()));
return; return;
} }
case Instructions::global_set.value(): { case Instructions::global_set.value(): {
auto global_index = instruction.arguments().get<GlobalIndex>(); auto global_index = instruction.arguments().get<GlobalIndex>();
TRAP_IF_NOT(configuration.frame()->module().globals().size() > global_index.value()); TRAP_IF_NOT(configuration.frame()->module().globals().size() > global_index.value());
auto address = configuration.frame()->module().globals()[global_index.value()]; auto address = configuration.frame()->module().globals()[global_index.value()];
auto value = *configuration.stack().pop().get<NonnullOwnPtr<Value>>(); auto value = configuration.stack().pop().get<Value>();
dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value()); dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
auto global = configuration.store().get(address); auto global = configuration.store().get(address);
global->set_value(move(value)); global->set_value(move(value));
@ -594,20 +594,20 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
auto instance = configuration.store().get(address); auto instance = configuration.store().get(address);
auto pages = instance->size() / Constants::page_size; auto pages = instance->size() / Constants::page_size;
dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages); dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
configuration.stack().push(make<Value>((i32)pages)); configuration.stack().push(Value((i32)pages));
return; return;
} }
case Instructions::memory_grow.value(): { case Instructions::memory_grow.value(): {
auto address = configuration.frame()->module().memories()[0]; auto address = configuration.frame()->module().memories()[0];
auto instance = configuration.store().get(address); auto instance = configuration.store().get(address);
i32 old_pages = instance->size() / Constants::page_size; i32 old_pages = instance->size() / Constants::page_size;
auto new_pages = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>(); auto new_pages = configuration.stack().pop().get<Value>().to<i32>();
TRAP_IF_NOT(new_pages.has_value()); TRAP_IF_NOT(new_pages.has_value());
dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages); dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
if (instance->grow(new_pages.value() * Constants::page_size)) if (instance->grow(new_pages.value() * Constants::page_size))
configuration.stack().push(make<Value>((i32)old_pages)); configuration.stack().push(Value((i32)old_pages));
else else
configuration.stack().push(make<Value>((i32)-1)); configuration.stack().push(Value((i32)-1));
return; return;
} }
case Instructions::table_get.value(): case Instructions::table_get.value():
@ -622,11 +622,11 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
case Instructions::select.value(): case Instructions::select.value():
case Instructions::select_typed.value(): { case Instructions::select_typed.value(): {
// Note: The type seems to only be used for validation. // Note: The type seems to only be used for validation.
auto value = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>(); auto value = configuration.stack().pop().get<Value>().to<i32>();
TRAP_IF_NOT(value.has_value()); TRAP_IF_NOT(value.has_value());
dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value()); dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
auto rhs = move(configuration.stack().pop().get<NonnullOwnPtr<Value>>()); auto rhs = move(configuration.stack().pop().get<Value>());
auto lhs = move(configuration.stack().pop().get<NonnullOwnPtr<Value>>()); auto lhs = move(configuration.stack().pop().get<Value>());
configuration.stack().push(value.value() != 0 ? move(lhs) : move(rhs)); configuration.stack().push(value.value() != 0 ? move(lhs) : move(rhs));
return; return;
} }

View file

@ -34,7 +34,7 @@ private:
template<typename T> template<typename T>
T read_value(ReadonlyBytes data); T read_value(ReadonlyBytes data);
Vector<NonnullOwnPtr<Value>> pop_values(Configuration& configuration, size_t count); Vector<Value> pop_values(Configuration& configuration, size_t count);
bool trap_if_not(bool value) bool trap_if_not(bool value)
{ {
if (!value) if (!value)