Skip navigation.
 
mlRe: Returning values from objc_msgSend etc
FROM : Greg Parker
DATE : Sat Mar 01 14:00:07 2008

On Mar 1, 2008, at 1:22 AM, Sherm Pendley wrote:
> On Fri, Feb 29, 2008 at 2:46 PM, Greg Parker <<email_removed>> 
> wrote:
>
> When in doubt, write Objective-C code that returns the type you want,
> compile it, and use whichever function the generated assembly code
> chose.
>
> What's your advice about what to do when the choice must be made at 
> runtime? I'm using libffi. Does it know about the implicit first 
> argument for struct returns? That is, do I take the moral equivalent 
> of the suggestion in objc/objc-runtime.h, call objc_msgSend_stret() 
> using a struct return type and leave the argument shuffling for ffi 
> to handle? Or, do I call it as declared, with a void return type, 
> setting up the return pointer argument myself and disregarding the 
> comment in the header file?


libffi is smart enough to know about all of the ABI rules. If your 
ffi_types correctly describe the method's parameters, then libffi will 
marshal them properly. (If it doesn't, then that's a libffi bug.)

However, you still need to choose the correct objc_msgSend variant. 
libffi will correctly set up the implicit struct return argument or 
not as required, but there's no way for you to know whether it's going 
to do that or not. So if your C structure is returned in registers, 
then libffi will handle it correctly but you'll crash if you use 
objc_msgSend_stret instead of objc_msgSend.

The right way to fix this is to add new API in libobjc: given an 
ffi_type, return the correct objc_msgSend function pointer for that 
return type. I filed the feature request for that today. Until then 
you'll still need to handle that choice yourself. All of the required 
code is in libffi somewhere, but there's no way for you to call it 
directly. libffi is open source, so you could try digging through the 
implementation and copying the code you want into your program.


> Also, do you know what, if any, type promotions are relevant, and 
> whether libffi automatically handles them?


I don't think libffi does any type promotion.


--
Greg Parker    <email_removed>    Runtime Wrangler

Related mailsAuthorDate
mlReturning values from objc_msgSend etc Duncan McGregor Feb 29, 20:05
mlRe: Returning values from objc_msgSend etc Greg Parker Feb 29, 20:46
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 1, 10:22
mlRe: Returning values from objc_msgSend etc Greg Parker Mar 1, 14:00
mlRe: Returning values from objc_msgSend etc Bill Bumgarner Mar 1, 18:30
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 2, 00:22
mlRe: Returning values from objc_msgSend etc Duncan McGregor Mar 3, 11:52
mlRe: Returning values from objc_msgSend etc Ronald Oussoren Mar 3, 12:10
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 3, 12:54
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 31, 17:19
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 31, 17:34
mlRe: Returning values from objc_msgSend etc Jean-Daniel Dupas Mar 31, 17:54
mlRe: Returning values from objc_msgSend etc Greg Parker Mar 31, 19:03
mlRe: Returning values from objc_msgSend etc Sherm Pendley Mar 31, 23:19