LLVM provides a few tools to work with JIT engines. The examples of such tools are lli
and llvm-rtdyld
.
The interpreter tool (lli
) implements an LLVM bitcode interpreter and JIT compiler as well by using the LLVM execution engines studied in this chapter. Let's consider the source file, sum-main.c
:
#include <stdio.h> int sum(int a, int b) { return a + b; } int main() { printf("sum: %d\n", sum(2, 3) + sum(3, 4)); return 0; }
The lli
tool is capable of running bitcode files when a main function is provided. Generate the sum-main.bc
bitcode file by using clang
:
$ clang -emit-llvm -c sum-main.c -o sum-main.bc
Now, run the bitcode through lli
by using the old JIT compilation engine:
$ lli sum-main.bc sum: 12
Alternatively, use the MCJIT engine:
$ lli -use-mcjit sum-main.bc sum: 12
There is also a flag to use the interpreter, which is usually much slower:
$ lli -force-interpreter sum-main.bc sum:12