Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM - linking problem

Tags:

c

linker

llvm

I am writing a LLVM code generator for the language Timber, the current compiler emits C-code. My problem is that I need to call C functions from the generated LLVM files, for example the compiler has a real-time garbage collector and i need to call functions to notify when new objects are allocated on the heap. I have no idea on how to link these functions with my generated LLVM files.

The code generation is made by generate .ll-files and then manually compile these.

I'm trying to call an external function from LLVM but i have no luck. In the examples I've >found only C standard functions like "puts" and "printf" are called, but I want to call a >homemade function. I'm stuck.

like image 851
capitrane Avatar asked Sep 13 '09 23:09

capitrane


1 Answers

I'm assuming you're writing an LLVM transformation, and you want to add calls to external functions into transformed code. If this is not the case, edit your question and include more information.

Before you can call an external function from LLVM code, you need to insert a declaration for it. For example:

virtual bool runOnModule(Module &m) {
    Constant *log_func = m.getOrInsertFunction("log_func",
                                               Type::VoidTy,
                                               PointerType::getUnqual(Type::Int8Ty),
                                               Type::Int32Ty,
                                               Type::Int32Ty,
                                               NULL);
    ...
}

The code above declares a function log_func which returns void and takes three arguments: a byte pointer (string), and two 32-bit integers. getOrInsertFunction is a method of Module.

To actually call the function, you have to insert a CallInst. There are several static Create methods for this.

like image 65
Jay Conrod Avatar answered Oct 09 '22 17:10

Jay Conrod