ladybird/Userland/Libraries/LibWasm/AbstractMachine/Configuration.h
Ali Mohammad Pur 056be42c0b LibWasm: Start implementing a naive bytecode interpreter
As the parser now flattens out the instructions and inserts synthetic
nesting/structured instructions where needed, we can treat the whole
thing as a simple parsed bytecode stream.
This currently knows how to execute the following instructions:
- unreachable
- nop
- local.get
- local.set
- {i,f}{32,64}.const
- block
- loop
- if/else
- branch / branch_if
- i32_add
- i32_and/or/xor
- i32_ne

This also extends the 'wasm' utility to optionally execute the first
function in the module with optionally user-supplied arguments.
2021-05-17 23:25:30 +02:00

53 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWasm/AbstractMachine/AbstractMachine.h>
namespace Wasm {
typedef u64 (*HostFunctionType)(Store&, Vector<Value>&);
class Configuration {
public:
explicit Configuration(Store& store)
: m_store(store)
{
}
Optional<Label> nth_label(size_t);
void set_frame(NonnullOwnPtr<Frame> frame)
{
m_current_frame = frame.ptr();
m_stack.push(move(frame));
m_stack.push(make<Label>(m_current_frame->arity(), m_current_frame->expression().instructions().size() - 1));
}
auto& frame() const { return m_current_frame; }
auto& frame() { return m_current_frame; }
auto& ip() const { return m_ip; }
auto& ip() { return m_ip; }
auto& depth() const { return m_depth; }
auto& depth() { return m_depth; }
auto& stack() const { return m_stack; }
auto& stack() { return m_stack; }
auto& store() const { return m_store; }
auto& store() { return m_store; }
Result call(FunctionAddress, Vector<Value> arguments);
Result execute();
void dump_stack();
private:
Store& m_store;
Frame* m_current_frame { nullptr };
Stack m_stack;
size_t m_depth { 0 };
InstructionPointer m_ip;
};
}