在LLVM中创建文字指针值

Pup*_*ppy 5 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) };
    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();
Run Code Online (Sandbox Code Playgroud)

显然我不能直接通过&开始和结束,因为它们不是llvm::Values.但是,我如何创建一个LLVM指针值,直接指向我可以传递给它们的那些CreateStore

Oak*_*Oak 5

就JIT而言,这些本地人的内容和地址只是常数.

因此,如果您想传递内容begin,请使用:

Constant* beginConstInt = ConstantInt::get(Type::Int64Ty, begin);
Run Code Online (Sandbox Code Playgroud)

如果要获取其地址,则必须先创建一个整型常量,然后将其转换为指针:

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

例如,如果地址begin为1000,则生成的常量应如下所示inttoptr (i64 1000 to i64*).所以你store会看起来像:

store i64 %extractvalue, i64* inttoptr (i64 1000 to i64*)
Run Code Online (Sandbox Code Playgroud)