I'm writing my own language in LLVM and I'm using external C functions from std and custom. I'm now adding declarations using C++ classes for LLVM IR. Like this:
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);
}
I'm gonna define tens of external functions, is there some easy way to define them, and how?
I think about "including" C header(or LLVM IR file .ll) to the module. But I couldn't find any example how to do this...