Created
December 28, 2013 18:42
-
-
Save tomas789/8162661 to your computer and use it in GitHub Desktop.
Another LLVM JIT - Loading variable from memory clang++-mp-3.3 `llvm-config --cppflags --ldflags --libs core jit X86` jit2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdint> | |
#include <string> | |
#include "llvm/ExecutionEngine/JIT.h" | |
#include "llvm/IR/IRBuilder.h" | |
#include "llvm/IR/Module.h" | |
#include "llvm/PassManager.h" | |
#include "llvm/Support/TargetSelect.h" | |
#include "llvm/Analysis/Verifier.h" | |
using namespace llvm; | |
int main(int argc, char * argv[]) { | |
InitializeNativeTarget(); | |
/* LLVM Global Context */ | |
LLVMContext & ctx = getGlobalContext(); | |
/* Module */ | |
Module * module = new Module("MyModule", ctx); | |
/* IRBuilder */ | |
IRBuilder<> builder(ctx); | |
/* Create execution engine */ | |
std::string err_str; | |
CodeGenOpt::Level opt = CodeGenOpt::Default; | |
ExecutionEngine * engine = ExecutionEngine::createJIT(module, &err_str, | |
0, opt); | |
if (!engine) { | |
std::cerr << "Could not create ExecutionEngine:" | |
<< err_str << std::endl; | |
return 1; | |
} | |
/* LLVM Type of int64_t* */ | |
PointerType * int64_ptr_ty = Type::getInt64PtrTy(ctx, false); | |
/* LLVM Type of int64_t */ | |
IntegerType * int64_ty = Type::getInt64Ty(ctx); | |
/* Create prototype of result function - int64_t() */ | |
std::vector<Type *> arguments; | |
FunctionType * proto = FunctionType::get(int64_ty, arguments, false); | |
/* Create function body */ | |
Function * fce = Function::Create(proto, Function::ExternalLinkage, | |
"MyFunction", module); | |
BasicBlock * bb = BasicBlock::Create(ctx, "entry", fce); | |
/* Point builder to add instructions to function body */ | |
builder.SetInsertPoint(bb); | |
/* Actual value to return */ | |
int64_t value = 765; | |
/* Address of value */ | |
Value * addr = ConstantInt::get(int64_ty, APInt(64, (uint64_t)&value)); | |
/* LLVM Pointer to int64_t value */ | |
Value * value_ptr = builder.CreateIntToPtr(addr, | |
int64_ptr_ty, | |
"int_to_ptr_cast"); | |
/* LLVM Value */ | |
Value * v = builder.CreateLoad(value_ptr, "load_value"); | |
/* Return value */ | |
builder.CreateRet(v); | |
/* Function pass manager */ | |
FunctionPassManager pass_manager(module); | |
/* Add data layout */ | |
pass_manager.add(new DataLayout(*engine->getDataLayout())); | |
pass_manager.doInitialization(); | |
/* Do optimizations */ | |
pass_manager.run(*fce); | |
/* JIT it */ | |
void * fce_ptr = engine->getPointerToFunction(fce); | |
int64_t (*fce_typed_ptr)() = (int64_t(*)())fce_ptr; | |
/* Excute JITed function */ | |
std::cout << "Result: " << (*fce_typed_ptr)() | |
<< std::endl << std::endl; | |
/* Dump LLVM IR */ | |
std::cout << "LLVM IR dump: " << std::endl; | |
module->dump(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment