2 ms·
This is not terribly hard, though,—Forth implementations have long explored[1] the road from emiting a list of VM instructions (“token threading”) to emitting a
by mananaysiempre 4y ago
This is not terribly hard, though,—Forth implementations have long explored[1] the road from emiting a list of VM instructions (“token threading”) to emitting a list of VM instruction implementation entry points (“direct threading”, although “indirect threading” arguably fits this as well) to emitting a stream of call instructions targeting said entry points (“subroutine threading”) to copying the implementation bodies into the stream (“inline threading”).
With sufficient dedication, you can even cut out the bodies from the binary the compiler of your high-level implementation language produced, to piggyback on its target support. Gforth[2] among others does this in the land of Forth, but the more well-known implementation is perhaps the dyngen code generator[3] used in Qemu before the switch to TCG in version 0.10.
Generating machine code isn’t difficult, and neither is modifying a (low-level) bytecode generator to do it[4]. Generaring good machine code is difficult.
[1] http://www.bradrodriguez.com/papers/moving1.htm http://www.bradrodriguez.com/papers/moving1.htm
[2] https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Dynamic-Superinstructions.html https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Dyn...
[3] https://gitlab.com/qemu-project/qemu/-/blob/v0.9.1/qemu-tech.texi#L289 https://gitlab.com/qemu-project/qemu/-/blob/v0.9.1/qemu-tech..., rendered as https://web.archive.org/web/20081120011949if_/http://bellard.org/qemu/qemu-tech.html#SEC9 https://web.archive.org/web/20081120011949if_/http://bellard...
[4] https://github.com/EarlGray/c4/blob/master/JIT.md https://github.com/EarlGray/c4/blob/master/JIT.md