首页 > 解决方案 > LLVM 函数调用

问题描述

我最近开始使用 LLVM,在我的 IR 代码中插入函数时遇到问题。起初我的功能是这样的:

print(){
     printf("Hello")
  }

我设法用这段代码在每条 CALL 指令之前插入它:

Function *fun;
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
 if (F->getName() == "print") {       
    fun= cast<Function>(F);
}
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {

          for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {

           if (isa<CallInst>(&(*BI))) {
              CallInst *CI = dyn_cast<CallInst>(BI);
              Instruction *newInst = CallInst::Create(fun, "");
              BB->getInstList().insert(BI, newInst);

            }
          }
 }

现在我正在尝试做同样的事情(在每个调用指令之前插入我的打印函数)但我的打印函数必须是这种类型的函数:

void print (string something){
   printf ("hello");
   do something with "something" string but whatever;
}

我尝试了所有我发现但没有成功的东西......(我不明白 IRBuiler、createGlobalString/Ptr 和 getOrInsertFunction() 是如何工作的,这一定是我的问题......有人可以帮助我吗?谢谢......

标签: stringparameter-passingllvmllvm-ir

解决方案


检测这种函数调用的方法就像

CallInst* callonFree = CallInst::Create(your_print_fun, {CI->getOperand(0)},"",CI);

通常,您可以尝试将字符串转换为 char*,这样更易​​于检测。


推荐阅读