I've written a Brainfuck implementation (C++) that works like this:

  1. Read input brainfuck file
  2. Do trivial optimizations
  3. Convert brainfuck to machine code for the VM
  4. 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:

  1. How does this work, how does the generated machine code get executed?
  2. Are there any C++ libraries for generating native machine code?
Edit
Report