Commit Graph

35 Commits

Author SHA1 Message Date
Ali Mohammad Pur
bc936a5fac LibWasm: Make Frame a value type as well
This means stack operations will no longer do extra allocations.
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
73eb0785e0 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 :^)
2021-05-27 17:28:41 +04:30
Ali Mohammad Pur
ba5da79617 LibWasm: Add execution hooks and a debugger mode to the wasm tool
This is useful for debugging *our* implementation of wasm :P
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
800bcb9965 LibWasm: Drop previous frame when a structured end instruction is run 2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
7966168fea LibWasm: Turn memory read failures into traps 2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
c31a4e9013 LibWasm: Once more fix structured instruction label indices
This finally works correctly. ™️
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
d05e5dbdcf LibWasm: Drop the correct number of frames
Prior to this commit, we would be dropping an extra frame.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
a21ebae652 LibWasm: Implement checked truncation instructions
This implements the 8 i<n>.truncate.f<n>_<s> instructions.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
cf8b75c2e5 LibWasm+LibWeb: Partially resolve memory exports
This allows the JS side to access the wasm memory, assuming it's
exported by the module.
This can be used to draw stuff on the wasm side and display them from
the js side, for example :^)
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
4a459d2430 LibWasm: Correct memory init size when instantiating
These limits are in units of page size, not bytes.
Also fixes incorrect debug logs.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
3926eab3b7 LibWasm+LibWeb: Implement (a very basic version of) the JS link/import
This allows Wasm code to call javascript functions.
2021-05-26 15:34:13 +04:30
Ali Mohammad Pur
8cbdcffd05 LibWasm: Print instruction arguments too 2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
207379165f LibWasm: Fix nested structured instruction parsing
Previously, the ip would not be propagated correctly, and we would
produce invalid jumps when more than one level of nesting was involved.
This makes loops work :P
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
29b193d25d LibWasm: Resolve labels starting from the top of the stack
Otherwise "label index 0" would be the first ever created label, not the
last one (as the spec wants) :^(
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
35b3ae26ed LibWasm: Implement a very basic linker
This will simply "link" any given module instances and produce a list of
external values that can be used to instantiate a module.
Note that this is extremely basic and cannot resolve circular
dependencies, and depends on the instance order.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
3283c8a495 LibWasm: Make the instantiation process produce an OwnPtr
Managing the instantiated modules becomes a pain if they're on the
stack, since an instantiated module will eventually reference itself.
To make using this simpler, just avoid copying the instance.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
efb106069b LibWasm: Decouple ModuleInstance from the AbstractMachine
This fixes a FIXME and will allow linking only select modules together,
instead of linking every instantiated module into a big mess of exported
entities :P
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
541091500c LibWasm: Trap instead of VERIFY()'ing
...unless something really is an assertion.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
b3c13c3e8a LibWasm+Meta: Add test-wasm and optionally test the conformance tests
This only tests "can it be parsed", but the goal of this commit is to
provide a test framework that can be built upon :)
The conformance tests are downloaded, compiled* and installed only if
the INCLUDE_WASM_SPEC_TESTS cmake option is enabled.
(*) Since we do not yet have a wast parser, the compilation is delegated
to an external tool from binaryen, `wasm-as`, which is required for the
test suite download/install to succeed.
This *does* run the tests in CI, but it currently does not include the
spec conformance tests.
2021-05-21 00:15:23 +01:00
Ali Mohammad Pur
7fec66dd1c LibWasm: Make clang happy by removing an 'extra' set of parenthesis
These aren't actually an extra set, without them the fold operation
would be syntactically invalid.
Also remove possible cast of float->double/double->float in Value::to()
2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
95b9821f26 LibWasm: Implement memory.grow, memory.size and drop
These allow a very basic memory-using program to work.
2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
3402381d7a LibWasm: Execute the start function, if one exists 2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
84e3957dc3 LibWasm: Implement most of the remaining instructions
This commit is a bit of a mixed bag, but most of the changes are
repetitive enough to just include in a single commit.
The following instructions remain unimplemented:
- br.table
- table.init
- table.get
- table.set
- table.copy
- table.size
- table.grow
- table.fill
- ref.null
- ref.func
- ref.is_null
- drop
- i32/i64.clz
- i32/i64.ctz
- i32/i64.popcnt
- i32/i64.rotl
- i32/i64.rotr
- X.trunc.Y
- X.trunc_sat.Y
- memory.size
- memory.grow
- memory.init
- memory.copy
- memory.fill
- elem.drop
- data.drop
2021-05-17 23:25:30 +02:00
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
Ali Mohammad Pur
faa34a0a8b LibWasm: Do not resize() the function signature list to preallocate
Instead, use `ensure_capacity()`.
2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
025b3349e4 LibWasm: Make structured_end() jump to the instruction after itself 2021-05-17 23:25:30 +02:00
Ali Mohammad Pur
a5194274af LibWasm: Stub out/implement parsing of all ElementSection segments
Previously, this was parsing only one kind because I mistakenly assumed
that they all had the same shape, now it can parse two kinds, and will
return NotImplemented for the rest.
2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
4d9246ac9d LibWasm: Add basic support for module instantiation and execution stubs
This adds very basic support for module instantiation/allocation, as
well as a stub for an interpreter (and executions APIs).
The 'wasm' utility is further expanded to instantiate, and attempt
executing the first non-imported function in the module.
Note that as the execution is a stub, the expected result is a zero.
Regardless, this will allow future commits to implement the JS
WebAssembly API. :^)
2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
2b755f1fbf LibWasm: Make the Module ctor generate a list of module functions
This list is supposed to be accessed rather frequently, so there's no
reason to make things slower by generating it many times on the spot.
2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
6e891822c5 LibWasm: Implement parsing of the DataCount section
With this, the parser should technically be able to parse all wasm
modules. Any parse failure on correct modules should henceforth be
labelled a bug :^)
2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
bd8dac111c LibWasm: Add a module pretty printer 2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
7a12f23c28 LibWasm: Un-nest the structured instructions 2021-05-13 19:44:32 +01:00
Ali Mohammad Pur
50cb80649f LibWasm: Implement parsing all remaining instructions
With this, we can parse a module at least as simple as the following C
code would generate:
```c
int add(int x, int y) {
    if (x > y)
        return x + y;
    return y - x; // Haha goteeem
}
```
2021-05-08 22:14:39 +02:00
Ali Mohammad Pur
426878c884 LibWasm: Add some more descriptive parse errors
It's much better to tell the user "hey, the magic numbers don't check
out" than "oh there was a problem with your input" :P
Also refactors some stuff to make it possible to efficiently use the
parser error enum without it getting in the way.
2021-05-08 22:14:39 +02:00
Ali Mohammad Pur
aa4d8d26b9 LibWasm: Start implementing a basic WebAssembly binary format parser
This can currently parse a really simple module.
Note that it cannot parse the DataCount section, and it's still missing
almost all of the instructions.
This commit also adds a 'wasm' test utility that tries to parse a given
webassembly binary file.
It currently does nothing but exit when the parse fails, but it's a
start :^)
2021-05-08 22:14:39 +02:00