Hello,
I am trying to run this test program via JIT on my Mac (10.7.1) where I have compiled the latest LLVM code for 32-bit and it is not working properly. Running the same code against LLVM 2.9 works fine. I also tried recompiling my same checkout of LLVM as 64-bit and re-compiled my test program and that works fine, for some reason the 32-bit version is yielding unexpected behavior. I believe I am linking with all of the required LLVM .a files. Here’s my code:
#include
#include “llvm/LLVMContext.h”
#include “llvm/Module.h”
#include “llvm/Constants.h”
#include “llvm/DerivedTypes.h”
#include “llvm/Instructions.h”
#include “llvm/ExecutionEngine/JIT.h”
#include “llvm/ExecutionEngine/Interpreter.h”
#include “llvm/ExecutionEngine/GenericValue.h”
#include “llvm/Support/TargetSelect.h”
#include “llvm/Support/ManagedStatic.h”
#include “llvm/Support/raw_ostream.h”
#include “llvm/Support/IRBuilder.h”
using namespace std;
using namespace llvm;
int main(int argc, char** argv){
InitializeNativeTarget();
LLVMContext context;
Module* module = new Module(“test”, context);
vector<Type*> args;
args.push_back(Type::getInt32Ty(context));
FunctionType* ft = FunctionType::get(Type::getInt32Ty(context),
args, false);
Function* f = Function::Create(ft,
GlobalValue::ExternalLinkage,
“f”, module);
Value* arg = f->arg_begin();
BasicBlock* bb = BasicBlock::Create(context, “entry”, f);
IRBuilder<> builder(bb);
Value* one = builder.getInt32(1);
Value* v = builder.CreateAdd(one, arg);
builder.CreateRet(v);
ExecutionEngine* engine = EngineBuilder(module).create();
void* vpf = engine->getPointerToFunction(f);
int32_t (*fp)(int32_t) =
(int32_t (*)(int32_t))(intptr_t)vpf;
int32_t out = fp(10);
cout << "out is: " << out << endl;
return 0;
}
I appreciate your help. Thanks,
Nick