Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create literal pointer value in LLVM

Tags:

c++

llvm

I have some LLVM code that I'd like to refer to an existing variable. I'm going to JIT and execute this code in my process so I'd like the function to refer directly to the variables I have now.

For example,

    int64_t begin, end;
    auto&& con = g.module->getContext();
    std::vector<llvm::Type*> types = { llvm::Type::getInt64PtrTy(con), llvm::Type::getInt64PtrTy(con) };
    auto tramp = llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(con), types, false), llvm::GlobalValue::LinkageTypes::ExternalLinkage, "", g.module.get());
    auto bb = llvm::BasicBlock::Create(con, "entry", tramp);
    auto builder = llvm::IRBuilder<>(bb);
    auto call = builder.CreateCall(g.module->getFunction(failfunc->GetName()));
    builder.CreateStore(builder.CreateExtractValue(call, { tupty->GetFieldIndex(1) }), &begin);
    builder.CreateStore(builder.CreateExtractValue(call, { tupty->GetFieldIndex(2) }), &end);
    builder.CreateRetVoid();

Obviously I can't pass &begin and &end here directly since they are not llvm::Values. But how can I create an LLVM pointer value that points directly to them that I can pass to CreateStore?

like image 525
Puppy Avatar asked May 27 '14 12:05

Puppy


1 Answers

As far as the JIT is concerned, the content and address of these locals are just constants.

So if you want to pass the content of begin, use:

Constant* beginConstInt = ConstantInt::get(Type::Int64Ty, begin);

If you want to get its address, you'll have to first create an integer constant and then convert it to a pointer:

Constant* beginConstAddress = ConstantInt::get(Type::Int64Ty, (int64_t)&begin);
Value* beginConstPtr = ConstantExpr::getIntToPtr(
    beginConstAddress , PointerType::getUnqual(Type::Int64Ty)); 

For example, if the address of begin is 1000, the resulting constant should look like inttoptr (i64 1000 to i64*). So your store would look something like:

store i64 %extractvalue, i64* inttoptr (i64 1000 to i64*)
like image 198
Oak Avatar answered Sep 23 '22 19:09

Oak