authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-22 17:25:02-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-22 17:25:02-07:00
logc96f85852ed2e1d5b2ecb43770a3c41d7f38f284
tree328b0365662d8027719d59735192c2d8dc818de1
parentc50f33b1118f2e81597bd9ef5976fcb3eb961dee

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.

1 files changed, 127 insertions(+), 1 deletions(-)

src/type.zig+127-1
...@@ -6926,7 +6926,7 @@ pub const CType = enum {...@@ -6926,7 +6926,7 @@ pub const CType = enum {
6926 else => {},6926 else => {},
6927 }6927 }
69286928
6929 // Self-aligned, up to a maximum.6929 // Next-power-of-two-aligned, up to a maximum.
6930 return @min(6930 return @min(
6931 std.math.ceilPowerOfTwoAssert(u16, (self.sizeInBits(target) + 7) / 8),6931 std.math.ceilPowerOfTwoAssert(u16, (self.sizeInBits(target) + 7) / 8),
6932 switch (target.cpu.arch) {6932 switch (target.cpu.arch) {
...@@ -7013,4 +7013,130 @@ pub const CType = enum {...@@ -7013,4 +7013,130 @@ pub const CType = enum {
7013 },7013 },
7014 );7014 );
7015 }7015 }
7016
7017 pub fn preferredAlignment(self: CType, target: Target) u16 {
7018
7019 // Overrides for unusual alignments
7020 switch (target.cpu.arch) {
7021 .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) {
7022 .netbsd => switch (target.abi) {
7023 .gnueabi,
7024 .gnueabihf,
7025 .eabi,
7026 .eabihf,
7027 .android,
7028 .musleabi,
7029 .musleabihf,
7030 => {},
7031
7032 else => switch (self) {
7033 .longdouble => return 4,
7034 else => {},
7035 },
7036 },
7037 .ios, .tvos, .watchos => switch (self) {
7038 .longdouble => return 4,
7039 else => {},
7040 },
7041 else => {},
7042 },
7043 .arc => switch (self) {
7044 .longdouble => return 4,
7045 else => {},
7046 },
7047 .avr => switch (self) {
7048 .int, .uint, .long, .ulong, .float, .longdouble => return 1,
7049 .short, .ushort => return 2,
7050 .double => return 4,
7051 .longlong, .ulonglong => return 8,
7052 },
7053 .i386 => switch (target.os.tag) {
7054 .windows, .uefi => switch (self) {
7055 .longdouble => switch (target.abi) {
7056 .gnu, .gnuilp32, .cygnus => return 4,
7057 else => return 8,
7058 },
7059 else => {},
7060 },
7061 else => switch (self) {
7062 .longdouble => return 4,
7063 else => {},
7064 },
7065 },
7066 else => {},
7067 }
7068
7069 // Next-power-of-two-aligned, up to a maximum.
7070 return @min(
7071 std.math.ceilPowerOfTwoAssert(u16, (self.sizeInBits(target) + 7) / 8),
7072 switch (target.cpu.arch) {
7073 .msp430 => @as(u16, 2),
7074
7075 .csky,
7076 .xcore,
7077 .dxil,
7078 .loongarch32,
7079 .tce,
7080 .tcele,
7081 .le32,
7082 .amdil,
7083 .hsail,
7084 .spir,
7085 .spirv32,
7086 .kalimba,
7087 .shave,
7088 .renderscript32,
7089 .ve,
7090 .spu_2,
7091 => 4,
7092
7093 .arc,
7094 .arm,
7095 .armeb,
7096 .avr,
7097 .thumb,
7098 .thumbeb,
7099 .aarch64_32,
7100 .amdgcn,
7101 .amdil64,
7102 .bpfel,
7103 .bpfeb,
7104 .hexagon,
7105 .hsail64,
7106 .i386,
7107 .loongarch64,
7108 .m68k,
7109 .mips,
7110 .mipsel,
7111 .sparc,
7112 .sparcel,
7113 .sparc64,
7114 .lanai,
7115 .le64,
7116 .nvptx,
7117 .nvptx64,
7118 .r600,
7119 .s390x,
7120 .spir64,
7121 .spirv64,
7122 .renderscript64,
7123 => 8,
7124
7125 .aarch64,
7126 .aarch64_be,
7127 .mips64,
7128 .mips64el,
7129 .powerpc,
7130 .powerpcle,
7131 .powerpc64,
7132 .powerpc64le,
7133 .riscv32,
7134 .riscv64,
7135 .x86_64,
7136 .wasm32,
7137 .wasm64,
7138 => 16,
7139 },
7140 );
7141 }
7016};7142};