WebAssembly / binaryen

Optimizer and compiler/toolchain library for WebAssembly
Apache License 2.0
7.54k stars 745 forks source link

Question: How do you compile a flat list of instructions? #4737

Open Heath123 opened 2 years ago

Heath123 commented 2 years ago

The page https://github.com/WebAssembly/binaryen/wiki/Compiling-to-WebAssembly-with-Binaryen says:

If, instead, you have a more "flat" representation of simple instructions in lists in basic blocks, then you can pass that in as well, just create Blocks and fill them with such simple instructions. That will use more local variables, but the Binaryen optimizer can optimize those out.

However, e.g. BinaryenBinary takes left and right parameters, and passing in null results in:

void wasm::Binary::finalize(): Assertion `left && right' failed.

So how would I make a module from a list of WebAssembly instructions?

Heath123 commented 2 years ago

Should this be a discussion?

kripken commented 2 years ago

Good point, that text isn't very clear. I edited it now, and added an example - does that help?

(This could be a discussion, yeah - we aren't very strict about that here though.)

Heath123 commented 2 years ago

Ah, thanks, I see what that meant now - still using the tree structure but storing and loading locals each time. That wasn't quite what I was looking for though, I wanted to generate the normal stack machine instructions so instead of

 (i32.add
  (local.get $0)
  (local.get $1)
 )

I could do the normal

(local.get $0)
(local.get $1)
(i32.add)

Is that possible as well? I know BInaryen doesn't use that format internally but it has to be able to understand it to read WASM files, is there an API for it?

kripken commented 2 years ago

There isn't an API for that atm. Binaryen can't even read wat in that format, actually. At least the wat side may get fixed with @tlively 's current work on a new parser, though.

Are you using text, or the C API, or something else?

Heath123 commented 2 years ago

There isn't an API for that atm. Binaryen can't even read wat in that format, actually. At least the wat side may get fixed with @tlively 's current work on a new parser, though.

Are you using text, or the C API, or something else?

I’m using Python bindings for the C API, I wanted to mess with trying to compile Python bytecode to WASM and since Python bytecode is also a stack machine I wanted to avoid converting it out of the stack machine format and back into it

Heath123 commented 2 years ago

I’d probably have to convert it out of the stack machine format anyway though because Python bytecode is a lot less strict so maybe that’s not a problem

I have no idea if this will actually be possible but it’s a fun hobby project if I can get it working for simple code I guess

kripken commented 2 years ago

Sounds like a cool project!

I hope adding locals and local operations wouldn't be too hard to do. But eventually it might be nice to add a C API for real stack machine input, where Binaryen would automatically convert it to use locals and local operations.