Calling functions

Hello!

I'm trying LLVM for generating code and I found something I cannot figure out or find in the documentation:

I'm doing something like this to call "sin" from std:

  std::vector<const Type*> params;
  params.push_back( Type::FloatTy );
  FunctionType *FT = FunctionType::get( Type::FloatTy, params, false);
  Function *F = new Function( FT, Function::ExternalLinkage, "sin", M );
  CallInst *CallExternal = new CallInst( F, SomeValue, "external_call", BB );

I can make some calls to functions like glclear() as I'm using opengl:
...
  Function *F = new Function( FT, Function::ExternalLinkage, "glclear", M );
...

But if I want to call any function inside a C++ namespace I don't find the way to do it. Imagine you want to call

othernamespace::globalFunction()

How should I do it?
I've tried all the linkage types, but it's clear that's not the solution.

And another question:
is it possible to call a function of an instance of a C++ class? Which are the limits of interaction between generated code and "precompiled" code (I mean any library for example)?
I want to generate code able to use functionality from c/c++ libraries...

like:
classinstance.memberFunction()

Thank you very much,

.alvaro.castro.

Hi Álvaro,

Google for C++ name mangling to understand why what you're attempting doesn't work. Looking at the output of llvm-gcc may help you get it right, but it's probably easier to write C wrappers for your C++ code.

— Gordon

Álvaro Castro wrote:

Hello!

I'm trying LLVM for generating code and I found something I cannot figure

out or find in the documentation:

I'm doing something like this to call "sin" from std:

  std::vector<const Type*> params;
  params.push_back( Type::FloatTy );
  FunctionType *FT = FunctionType::get( Type::FloatTy, params, false);
  Function *F = new Function( FT, Function::ExternalLinkage, "sin", M );
  CallInst *CallExternal = new CallInst( F, SomeValue, "external_call", BB );

I can make some calls to functions like glclear() as I'm using opengl:
...
  Function *F = new Function( FT, Function::ExternalLinkage, "glclear", M );
...

But if I want to call any function inside a C++ namespace I don't find the

way to do it. Imagine you want to call

othernamespace::globalFunction()

See this older message:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-December/011616.html

And another question:
is it possible to call a function of an instance of a C++ class? Which are

the limits of interaction between generated code and "precompiled" code (I
mean any library for example)?

I want to generate code able to use functionality from c/c++ libraries...

like:
classinstance.memberFunction()

This is... not simple. If you take the address of a member function you
don't actually get back a simple pointer, you get back a
member-pointer-to-function which has metadata about whether the function is
virtual or not and other details.

C++ member functions calls also use a special calling convention. See
http://en.wikipedia.org/wiki/Stdcall#thiscall for details. LLVM currently
does not support MSVC-style thiscall convention.

If the function you want to call is virtual, you can call it through a
function pointer by dereferencing the vtable. If the function is
non-virtual, you'll need to get it's address somehow (using the mangled
name, perhaps) and then call it using the thiscall convention.

- --BDS

- --

Benjamin Smedberg
Platform Guru
Mozilla Corporation
benjamin@smedbergs.us
http://benjamin.smedbergs.us/