LLVM on 64-bit PowerPC Linux

Hi all,

I'm trying to use LLVM on a 64-bit PowerPC Linux box. This platform
has a weird way of dealing with function pointers which I don't think
LLVM has support for. Can anyone point me in the right direction so
I can start implementing it?

The problem is this. On most platforms, a function pointer in C is
the address of the entry point of the function. This is the case for
32-bit Linux PowerPC, and, from looking at the code in LLVM it seems
to be the case for both 32- and 64-bit Darwin. A call on these
platforms looks like this, assuming the function pointer is in rX,
and all arguments to the call have been put in the appropriate

  mtctr rX

Simple. On 64-bit Linux PowerPC, however, a function pointer in C
is the address of a function descriptor, a structure containing three

- The first pointer contains the address of the entry point of
   the function.
- The second pointer contains the TOC base address for the function.
- The third pointer contains the environment pointer (for languages
   that need one).

The TOC base address needs to be in r2, and the environment pointer
needs to be in r11. Starting with the C function pointer in rX, a
call on 64-bit Linux PowerPC looks something like this:

  ld rY, 0(rX) ; where rY is some free register
  mtctr rY
  std r2, 40(r1) ; optional
  ld r2, 8(rX)
  ld r11, 16(rX)
  ld r2, 40(r1) ; optional

The two lines marked "optional" could be omitted since LLVM doesn't
generate or use a TOC.

Any idea where I need to look to implement this?


You can support lowering a call any way you want for your target by
custom-lowering the CALL instruction in CodeGen. The relevant code is
PPCTargetLowering::LowerCALL in
llvm/lib/Target/PowerPC/PPCISelLowering.cpp. I'd suggest starting
there, and asking if there's anything you can't figure out.


btw, this PPC convention is not unique to 64-bit Linux; it is used on AIX 32-bit and I believe it was used on earlier versions of MacOSX (but is no longer). Neither of those is an immediate target but you might want to keep it in mind when deciding where to put the new code you're adding.