【问题标题】:Create literal pointer value in LLVM在 LLVM 中创建文字指针值
【发布时间】:2014-05-27 12:00:12
【问题描述】:

我有一些 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();

显然我不能在这里直接传递 &begin 和 &end 因为它们不是llvm::Values。但是如何创建一个直接指向它们的 LLVM 指针值,我可以将它们传递给CreateStore

【问题讨论】:

    标签: c++ llvm


    【解决方案1】:

    就 JIT 而言,这些 locals 的内容和地址只是常量。

    所以如果你想传递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*)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      相关资源
      最近更新 更多