Como carregar o arquivo de código de bits LLVM de um ifstream?
Estou tentando carregar um módulo LLVM definido em um.bc
em tempo de execução, mas encontraram um problema.
O código de bits de interesse foi gerado a partir dehello.cpp
:
// hello.cpp
// build with:
// clang-3.4 -c -emit-llvm hello.cpp -o hello.bc
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
Quando o programa abaixo tenta carregá-lo em tempo de execução, ele trava dentrollvm::BitstreamCursor::Read()
:
// main.cpp
// build with:
// g++ main.cpp `llvm-config-3.4 --cppflags --ldflags --libs` -ldl -lpthread -lcurses
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <fstream>
#include <iostream>
llvm::Module *load_module(std::ifstream &stream)
{
if(!stream)
{
std::cerr << "error after open stream" << std::endl;
return 0;
}
// load bitcode
std::string ir((std::istreambuf_iterator<char>(stream)), (std::istreambuf_iterator<char>()));
// parse it
using namespace llvm;
LLVMContext context;
SMDiagnostic error;
Module *module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir.c_str())), error, context);
if(!module)
{
std::string what;
llvm::raw_string_ostream os(what);
error.print("error after ParseIR()", os);
std::cerr << what;
} // end if
return module;
}
int main()
{
std::ifstream stream("hello.bc", std::ios_base::binary);
llvm::Module *m = load_module(stream);
if(m)
{
m->dump();
}
return 0;
}
Estou construindo contra o LLVM v3.4 usando as linhas de comando mencionadas nos comentários.
Alguma idéia do que estou fazendo de errado?