AlexAltea / unicorn.js

Unicorn CPU emulator framework port for JavaScript
https://alexaltea.github.io/unicorn.js/
GNU General Public License v2.0
566 stars 36 forks source link

feature request: Make TCG create Webassembly instead of using TCI #16

Open petersalomonsen opened 4 years ago

petersalomonsen commented 4 years ago

As @atrosinenko has achieved with https://github.com/atrosinenko/qemujs, it would be great to have just in time compilation to webassembly instead of using the TCI interpreter.

AlexAltea commented 4 years ago

As of today, WebAssembly is not designed to do JIT compilation [1]. Every recompiled Translation Block (TB) of TCG, needs results in a new WebAssembly module.

This has quite a significant performance overhead on most browsers, with Firefox being the most reliable of them all. Chrome's engine reserves 4 GiB of virtual address space for every WebAssembly module so theoretically it can only handle 4096 WebAssembly modules before crashing which is certainly not enough for us.

That said, we already have the code, and TCG-WASM translation is slightly faster (on benchmarks), and the code is slightly simpler, but the bottleneck is still the WASM engines in browsers. It seems, @atrosinenko solved this issue by interpreting TBs (at Binaryen level!) that are not frequently used. We haven't written anything like that yet, but if JIT keeps being delayed from the WASM specification, then we will (though relying on TCI rather than interpreting WASM bytecode).

We will probably release the code soon so stay tuned! I'll update this thread when we're ready.

[1] https://webassembly.org/docs/jit-library/

petersalomonsen commented 4 years ago

That sounds good! Looking very much forward to the new release.

My use case is a very simple one. I've tested unicorn.js for running 4klang in the browser. It works fine, and your api is great, but interpretation is too slow for generating audio in real time. However this code is so small that I don't think it should generate lot's of TBs and maybe what you have already is sufficient for running this.

Thank you for the great work in porting unicorn to the browser!