繁体   English   中英

LLVM如何获取指令的返回值

[英]LLVM How to get return value of an instruction

我有一个程序可以像这样从堆栈分配内存:

%x = alloca i32, align 4

在我的过程中,我想获取在运行时指向此已分配内存的实际内存指针。 这应该是%x。 如何通过我的通行证?

Instruction* I;
if (AllocaInst* AI = dyn_cast<AllocaInst>(I)) {
    //How to get %x?
} 

您可以将Instruction *作为Value *使用(并且Instruction继承自Value),然后使用该指令的结果/返回值。 我已经从LLVM Pass中修改了一些代码,以演示如何使用alloca分配空间,然后将其存储到该位置。 注意,指令的结果可以作为值直接传递给其他指令。

    // M is the module
    // ci is the current instruction
    LLVMContext &ctx = M.getContext();
    Type* int32Ty = Type::getInt32Ty(ctx);
    Type* int8Ty = Type::getInt8Ty(ctx);
    Type* voidPtrTy = int8Ty->getPointerTo();

    // Get an identifier for rand()
    Constant* = M.getOrInsertFunction("rand", FunctionType::get(cct.int32Ty, false));

    // Construct the struct and allocate space
    Type* strTy[] = {int32Ty, voidPtrTy};
    Type* t = StructType::create(strTy);
    Instruction* nArg = new AllocaInst(t, "Wrapper Struct", ci);

    // Add Store insts here
    Value* gepArgs[2] = {ConstantInt::get(int32Ty, 0), ConstantInt::get(int32Ty, 0)};
    Instruction* prand = GetElementPtrInst::Create(NULL, nArg, ArrayRef<Value*>(gepArgs, 2), "RandPtr", ci);

    // Get a random number 
    Instruction* tRand = CallInst::Create(getRand, "", ci);

    // Store the random number into the struct
    Instruction* stPRand = new StoreInst(tRand, prand, ci);

如果要存储或加载到%x,则只需使用存储或盖子指令

如果要使用指针的数值,请使用ptrtoint指令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM