从C程序调用LLVM Jit [英] Call LLVM Jit from c program
问题描述
我已经使用llvm.org上的在线编译器生成了一个bc文件,并且我想知道是否可以从ac或c ++程序加载该bc文件,并使用llvm jit在bc文件中执行IR (以编程方式在c程序中),然后获取结果.
I have generated a bc file with the online compiler on llvm.org, and I would like to know if it is possible to load this bc file from a c or c++ program, execute the IR in the bc file with the llvm jit (programmatically in the c program), and get the results.
我该怎么做?
推荐答案
以下是基于Nathan Howell的一些工作代码:
Here's some working code based on Nathan Howell's:
#include <string>
#include <memory>
#include <iostream>
#include <llvm/LLVMContext.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
using namespace std;
using namespace llvm;
int main()
{
InitializeNativeTarget();
llvm_start_multithreaded();
LLVMContext context;
string error;
Module *m = ParseBitcodeFile(MemoryBuffer::getFile("tst.bc"), context, &error);
ExecutionEngine *ee = ExecutionEngine::create(m);
Function* func = ee->FindFunctionNamed("main");
typedef void (*PFN)();
PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
pfn();
delete ee;
}
一个奇怪的地方是,如果没有最后的包含,则ee为NULL.奇怪.
One oddity was that without the final include, ee is NULL. Bizarre.
要生成我的tst.bc,我使用了 http://llvm.org/demo/index.cgi 和llvm-as命令行工具.
To generate my tst.bc, I used http://llvm.org/demo/index.cgi and the llvm-as command-line tool.
这篇关于从C程序调用LLVM Jit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!