How to implement an IMP function that returns a large structure type defined at runtime?

Reference Information. CamelBones registers Perl classes with Objective-C runtime. To do this, each Perl method is registered with the same IMP function; this function checks its self and _cmd arguments to find which Perl method to call.

This worked quite well for several years for messages that were sent using objc_msgSend . But now I want to add support for returning floating-point types and large structures from Perl methods. Floating point is not difficult; I will just write another IMP that double returns to handle messages sent using objc_msgSend_fpret .

The question is what to do with objc_msgSend_stret . Writing a separate IMP for each possible return type is impractical, for two reasons: firstly, because even if I only did this for the struct types that are known at compile time, this is an absurd amount of functions. Secondly, since we are talking about a structure that can be associated with any arbitrary Objective-C and Perl code, we do not know all the potential structure types when compiling a frame.

What I hope to do is write one IMP that can handle any return type sent via objc_msgSend_stret . Can I write it like returning a void and taking a pointer argument into the return buffer, for example the old objc_msgSend_stret been declared? Even if this happened to work at the moment, can I rely on it while continuing to work in the future?

Thanks for any advice - I'm already puzzling over this. :-)

Update:

Here is the advice I received from one of the Apple engineers at runtime on my objc mailing list:

You must write assembly code to handle this case.

Your suggestion does not work on some architectures, where the ABI for a function returns a void with a pointer to struct as the first argument โ€œdiffers from theโ€œ function returning the structure. โ€ This is why the prototype for objc_msgSend_stret been changed.

The assembly code will capture the return address of the structure, smuggle into it a non-struct-return C function call without violating the parameters, and then perform the correct ABI-specific cleanup upon exit ( ret $4 on i386). Alternatively, assembly code can capture all parameters. Transportation equipment does something similar. This code can be in open source CoreFoundation if you want to see what methods look.

I will leave this question open, in case someone brainstorms better, but if it comes directly from Apple, the โ€œrun-time violator,โ€ I think this is probably an authoritative answer, as Iโ€™m most likely I will receive. Time trying to get rid of the x86 reference manuals and knock rust off my assembler-fu, I think ...

+59
objective-c objective-c-runtime perl
Mar 29 '11 at 17:55
source share
1 answer

The Apple engineer seems to be right: the only way is to build code. Here are some helpful tips to get you started:

  • From Objective-C runtime code: i386 and x86_64 hand-processed instant messengers for various messaging methods.
  • Answer "Answer" , which provides an overview of scheduling.
  • In-depth review of dispatch mechanism with phased analysis of assembly code

Hope this helps.

+5
May 23 '11 at 7:48 a.m.
source share
โ€” -



All Articles