I've written a Brainfuck implementation (C++) that works like this:
- Read input brainfuck file
- Do trivial optimizations
- Convert brainfuck to machine code for the VM
- Execute this machine code in the VM
This is pretty fast, but the bottleneck is now at the VM. It's written in C++ and reads a token, executes an action (which aren't many at all, if you know Brainfuck) and so on.
What I want to do is strip out the VM and generate native machine code on the fly (so basicly, a JIT compiler). This can easily be a 20x speedup.
This would mean step 3 gets replaced by a JIT compiler and step 4 with the executing of the generated machine code.
I don't know really where to start, so I have a few questions:
- How does this work, how does the generated machine code get executed?
- Are there any C++ libraries for generating native machine code?