thepowersgang / mrustc

Alternative rust compiler (re-implementation)
MIT License
2.11k stars 106 forks source link

Alignment and sizes #83

Closed daym closed 2 years ago

daym commented 5 years ago

This issue collects the alignment of struct members on gcc.

The following program has been run:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>

struct foo {
        unsigned char a;
        uint16_t b;
};

int main() {
        struct foo f;
        printf("b_offset = %zu\n", offsetof(struct foo, b));
        printf("b_size = %zu\n", sizeof(f.b));
        return 0;
}

On armhf, this results in: b_offset = 2, b_size = 2.

Then I modified it accordingly to get:

armhf (both gcc 7.3.0 and gcc 5.4.0):

Type         Alignment/Bytes  Size/Bytes
int8_t       1                1
int16_t      2                2
int32_t      4                4
int64_t      8                8
__int128     not supported
uint8_t      1                1
uint16_t     2                2
uint32_t     4                4
uint64_t     8                8
float        4                4
double       8                8
long double  8                8
size_t       4                4
off_t        4                4
void*        4                4

x86_64 (gcc 7.3.0):

Type         Alignment/Bytes  Size/Bytes
int8_t       1                1
int16_t      2                2
int32_t      4                4
int64_t      8                8
__int128     16               16
uint8_t      1                1
uint16_t     2                2
uint32_t     4                4
uint64_t     8                8
float        4                4
double       8                8
long double  16               16
size_t       8                8
off_t        8                8
void*        8                8

i686 (gcc 7.3.0):

Type         Alignment/Bytes  Size/Bytes
int8_t       1                1
int16_t      2                2
int32_t      4                4
int64_t      4                8
__int128     not supported
uint8_t      1                1
uint16_t     2                2
uint32_t     4                4
uint64_t     4                8
float        4                4
double       4                8
long double  4                12
size_t       4                4
off_t        4                4
void*        4                4
glaubitz commented 5 years ago

m68k (gcc 8.2.0):

Type         Alignment/Bytes  Size/Bytes
int8_t       1                1
int16_t      2                2
int32_t      2                4
int64_t      2                8
__int128     not supported
uint8_t      1                1
uint16_t     2                2
uint32_t     2                4
uint64_t     2                8
float        2                4
double       2                8
long double  2                12
size_t       2                4
off_t        2                4
void*        2                4
eddyp commented 5 years ago

Does it make sense to have the alignments and sizes for other compilers than gcc/llvm?

daym commented 5 years ago

I don't know, you tell me. Though if the libraries these compilers create interoperate with libraries other compilers create then those alignments and sizes have to be equal between compilers anyway.

glaubitz commented 5 years ago

Alignment sizes are dictated by the ABI which is usually independent of the compiler used.

See for example: https://wiki.osdev.org/System_V_ABI

eddyp commented 5 years ago

@glaubitz sure, but offsets are, AFAICT, implementation specific in the sense that there's no restriction to put a uint16_t in the first 2 bytes or the last ones in a struct where the uint16_t is before a uint32_t.

glaubitz commented 5 years ago

But wouldn't that make any code compiled with a different compiler ABI-incompatible with standard Linux environments, for example?

eddyp commented 5 years ago

It would. But I mostly thinking of bare metal code and embedded systems where the problem is not that obvious and using the same compiler is the go to solution.

glaubitz commented 5 years ago

Okay, but bare-metal code doesn't use the SysV ABI anyway, so we're talking about different things then.

thepowersgang commented 2 years ago

There are now assertions in the generated C code to ensure that the size+alignment match what mrustc expected.