aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/asm.zig
blob: f856f0128ed15c2ec693eb1e4412885418754d22 (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
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;

const is_x86_64_linux = builtin.cpu.arch == .x86_64 and builtin.os.tag == .linux;

comptime {
    if (is_x86_64_linux) {
        asm (
            \\.globl this_is_my_alias;
            \\.type this_is_my_alias, @function;
            \\.set this_is_my_alias, derp;
        );
    }
}

test "module level assembly" {
    if (is_x86_64_linux) {
        try expect(this_is_my_alias() == 1234);
    }
}

test "output constraint modifiers" {
    // This is only testing compilation.
    var a: u32 = 3;
    asm volatile (""
        : [_] "=m,r" (a),
        :
        : ""
    );
    asm volatile (""
        : [_] "=r,m" (a),
        :
        : ""
    );
}

test "alternative constraints" {
    // Make sure we allow commas as a separator for alternative constraints.
    var a: u32 = 3;
    asm volatile (""
        : [_] "=r,m" (a),
        : [_] "r,m" (a),
        : ""
    );
}

test "sized integer/float in asm input" {
    asm volatile (""
        :
        : [_] "m" (@as(usize, 3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(i15, -3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(u3, 3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(i3, 3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(u121, 3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(i121, 3)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(f32, 3.17)),
        : ""
    );
    asm volatile (""
        :
        : [_] "m" (@as(f64, 3.17)),
        : ""
    );
}

test "struct/array/union types as input values" {
    asm volatile (""
        :
        : [_] "m" (@as([1]u32, undefined)),
    ); // fails
    asm volatile (""
        :
        : [_] "m" (@as(struct { x: u32, y: u8 }, undefined)),
    ); // fails
    asm volatile (""
        :
        : [_] "m" (@as(union { x: u32, y: u8 }, undefined)),
    ); // fails
}

extern fn this_is_my_alias() i32;

export fn derp() i32 {
    return 1234;
}