Skip to content

Commit f84b3ba

Browse files
committed
put our aeabi mem{clr,cpy,set} implementations behind the "mem" Cargo feature
people not using the "mem" feature probably just want to use libc's mem{clr,cpy,set} everywhere
1 parent eec56b2 commit f84b3ba

File tree

1 file changed

+134
-46
lines changed

1 file changed

+134
-46
lines changed

src/arm.rs

+134-46
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,69 @@ use core::intrinsics;
33
#[cfg(feature = "mem")]
44
use mem::memmove;
55

6-
// NOTE This function and the ones below are implemented using assembly because they using a custom
7-
// calling convention which can't be implemented using a normal Rust function
6+
// NOTE This function and the ones below are implemented using assembly
7+
// because they using a custom calling convention which can't be implemented
8+
// using a normal Rust function
89
#[naked]
910
#[cfg_attr(not(test), no_mangle)]
1011
pub unsafe fn __aeabi_uidivmod() {
11-
asm!("push {lr}
12-
sub sp, sp, #4
13-
mov r2, sp
14-
bl __udivmodsi4
15-
ldr r1, [sp]
16-
add sp, sp, #4
17-
pop {pc}");
12+
asm!(
13+
"push {lr}
14+
sub sp, sp, #4
15+
mov r2, sp
16+
bl __udivmodsi4
17+
ldr r1, [sp]
18+
add sp, sp, #4
19+
pop {pc}"
20+
);
1821
intrinsics::unreachable();
1922
}
2023

2124
#[naked]
2225
#[cfg_attr(not(test), no_mangle)]
2326
pub unsafe fn __aeabi_uldivmod() {
24-
asm!("push {r4, lr}
25-
sub sp, sp, #16
26-
add r4, sp, #8
27-
str r4, [sp]
28-
bl __udivmoddi4
29-
ldr r2, [sp, #8]
30-
ldr r3, [sp, #12]
31-
add sp, sp, #16
32-
pop {r4, pc}");
27+
asm!(
28+
"push {r4, lr}
29+
sub sp, sp, #16
30+
add r4, sp, #8
31+
str r4, [sp]
32+
bl __udivmoddi4
33+
ldr r2, [sp, #8]
34+
ldr r3, [sp, #12]
35+
add sp, sp, #16
36+
pop {r4, pc}"
37+
);
3338
intrinsics::unreachable();
3439
}
3540

3641
#[naked]
3742
#[cfg_attr(not(test), no_mangle)]
3843
pub unsafe fn __aeabi_idivmod() {
39-
asm!("push {r0, r1, r4, lr}
40-
bl __divsi3
41-
pop {r1, r2}
42-
muls r2, r2, r0
43-
subs r1, r1, r2
44-
pop {r4, pc}");
44+
asm!(
45+
"push {r0, r1, r4, lr}
46+
bl __divsi3
47+
pop {r1, r2}
48+
muls r2, r2, r0
49+
subs r1, r1, r2
50+
pop {r4, pc}"
51+
);
4552
intrinsics::unreachable();
4653
}
4754

4855
#[naked]
4956
#[cfg_attr(not(test), no_mangle)]
5057
pub unsafe fn __aeabi_ldivmod() {
51-
asm!("push {r4, lr}
52-
sub sp, sp, #16
53-
add r4, sp, #8
54-
str r4, [sp]
55-
bl __divmoddi4
56-
ldr r2, [sp, #8]
57-
ldr r3, [sp, #12]
58-
add sp, sp, #16
59-
pop {r4, pc}");
58+
asm!(
59+
"push {r4, lr}
60+
sub sp, sp, #16
61+
add r4, sp, #8
62+
str r4, [sp]
63+
bl __divmoddi4
64+
ldr r2, [sp, #8]
65+
ldr r3, [sp, #12]
66+
add sp, sp, #16
67+
pop {r4, pc}"
68+
);
6069
intrinsics::unreachable();
6170
}
6271

@@ -80,7 +89,10 @@ pub extern "aapcs" fn __aeabi_fsub(a: f32, b: f32) -> f32 {
8089
::float::sub::__subsf3(a, b)
8190
}
8291

83-
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
92+
#[cfg(not(all(feature = "c",
93+
target_arch = "arm",
94+
not(target_os = "ios"),
95+
not(thumbv6m))))]
8496
#[cfg_attr(not(test), no_mangle)]
8597
pub extern "aapcs" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
8698
::int::sdiv::__divsi3(a, b)
@@ -106,7 +118,10 @@ pub extern "aapcs" fn __aeabi_lmul(a: u64, b: u64) -> u64 {
106118
::int::mul::__muldi3(a, b)
107119
}
108120

109-
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
121+
#[cfg(not(all(feature = "c",
122+
target_arch = "arm",
123+
not(target_os = "ios"),
124+
not(thumbv6m))))]
110125
#[cfg_attr(not(test), no_mangle)]
111126
pub extern "aapcs" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
112127
::int::udiv::__udivsi3(a, b)
@@ -118,15 +133,8 @@ pub extern "C" fn __aeabi_ui2d(a: u32) -> f64 {
118133
::float::conv::__floatunsidf(a)
119134
}
120135

121-
// TODO: These aeabi_* functions should be defined as aliases
122-
#[cfg(not(feature = "mem"))]
123-
extern "C" {
124-
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
125-
fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
126-
fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
127-
}
128-
129136
// Assembly optimized memclr{,4,8} and memset{,4,8}
137+
#[cfg(feature = "mem")]
130138
global_asm!(r"
131139
@ fn __aeabi_memclr(r0: *mut u8, r1: usize)
132140
.global __aeabi_memclr
@@ -201,6 +209,7 @@ __aeabi_memset:
201209
"#);
202210

203211
// Assembly optimized memcpy{,4,8}
212+
#[cfg(feature = "mem")]
204213
global_asm!(r"
205214
@ fn __aeabi_memcpy(r0: *mut u8, r1: *const u8, r2: usize)
206215
.global __aeabi_memcpy
@@ -243,16 +252,95 @@ __aeabi_memcpy:
243252
2: bx lr
244253
"#);
245254

255+
#[cfg(not(feature = "mem"))]
256+
extern "C" {
257+
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
258+
fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
259+
fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
260+
}
261+
246262
// FIXME: The `*4` and `*8` variants should be defined as aliases.
263+
#[cfg(not(feature = "mem"))]
264+
#[cfg_attr(not(test), no_mangle)]
265+
pub unsafe extern "aapcs" fn __aeabi_memclr(dest: *mut u8, n: usize) {
266+
memset(dest, 0, n);
267+
}
268+
#[cfg(not(feature = "mem"))]
269+
#[cfg_attr(not(test), no_mangle)]
270+
pub unsafe extern "aapcs" fn __aeabi_memclr4(dest: *mut u8, n: usize) {
271+
memset(dest, 0, n);
272+
}
273+
#[cfg(not(feature = "mem"))]
247274
#[cfg_attr(not(test), no_mangle)]
248-
pub unsafe extern "aapcs" fn __aeabi_memmove(dest: *mut u8, src: *const u8, n: usize) {
275+
pub unsafe extern "aapcs" fn __aeabi_memclr8(dest: *mut u8, n: usize) {
276+
memset(dest, 0, n);
277+
}
278+
279+
#[cfg(not(feature = "mem"))]
280+
#[cfg_attr(not(test), no_mangle)]
281+
pub unsafe extern "aapcs" fn __aeabi_memcpy(
282+
dest: *mut u8,
283+
src: *const u8,
284+
n: usize,
285+
) {
286+
memcpy(dest, src, n);
287+
}
288+
#[cfg(not(feature = "mem"))]
289+
#[cfg_attr(not(test), no_mangle)]
290+
pub unsafe extern "aapcs" fn __aeabi_memcpy4(
291+
dest: *mut u8,
292+
src: *const u8,
293+
n: usize,
294+
) {
295+
memcpy(dest, src, n);
296+
}
297+
#[cfg(not(feature = "mem"))]
298+
#[cfg_attr(not(test), no_mangle)]
299+
pub unsafe extern "aapcs" fn __aeabi_memcpy8(
300+
dest: *mut u8,
301+
src: *const u8,
302+
n: usize,
303+
) {
304+
memcpy(dest, src, n);
305+
}
306+
307+
#[cfg_attr(not(test), no_mangle)]
308+
pub unsafe extern "aapcs" fn __aeabi_memmove(
309+
dest: *mut u8,
310+
src: *const u8,
311+
n: usize,
312+
) {
249313
memmove(dest, src, n);
250314
}
251315
#[cfg_attr(not(test), no_mangle)]
252-
pub unsafe extern "aapcs" fn __aeabi_memmove4(dest: *mut u8, src: *const u8, n: usize) {
316+
pub unsafe extern "aapcs" fn __aeabi_memmove4(
317+
dest: *mut u8,
318+
src: *const u8,
319+
n: usize,
320+
) {
253321
memmove(dest, src, n);
254322
}
255323
#[cfg_attr(not(test), no_mangle)]
256-
pub unsafe extern "aapcs" fn __aeabi_memmove8(dest: *mut u8, src: *const u8, n: usize) {
324+
pub unsafe extern "aapcs" fn __aeabi_memmove8(
325+
dest: *mut u8,
326+
src: *const u8,
327+
n: usize,
328+
) {
257329
memmove(dest, src, n);
258330
}
331+
332+
#[cfg(not(feature = "mem"))]
333+
#[cfg_attr(not(test), no_mangle)]
334+
pub unsafe extern "aapcs" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) {
335+
memset(dest, c, n);
336+
}
337+
#[cfg(not(feature = "mem"))]
338+
#[cfg_attr(not(test), no_mangle)]
339+
pub unsafe extern "aapcs" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) {
340+
memset(dest, c, n);
341+
}
342+
#[cfg(not(feature = "mem"))]
343+
#[cfg_attr(not(test), no_mangle)]
344+
pub unsafe extern "aapcs" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) {
345+
memset(dest, c, n);
346+
}

0 commit comments

Comments
 (0)