English 中文(简体)
Call LLVM Jit from c program
原标题:

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.

How can I accomplish this?

问题回答

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;
}

One oddity was that without the final include, ee is NULL. Bizarre.

To generate my tst.bc, I used http://llvm.org/demo/index.cgi and the llvm-as command-line tool.

This should (more or less) work using LLVM 2.6. It looks like there are some more helper functions in SVN to create a lazy ModuleProvider on top of a bitcode file. I haven t tried compiling it though, just glued together some bits from one of my JIT applications.

#include <string>
#include <memory>

#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;
    auto_ptr<MemoryBuffer> buffer(MemoryBuffer::getFile("bitcode.bc"));
    auto_ptr<Module> module(ParseBitcodeFile(buffer.get(), context, &error));
    auto_ptr<ModuleProvider> mp(new ExistingModuleProvider(module));
    module.release();

    auto_ptr<ExecutionEngine> ee(ExecutionEngine::createJIT(mp.get(), &error));
    mp.release();

    Function* func = ee->getFunction("foo");

    typedef void (*PFN)();
    PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
    pfn();
}




相关问题
How to generate LLVM IR with ANTLR and C target

im currently trying to generate LLVM IR with ANTLR3. But the problem ist, that i need the C target (C++ would be better but is not working yet, or is it?) but from C i can t call the LLVM C++ API for ...

How to build LLVM using GCC 4 on Windows?

I have been able to build LLVM 2.6 (the llvm-2.6.tar.gz package) using MinGW GCC 3.4.5. I haven t tested properly, but it seems to work. The trouble is, I have libraries of my own which don t build ...

Pros/Cons of Static and Dynamic Instrumentation

There are many static and dynamic instrumentation tools. Soot is a static instrumentation tool for Java bytecode. Pin and Valgrind are dynamic instrumentation tools for binaries. What are pros and ...

Bootstrapping a language on LLVM

I m bootstrapping a programming language compiler on top of LLVM. Currently I m mostly done writing a compiler for a subset of C which is self-compiling. When I m finished with that, I ll bootstrap ...

Call LLVM Jit from c program

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 ...

LLVM MinGW installation on Vista?

From llvm.org I ve downloaded llvm-2.6-x86-mingw32.tar.bz2 into c:llvm and llvm-gcc-4.2-2.6-x86-mingw32-tar.bz2 into c:llvm-gcc as well as setup a desktop shortcut the following batch file in c:...

热门标签