[LLVMdev] Calling functions

Benjamin Smedberg benjamin at smedbergs.us
Fri Feb 22 13:36:40 PST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Á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 at smedbergs.us
http://benjamin.smedbergs.us/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHv0BoSSwGp5sTYNkRAuW6AKDYIiiEMZo7ZghN11qjAxHMY3fYlgCfe4QF
xTF0Tx/8808bsSgRu6SCkTs=
=ujKD
-----END PGP SIGNATURE-----



More information about the llvm-dev mailing list