Some side project: Brainfuck-JIT
I actually have wondered how JIT (Just-In-Time compiling) works for quite a long time, because it is basic knowledge that code segment in memory is write-protected, and you cannot move CPU instruction pointer to stack frame or heap for to security reason. But to do JIT you need to generate new machine code in memory and instantly execute it. How?
I stumbled upon this article titled “The Joy of Simple JITs”, which get me started. Apparently, you can do mmap
(POSIX) or VirtualAlloc
(Win32) to allocate virtual file in memory. And you can write to this section. And you can execute it. Looking great?
If you have read that article, you would have noticed that the hardest part in doing JIT is instruction encoding. I am not going to do full detail here – you can easily find how to encode instruction on various platform easily (though need not to be interesting). I have used some JIT engine in other projects before (was using SoftWire, but now it does not use JIT anymore). And the JIT library alone is half the binary size!
DynASM, mentioned in that article, is very interesting. Because it encodes all instructions compile-time! On runtime it’s just concatenation and substituting parameter. This make runtime very small.
So in order to play with it, I write some little brainfuck JIT program. (Technically not JIT, it’s a brainfuck dynamic code generator) with some optimization. It was quite fun. This is very first time I have actually write some assembly myself (normally I use C intrinsics function). The hardest part is actually to get function prologue and epilogue right.
The code is available on GitHub.