https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128772

Uroš Bizjak <ubizjak at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org

--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
And here begins the fun with the flexible array member handling...

The following testcase with struct:

--cut here--
struct S { float x; int a[]; };

struct S f (float v) { struct S s = { v }; return s; }
--cut here--

compiles (-O2) for C to:

f:
        movss   %xmm0, -16(%rsp)
        movl    -16(%rsp), %eax
        movl    %eax, -12(%rsp)
        movss   -12(%rsp), %xmm0
        ret

where a psABI note is emitted:

note: the ABI of passing struct with a flexible array member has changed in GCC
4.4

For this struct, the existing flexible-array skip leaves classes[0] as SSE. The
%eax in the middle is only a temporary used to copy the float through the
stack, not the return location.

when compiled (-O2) for C++:

_Z1ff:
        movq    %rdi, %rax
        movss   %xmm0, (%rdi)
        ret

No psABI note is emitted. In C++ the same struct goes to MEMORY (%rdi). The two
outputs show that GCC C and GCC C++ classify this struct differently. The C++
a[] has no TYPE_DOMAIN, so the existing skip, which requires a domain with a
NULL max, never matches:

                      /* Flexible array member is ignored.  */
                      if (TYPE_MODE (type) == BLKmode
                          && TREE_CODE (type) == ARRAY_TYPE
                          && TYPE_SIZE (type) == NULL_TREE
                          && TYPE_DOMAIN (type) != NULL_TREE
                          && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
                              == NULL_TREE))

clang returns this struct in memory for both C and C++.

And similar testcase with union:

--cut here--
union U { float f; int a[]; };

union U f (float v) { union U u = { v }; return u; }
--cut here--

C and C++ compile (-O2) to:

f or _Z1ff:
        movq    %rdi, %rax
        movss   %xmm0, (%rdi)
        ret

No psABI note is emitted. The union loop has no skip for flexible array
members, so the union is returned in memory via a hidden pointer. MEMORY for a
union does not appear to be intentional, but clang does the same.

union { ...; int a[]; } is a GNU extension, since ISO C doesn’t allow a
flexible array member in a union.

Making C++ skip the flexible array member like C would make both GCC languages
return struct S in %xmm0, but it would change the C++ ABI and move it away from
clang, which returns this struct in memory. Since the psABI doesn’t say how a
flexible array member is classified, I’d like that settled there before
changing either language’s classification.

Reply via email to