如何获得一个指令的返回值
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?
}
您可以将指令*作为值*(并且指令继承自值),然后您正在处理该指令的结果/返回值。我从我的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,只需使用store或lid指令
如果你想要指针的数值,使用ptrtoint指令
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 在一个指令中声明更多指针的运算符优先级
- C++-更改另一个文件的ASM指令
- 为什么在这个分解的 std::string dtor 中有一个锁定的 xadd 指令?
- 将多个Pragma指令放入一个预处理器定义中
- 为什么 ARM 使用两个指令来屏蔽一个值
- 获取列表的第一个和最后一个元素<string>给我非法指令错误
- 在命名空间中放入一个标准的纯C头#include指令可以吗
- 是否可以定义另一个预处理器指令
- 为什么VS2013将函数调用编译成两个指令而不是一个
- 有一个简单的程序文件/程序文件(x86)指令的c++在windows
- 让一个任务运行固定数量的指令
- 如何获得一个指令的返回值
- 信号11能在一个mov指令中从一个寄存器被激发到另一个寄存器吗?
- 将包含指令合并到一个文件中是否是一种好的做法
- 在哪里找到一个很好的指令如何解析一个c++头文件