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 {
5959 plan9,
6060 other,
6161
62 pub fn isDarwin(tag: Tag) bool {
62 pub inline fn isDarwin(tag: Tag) bool {
6363 return switch (tag) {
6464 .ios, .macos, .watchos, .tvos => true,
6565 else => false,
6666 };
6767 }
6868
69 pub fn isBSD(tag: Tag) bool {
69 pub inline fn isBSD(tag: Tag) bool {
7070 return tag.isDarwin() or switch (tag) {
7171 .kfreebsd, .freebsd, .openbsd, .netbsd, .dragonfly => true,
7272 else => false,
......@@ -135,7 +135,7 @@ pub const Target = struct {
135135 };
136136
137137 /// 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 {
139139 return @intFromEnum(self) >= @intFromEnum(ver);
140140 }
141141
......@@ -143,13 +143,13 @@ pub const Target = struct {
143143 min: WindowsVersion,
144144 max: WindowsVersion,
145145
146 pub fn includesVersion(self: Range, ver: WindowsVersion) bool {
146 pub inline fn includesVersion(self: Range, ver: WindowsVersion) bool {
147147 return @intFromEnum(ver) >= @intFromEnum(self.min) and @intFromEnum(ver) <= @intFromEnum(self.max);
148148 }
149149
150150 /// Checks if system is guaranteed to be at least `version` or older than `version`.
151151 /// 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 {
153153 if (@intFromEnum(self.min) >= @intFromEnum(ver)) return true;
154154 if (@intFromEnum(self.max) < @intFromEnum(ver)) return false;
155155 return null;
......@@ -187,13 +187,13 @@ pub const Target = struct {
187187 range: Version.Range,
188188 glibc: Version,
189189
190 pub fn includesVersion(self: LinuxVersionRange, ver: Version) bool {
190 pub inline fn includesVersion(self: LinuxVersionRange, ver: Version) bool {
191191 return self.range.includesVersion(ver);
192192 }
193193
194194 /// Checks if system is guaranteed to be at least `version` or older than `version`.
195195 /// 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 {
197197 return self.range.isAtLeast(ver);
198198 }
199199 };
......@@ -360,7 +360,7 @@ pub const Target = struct {
360360
361361 /// Provides a tagged union. `Target` does not store the tag because it is
362362 /// 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 {
364364 switch (self.tag) {
365365 .linux => return TaggedVersionRange{ .linux = self.version_range.linux },
366366 .windows => return TaggedVersionRange{ .windows = self.version_range.windows },
......@@ -382,7 +382,7 @@ pub const Target = struct {
382382
383383 /// Checks if system is guaranteed to be at least `version` or older than `version`.
384384 /// 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 {
386386 if (self.tag != tag) return false;
387387
388388 return switch (tag) {
......@@ -395,7 +395,7 @@ pub const Target = struct {
395395 /// On Darwin, we always link libSystem which contains libc.
396396 /// Similarly on FreeBSD and NetBSD we always link system libc
397397 /// since this is the stable syscall interface.
398 pub fn requiresLibC(os: Os) bool {
398 pub inline fn requiresLibC(os: Os) bool {
399399 return switch (os.tag) {
400400 .freebsd,
401401 .netbsd,
......@@ -569,21 +569,21 @@ pub const Target = struct {
569569 }
570570 }
571571
572 pub fn isGnu(abi: Abi) bool {
572 pub inline fn isGnu(abi: Abi) bool {
573573 return switch (abi) {
574574 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
575575 else => false,
576576 };
577577 }
578578
579 pub fn isMusl(abi: Abi) bool {
579 pub inline fn isMusl(abi: Abi) bool {
580580 return switch (abi) {
581581 .musl, .musleabi, .musleabihf => true,
582582 else => false,
583583 };
584584 }
585585
586 pub fn floatAbi(abi: Abi) FloatAbi {
586 pub inline fn floatAbi(abi: Abi) FloatAbi {
587587 return switch (abi) {
588588 .gnueabihf,
589589 .eabihf,
......@@ -871,95 +871,95 @@ pub const Target = struct {
871871 // map one-to-one with the ZigLLVM_ArchType enum.
872872 spu_2,
873873
874 pub fn isX86(arch: Arch) bool {
874 pub inline fn isX86(arch: Arch) bool {
875875 return switch (arch) {
876876 .x86, .x86_64 => true,
877877 else => false,
878878 };
879879 }
880880
881 pub fn isARM(arch: Arch) bool {
881 pub inline fn isARM(arch: Arch) bool {
882882 return switch (arch) {
883883 .arm, .armeb => true,
884884 else => false,
885885 };
886886 }
887887
888 pub fn isAARCH64(arch: Arch) bool {
888 pub inline fn isAARCH64(arch: Arch) bool {
889889 return switch (arch) {
890890 .aarch64, .aarch64_be, .aarch64_32 => true,
891891 else => false,
892892 };
893893 }
894894
895 pub fn isThumb(arch: Arch) bool {
895 pub inline fn isThumb(arch: Arch) bool {
896896 return switch (arch) {
897897 .thumb, .thumbeb => true,
898898 else => false,
899899 };
900900 }
901901
902 pub fn isArmOrThumb(arch: Arch) bool {
902 pub inline fn isArmOrThumb(arch: Arch) bool {
903903 return arch.isARM() or arch.isThumb();
904904 }
905905
906 pub fn isWasm(arch: Arch) bool {
906 pub inline fn isWasm(arch: Arch) bool {
907907 return switch (arch) {
908908 .wasm32, .wasm64 => true,
909909 else => false,
910910 };
911911 }
912912
913 pub fn isRISCV(arch: Arch) bool {
913 pub inline fn isRISCV(arch: Arch) bool {
914914 return switch (arch) {
915915 .riscv32, .riscv64 => true,
916916 else => false,
917917 };
918918 }
919919
920 pub fn isMIPS(arch: Arch) bool {
920 pub inline fn isMIPS(arch: Arch) bool {
921921 return switch (arch) {
922922 .mips, .mipsel, .mips64, .mips64el => true,
923923 else => false,
924924 };
925925 }
926926
927 pub fn isPPC(arch: Arch) bool {
927 pub inline fn isPPC(arch: Arch) bool {
928928 return switch (arch) {
929929 .powerpc, .powerpcle => true,
930930 else => false,
931931 };
932932 }
933933
934 pub fn isPPC64(arch: Arch) bool {
934 pub inline fn isPPC64(arch: Arch) bool {
935935 return switch (arch) {
936936 .powerpc64, .powerpc64le => true,
937937 else => false,
938938 };
939939 }
940940
941 pub fn isSPARC(arch: Arch) bool {
941 pub inline fn isSPARC(arch: Arch) bool {
942942 return switch (arch) {
943943 .sparc, .sparcel, .sparc64 => true,
944944 else => false,
945945 };
946946 }
947947
948 pub fn isSpirV(arch: Arch) bool {
948 pub inline fn isSpirV(arch: Arch) bool {
949949 return switch (arch) {
950950 .spirv32, .spirv64 => true,
951951 else => false,
952952 };
953953 }
954954
955 pub fn isBpf(arch: Arch) bool {
955 pub inline fn isBpf(arch: Arch) bool {
956956 return switch (arch) {
957957 .bpfel, .bpfeb => true,
958958 else => false,
959959 };
960960 }
961961
962 pub fn isNvptx(arch: Arch) bool {
962 pub inline fn isNvptx(arch: Arch) bool {
963963 return switch (arch) {
964964 .nvptx, .nvptx64 => true,
965965 else => false,
......@@ -975,7 +975,7 @@ pub const Target = struct {
975975 return error.UnknownCpuModel;
976976 }
977977
978 pub fn toElfMachine(arch: Arch) std.elf.EM {
978 pub inline fn toElfMachine(arch: Arch) std.elf.EM {
979979 return switch (arch) {
980980 .avr => .AVR,
981981 .msp430 => .MSP430,
......@@ -1040,7 +1040,7 @@ pub const Target = struct {
10401040 };
10411041 }
10421042
1043 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {
1043 pub inline fn toCoffMachine(arch: Arch) std.coff.MachineType {
10441044 return switch (arch) {
10451045 .avr => .Unknown,
10461046 .msp430 => .Unknown,
......@@ -1105,7 +1105,7 @@ pub const Target = struct {
11051105 };
11061106 }
11071107
1108 pub fn endian(arch: Arch) std.builtin.Endian {
1108 pub inline fn endian(arch: Arch) std.builtin.Endian {
11091109 return switch (arch) {
11101110 .avr,
11111111 .arm,
......@@ -1176,7 +1176,7 @@ pub const Target = struct {
11761176 }
11771177
11781178 /// 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 {
11801180 const is_nvptx = arch == .nvptx or arch == .nvptx64;
11811181 const is_spirv = arch == .spirv32 or arch == .spirv64;
11821182 const is_gpu = is_nvptx or is_spirv or arch == .amdgcn;
......@@ -1418,51 +1418,51 @@ pub const Target = struct {
14181418 return libPrefix_os_abi(self.os.tag, self.abi);
14191419 }
14201420
1421 pub fn isMinGW(self: Target) bool {
1421 pub inline fn isMinGW(self: Target) bool {
14221422 return self.os.tag == .windows and self.isGnu();
14231423 }
14241424
1425 pub fn isGnu(self: Target) bool {
1425 pub inline fn isGnu(self: Target) bool {
14261426 return self.abi.isGnu();
14271427 }
14281428
1429 pub fn isMusl(self: Target) bool {
1429 pub inline fn isMusl(self: Target) bool {
14301430 return self.abi.isMusl();
14311431 }
14321432
1433 pub fn isAndroid(self: Target) bool {
1433 pub inline fn isAndroid(self: Target) bool {
14341434 return self.abi == .android;
14351435 }
14361436
1437 pub fn isWasm(self: Target) bool {
1437 pub inline fn isWasm(self: Target) bool {
14381438 return self.cpu.arch.isWasm();
14391439 }
14401440
1441 pub fn isDarwin(self: Target) bool {
1441 pub inline fn isDarwin(self: Target) bool {
14421442 return self.os.tag.isDarwin();
14431443 }
14441444
1445 pub fn isBSD(self: Target) bool {
1445 pub inline fn isBSD(self: Target) bool {
14461446 return self.os.tag.isBSD();
14471447 }
14481448
1449 pub fn isBpfFreestanding(self: Target) bool {
1449 pub inline fn isBpfFreestanding(self: Target) bool {
14501450 return self.cpu.arch.isBpf() and self.os.tag == .freestanding;
14511451 }
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 {
14541454 return os_tag == .linux and abi.isGnu();
14551455 }
14561456
1457 pub fn isGnuLibC(self: Target) bool {
1457 pub inline fn isGnuLibC(self: Target) bool {
14581458 return isGnuLibC_os_tag_abi(self.os.tag, self.abi);
14591459 }
14601460
1461 pub fn supportsNewStackCall(self: Target) bool {
1461 pub inline fn supportsNewStackCall(self: Target) bool {
14621462 return !self.cpu.arch.isWasm();
14631463 }
14641464
1465 pub fn isSpirV(self: Target) bool {
1465 pub inline fn isSpirV(self: Target) bool {
14661466 return self.cpu.arch.isSpirV();
14671467 }
14681468
......@@ -1472,11 +1472,11 @@ pub const Target = struct {
14721472 soft_fp,
14731473 };
14741474
1475 pub fn getFloatAbi(self: Target) FloatAbi {
1475 pub inline fn getFloatAbi(self: Target) FloatAbi {
14761476 return self.abi.floatAbi();
14771477 }
14781478
1479 pub fn hasDynamicLinker(self: Target) bool {
1479 pub inline fn hasDynamicLinker(self: Target) bool {
14801480 if (self.cpu.arch.isWasm()) {
14811481 return false;
14821482 }
......@@ -1832,7 +1832,7 @@ pub const Target = struct {
18321832 };
18331833 }
18341834
1835 pub fn ptrBitWidth(target: Target) u16 {
1835 pub inline fn ptrBitWidth(target: Target) u16 {
18361836 switch (target.abi) {
18371837 .gnux32, .muslx32, .gnuabin32, .gnuilp32 => return 32,
18381838 .gnuabi64 => return 64,
......@@ -1909,7 +1909,7 @@ pub const Target = struct {
19091909 }
19101910 }
19111911
1912 pub fn stackAlignment(target: Target) u16 {
1912 pub inline fn stackAlignment(target: Target) u16 {
19131913 return switch (target.cpu.arch) {
19141914 .m68k => 2,
19151915 .amdgcn => 4,
......@@ -1954,7 +1954,7 @@ pub const Target = struct {
19541954 /// Default signedness of `char` for the native C compiler for this target
19551955 /// Note that char signedness is implementation-defined and many compilers provide
19561956 /// 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 {
19581958 switch (target.cpu.arch) {
19591959 .aarch64,
19601960 .aarch64_32,
......@@ -1993,7 +1993,7 @@ pub const Target = struct {
19931993 longdouble,
19941994 };
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 {
19971997 return switch (c_type) {
19981998 .char,
19991999 .short,
......@@ -2019,7 +2019,7 @@ pub const Target = struct {
20192019 };
20202020 }
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 {
20232023 switch (target.os.tag) {
20242024 .freestanding, .other => switch (target.cpu.arch) {
20252025 .msp430 => switch (c_type) {
......@@ -2333,7 +2333,7 @@ pub const Target = struct {
23332333 }
23342334 }
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 {
23372337 // Overrides for unusual alignments
23382338 switch (target.cpu.arch) {
23392339 .avr => return 1,
......@@ -2440,7 +2440,7 @@ pub const Target = struct {
24402440 );
24412441 }
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 {
24442444 // Overrides for unusual alignments
24452445 switch (target.cpu.arch) {
24462446 .arm, .armeb, .thumb, .thumbeb => switch (target.os.tag) {