[Haskell-cafe] Re: FFI and returning structs
donn at avvanta.com
Wed Nov 19 12:11:28 EST 2008
Quoth Maurcio <briqueabraque at yahoo.com>:
|> (...) Ironically, that's actually just what the original
|> function is doing - cf. "struct return convention", where the
|> caller allocates space and passes a pointer as a hidden
|> parameter. (...)
| Can I be sure things always goes like that? I remember reading
| somewhere that if, for instance, a struct is smaller in size than
| a pointer (like a struct made of three chars), the struct could be
| returned by itself.
I think you can be reasonably sure it doesn't always work that way!
But if you're interested, you can see what the compiler does with
that smaller struct, looking at the assembly code it generates.
I'm no Intel programmer, but if my test function uses a parameter, I
can see that the parameter is found at an offset, compared to the same
function returning a plain scalar value. So - yes, the struct value
is returned in a register - but the convention is also observed, with
the hidden parameter. Using gcc for Intel 80386 family CPU. Maybe
later I will see how it works on AMD64.
| Also, why Haskell FFI do not automate that? As long as it is
| Storable data, can't it get the pointer and 'peek' it for me?
I can't speak for the FFI authors, and note that I'm not even saying
that it can't do this. Your inquiry was rather short on details about
what happened, and anyway it's only convenient for me to try it with
NHC, which may not be the compiler you're using. But if it were up
to me, I doubt that I would support this (struct return), since it's
rare, at best it adds to the difficulty of supporting different hardware
platforms and compilers, and it's easy to work around.
More information about the Haskell-Cafe