FFI/StructReturnsReturning C structs by value on different platforms. This was transcribed from the Factor FFI implementation and may be incorrect. References:
x86-32On x86-32, we distinguish between "small structs" and "large structs". On Linux, NetBSD and Solaris, all structs are large structs except C99 On Mac OS X, Windows, FreeBSD and OpenBSD, all structs are large structs except those that are precisely 1, 2, 4 or 8 bytes in size. Note that a struct that's, for instance, 3 bytes in size is considered a large struct. Small structsSmall structs are returned in EAX:EDX, with the first 4 bytes in EAX and the second 4 bytes in EDX. If the struct is smaller than 4 bytes in size, EDX is undefined. Large structsLarge structs are handled as follows. The struct-returning function actually has a hidden first parameter which must be a pointer to a memory area large enough to store the returned struct. This memory area must be supplied by the caller. The EAX and EDX registers are unused in this case. If the struct-returning function uses the stdcall calling convention (Windows only), then it behaves exactly like a void-returning function with an extra first parameter. However, on non-Windows platforms where the C calling convention is used, the struct-returning function returns with the stack pointer incremented by 4 bytes. So after calling such a function, if you care about the value of ESP, you must decrement ESP by 4 bytes, by pushing a dummy value, for instance. x86-64On x86-64, the situation is similar; there are small structs and large structs. Large structsLarge structs are handed in a similar way to x86-32, where the caller passes a pointer to a memory area large enough to hold the struct as the first parameter. Unlike x86-32, there is no need to fix up RSP afterward. Small structsOn Unix, a small struct is one that is 16 bytes in size or smaller. On Windows, a small struct is one that is precisely 1, 2, 4, 8 bytes in size. All other structs are large structs. Small structs are "flattened" into a pair of C types, where each element of each pair is either
On Unix, structs are flattened by looking at the first and second 8 bytes of the struct. If a component consists entirely of floating point data (either a single Eg, the following struct, struct foo { int x; float y; double z; }; flattens to On Windows, all small structs flatten into Once the small struct has been flattened, we can compute which registers it will be returned in:
Note that if a component contains two So in the above PowerPC 32-bitOn PowerPC 32-bit, all structs are returned by the caller passing a pointer to a memory area as the first parameter, except for C99 PowerPC 64-bitFactor does not run on PowerPC 64-bit. This revision created on Tue, 21 Jul 2009 01:48:31 by slava |
|
|
All content is © 2008-2010 by its respective authors. By adding content to this wiki, you agree to release it under the BSD license. |
|