| 1 | const builtin = @import("builtin"); |
| 2 | |
| 3 | /// Elements are const_linksection, var_linksection, fn_linksection |
| 4 | const linksections: ?[3][]const u8 = switch (builtin.target.ofmt) { |
| 5 | .elf => .{ ".rodata", ".data", ".text" }, |
| 6 | .coff => .{ ".rdata", ".data", ".text" }, |
| 7 | .macho => .{ "__TEXT,__const", "__DATA,__data", "__TEXT,__text" }, |
| 8 | else => null, |
| 9 | }; |
| 10 | const const_linksection = linksections.?[0]; |
| 11 | const var_linksection = linksections.?[1]; |
| 12 | const fn_linksection = linksections.?[2]; |
| 13 | |
| 14 | test "addrspace on container-level const" { |
| 15 | const S = struct { |
| 16 | const a: u32 addrspace(.generic) = 123; |
| 17 | fn check(ptr: anytype) !void { |
| 18 | if (ptr.* != 123) return error.TestFailed; |
| 19 | } |
| 20 | }; |
| 21 | try S.check(&S.a); |
| 22 | try comptime S.check(&S.a); |
| 23 | } |
| 24 | |
| 25 | test "linksection on container-level const" { |
| 26 | if (linksections == null) return; |
| 27 | const S = struct { |
| 28 | const a: u32 linksection(const_linksection) = 123; |
| 29 | fn check(ptr: anytype) !void { |
| 30 | if (ptr.* != 123) return error.TestFailed; |
| 31 | } |
| 32 | }; |
| 33 | try S.check(&S.a); |
| 34 | try comptime S.check(&S.a); |
| 35 | } |
| 36 | |
| 37 | test "addrspace and linksection on container-level const" { |
| 38 | if (linksections == null) return; |
| 39 | const S = struct { |
| 40 | const a: u32 addrspace(.generic) linksection(const_linksection) = 123; |
| 41 | fn check(ptr: anytype) !void { |
| 42 | if (ptr.* != 123) return error.TestFailed; |
| 43 | } |
| 44 | }; |
| 45 | try S.check(&S.a); |
| 46 | try comptime S.check(&S.a); |
| 47 | } |
| 48 | |
| 49 | test "addrspace on container-level var" { |
| 50 | const S = struct { |
| 51 | var a: u32 addrspace(.generic) = 123; |
| 52 | fn check(ptr: anytype) !void { |
| 53 | if (ptr.* != 123) return error.TestFailed; |
| 54 | } |
| 55 | }; |
| 56 | try S.check(&S.a); |
| 57 | } |
| 58 | |
| 59 | test "linksection on container-level var" { |
| 60 | if (linksections == null) return; |
| 61 | const S = struct { |
| 62 | var a: u32 linksection(var_linksection) = 123; |
| 63 | fn check(ptr: anytype) !void { |
| 64 | if (ptr.* != 123) return error.TestFailed; |
| 65 | } |
| 66 | }; |
| 67 | try S.check(&S.a); |
| 68 | } |
| 69 | |
| 70 | test "addrspace and linksection on container-level var" { |
| 71 | if (linksections == null) return; |
| 72 | const S = struct { |
| 73 | var a: u32 addrspace(.generic) linksection(var_linksection) = 123; |
| 74 | fn check(ptr: anytype) !void { |
| 75 | if (ptr.* != 123) return error.TestFailed; |
| 76 | } |
| 77 | }; |
| 78 | try S.check(&S.a); |
| 79 | } |
| 80 | |
| 81 | test "addrspace on fn" { |
| 82 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 83 | |
| 84 | const S = struct { |
| 85 | fn f() addrspace(.generic) u32 { |
| 86 | return 123; |
| 87 | } |
| 88 | fn check(fnPtr: anytype) !void { |
| 89 | if (fnPtr() != 123) return error.TestFailed; |
| 90 | } |
| 91 | }; |
| 92 | try S.check(&S.f); |
| 93 | try comptime S.check(&S.f); |
| 94 | } |
| 95 | |
| 96 | test "linksection on fn" { |
| 97 | if (linksections == null) return; |
| 98 | const S = struct { |
| 99 | fn f() linksection(fn_linksection) u32 { |
| 100 | return 123; |
| 101 | } |
| 102 | fn check(fnPtr: anytype) !void { |
| 103 | if (fnPtr() != 123) return error.TestFailed; |
| 104 | } |
| 105 | }; |
| 106 | try S.check(&S.f); |
| 107 | try comptime S.check(&S.f); |
| 108 | } |
| 109 | |
| 110 | test "addrspace and linksection on fn" { |
| 111 | if (linksections == null) return; |
| 112 | const S = struct { |
| 113 | fn f() addrspace(.generic) linksection(fn_linksection) u32 { |
| 114 | return 123; |
| 115 | } |
| 116 | fn check(fnPtr: anytype) !void { |
| 117 | if (fnPtr() != 123) return error.TestFailed; |
| 118 | } |
| 119 | }; |
| 120 | try S.check(&S.f); |
| 121 | try comptime S.check(&S.f); |
| 122 | } |