我正在用 LLVM 编写自己的语言,并且正在使用来自 std 和 custom 的外部 C 函数。我现在正在为 LLVM IR 添加使用 C++ 类的声明。像这样:
void register_malloc(llvm::Module *module) {
std::vector<llvm::Type*> arg_types;
arg_types.push_back(Type::getInt32Ty(getGlobalContext()));
FunctionType* type = FunctionType::get(
Type::getInt8PtrTy(getGlobalContext()), arg_types, false);
Function *func = Function::Create(
type, llvm::Function::ExternalLinkage,
llvm::Twine("malloc"),
module
);
func->setCallingConv(llvm::CallingConv::C);
}
void register_printf(llvm::Module *module) {
std::vector<llvm::Type*> printf_arg_types;
printf_arg_types.push_back(llvm::Type::getInt8PtrTy(getGlobalContext()));
llvm::FunctionType* printf_type =
llvm::FunctionType::get(
llvm::Type::getInt32Ty(getGlobalContext()), printf_arg_types, true);
llvm::Function *func = llvm::Function::Create(
printf_type, llvm::Function::ExternalLinkage,
llvm::Twine("printf"),
module
);
func->setCallingConv(llvm::CallingConv::C);
}
我要定义数十个外部函数,有没有一些简单的方法来定义它们,以及如何定义它们?
我考虑将 C 标头(或 LLVM IR 文件 .ll)“包含”到模块中。但我找不到任何例子如何做到这一点......