C++ 在LLVM中创建文字指针值
我有一些LLVM代码,我想引用一个现有变量。我将使用JIT并在流程中执行此代码,因此我希望函数直接引用我现在拥有的变量 比如说,C++ 在LLVM中创建文字指针值,c++,llvm,C++,Llvm,我有一些LLVM代码,我想引用一个现有变量。我将使用JIT并在流程中执行此代码,因此我希望函数直接引用我现在拥有的变量 比如说, int64_t begin, end; auto&& con = g.module->getContext(); std::vector<llvm::Type*> types = { llvm::Type::getInt64PtrTy(con), llvm::Type::getInt64PtrTy(con) }
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();
int64\t开始,结束;
auto&&con=g.module->getContext();
std::vector types={llvm::Type::getInt64PtrTy(con),llvm::Type::getInt64PtrTy(con)};
auto-tramp=llvm::Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(con),Type,false),llvm::GlobalValue::LinkageTypes::ExternalLinkage,“,g.module.get());
auto bb=llvm::BasicBlock::Create(con,“entry”,tramp);
autobuilder=llvm::IRBuilder(bb);
auto call=builder.CreateCall(g.module->getFunction(failfunc->GetName());
CreateStore(builder.CreateExtractValue(调用,{tupty->GetFieldIndex(1)}),&begin);
CreateStore(builder.CreateExtractValue(调用,{tupty->GetFieldIndex(2)}),&end);
CreateRetVoid();
显然,我不能在这里直接传递&begin和&end,因为它们不是
llvm::Value
s。但是如何创建一个直接指向它们的LLVM指针值,并将其传递给CreateStore
?就JIT而言,这些局部变量的内容和地址只是常量
因此,如果要传递开始
的内容,请使用:
Constant* beginConstInt = ConstantInt::get(Type::Int64Ty, begin);
如果要获取其地址,必须首先创建一个整数常量,然后将其转换为指针:
Constant* beginConstAddress = ConstantInt::get(Type::Int64Ty, (int64_t)&begin);
Value* beginConstPtr = ConstantExpr::getIntToPtr(
beginConstAddress , PointerType::getUnqual(Type::Int64Ty));
例如,如果begin
的地址为1000,则生成的常量应类似于inttoptr(i64 1000到i64*)
。因此,您的商店
看起来像:
store i64 %extractvalue, i64* inttoptr (i64 1000 to i64*)