<p>How does a virtual machine generate native machine code on the fly and execute it?</p>

<p>Assuming you can figure out what are the native machine op-codes you want to emit, how do you go about actually running it?</p>

<p>Is it something as hacky as mapping the mnemonic instructions to binary codes, stuffing it into an char* pointer and casting it as a function and executing?</p>

<p>Or would you generate a temporary shared library (.dll or .so or whatever) and load it into memory using standard functions like <code>LoadLibrary</code> ?</p>

As far as i know it compiles everything in memory because it has to run some heuristics to    to optimize the code (i.e.: inlining over time) but you can have a look at the
Shared Source Common Language Infrastructure 2.0
rotor release. The whole codebase is identical to .NET except for the Jitter and the GC.