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

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.
This commit is contained in:
Ali Mohammad Pur 2021-05-01 03:19:01 +04:30 committed by Andreas Kling
parent faa34a0a8b
commit 056be42c0b
10 changed files with 513 additions and 30 deletions

View file

@ -49,7 +49,7 @@ public:
}
template<typename T>
requires(sizeof(T) <= sizeof(u64)) explicit Value(ValueType type, T raw_value)
requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value)
: m_value(0)
, m_type(type)
{
@ -89,6 +89,33 @@ public:
{
}
Value& operator=(Value&& value)
{
m_value = move(value.m_value);
m_type = move(value.m_type);
return *this;
}
template<typename T>
Optional<T> to()
{
Optional<T> result;
m_value.visit(
[&](auto value) {
if constexpr (!IsSame<T, FunctionAddress> && !IsSame<T, ExternAddress>)
result = value;
},
[&](const FunctionAddress& address) {
if constexpr (IsSame<T, FunctionAddress>)
result = address;
},
[&](const ExternAddress& address) {
if constexpr (IsSame<T, ExternAddress>)
result = address;
});
return result;
}
auto& type() const { return m_type; }
auto& value() const { return m_value; }
@ -317,14 +344,17 @@ private:
class Label {
public:
explicit Label(InstructionPointer continuation)
: m_continuation(continuation)
explicit Label(size_t arity, InstructionPointer continuation)
: m_arity(arity)
, m_continuation(continuation)
{
}
auto continuation() const { return m_continuation; }
auto arity() const { return m_arity; }
private:
size_t m_arity { 0 };
InstructionPointer m_continuation;
};
@ -342,6 +372,7 @@ public:
auto& module() const { return m_module; }
auto& locals() const { return m_locals; }
auto& locals() { return m_locals; }
auto& expression() const { return m_expression; }
auto arity() const { return m_arity; }