[LLVMdev] Methodology for interfacing LLVM JITed code with C++
Chris Lattner
sabre at nondot.org
Wed Sep 12 13:06:20 PDT 2007
On Wed, 12 Sep 2007, Austin Robison wrote:
> and then compile this file with llvm-g++ to bitcode. At runtime, the
> JIT would load both our scripting code and this stub bitcode, and link
> everything together with the LTO. This hopefully alleviates the need to
> manually tweak name mangled symbols when generating the LLVM code for
> the scripting language and will automatically generate llvm type
> information for the C++ classes and types that live in the library.
Yep, that would work.
> Other than name-mangling, calling virtual functions presents a problem.
> It would seem that llvm-g++ is required to generate the vtable layouts
> for C++ classes so our generated LLVM code can grab the appropriate
> function pointer to make a call. But what is the right way to patch
> everything together so that our compiler can output LLVM code that can
> call these virtual C++ methods?
Unfortunately, LLVM doesn't give you a lot of help here. Since LLVM
doesn't know anything specifically about C++ class layout, it doesn't
insulate you from it or provide much help. The bigger issue is that C++
class/vtable layout is ABI specific. In practice, llvm-g++ and G++ both
use the itanium ABI for class layout which is very well specified and is
an open standard. This means that, if you want to have your frontend
generate calls through vtables, you will need your compiler to know the
rules of the itanium ABI for vtable layout.
One significant note though: MSVC++ does not use this ABI, so your code
will not work on windows. The only way that I know of to get C++ compiler
independence is to use "extern C" wrapper functions.
> And as a side question, how do the target triple and data layout of the
> module play into this? I assume they must match the C++ ABI
> definitions. llvm-config can get the target-triple, but what about the
> native C++ data layout? It seems like this use case would come up
> often, but I haven't been able to find any discussion or documentation
> related to it.
If you are interfacing with C or C++ code, the data-layout string should
match the target you are generating code for (because sometimes C/C++ code
can depend on this). The easiest way to get this is probably to invoke
llvm-gcc, compiling an empty file to a .ll file.
-Chris
--
http://nondot.org/sabre/
http://llvm.org/
More information about the llvm-dev
mailing list