[LLVMdev] Using JIT code to code a program to call C++

Óscar Fuentes ofv at wanadoo.es
Wed Mar 14 17:51:11 PDT 2012


"Paul J. Lucas" <paul at lucasmail.org> writes:

[snip]

> I'm aware that LLVM doesn't know anything about C++ and that one way
> to call C++ functions is to wrap them in C thunks:

Yes, this is the only sane way unless you are willing to replicate the
ABI of your chosen C++ implementation onto your system. Let's forget
about dealing with more than one C++ ABI ;-)

[snip]

> I would think that, because it's a StructType, the alignment would be
> correct and the sizeof(item) would get the size right.  Will that
> work?

Yes. I do the same on my compiler. Please note that this trick is only
necessary if you pretend to put C++ objects on your stack. If all C++
objects your JITted code creates are on the heap, you can simply use
`new' instead of placement `new' in the C function that creates the C++
object and deal with the returned pointer..

> Is there a better way?

It all depends on the charateristics of your language.

[snip]

> but verifyModule() still complains at me because, apparently, there's
> no automatic casting to void* types in LLVM.  How can I solve this
> problem?

You must do the cast yourself, or simply use void* for all pointers for
all your C++ objects and deal with its real type on the upper part of
the compiler.

As said above, it all depends on the details of your language.




More information about the llvm-dev mailing list