Compile and run C code using the clang API
I would like to use the clang / llvm API to compile a c function defined in a string and execute it immediately. Something like:
void main() {
std::string codestr = "int foo(int bar) { return bar * 2; }"
clang::??? *code = clang::???.compile(codestr);
int result = code->call("foo", 5);
}
I'm looking for tutorials, but what I've found so far doesn't quite fit my purpose or doesn't work because it refers to the legacy LLVM version. I am currently using LLVM 3.5.
Does anyone have a good guide?
source to share
I recommend using MCJIT because the old JIT framework will be removed in the next release. I can't point you to a complete tutorial, and I can't promise that the API hasn't changed since the blog was posted , but here's a guide on how to use MCJIT with LLVM's Kaleidoscope example, and here it is. Examples and tutorials are hard to find for LLVM / Clang. However, I suggest giving it a try, and perhaps you can document your journey with a short example.
Julia project also uses MCJIT to compile C ++ jit code inside Julia lang. You might be able to peek into the code and learn how to use MCJIT.
Good luck;)
source to share