]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/asm.rs
Auto merge of #91728 - Amanieu:stable_asm, r=joshtriplett
[rust.git] / compiler / rustc_codegen_gcc / tests / run / asm.rs
1 // Compiler:
2 //
3 // Run-time:
4 //   status: 0
5
6 global_asm!("
7     .global add_asm
8 add_asm:
9      mov rax, rdi
10      add rax, rsi
11      ret"
12 );
13
14 extern "C" {
15     fn add_asm(a: i64, b: i64) -> i64;
16 }
17
18 fn main() {
19     unsafe {
20         asm!("nop");
21     }
22
23     let x: u64;
24     unsafe {
25         asm!("mov $5, {}",
26             out(reg) x,
27             options(att_syntax)
28         );
29     }
30     assert_eq!(x, 5);
31
32     let x: u64;
33     let input: u64 = 42;
34     unsafe {
35         asm!("mov {input}, {output}",
36              "add $1, {output}",
37             input = in(reg) input,
38             output = out(reg) x,
39             options(att_syntax)
40         );
41     }
42     assert_eq!(x, 43);
43
44     let x: u64;
45     unsafe {
46         asm!("mov {}, 6",
47             out(reg) x,
48         );
49     }
50     assert_eq!(x, 6);
51
52     let x: u64;
53     let input: u64 = 42;
54     unsafe {
55         asm!("mov {output}, {input}",
56              "add {output}, 1",
57             input = in(reg) input,
58             output = out(reg) x,
59         );
60     }
61     assert_eq!(x, 43);
62
63     // check inout(reg_class) x 
64     let mut x: u64 = 42;
65     unsafe {
66         asm!("add {0}, {0}",
67             inout(reg) x 
68         );
69     }
70     assert_eq!(x, 84);
71
72     // check inout("reg") x
73     let mut x: u64 = 42;
74     unsafe {
75         asm!("add r11, r11",
76             inout("r11") x 
77         );
78     }
79     assert_eq!(x, 84);
80
81     // check a mix of
82     // in("reg")
83     // inout(class) x => y
84     // inout (class) x
85     let x: u64 = 702;
86     let y: u64 = 100;
87     let res: u64;
88     let mut rem: u64 = 0;
89     unsafe {
90         asm!("div r11",
91             in("r11") y,
92             inout("eax") x => res,
93             inout("edx") rem,
94         );
95     }
96     assert_eq!(res, 7);
97     assert_eq!(rem, 2);
98
99     // check const 
100     let mut x: u64 = 42;
101     unsafe {
102         asm!("add {}, {}",
103             inout(reg) x,
104             const 1 
105         );
106     }
107     assert_eq!(x, 43);
108
109     // check const (ATT syntax)
110     let mut x: u64 = 42;
111     unsafe {
112         asm!("add {}, {}",
113             const 1,
114             inout(reg) x,
115             options(att_syntax)
116         );
117     }
118     assert_eq!(x, 43);
119
120     // check sym fn
121     extern "C" fn foo() -> u64 { 42 }
122     let x: u64;
123     unsafe {
124         asm!("call {}", sym foo, lateout("rax") x);
125     }
126     assert_eq!(x, 42);
127
128     // check sym fn (ATT syntax)
129     let x: u64;
130     unsafe {
131         asm!("call {}", sym foo, lateout("rax") x, options(att_syntax));
132     }
133     assert_eq!(x, 42);
134
135     // check sym static
136     static FOO: u64 = 42;
137     let x: u64;
138     unsafe {
139         asm!("mov {1}, qword ptr [rip + {0}]", sym FOO, lateout(reg) x);
140     }
141     assert_eq!(x, 42);
142
143     // check sym static (ATT syntax)
144     let x: u64;
145     unsafe {
146         asm!("movq {0}(%rip), {1}", sym FOO, lateout(reg) x, options(att_syntax));
147     }
148     assert_eq!(x, 42);
149
150     assert_eq!(unsafe { add_asm(40, 2) }, 42);
151 }