1
votes

I want to make a function in LLVM which is an adapter with only a function call foo(idx, mn). The function prototype of foo is 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;
}

Then I got this error:

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.

It seems the first argument of foo has a type mismatch. How can I cast the Value greater to unsigned char type?

1

1 Answers

0
votes

I fixed this error by cast greater with CreateZExt.

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