[LLVMdev] Calling Conventions Cont'd

Duncan Sands baldrick at free.fr
Sat Apr 12 23:40:23 PDT 2008


Hi Jon,

> What is the correct procedure for translating a function signature from 
> a high-order language to LLVM?

you have to do it carefully so that LLVM will end up producing object code
that conforms to the platform ABI.  For example, suppose you are using cdecl
with a small struct (a pair of ints, say).  Then your function should get two
integer parameters, which LLVM will assign to registers.  If using a large
struct then use byval which will pass it on the stack.

I know this is painful, hopefully LLVM will get some helpers for this one
day.  The reason is that some ABIs make distinctions that don't exist at
the LLVM level.  For example at least one ABI says that "complex"
( { double, double } ) should be passed differently to a struct containing
a pair of doubles.

> It looks like I replace each  
> struct/array parameter with a 'byval' pointer parameter, and I replace a 
> result struct/array with an 'sret' pointer parameter.

No, it's not so easy, sorry.

> The reason I ask  
> is that each calling convention has subtle variations for each 
> architecture and platform.  For example, cdecl in Win32 returns small (8 
> bytes or less) structures in EAX or EAX:EDX; whereas, cdecl in Linux 
> always returns structures "in memory" (Source: 
> http://www.programmersheaven.com/2/Calling-conventions).  Does LLVM 
> handle all of these nuances for me?

No it doesn't.

Best wishes,

Duncan.



More information about the llvm-dev mailing list