[Haskell-cafe] Re: Could FFI support pass-by-value of structs?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Jun 29 20:18:32 EDT 2009

On Tue, 2009-06-30 at 00:45 +0400, Bulat Ziganshin wrote:
> Hello Duncan,
> Tuesday, June 30, 2009, 12:03:15 AM, you wrote:
> > struct ex {
> >      int x;
> >      int y;
> >      int z;
> > };
> > ex example_functions (ex p)
> afaik, there is C ABI, that defines how to pass and return parameters
> of simple types, it's common for all compilers supporting so-called
> "cdecl" on any architecture (x86, x86-64, ppc, so on). C ABI doesn't
> define how to pass structures, so we can't generate universal
> assembler code for this example - it depends on what compiler we want
> to interact to

Actually passing structs and unions as arguments or function results is
specified by the C ABI. See for example the IA32 ABI:


linked from the LSB:

See the section Function Calling Sequence, 3-9. Specifically:
  3-14: Functions Returning Structures or Unions
  3-18: Structure and Union Arguments

On IA32 structs/unions passed as parameters go by value on the stack.
For structs/unions as function results, they are stored into a
caller-allocated area on the stack, pointed to by a "hidden" first arg.

It's different on each arch, but it's all completely specified.

> moreover, even for pointers-to-structures, there is no definitive
> layout due to packing problem

Actually the padding is also specified by the C ABI. See section 3-3.
Compiler pragmas to use packed layout are not ABI conformant (and so
typically are only used internally).


More information about the Haskell-Cafe mailing list