aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/addrspace_and_linksection.zig
blob: 5af04595baa0005f47bd9c35e0cd1833f0c895b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const builtin = @import("builtin");

/// Elements are const_linksection, var_linksection, fn_linksection
const linksections: ?[3][]const u8 = switch (builtin.target.ofmt) {
    .elf => .{ ".rodata", ".data", ".text" },
    .coff => .{ ".rdata", ".data", ".text" },
    .macho => .{ "__TEXT,__const", "__DATA,__data", "__TEXT,__text" },
    else => null,
};
const const_linksection = linksections.?[0];
const var_linksection = linksections.?[1];
const fn_linksection = linksections.?[2];

test "addrspace on container-level const" {
    const S = struct {
        const a: u32 addrspace(.generic) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
    try comptime S.check(&S.a);
}

test "linksection on container-level const" {
    if (linksections == null) return;
    const S = struct {
        const a: u32 linksection(const_linksection) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
    try comptime S.check(&S.a);
}

test "addrspace and linksection on container-level const" {
    if (linksections == null) return;
    const S = struct {
        const a: u32 addrspace(.generic) linksection(const_linksection) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
    try comptime S.check(&S.a);
}

test "addrspace on container-level var" {
    const S = struct {
        var a: u32 addrspace(.generic) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
}

test "linksection on container-level var" {
    if (linksections == null) return;
    const S = struct {
        var a: u32 linksection(var_linksection) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
}

test "addrspace and linksection on container-level var" {
    if (linksections == null) return;
    const S = struct {
        var a: u32 addrspace(.generic) linksection(var_linksection) = 123;
        fn check(ptr: anytype) !void {
            if (ptr.* != 123) return error.TestFailed;
        }
    };
    try S.check(&S.a);
}

test "addrspace on fn" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        fn f() addrspace(.generic) u32 {
            return 123;
        }
        fn check(fnPtr: anytype) !void {
            if (fnPtr() != 123) return error.TestFailed;
        }
    };
    try S.check(&S.f);
    try comptime S.check(&S.f);
}

test "linksection on fn" {
    if (linksections == null) return;
    const S = struct {
        fn f() linksection(fn_linksection) u32 {
            return 123;
        }
        fn check(fnPtr: anytype) !void {
            if (fnPtr() != 123) return error.TestFailed;
        }
    };
    try S.check(&S.f);
    try comptime S.check(&S.f);
}

test "addrspace and linksection on fn" {
    if (linksections == null) return;
    const S = struct {
        fn f() addrspace(.generic) linksection(fn_linksection) u32 {
            return 123;
        }
        fn check(fnPtr: anytype) !void {
            if (fnPtr() != 123) return error.TestFailed;
        }
    };
    try S.check(&S.f);
    try comptime S.check(&S.f);
}