[LLVMdev] Using LLVM to generate x86 dynamically in memory

Reid Kleckner rnk at mit.edu
Tue Feb 2 19:11:50 PST 2010

On Tue, Feb 2, 2010 at 7:39 PM, Shasank Chavan
<shanko_chavano at hotmail.com> wrote:
> Hi Jim.  Thanks for your speedy response.  I'm not entirely sure if a JIT is
> what I'm looking for.  I'm basically looking for a dll with an interface
> that takes a C program as input and compiles and optimizes it to native x86
> instructions in an in-memory buffer.  I don't want the dll the execute it,
> and I don't particularly want to translate our expressions into LLVM bitcode
> (although I can if the rest of the pieces are there).  Also, I briefly read
> up on lli.  This looks like a separate process will have to be spawned to
> invoke the JIT to execute programs in LLVM bytecode.  This will definitely
> incur an overhead penalty that we wouldn't want to pay.  Thanks in advance
> for your response.

Compiling code to native code in an in-memory buffer is really all a JIT is.

If you're attached to writing the definitions of each opcode in C,
here's an old idea in JIT compilation.  For every opcode in your
bytecode, write a corresponding C function that takes relevant
parameters and implements the opcode.  Mark each action as
__attribute__((always_inline))  Compile this C file with clang to a
.bc.  Load that module from disk at runtime into the JIT.  For each
bytecode string you want to execute, translate it from bytecode to
LLVM IR (with IRBuilder) that simply calls the opcode action
functions.  The run an inlining optimization pass to inline all the
actions, and ask the JIT for a pointer to the function.  Then you can
call it like a C function pointer.

lli is just a driver.  It's source code is an example of how you would
use LLVM to embed a JIT into your program.


More information about the llvm-dev mailing list