3

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...

3
  • i guess there is no include; for a little bit convenience, take a look at my previous answer to another question, specially for printf_prototype. Commented Sep 24, 2015 at 5:50
  • 1
    You can use clang -S -emit-llvm to "compile" headers and then use CPPBackend to generate C++ code that creates these definitions. Commented Sep 24, 2015 at 7:01
  • @arrowd I was thinking about something like that. How do I add/load declarations from LLVM file to the module? Commented Sep 24, 2015 at 10:12

1 Answer 1

2

Create an empty C source and include every header you need, then compile it to LLVM IR with clang -S -emit-llvm. This source would contain declarations for every function from headers. Now use llc -march=cpp out.ll and it will produce C++ source that calls LLVM API to generate given IR. You can copy-paste this code into your program.

Make sure you have cpp backend enabled during LLVM build.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to do that within a Module pass instead of copy and pasting the code at the beginning of the file? I have a pass that needs to inject an external function call.
As there is no cpp backend now, the only way left is to compile to bitcode and load it in the pass using parseIRFile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.