llvm createCall 调用签名错误的函数

llvm createCall Calling a function with a bad signature

我想在 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 >, const llvm::Twine&): Assertion `(i >= FTy->getNumParams() || FTy->getParamType(i) == Args[i]->getType()) && "Calling a function with a bad signature!"' failed.

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

我通过使用 CreateZExt.

转换 greater 修复了这个错误
llvm::Value *castuchar =
    builder.CreateZExt(greater, llvm::Type::getInt8Ty(Ctx), "tmp1");