SwadicalRag / wasm2lua

wasm2lua: converting WASM into Lua
MIT License
190 stars 10 forks source link

Code generation without depending on `goto`s #29

Open swang206 opened 3 years ago

swang206 commented 3 years ago

It is very unfortunate world of warcraft uses Lua 5.1 which does not provide goto.

Although bit is provided, it does not provide all the functions that bitops provide which means they have to be emulated with lua apis itself.

BTW, how can I deal with wasi syscalls? should I implement those syscalls by myself?

    function __FUNCS__.func_3(reg0, reg1, reg2, reg3)
        local reg4;
        reg4 = __IMPORTS__.wasi_snapshot_preview1.fd_write(reg0,reg1,reg2,reg3);
        do return (bit_band(reg4,65535)); end;
    end
    function __FUNCS__.func_4(reg0)
        __IMPORTS__.wasi_snapshot_preview1.proc_exit(reg0);
        error('unreachable');
    end
SwadicalRag commented 3 years ago

It's been a while since I've last worked on this but if memory serves right, gotos were used because there is a limit to how far a lua vm can do a jump: wasm2lua uses these goto trampolines to split very large jump instructions into multiple smaller gotos.

It is theoretically possible to work around this by rewriting the compiler (and I recall having different ideas on how to do this, like by wrapping each simple statement block into their own stateless functions)

Another use for gotos in wasm2lua is for setjmp emulation - I bet it's theoretically possible to figure something out without gotos but...

I'm glad that you find this silly project useful, however I am no longer insane enough to continue to work on this project (but you are more than welcome to do whatever you want, so long as the license permits).

And yes, WASI syscalls need to be self-implemented (a la https://github.com/SwadicalRag/wasm2lua/blob/master/resources/wasilib.lua )