在LLVM中创建文字指针值

时间:2014-05-27 12:00:12

标签: 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();

显然我不能直接通过&amp; begin和&amp; end here,因为它们不是llvm::Value s。但是,如何创建一个直接指向它的LLVM指针值,我可以传递给CreateStore

1 个答案:

答案 0 :(得分:5)

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

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

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 to i64*)。所以你的store看起来像是:

store i64 %extractvalue, i64* inttoptr (i64 1000 to i64*)
相关问题