Skip to content

Instantly share code, notes, and snippets.

@zhangce
Created September 30, 2015 04:30
Show Gist options
  • Select an option

  • Save zhangce/346e859f9ce251dc8695 to your computer and use it in GitHub Desktop.

Select an option

Save zhangce/346e859f9ce251dc8695 to your computer and use it in GitHub Desktop.
#include <string>
#include <memory>
#include <iostream>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/MemoryBuffer.h>
#include "llvm/Support/SourceMgr.h"
using namespace std;
using namespace llvm;
int main()
{
InitializeNativeTarget();
LLVMContext Context;
MemoryBufferRef Buffer = MemoryBuffer::getFile("tst.bc").get()->getMemBufferRef();
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buffer, Context);
if (std::error_code EC = ModuleOrErr.getError()) {
SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
EC.message());
std::cout << "WRONG" << std::endl;
return 1;
}
llvm::EngineBuilder factory(std::move(ModuleOrErr.get()));
llvm::TargetOptions Opts;
factory.setEngineKind(llvm::EngineKind::JIT);
factory.setTargetOptions(Opts);
//std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new SectionMemoryManager());
//factory.setMCJITMemoryManager(std::move(MemMgr));
llvm::ExecutionEngine *executionEngine = factory.create();
Function* func = executionEngine->FindFunctionNamed("main");
typedef void (*PFN)();
PFN pfn = reinterpret_cast<PFN>(executionEngine->getPointerToFunction(func));
int a = 5;
int b = 10;
pfn();
std::cout << a << std::endl;
delete executionEngine;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment