Skip to content

Commit 130acba

Browse files
authored
Merge pull request #410 from Amanieu/asm
Replace llvm_asm! with asm!
2 parents d40d61d + f06e6d1 commit 130acba

File tree

5 files changed

+194
-199
lines changed

5 files changed

+194
-199
lines changed

examples/intrinsics.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![allow(unused_features)]
77
#![cfg_attr(thumb, no_main)]
88
#![deny(dead_code)]
9-
#![feature(llvm_asm)]
9+
#![feature(test)]
1010
#![feature(lang_items)]
1111
#![feature(start)]
1212
#![feature(allocator_api)]
@@ -276,14 +276,9 @@ mod intrinsics {
276276
}
277277

278278
fn run() {
279+
use core::hint::black_box as bb;
279280
use intrinsics::*;
280281

281-
// A copy of "test::black_box". Used to prevent LLVM from optimizing away the intrinsics during LTO
282-
fn bb<T>(dummy: T) -> T {
283-
unsafe { llvm_asm!("" : : "r"(&dummy)) }
284-
dummy
285-
}
286-
287282
bb(aeabi_d2f(bb(2.)));
288283
bb(aeabi_d2i(bb(2.)));
289284
bb(aeabi_d2l(bb(2.)));

src/arm.rs

+94-94
Original file line numberDiff line numberDiff line change
@@ -9,135 +9,135 @@ use core::intrinsics;
99
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
1010
#[naked]
1111
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
12-
pub unsafe fn __aeabi_uidivmod() {
13-
llvm_asm!("
14-
push {lr}
15-
sub sp, sp, #4
16-
mov r2, sp
17-
bl __udivmodsi4
18-
ldr r1, [sp]
19-
add sp, sp, #4
20-
pop {pc}
21-
" ::: "memory" : "volatile");
22-
intrinsics::unreachable();
12+
pub unsafe extern "C" fn __aeabi_uidivmod() {
13+
asm!(
14+
"push {{lr}}",
15+
"sub sp, sp, #4",
16+
"mov r2, sp",
17+
"bl __udivmodsi4",
18+
"ldr r1, [sp]",
19+
"add sp, sp, #4",
20+
"pop {{pc}}",
21+
options(noreturn)
22+
);
2323
}
2424

2525
#[cfg(target_os = "ios")]
2626
#[naked]
2727
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
28-
pub unsafe fn __aeabi_uidivmod() {
29-
llvm_asm!("
30-
push {lr}
31-
sub sp, sp, #4
32-
mov r2, sp
33-
bl ___udivmodsi4
34-
ldr r1, [sp]
35-
add sp, sp, #4
36-
pop {pc}
37-
" ::: "memory" : "volatile");
38-
intrinsics::unreachable();
28+
pub unsafe extern "C" fn __aeabi_uidivmod() {
29+
asm!(
30+
"push {{lr}}",
31+
"sub sp, sp, #4",
32+
"mov r2, sp",
33+
"bl ___udivmodsi4",
34+
"ldr r1, [sp]",
35+
"add sp, sp, #4",
36+
"pop {{pc}}",
37+
options(noreturn)
38+
);
3939
}
4040

4141
#[cfg(not(target_os = "ios"))]
4242
#[naked]
4343
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
44-
pub unsafe fn __aeabi_uldivmod() {
45-
llvm_asm!("
46-
push {r4, lr}
47-
sub sp, sp, #16
48-
add r4, sp, #8
49-
str r4, [sp]
50-
bl __udivmoddi4
51-
ldr r2, [sp, #8]
52-
ldr r3, [sp, #12]
53-
add sp, sp, #16
54-
pop {r4, pc}
55-
" ::: "memory" : "volatile");
56-
intrinsics::unreachable();
44+
pub unsafe extern "C" fn __aeabi_uldivmod() {
45+
asm!(
46+
"push {{r4, lr}}",
47+
"sub sp, sp, #16",
48+
"add r4, sp, #8",
49+
"str r4, [sp]",
50+
"bl __udivmoddi4",
51+
"ldr r2, [sp, #8]",
52+
"ldr r3, [sp, #12]",
53+
"add sp, sp, #16",
54+
"pop {{r4, pc}}",
55+
options(noreturn)
56+
);
5757
}
5858

5959
#[cfg(target_os = "ios")]
6060
#[naked]
6161
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
62-
pub unsafe fn __aeabi_uldivmod() {
63-
llvm_asm!("
64-
push {r4, lr}
65-
sub sp, sp, #16
66-
add r4, sp, #8
67-
str r4, [sp]
68-
bl ___udivmoddi4
69-
ldr r2, [sp, #8]
70-
ldr r3, [sp, #12]
71-
add sp, sp, #16
72-
pop {r4, pc}
73-
" ::: "memory" : "volatile");
74-
intrinsics::unreachable();
62+
pub unsafe extern "C" fn __aeabi_uldivmod() {
63+
asm!(
64+
"push {{r4, lr}}",
65+
"sub sp, sp, #16",
66+
"add r4, sp, #8",
67+
"str r4, [sp]",
68+
"bl ___udivmoddi4",
69+
"ldr r2, [sp, #8]",
70+
"ldr r3, [sp, #12]",
71+
"add sp, sp, #16",
72+
"pop {{r4, pc}}",
73+
options(noreturn)
74+
);
7575
}
7676

7777
#[cfg(not(target_os = "ios"))]
7878
#[naked]
7979
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
80-
pub unsafe fn __aeabi_idivmod() {
81-
llvm_asm!("
82-
push {r0, r1, r4, lr}
83-
bl __aeabi_idiv
84-
pop {r1, r2}
85-
muls r2, r2, r0
86-
subs r1, r1, r2
87-
pop {r4, pc}
88-
" ::: "memory" : "volatile");
89-
intrinsics::unreachable();
80+
pub unsafe extern "C" fn __aeabi_idivmod() {
81+
asm!(
82+
"push {{r0, r1, r4, lr}}",
83+
"bl __aeabi_idiv",
84+
"pop {{r1, r2}}",
85+
"muls r2, r2, r0",
86+
"subs r1, r1, r2",
87+
"pop {{r4, pc}}",
88+
options(noreturn)
89+
);
9090
}
9191

9292
#[cfg(target_os = "ios")]
9393
#[naked]
9494
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
95-
pub unsafe fn __aeabi_idivmod() {
96-
llvm_asm!("
97-
push {r0, r1, r4, lr}
98-
bl ___aeabi_idiv
99-
pop {r1, r2}
100-
muls r2, r2, r0
101-
subs r1, r1, r2
102-
pop {r4, pc}
103-
" ::: "memory" : "volatile");
104-
intrinsics::unreachable();
95+
pub unsafe extern "C" fn __aeabi_idivmod() {
96+
asm!(
97+
"push {{r0, r1, r4, lr}}",
98+
"bl ___aeabi_idiv",
99+
"pop {{r1, r2}}",
100+
"muls r2, r2, r0",
101+
"subs r1, r1, r2",
102+
"pop {{r4, pc}}",
103+
options(noreturn)
104+
);
105105
}
106106

107107
#[cfg(not(target_os = "ios"))]
108108
#[naked]
109109
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
110-
pub unsafe fn __aeabi_ldivmod() {
111-
llvm_asm!("
112-
push {r4, lr}
113-
sub sp, sp, #16
114-
add r4, sp, #8
115-
str r4, [sp]
116-
bl __divmoddi4
117-
ldr r2, [sp, #8]
118-
ldr r3, [sp, #12]
119-
add sp, sp, #16
120-
pop {r4, pc}
121-
" ::: "memory" : "volatile");
122-
intrinsics::unreachable();
110+
pub unsafe extern "C" fn __aeabi_ldivmod() {
111+
asm!(
112+
"push {{r4, lr}}",
113+
"sub sp, sp, #16",
114+
"add r4, sp, #8",
115+
"str r4, [sp]",
116+
"bl __divmoddi4",
117+
"ldr r2, [sp, #8]",
118+
"ldr r3, [sp, #12]",
119+
"add sp, sp, #16",
120+
"pop {{r4, pc}}",
121+
options(noreturn)
122+
);
123123
}
124124

125125
#[cfg(target_os = "ios")]
126126
#[naked]
127127
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
128-
pub unsafe fn __aeabi_ldivmod() {
129-
llvm_asm!("
130-
push {r4, lr}
131-
sub sp, sp, #16
132-
add r4, sp, #8
133-
str r4, [sp]
134-
bl ___divmoddi4
135-
ldr r2, [sp, #8]
136-
ldr r3, [sp, #12]
137-
add sp, sp, #16
138-
pop {r4, pc}
139-
" ::: "memory" : "volatile");
140-
intrinsics::unreachable();
128+
pub unsafe extern "C" fn __aeabi_ldivmod() {
129+
asm!(
130+
"push {{r4, lr}}",
131+
"sub sp, sp, #16",
132+
"add r4, sp, #8",
133+
"str r4, [sp]",
134+
"bl ___divmoddi4",
135+
"ldr r2, [sp, #8]",
136+
"ldr r3, [sp, #12]",
137+
"add sp, sp, #16",
138+
"pop {{r4, pc}}",
139+
options(noreturn)
140+
);
141141
}
142142

143143
// The following functions use weak linkage to allow users to override

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg_attr(feature = "compiler-builtins", compiler_builtins)]
22
#![cfg_attr(not(feature = "no-asm"), feature(asm))]
33
#![feature(abi_unadjusted)]
4-
#![cfg_attr(not(feature = "no-asm"), feature(llvm_asm))]
54
#![cfg_attr(not(feature = "no-asm"), feature(global_asm))]
65
#![feature(cfg_target_has_atomic)]
76
#![feature(compiler_builtins)]

src/x86.rs

+49-47
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ use core::intrinsics;
1616
))]
1717
#[naked]
1818
#[no_mangle]
19-
pub unsafe fn ___chkstk_ms() {
20-
llvm_asm!("
21-
push %ecx
22-
push %eax
23-
cmp $$0x1000,%eax
24-
lea 12(%esp),%ecx
25-
jb 1f
26-
2:
27-
sub $$0x1000,%ecx
28-
test %ecx,(%ecx)
29-
sub $$0x1000,%eax
30-
cmp $$0x1000,%eax
31-
ja 2b
32-
1:
33-
sub %eax,%ecx
34-
test %ecx,(%ecx)
35-
pop %eax
36-
pop %ecx
37-
ret" ::: "memory" : "volatile");
38-
intrinsics::unreachable();
19+
pub unsafe extern "C" fn ___chkstk_ms() {
20+
asm!(
21+
"push %ecx",
22+
"push %eax",
23+
"cmp $0x1000,%eax",
24+
"lea 12(%esp),%ecx",
25+
"jb 1f",
26+
"2:",
27+
"sub $0x1000,%ecx",
28+
"test %ecx,(%ecx)",
29+
"sub $0x1000,%eax",
30+
"cmp $0x1000,%eax",
31+
"ja 2b",
32+
"1:",
33+
"sub %eax,%ecx",
34+
"test %ecx,(%ecx)",
35+
"pop %eax",
36+
"pop %ecx",
37+
"ret",
38+
options(noreturn, att_syntax)
39+
);
3940
}
4041

4142
// FIXME: __alloca should be an alias to __chkstk
@@ -47,10 +48,11 @@ pub unsafe fn ___chkstk_ms() {
4748
))]
4849
#[naked]
4950
#[no_mangle]
50-
pub unsafe fn __alloca() {
51-
llvm_asm!("jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"
52-
::: "memory" : "volatile");
53-
intrinsics::unreachable();
51+
pub unsafe extern "C" fn __alloca() {
52+
asm!(
53+
"jmp ___chkstk", // Jump to ___chkstk since fallthrough may be unreliable"
54+
options(noreturn, att_syntax)
55+
);
5456
}
5557

5658
#[cfg(all(
@@ -61,27 +63,27 @@ pub unsafe fn __alloca() {
6163
))]
6264
#[naked]
6365
#[no_mangle]
64-
pub unsafe fn ___chkstk() {
65-
llvm_asm!("
66-
push %ecx
67-
cmp $$0x1000,%eax
68-
lea 8(%esp),%ecx // esp before calling this routine -> ecx
69-
jb 1f
70-
2:
71-
sub $$0x1000,%ecx
72-
test %ecx,(%ecx)
73-
sub $$0x1000,%eax
74-
cmp $$0x1000,%eax
75-
ja 2b
76-
1:
77-
sub %eax,%ecx
78-
test %ecx,(%ecx)
79-
80-
lea 4(%esp),%eax // load pointer to the return address into eax
81-
mov %ecx,%esp // install the new top of stack pointer into esp
82-
mov -4(%eax),%ecx // restore ecx
83-
push (%eax) // push return address onto the stack
84-
sub %esp,%eax // restore the original value in eax
85-
ret" ::: "memory" : "volatile");
86-
intrinsics::unreachable();
66+
pub unsafe extern "C" fn ___chkstk() {
67+
asm!(
68+
"push %ecx",
69+
"cmp $0x1000,%eax",
70+
"lea 8(%esp),%ecx", // esp before calling this routine -> ecx
71+
"jb 1f",
72+
"2:",
73+
"sub $0x1000,%ecx",
74+
"test %ecx,(%ecx)",
75+
"sub $0x1000,%eax",
76+
"cmp $0x1000,%eax",
77+
"ja 2b",
78+
"1:",
79+
"sub %eax,%ecx",
80+
"test %ecx,(%ecx)",
81+
"lea 4(%esp),%eax", // load pointer to the return address into eax
82+
"mov %ecx,%esp", // install the new top of stack pointer into esp
83+
"mov -4(%eax),%ecx", // restore ecx
84+
"push (%eax)", // push return address onto the stack
85+
"sub %esp,%eax", // restore the original value in eax
86+
"ret",
87+
options(noreturn, att_syntax)
88+
);
8789
}

0 commit comments

Comments
 (0)