llvm createCall调用带有错误签名的函数

时间:2016-12-18 10:41:41

标签: llvm

我想在LLVM中创建一个函数,这是一个只有函数调用foo(idx,mn)的适配器。 foo的函数原型是void foo(unsigned char, const char*)

// adapter Function with only a function call foo(idx, mn)
llvm::Function* createCallFun(llvm::Module* M, llvm::Function* exit_f) {

    llvm::LLVMContext& Ctx = M->getContext();
    llvm::Function* foo_f = foo_prototype(Ctx, M);

    llvm::Constant* c = M->getOrInsertFunction("__call_fun", FunctionType::getVoidTy(Ctx), llvm::Type::getInt32Ty(Ctx), llvm::Type::getInt32Ty(Ctx), NULL);
    llvm::Function* call_fun_f = llvm::cast<llvm::Function>(c);

    llvm::BasicBlock* entry = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", call_fun_f);
    llvm::IRBuilder<> builder(entry);

    llvm::Function::arg_iterator args = call_fun_f->arg_begin();
    llvm::Value* idx = &*args++;
    idx->setName("idx");
    llvm::Value* mn = &*args++;
    mn->setName("mn");
    llvm::Value* greater = builder.CreateICmpSGE(idx, mn, "tmp");

    std::vector<llvm::Value*> fun_args;
    fun_args.push_back(greater);
    fun_args.push_back(err_msg);
    builder.CreateCall(foo_f, fun_args);

    return call_fun_f;
}

然后我收到了这个错误:

  

lib / IR / Instructions.cpp:245:void llvm :: CallInst :: init(llvm :: FunctionType *,llvm :: Value *,llvm :: ArrayRef,llvm :: ArrayRef&gt;,const llvm :: Twine&amp;):断言`(i&gt; = FTy-&gt; getNumParams()|| FTy-&gt; getParamType(i)== Args [i] - &gt; getType())&amp;&amp; “调用签名错误的函数!”'失败了。

似乎foo的第一个参数有一个类型不匹配。如何将值greater转换为unsigned char类型?

1 个答案:

答案 0 :(得分:0)

我通过使用greater转换CreateZExt来解决此错误。

llvm::Value *castuchar =
    builder.CreateZExt(greater, llvm::Type::getInt8Ty(Ctx), "tmp1");