about summary refs log tree commit diff
path: root/tests/ui/asm/cfg.rs
blob: bcf86340b9d8a2d1ade2c088eb596b3b7c73631c (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
// Check that `cfg` and `cfg_attr` work as expected.
//
//@ revisions: reva revb
//@ only-x86_64
//@ run-pass
#![feature(asm_cfg, cfg_select)]

use std::arch::{asm, naked_asm};

#[unsafe(naked)]
extern "C" fn ignore_const_operand() -> u64 {
    naked_asm!(
        "mov rax, 5",
        #[cfg(revb)]
        "mov rax, {a}",
        "ret",
        #[cfg(revb)]
        a = const 10,
    )
}

#[unsafe(naked)]
extern "C" fn ignore_const_operand_cfg_attr() -> u64 {
    naked_asm!(
        "mov rax, 5",
        #[cfg_attr(true, cfg(revb))]
        "mov rax, {a}",
        "ret",
        #[cfg_attr(true, cfg(revb))]
        a = const 10,
    )
}

#[unsafe(naked)]
extern "C" fn const_operand() -> u64 {
    naked_asm!(
        "mov rax, {a}",
        "ret",
        #[cfg(reva)]
        a = const 5,
        #[cfg(revb)]
        a = const 10,
    )
}

fn options() {
    // Without the cfg, this throws an error that the `att_syntax` option is provided twice.
    unsafe {
        asm!(
            "nop",
            #[cfg(false)]
            options(att_syntax),
            options(att_syntax)
        )
    }
}

fn clobber_abi() {
    // Without the cfg, this throws an error that the "C" abi is provided twice.
    unsafe {
        asm!(
            "nop",
            #[cfg(false)]
            clobber_abi("C"),
            clobber_abi("C"),
        );
    }
}

#[unsafe(naked)]
extern "C" fn first_template() -> u64 {
    naked_asm!(
        #[cfg(reva)]
        "mov rax, 5",
        #[cfg(revb)]
        "mov rax, 10",
        "ret",
    )
}

#[unsafe(naked)]
extern "C" fn true_and_false() -> u64 {
    naked_asm!(
        "mov rax, 5",
        #[cfg(true)]
        #[cfg(false)]
        "mov rax, 10",
        "ret",
    )
}

#[unsafe(naked)]
extern "C" fn false_and_true() -> u64 {
    naked_asm!(
        "mov rax, 5",
        #[cfg(false)]
        #[cfg(true)]
        "mov rax, 10",
        "ret",
    )
}

pub fn main() {
    std::cfg_select! {
        reva => {
            assert_eq!(const_operand(), 5);
            assert_eq!(ignore_const_operand_cfg_attr(), 5);
            assert_eq!(ignore_const_operand(), 5);
            assert_eq!(first_template(), 5);

        }
        revb => {
            assert_eq!(const_operand(), 10);
            assert_eq!(ignore_const_operand_cfg_attr(), 10);
            assert_eq!(ignore_const_operand(), 10);
            assert_eq!(first_template(), 10);

        }
    }
    options();
    clobber_abi();

    assert_eq!(true_and_false(), 5);
    assert_eq!(false_and_true(), 5);
}