diff options
| author | Cody Tapscott <topolarity@tapscott.me> | 2022-10-22 17:25:02 -0700 |
|---|---|---|
| committer | Cody Tapscott <topolarity@tapscott.me> | 2022-10-22 17:25:02 -0700 |
| commit | c96f85852ed2e1d5b2ecb43770a3c41d7f38f284 (patch) | |
| tree | 328b0365662d8027719d59735192c2d8dc818de1 /src | |
| parent | c50f33b1118f2e81597bd9ef5976fcb3eb961dee (diff) | |
| download | zig-c96f85852ed2e1d5b2ecb43770a3c41d7f38f284.tar.gz zig-c96f85852ed2e1d5b2ecb43770a3c41d7f38f284.zip | |
CType: Add `preferredAlignment`
This value corresponds to clang/gcc's `__alignof` (rather than
`_Alignof` which reports the minimum alignment). We don't use this
information yet, but it might be useful for implementing ABIs so it
is included here.
Diffstat (limited to 'src')
| -rw-r--r-- | src/type.zig | 128 |
1 files changed, 127 insertions, 1 deletions
diff --git a/src/type.zig b/src/type.zig index 51b326e18e..d78e2909b5 100644 --- a/src/type.zig +++ b/src/type.zig @@ -6926,7 +6926,7 @@ pub const CType = enum { else => {}, } - // Self-aligned, up to a maximum. + // Next-power-of-two-aligned, up to a maximum. return @min( std.math.ceilPowerOfTwoAssert(u16, (self.sizeInBits(target) + 7) / 8), switch (target.cpu.arch) { @@ -7013,4 +7013,130 @@ pub const CType = enum { }, ); } + + pub fn preferredAlignment(self: CType, target: Target) u16 { + + // Overrides for unusual alignments + switch (target.cpu.arch) { + .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) { + .netbsd => switch (target.abi) { + .gnueabi, + .gnueabihf, + .eabi, + .eabihf, + .android, + .musleabi, + .musleabihf, + => {}, + + else => switch (self) { + .longdouble => return 4, + else => {}, + }, + }, + .ios, .tvos, .watchos => switch (self) { + .longdouble => return 4, + else => {}, + }, + else => {}, + }, + .arc => switch (self) { + .longdouble => return 4, + else => {}, + }, + .avr => switch (self) { + .int, .uint, .long, .ulong, .float, .longdouble => return 1, + .short, .ushort => return 2, + .double => return 4, + .longlong, .ulonglong => return 8, + }, + .i386 => switch (target.os.tag) { + .windows, .uefi => switch (self) { + .longdouble => switch (target.abi) { + .gnu, .gnuilp32, .cygnus => return 4, + else => return 8, + }, + else => {}, + }, + else => switch (self) { + .longdouble => return 4, + else => {}, + }, + }, + else => {}, + } + + // Next-power-of-two-aligned, up to a maximum. + return @min( + std.math.ceilPowerOfTwoAssert(u16, (self.sizeInBits(target) + 7) / 8), + switch (target.cpu.arch) { + .msp430 => @as(u16, 2), + + .csky, + .xcore, + .dxil, + .loongarch32, + .tce, + .tcele, + .le32, + .amdil, + .hsail, + .spir, + .spirv32, + .kalimba, + .shave, + .renderscript32, + .ve, + .spu_2, + => 4, + + .arc, + .arm, + .armeb, + .avr, + .thumb, + .thumbeb, + .aarch64_32, + .amdgcn, + .amdil64, + .bpfel, + .bpfeb, + .hexagon, + .hsail64, + .i386, + .loongarch64, + .m68k, + .mips, + .mipsel, + .sparc, + .sparcel, + .sparc64, + .lanai, + .le64, + .nvptx, + .nvptx64, + .r600, + .s390x, + .spir64, + .spirv64, + .renderscript64, + => 8, + + .aarch64, + .aarch64_be, + .mips64, + .mips64el, + .powerpc, + .powerpcle, + .powerpc64, + .powerpc64le, + .riscv32, + .riscv64, + .x86_64, + .wasm32, + .wasm64, + => 16, + }, + ); + } }; |
