我是 llvm 的新手,我正在尝试制作一个使用 createcall 编译函数的简单程序 函数定义:
#pragma once
#include <iostream>
void testfunc(){
std::cout << "hello bitch" << std::endl;
}
代码:
#include "llvm-14/llvm/IR/Module.h"
#include "llvm-14/llvm/IR/IRBuilder.h"
#include "testfunc.h"
int main(){
llvm::LLVMContext context;
std::unique_ptr<llvm::Module> mod;
mod = std::make_unique<llvm::Module>("llvm test module", context);
llvm::IRBuilder<> builder(context);
llvm::FunctionType *functionReturnType =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), false);
llvm::Function *mainFunction =
llvm::Function::Create(functionReturnType,
llvm::Function::ExternalLinkage,
"main",
mod.get());
llvm::BasicBlock *body = llvm::BasicBlock::Create(context, "body", mainFunction);
builder.SetInsertPoint(body);
llvm::Type *intTy = llvm::Type::getInt32Ty(context);
std::vector<llvm::Type *> params;
params.push_back(llvm::Type::getInt8PtrTy(context));
llvm::FunctionType *mainfunc =
llvm::FunctionType::get(builder.getInt32Ty(), params, true);
llvm::Function::Create(mainfunc, llvm::Function::ExternalLinkage, "testfunc",
mod.get());
builder.CreateCall(mod->getFunction("testfunc"),llvm::None ); // this line
builder.CreateRetVoid();
mod->print(llvm::outs(), nullptr);
return 0;
}
和错误:
llvmtest.ll:6:13: error: not enough parameters specified for call
%0 = call i32 (i8*, ...) @testfunc()
^
1 error generated.
如您所见,我的函数没有任何参数,但 builder.createcall() 需要一个参数。 无论如何要传递 createcall 参数?
谢谢。