On 1/9/26 12:57, grischka wrote:
On 09.01.2026 08:04, Herman ten Brugge via Tinycc-devel wrote:
While debugging some i386 code I found some memory alignment differences.

1) The double and long long type differ inside and outside struct.

outside struct (size = 8, align = 8):
double a;
long long b;

inside struct (size = 8 align = 4)
struct {
    double a;
    long long b;
};

This only happens on x86 (and perhaps on !TCC_ARM_EABI which I cannot test).

I cannot reproduce this.  Alignment is 4 in both cases
I tried again with gcc4/gcc15/gcc16/clang21/clang22 with program below.
All with -m32 option:

#include <stdio.h>

double a;
long long b;
struct {
  double a;
  long long b;
} c;

int
main(void)
{
   printf("%u %u\n", __alignof__(a), __alignof__(b));
   printf("%u %u\n", __alignof__(c.a), __alignof__(c.b));
}

The output is always:
8 8
4 4

I do not know what compiler you used. But gcc/clang agree.

2) The memory alignment of a struct/array depends on its size.

The memory alignment is 4 for a, 8 for b, 16 for c and 32 for d and e.
int a[1];
int b[2];
int c[4];
int d[8];
int e[16];

I cannot reproduce this either. Alignment is 4 in all cases.

using gcc -S and then grep .align results in:
        .align 4
        .align 8
        .align 16
        .align 32
        .align 32
I make a mistake. This only happens on x86_64.
I really don't care about this and do not
need this patch. I noticed this while checking
alignments.

You can check this alignment with compiling above code with gcc -S
and check assemby code for .align/.zero/.space depending on
platform (x86/x86_64/arm/arm64/riscv).
As far as I can tell this happens for all data. So also structs
get this alignment depending on size.

See patch that fixes both problems + testcase update.

The compiler can align objects as it likes as long as it satisfies
the ABI.

GCC may have its priorities, but for TCC the number one priority
is to stay as simple and fast as possible.

I still think we should fix the i386 alignment.
I cannot test if the arm code uses the same alignment.
Perhaps the #if in type_size should be changed.

We can write the patch in another way.
Return 8 for alignment for double and long long and
then check in struct_layout for i386 if alignment
is > 4 and then change it to 4.
This might be a little bit faster.

    Herman


_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to