about summary refs log tree commit diff
path: root/tests/ui/asm/x86_64/goto.rs
blob: 00a8e588f9673a835b4e7b8156793caf0b668ffd (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//@ only-x86_64
//@ run-pass
//@ needs-asm-support

#![deny(unreachable_code)]
#![feature(asm_goto_with_outputs)]

use std::arch::asm;

fn goto_fallthough() {
    unsafe {
        asm!(
            "/* {} */",
            label {
                unreachable!();
            }
        )
    }
}

fn goto_jump() {
    unsafe {
        let mut value = false;
        asm!(
            "jmp {}",
            label {
                value = true;
            }
        );
        assert!(value);
    }
}

fn goto_out_fallthrough() {
    unsafe {
        let mut out: usize;
        asm!(
            "lea {}, [{} + 1]",
            "/* {} */",
            out(reg) out,
            in(reg) 0x12345678usize,
            label {
                unreachable!();
            }
        );
        assert_eq!(out, 0x12345679);
    }
}

fn goto_out_jump() {
    unsafe {
        let mut value = false;
        let mut out: usize;
        asm!(
            "lea {}, [{} + 1]",
            "jmp {}",
            out(reg) out,
            in(reg) 0x12345678usize,
            label {
                value = true;
                assert_eq!(out, 0x12345679);
            }
        );
        assert!(value);
    }
}

fn goto_out_jump_noreturn() {
    unsafe {
        let mut value = false;
        let mut out: usize;
        asm!(
            "lea {}, [{} + 1]",
            "jmp {}",
            out(reg) out,
            in(reg) 0x12345678usize,
            label {
                value = true;
                assert_eq!(out, 0x12345679);
            },
            options(noreturn)
        );
        assert!(value);
    }
}

// asm goto with outputs cause miscompilation in LLVM when multiple outputs are present.
// The code sample below is adapted from https://github.com/llvm/llvm-project/issues/74483
// and does not work with `-C opt-level=0`
#[expect(unused)]
fn goto_multi_out() {
    #[inline(never)]
    #[allow(unused)]
    fn goto_multi_out(a: usize, b: usize) -> usize {
        let mut x: usize;
        let mut y: usize;
        let mut z: usize;
        unsafe {
            core::arch::asm!(
                "mov {x}, {a}",
                "test {a}, {a}",
                "jnz {label1}",
                "/* {y} {z} {b} {label2} */",
                x = out(reg) x,
                y = out(reg) y,
                z = out(reg) z,
                a = in(reg) a,
                b = in(reg) b,
                label1 = label { return x },
                label2 = label { return 1 },
            );
            0
        }
    }

    assert_eq!(goto_multi_out(11, 22), 11);
}

fn goto_noreturn() {
    unsafe {
        let a;
        asm!(
            "jmp {}",
            label {
                a = 1;
            },
            options(noreturn)
        );
        assert_eq!(a, 1);
    }
}

#[warn(unreachable_code)]
fn goto_noreturn_diverge() {
    unsafe {
        asm!(
            "jmp {}",
            label {
                return;
            },
            options(noreturn)
        );
        unreachable!();
        //~^ WARN unreachable statement
    }
}

fn main() {
    goto_fallthough();
    goto_jump();
    goto_out_fallthrough();
    goto_out_jump();
    goto_out_jump_noreturn();
    // goto_multi_out();
    goto_noreturn();
    goto_noreturn_diverge();
}