How to determine the alignment arguments on the stack of struct or union for argument passing?

4 views
Skip to first unread message

chen liu

unread,
Apr 2, 2020, 11:15:07 PM4/2/20
to IA32 System V Application Binary Interface
The gcc version I use is 7.3.0 and the option is -m32.

For example:

#include <immintrin.h>

typedef __attribute__((aligned(16)))

int alignedint;
struct __attribute__((aligned(64))) X
{ int x;
 
// __m128 y;
 
// alignedint y;
};
void g(int x, struct X);
_Static_assert
(_Alignof(struct X) == 64);
struct X gx;

void f() { g(1, gx); }
In this case, the gx  will align to 4 byte. If we uncomment either the __m128 or alignedint y, and  gcc aligns to 64 bytes. However, we cann't find any document about this
calling-conventions.
The logic of gcc alignment we now think is as below: 

StackAlignmentForType(T):

  1. If T's alignment is < 16 bytes(any type, including struct and union), return 4.
  2. If T is a struct/union/array type, then:                                           (A)recursively calculate on each member's type ( ignores any attribute((aligned(N))) directly on the fields of a struct, but not those that appear on typedefs, or the underlying types?).                                                                     (B)If all of those calls return alignments < 16, then return 4.
  3. Otherwise, return the alignment of T.
Is this logic correct for gcc?

Thanks.
Reply all
Reply to author
Forward
0 new messages