[LLVMdev] Please help with LLVM C++ integration

Gordon Henriksen gordonhenriksen at me.com
Tue Aug 19 11:27:16 PDT 2008

On Aug 19, 2008, at 13:36, kirill havok wrote:

> I got very interested in LLVM project, and I decided to start  
> writing my own scripting language based on it. By studying the  
> documentation, I could not find how to call external function,  
> written in C. That is, I have a set of functions written in C/C++, I  
> generate code, using LLVM C++ interface, how can I call(or register  
> in machine in run-time) my external functions? Maybe I just missed  
> something. Please, give in which direction to search.

Hi Kirill,

Since LLVM goes to the bare metal, there is no need for an "FFI" or  
bindings to call native code. Simply create a declaration for the  
function—a Function with no body—and call it as any other function in  
your program. (Also, of course, link with a library to define the  
symbols.) It might be instructive to examine the llvm-gcc -S -emit- 
llvm output of a program like this:

int MyExternalFn(int, int, int);

int main(int, const char**, const char**) {
   return MyExternalFn(1, 2, 3);

Where you tweak MyExternalFn to have the desired prototype.

To create a function declaration:

 From C++, simply call Function::Create and then do not add any basic  
 From LLVM assembly, use the keyword "declare" instead of "define": http://llvm.org/docs/LangRef.html#functionstructure

If you're using the JIT, you'll need to do the above, but also may  
need to register your functions using ExecutionEngine::addGlobalMapping.


Note that the C calling convention do not map directly to LLVM IR for  
many cases, so it's best to stick to simple parameter types (pointers  
and ints, for example) and also to verify that the LLVM FunctionTypes  
you use are correct on each platform you support.

Good luck!

— Gordon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080819/8819edec/attachment.html>

More information about the llvm-dev mailing list