April 15, 2013 Re: Bus error on 32bit OSX, not 64bit | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 2013-04-15 19:56, John Colvin wrote: > Casting a function that returns void to a function that returns > something seems bound to cause trouble. While it may be allowed in C It > seems a bit of a leap to assume that it would work properly in D. I don't know what to say, this is how you're supposed to call these "objc_msgSend" functions. They part of the Objective-C runtime. In C, if I call it like it's declare, returning void, I get a segfault: NSRect foo (id screen) { NSRect rect; ((void (*)(NSRect*, id, SEL))objc_msgSend_stret)(&rect, screen, sel_registerName("visibleFrame")); return rect; } Don't ask me why it works like this. -- /Jacob Carlborg |
April 16, 2013 Re: Bus error on 32bit OSX, not 64bit | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 2013-04-15 19:56, John Colvin wrote: > Casting a function that returns void to a function that returns > something seems bound to cause trouble. While it may be allowed in C It > seems a bit of a leap to assume that it would work properly in D. I've done some investigation about how the "objc_msgSend" family of functions work, including "objc_msgSend_stret". For those who don't know they're are part of the Objective-C runtime used for calling Objective-C methods. These functions are _not_ regular C functions, they're completely implemented using assembly. What they basically do is looking up the Objective-C method to call and just calls it (some caching is involved as well). What's special about this is that it won't mess with any resister or stack used for passing function arguments or sending back return values. What happens when it has found the correct Objective-C method is that it will just jump to the function. All registers and the stack are already correct, from the call to the objc_msgSend itself. I assume that is way the objc_msgSend method need to be cast to the correct signature before calling it. We don't want to use the ABI for variadic functions (as declared by objc_msgSend), we want the ABI used for the target function, that is, the Objective-C method. An Objective-C method is implement like a regular C function, following the ABI of the platform. It has to take at least these two parameters: void foo (id self, SEL _cmd); "self" would be the object we're calling the method on. "_cmd" is the selector (method) being called. Then what's objc_msgSend_stret used for. The objc_msgSend_stret function is used for returning structs that are too large to fit in the register. This is completely dependent on the platform ABI. I don't know if this helps. Some info about how objc_msgSend works: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/ Some info about why objc_msgSend_stret exists: http://www.sealiesoftware.com/blog/archive/2008/10/30/objc_explain_objc_msgSend_stret.html -- /Jacob Carlborg |
April 16, 2013 Re: Bus error on 32bit OSX, not 64bit | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 2013-04-14 16:48, Jacob Carlborg wrote: > This code: > > http://pastebin.com/U5XdFfDq > > Will result in a bus error when compiled as 32bit. Three ways the bus > error won't happen: > > * If I compile as 64bit everything works fine > * If the function "foo" is moved inline in the "main" function > everything works fine > * If store the return value of "fp" in "foo" in a temporary variable and > then return it > > DMD 2.062 > Mac OS X 10.8.2 > I think this problem is the same: http://forum.dlang.org/thread/kkk8kh$2jsu$1@digitalmars.com This is without all the casts and Objective-C runtime functions. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation