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::Value
s. But how can I create an LLVM pointer value that points directly to them that I can pass to CreateStore
?
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*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With