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