authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-08-03 08:05:00+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:37:10-07:00
log88fb4dab81ab67b66600868f9a72892ed77bbb21
tree80a8c9772f26a54509f3b3834f1f21d82f1eb03e
parent22662773b2cb0942d5036110eb8db34b25c84c40

std.target: mark helper functions inline

This was discussed in #16597. It makes sense for most of the functions in this file to be marked inline: many are simple helper functions so inlining is likely a strict win, and having the return values be comptime-known may improve userspace code in some cases by preventing it from unintentionally checking properties of the target at runtime. This changeset is somewhat conservative: the functions marked inline are generally returning booleans or simple integers, and many are simple one-line checks.

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

lib/std/target.zig+52-52
...@@ -59,14 +59,14 @@ pub const Target = struct {...@@ -59,14 +59,14 @@ pub const Target = struct {
59 plan9,59 plan9,
60 other,60 other,
6161
62 pub fn isDarwin(tag: Tag) bool {62 pub inline fn isDarwin(tag: Tag) bool {
63 return switch (tag) {63 return switch (tag) {
64 .ios, .macos, .watchos, .tvos => true,64 .ios, .macos, .watchos, .tvos => true,
65 else => false,65 else => false,
66 };66 };
67 }67 }
6868
69 pub fn isBSD(tag: Tag) bool {69 pub inline fn isBSD(tag: Tag) bool {
70 return tag.isDarwin() or switch (tag) {70 return tag.isDarwin() or switch (tag) {
71 .kfreebsd, .freebsd, .openbsd, .netbsd, .dragonfly => true,71 .kfreebsd, .freebsd, .openbsd, .netbsd, .dragonfly => true,
72 else => false,72 else => false,
...@@ -135,7 +135,7 @@ pub const Target = struct {...@@ -135,7 +135,7 @@ pub const Target = struct {
135 };135 };
136136
137 /// Returns whether the first version `self` is newer (greater) than or equal to the second version `ver`.137 /// Returns whether the first version `self` is newer (greater) than or equal to the second version `ver`.
138 pub fn isAtLeast(self: WindowsVersion, ver: WindowsVersion) bool {138 pub inline fn isAtLeast(self: WindowsVersion, ver: WindowsVersion) bool {
139 return @intFromEnum(self) >= @intFromEnum(ver);139 return @intFromEnum(self) >= @intFromEnum(ver);
140 }140 }
141141
...@@ -143,13 +143,13 @@ pub const Target = struct {...@@ -143,13 +143,13 @@ pub const Target = struct {
143 min: WindowsVersion,143 min: WindowsVersion,
144 max: WindowsVersion,144 max: WindowsVersion,
145145
146 pub fn includesVersion(self: Range, ver: WindowsVersion) bool {146 pub inline fn includesVersion(self: Range, ver: WindowsVersion) bool {
147 return @intFromEnum(ver) >= @intFromEnum(self.min) and @intFromEnum(ver) <= @intFromEnum(self.max);147 return @intFromEnum(ver) >= @intFromEnum(self.min) and @intFromEnum(ver) <= @intFromEnum(self.max);
148 }148 }
149149
150 /// Checks if system is guaranteed to be at least `version` or older than `version`.150 /// Checks if system is guaranteed to be at least `version` or older than `version`.
151 /// Returns `null` if a runtime check is required.151 /// Returns `null` if a runtime check is required.
152 pub fn isAtLeast(self: Range, ver: WindowsVersion) ?bool {152 pub inline fn isAtLeast(self: Range, ver: WindowsVersion) ?bool {
153 if (@intFromEnum(self.min) >= @intFromEnum(ver)) return true;153 if (@intFromEnum(self.min) >= @intFromEnum(ver)) return true;
154 if (@intFromEnum(self.max) < @intFromEnum(ver)) return false;154 if (@intFromEnum(self.max) < @intFromEnum(ver)) return false;
155 return null;155 return null;
...@@ -187,13 +187,13 @@ pub const Target = struct {...@@ -187,13 +187,13 @@ pub const Target = struct {
187 range: Version.Range,187 range: Version.Range,
188 glibc: Version,188 glibc: Version,
189189
190 pub fn includesVersion(self: LinuxVersionRange, ver: Version) bool {190 pub inline fn includesVersion(self: LinuxVersionRange, ver: Version) bool {
191 return self.range.includesVersion(ver);191 return self.range.includesVersion(ver);
192 }192 }
193193
194 /// Checks if system is guaranteed to be at least `version` or older than `version`.194 /// Checks if system is guaranteed to be at least `version` or older than `version`.
195 /// Returns `null` if a runtime check is required.195 /// Returns `null` if a runtime check is required.
196 pub fn isAtLeast(self: LinuxVersionRange, ver: Version) ?bool {196 pub inline fn isAtLeast(self: LinuxVersionRange, ver: Version) ?bool {
197 return self.range.isAtLeast(ver);197 return self.range.isAtLeast(ver);
198 }198 }
199 };199 };
...@@ -360,7 +360,7 @@ pub const Target = struct {...@@ -360,7 +360,7 @@ pub const Target = struct {
360360
361 /// Provides a tagged union. `Target` does not store the tag because it is361 /// Provides a tagged union. `Target` does not store the tag because it is
362 /// redundant with the OS tag; this function abstracts that part away.362 /// redundant with the OS tag; this function abstracts that part away.
363 pub fn getVersionRange(self: Os) TaggedVersionRange {363 pub inline fn getVersionRange(self: Os) TaggedVersionRange {
364 switch (self.tag) {364 switch (self.tag) {
365 .linux => return TaggedVersionRange{ .linux = self.version_range.linux },365 .linux => return TaggedVersionRange{ .linux = self.version_range.linux },
366 .windows => return TaggedVersionRange{ .windows = self.version_range.windows },366 .windows => return TaggedVersionRange{ .windows = self.version_range.windows },
...@@ -382,7 +382,7 @@ pub const Target = struct {...@@ -382,7 +382,7 @@ pub const Target = struct {
382382
383 /// Checks if system is guaranteed to be at least `version` or older than `version`.383 /// Checks if system is guaranteed to be at least `version` or older than `version`.
384 /// Returns `null` if a runtime check is required.384 /// Returns `null` if a runtime check is required.
385 pub fn isAtLeast(self: Os, comptime tag: Tag, version: anytype) ?bool {385 pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: anytype) ?bool {
386 if (self.tag != tag) return false;386 if (self.tag != tag) return false;
387387
388 return switch (tag) {388 return switch (tag) {
...@@ -395,7 +395,7 @@ pub const Target = struct {...@@ -395,7 +395,7 @@ pub const Target = struct {
395 /// On Darwin, we always link libSystem which contains libc.395 /// On Darwin, we always link libSystem which contains libc.
396 /// Similarly on FreeBSD and NetBSD we always link system libc396 /// Similarly on FreeBSD and NetBSD we always link system libc
397 /// since this is the stable syscall interface.397 /// since this is the stable syscall interface.
398 pub fn requiresLibC(os: Os) bool {398 pub inline fn requiresLibC(os: Os) bool {
399 return switch (os.tag) {399 return switch (os.tag) {
400 .freebsd,400 .freebsd,
401 .netbsd,401 .netbsd,
...@@ -569,21 +569,21 @@ pub const Target = struct {...@@ -569,21 +569,21 @@ pub const Target = struct {
569 }569 }
570 }570 }
571571
572 pub fn isGnu(abi: Abi) bool {572 pub inline fn isGnu(abi: Abi) bool {
573 return switch (abi) {573 return switch (abi) {
574 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,574 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
575 else => false,575 else => false,
576 };576 };
577 }577 }
578578
579 pub fn isMusl(abi: Abi) bool {579 pub inline fn isMusl(abi: Abi) bool {
580 return switch (abi) {580 return switch (abi) {
581 .musl, .musleabi, .musleabihf => true,581 .musl, .musleabi, .musleabihf => true,
582 else => false,582 else => false,
583 };583 };
584 }584 }
585585
586 pub fn floatAbi(abi: Abi) FloatAbi {586 pub inline fn floatAbi(abi: Abi) FloatAbi {
587 return switch (abi) {587 return switch (abi) {
588 .gnueabihf,588 .gnueabihf,
589 .eabihf,589 .eabihf,
...@@ -871,95 +871,95 @@ pub const Target = struct {...@@ -871,95 +871,95 @@ pub const Target = struct {
871 // map one-to-one with the ZigLLVM_ArchType enum.871 // map one-to-one with the ZigLLVM_ArchType enum.
872 spu_2,872 spu_2,
873873
874 pub fn isX86(arch: Arch) bool {874 pub inline fn isX86(arch: Arch) bool {
875 return switch (arch) {875 return switch (arch) {
876 .x86, .x86_64 => true,876 .x86, .x86_64 => true,
877 else => false,877 else => false,
878 };878 };
879 }879 }
880880
881 pub fn isARM(arch: Arch) bool {881 pub inline fn isARM(arch: Arch) bool {
882 return switch (arch) {882 return switch (arch) {
883 .arm, .armeb => true,883 .arm, .armeb => true,
884 else => false,884 else => false,
885 };885 };
886 }886 }
887887
888 pub fn isAARCH64(arch: Arch) bool {888 pub inline fn isAARCH64(arch: Arch) bool {
889 return switch (arch) {889 return switch (arch) {
890 .aarch64, .aarch64_be, .aarch64_32 => true,890 .aarch64, .aarch64_be, .aarch64_32 => true,
891 else => false,891 else => false,
892 };892 };
893 }893 }
894894
895 pub fn isThumb(arch: Arch) bool {895 pub inline fn isThumb(arch: Arch) bool {
896 return switch (arch) {896 return switch (arch) {
897 .thumb, .thumbeb => true,897 .thumb, .thumbeb => true,
898 else => false,898 else => false,
899 };899 };
900 }900 }
901901
902 pub fn isArmOrThumb(arch: Arch) bool {902 pub inline fn isArmOrThumb(arch: Arch) bool {
903 return arch.isARM() or arch.isThumb();903 return arch.isARM() or arch.isThumb();
904 }904 }
905905
906 pub fn isWasm(arch: Arch) bool {906 pub inline fn isWasm(arch: Arch) bool {
907 return switch (arch) {907 return switch (arch) {
908 .wasm32, .wasm64 => true,908 .wasm32, .wasm64 => true,
909 else => false,909 else => false,
910 };910 };
911 }911 }
912912
913 pub fn isRISCV(arch: Arch) bool {913 pub inline fn isRISCV(arch: Arch) bool {
914 return switch (arch) {914 return switch (arch) {
915 .riscv32, .riscv64 => true,915 .riscv32, .riscv64 => true,
916 else => false,916 else => false,
917 };917 };
918 }918 }
919919
920 pub fn isMIPS(arch: Arch) bool {920 pub inline fn isMIPS(arch: Arch) bool {
921 return switch (arch) {921 return switch (arch) {
922 .mips, .mipsel, .mips64, .mips64el => true,922 .mips, .mipsel, .mips64, .mips64el => true,
923 else => false,923 else => false,
924 };924 };
925 }925 }
926926
927 pub fn isPPC(arch: Arch) bool {927 pub inline fn isPPC(arch: Arch) bool {
928 return switch (arch) {928 return switch (arch) {
929 .powerpc, .powerpcle => true,929 .powerpc, .powerpcle => true,
930 else => false,930 else => false,
931 };931 };
932 }932 }
933933
934 pub fn isPPC64(arch: Arch) bool {934 pub inline fn isPPC64(arch: Arch) bool {
935 return switch (arch) {935 return switch (arch) {
936 .powerpc64, .powerpc64le => true,936 .powerpc64, .powerpc64le => true,
937 else => false,937 else => false,
938 };938 };
939 }939 }
940940
941 pub fn isSPARC(arch: Arch) bool {941 pub inline fn isSPARC(arch: Arch) bool {
942 return switch (arch) {942 return switch (arch) {
943 .sparc, .sparcel, .sparc64 => true,943 .sparc, .sparcel, .sparc64 => true,
944 else => false,944 else => false,
945 };945 };
946 }946 }
947947
948 pub fn isSpirV(arch: Arch) bool {948 pub inline fn isSpirV(arch: Arch) bool {
949 return switch (arch) {949 return switch (arch) {
950 .spirv32, .spirv64 => true,950 .spirv32, .spirv64 => true,
951 else => false,951 else => false,
952 };952 };
953 }953 }
954954
955 pub fn isBpf(arch: Arch) bool {955 pub inline fn isBpf(arch: Arch) bool {
956 return switch (arch) {956 return switch (arch) {
957 .bpfel, .bpfeb => true,957 .bpfel, .bpfeb => true,
958 else => false,958 else => false,
959 };959 };
960 }960 }
961961
962 pub fn isNvptx(arch: Arch) bool {962 pub inline fn isNvptx(arch: Arch) bool {
963 return switch (arch) {963 return switch (arch) {
964 .nvptx, .nvptx64 => true,964 .nvptx, .nvptx64 => true,
965 else => false,965 else => false,
...@@ -975,7 +975,7 @@ pub const Target = struct {...@@ -975,7 +975,7 @@ pub const Target = struct {
975 return error.UnknownCpuModel;975 return error.UnknownCpuModel;
976 }976 }
977977
978 pub fn toElfMachine(arch: Arch) std.elf.EM {978 pub inline fn toElfMachine(arch: Arch) std.elf.EM {
979 return switch (arch) {979 return switch (arch) {
980 .avr => .AVR,980 .avr => .AVR,
981 .msp430 => .MSP430,981 .msp430 => .MSP430,
...@@ -1040,7 +1040,7 @@ pub const Target = struct {...@@ -1040,7 +1040,7 @@ pub const Target = struct {
1040 };1040 };
1041 }1041 }
10421042
1043 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {1043 pub inline fn toCoffMachine(arch: Arch) std.coff.MachineType {
1044 return switch (arch) {1044 return switch (arch) {
1045 .avr => .Unknown,1045 .avr => .Unknown,
1046 .msp430 => .Unknown,1046 .msp430 => .Unknown,
...@@ -1105,7 +1105,7 @@ pub const Target = struct {...@@ -1105,7 +1105,7 @@ pub const Target = struct {
1105 };1105 };
1106 }1106 }
11071107
1108 pub fn endian(arch: Arch) std.builtin.Endian {1108 pub inline fn endian(arch: Arch) std.builtin.Endian {
1109 return switch (arch) {1109 return switch (arch) {
1110 .avr,1110 .avr,
1111 .arm,1111 .arm,
...@@ -1176,7 +1176,7 @@ pub const Target = struct {...@@ -1176,7 +1176,7 @@ pub const Target = struct {
1176 }1176 }
11771177
1178 /// Returns whether this architecture supports the address space1178 /// Returns whether this architecture supports the address space
1179 pub fn supportsAddressSpace(arch: Arch, address_space: std.builtin.AddressSpace) bool {1179 pub inline fn supportsAddressSpace(arch: Arch, address_space: std.builtin.AddressSpace) bool {
1180 const is_nvptx = arch == .nvptx or arch == .nvptx64;1180 const is_nvptx = arch == .nvptx or arch == .nvptx64;
1181 const is_spirv = arch == .spirv32 or arch == .spirv64;1181 const is_spirv = arch == .spirv32 or arch == .spirv64;
1182 const is_gpu = is_nvptx or is_spirv or arch == .amdgcn;1182 const is_gpu = is_nvptx or is_spirv or arch == .amdgcn;
...@@ -1418,51 +1418,51 @@ pub const Target = struct {...@@ -1418,51 +1418,51 @@ pub const Target = struct {
1418 return libPrefix_os_abi(self.os.tag, self.abi);1418 return libPrefix_os_abi(self.os.tag, self.abi);
1419 }1419 }
14201420
1421 pub fn isMinGW(self: Target) bool {1421 pub inline fn isMinGW(self: Target) bool {
1422 return self.os.tag == .windows and self.isGnu();1422 return self.os.tag == .windows and self.isGnu();
1423 }1423 }
14241424
1425 pub fn isGnu(self: Target) bool {1425 pub inline fn isGnu(self: Target) bool {
1426 return self.abi.isGnu();1426 return self.abi.isGnu();
1427 }1427 }
14281428
1429 pub fn isMusl(self: Target) bool {1429 pub inline fn isMusl(self: Target) bool {
1430 return self.abi.isMusl();1430 return self.abi.isMusl();
1431 }1431 }
14321432
1433 pub fn isAndroid(self: Target) bool {1433 pub inline fn isAndroid(self: Target) bool {
1434 return self.abi == .android;1434 return self.abi == .android;
1435 }1435 }
14361436
1437 pub fn isWasm(self: Target) bool {1437 pub inline fn isWasm(self: Target) bool {
1438 return self.cpu.arch.isWasm();1438 return self.cpu.arch.isWasm();
1439 }1439 }
14401440
1441 pub fn isDarwin(self: Target) bool {1441 pub inline fn isDarwin(self: Target) bool {
1442 return self.os.tag.isDarwin();1442 return self.os.tag.isDarwin();
1443 }1443 }
14441444
1445 pub fn isBSD(self: Target) bool {1445 pub inline fn isBSD(self: Target) bool {
1446 return self.os.tag.isBSD();1446 return self.os.tag.isBSD();
1447 }1447 }
14481448
1449 pub fn isBpfFreestanding(self: Target) bool {1449 pub inline fn isBpfFreestanding(self: Target) bool {
1450 return self.cpu.arch.isBpf() and self.os.tag == .freestanding;1450 return self.cpu.arch.isBpf() and self.os.tag == .freestanding;
1451 }1451 }
14521452
1453 pub fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {1453 pub inline fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {
1454 return os_tag == .linux and abi.isGnu();1454 return os_tag == .linux and abi.isGnu();
1455 }1455 }
14561456
1457 pub fn isGnuLibC(self: Target) bool {1457 pub inline fn isGnuLibC(self: Target) bool {
1458 return isGnuLibC_os_tag_abi(self.os.tag, self.abi);1458 return isGnuLibC_os_tag_abi(self.os.tag, self.abi);
1459 }1459 }
14601460
1461 pub fn supportsNewStackCall(self: Target) bool {1461 pub inline fn supportsNewStackCall(self: Target) bool {
1462 return !self.cpu.arch.isWasm();1462 return !self.cpu.arch.isWasm();
1463 }1463 }
14641464
1465 pub fn isSpirV(self: Target) bool {1465 pub inline fn isSpirV(self: Target) bool {
1466 return self.cpu.arch.isSpirV();1466 return self.cpu.arch.isSpirV();
1467 }1467 }
14681468
...@@ -1472,11 +1472,11 @@ pub const Target = struct {...@@ -1472,11 +1472,11 @@ pub const Target = struct {
1472 soft_fp,1472 soft_fp,
1473 };1473 };
14741474
1475 pub fn getFloatAbi(self: Target) FloatAbi {1475 pub inline fn getFloatAbi(self: Target) FloatAbi {
1476 return self.abi.floatAbi();1476 return self.abi.floatAbi();
1477 }1477 }
14781478
1479 pub fn hasDynamicLinker(self: Target) bool {1479 pub inline fn hasDynamicLinker(self: Target) bool {
1480 if (self.cpu.arch.isWasm()) {1480 if (self.cpu.arch.isWasm()) {
1481 return false;1481 return false;
1482 }1482 }
...@@ -1832,7 +1832,7 @@ pub const Target = struct {...@@ -1832,7 +1832,7 @@ pub const Target = struct {
1832 };1832 };
1833 }1833 }
18341834
1835 pub fn ptrBitWidth(target: Target) u16 {1835 pub inline fn ptrBitWidth(target: Target) u16 {
1836 switch (target.abi) {1836 switch (target.abi) {
1837 .gnux32, .muslx32, .gnuabin32, .gnuilp32 => return 32,1837 .gnux32, .muslx32, .gnuabin32, .gnuilp32 => return 32,
1838 .gnuabi64 => return 64,1838 .gnuabi64 => return 64,
...@@ -1909,7 +1909,7 @@ pub const Target = struct {...@@ -1909,7 +1909,7 @@ pub const Target = struct {
1909 }1909 }
1910 }1910 }
19111911
1912 pub fn stackAlignment(target: Target) u16 {1912 pub inline fn stackAlignment(target: Target) u16 {
1913 return switch (target.cpu.arch) {1913 return switch (target.cpu.arch) {
1914 .m68k => 2,1914 .m68k => 2,
1915 .amdgcn => 4,1915 .amdgcn => 4,
...@@ -1954,7 +1954,7 @@ pub const Target = struct {...@@ -1954,7 +1954,7 @@ pub const Target = struct {
1954 /// Default signedness of `char` for the native C compiler for this target1954 /// Default signedness of `char` for the native C compiler for this target
1955 /// Note that char signedness is implementation-defined and many compilers provide1955 /// Note that char signedness is implementation-defined and many compilers provide
1956 /// an option to override the default signedness e.g. GCC's -funsigned-char / -fsigned-char1956 /// an option to override the default signedness e.g. GCC's -funsigned-char / -fsigned-char
1957 pub fn charSignedness(target: Target) std.builtin.Signedness {1957 pub inline fn charSignedness(target: Target) std.builtin.Signedness {
1958 switch (target.cpu.arch) {1958 switch (target.cpu.arch) {
1959 .aarch64,1959 .aarch64,
1960 .aarch64_32,1960 .aarch64_32,
...@@ -1993,7 +1993,7 @@ pub const Target = struct {...@@ -1993,7 +1993,7 @@ pub const Target = struct {
1993 longdouble,1993 longdouble,
1994 };1994 };
19951995
1996 pub fn c_type_byte_size(t: Target, c_type: CType) u16 {1996 pub inline fn c_type_byte_size(t: Target, c_type: CType) u16 {
1997 return switch (c_type) {1997 return switch (c_type) {
1998 .char,1998 .char,
1999 .short,1999 .short,
...@@ -2019,7 +2019,7 @@ pub const Target = struct {...@@ -2019,7 +2019,7 @@ pub const Target = struct {
2019 };2019 };
2020 }2020 }
20212021
2022 pub fn c_type_bit_size(target: Target, c_type: CType) u16 {2022 pub inline fn c_type_bit_size(target: Target, c_type: CType) u16 {
2023 switch (target.os.tag) {2023 switch (target.os.tag) {
2024 .freestanding, .other => switch (target.cpu.arch) {2024 .freestanding, .other => switch (target.cpu.arch) {
2025 .msp430 => switch (c_type) {2025 .msp430 => switch (c_type) {
...@@ -2333,7 +2333,7 @@ pub const Target = struct {...@@ -2333,7 +2333,7 @@ pub const Target = struct {
2333 }2333 }
2334 }2334 }
23352335
2336 pub fn c_type_alignment(target: Target, c_type: CType) u16 {2336 pub inline fn c_type_alignment(target: Target, c_type: CType) u16 {
2337 // Overrides for unusual alignments2337 // Overrides for unusual alignments
2338 switch (target.cpu.arch) {2338 switch (target.cpu.arch) {
2339 .avr => return 1,2339 .avr => return 1,
...@@ -2440,7 +2440,7 @@ pub const Target = struct {...@@ -2440,7 +2440,7 @@ pub const Target = struct {
2440 );2440 );
2441 }2441 }
24422442
2443 pub fn c_type_preferred_alignment(target: Target, c_type: CType) u16 {2443 pub inline fn c_type_preferred_alignment(target: Target, c_type: CType) u16 {
2444 // Overrides for unusual alignments2444 // Overrides for unusual alignments
2445 switch (target.cpu.arch) {2445 switch (target.cpu.arch) {
2446 .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) {2446 .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) {