我已经搜索了几个小时,但找不到任何可以帮助我的东西。我正在从事一个涉及 FunctionPass 的项目。我已经实现了一个 runOnFunction(Function &f) 方法并且工作正常。基本上它需要:
1) 检测存储指令
2) 将存储指令的内存地址转换为Integer
3) 使用按位与操作 (0000FFFF) 改变整数
4) 将整数转换回指针
到目前为止,我得到了以下内容:
virtual bool runOnFunction(Function &F) {
for (Function::iterator bb = F.begin(), bbe = F.end(); bb != bbe; ++bb) {
BasicBlock& b = *bb;
for (BasicBlock::iterator i = b.begin(), ie = b.end(); i != ie; ++i) {
if(StoreInst *si = dyn_cast<StoreInst>(&*i)) {
PtrToIntInst* ptrToInt = new PtrToIntInst(si->getPointerOperand(), IntegerType::get(si->getContext(), 32), "", si);
}
}
}
return true;
}
我这辈子都想不出如何实际插入指令,甚至找不到创建 AND 指令的方法。如果有人能指出我正确的方向,那就太好了。
提前致谢。
最佳答案
我建议看一下 Programmer's Manual - 它对基础知识的覆盖面相当不错。
特别是,有 a section about creating and inserting new instructions .最简单的方法就是提供一个现有指令作为新指令构造函数的最后一个参数,然后该指令将紧接在现有指令之前插入该指令。
或者,如果您只想添加到它的末尾,您可以传递封闭的基本 block (但请记住您需要注意终止符!)。最后,您可以在封闭的基本 block 上调用 getInstList()
,然后调用 insert
或 push_back
以在其中插入新指令。
顺便说一句,您不必遍历所有 block 然后遍历每个 block 中的所有指令,您可以直接遍历指令;见the section about the instruction iterator in the programmer's manual .
关于c++ - 如何插入 LLVM 指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370306/