diff options
author | 2020-03-18 08:45:16 +0000 | |
---|---|---|
committer | 2020-03-18 08:45:16 +0000 | |
commit | 6a0432a038addf78cfe2fc7ab958b171b2134377 (patch) | |
tree | 4b7f8571181a386e8c4aa865a4e0c0adb4c4d5ff /src | |
parent | 12f11d29afabfb0149a660589f59bb8ba0f02161 (diff) | |
parent | 9c5740592e652b2f612b90478cb8f2431c6ccb4d (diff) | |
download | cortex-m-6a0432a038addf78cfe2fc7ab958b171b2134377.tar.gz cortex-m-6a0432a038addf78cfe2fc7ab958b171b2134377.tar.zst cortex-m-6a0432a038addf78cfe2fc7ab958b171b2134377.zip |
Merge #202
202: Prevent unnecessary bounds check in SCB::{get_priority, set_priority} r=therealprof a=qwerty19106
SystemHandler.index() gives true index for SCB.shpr array.
But now it produce unnecessary bounds check. For example, SCB::set_priority:
```
48: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::h622cd21bcc3321be ();
0x08000376 push {r7, lr}
0x08000378 mov r7, sp
0x0800037a bl cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5 ; sym.cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5
0x0800037e subs r0, 4
0x08000380 uxtb r0, r0
0x08000382 cmp r0, 0xb ; 11
0x08000384 itttt ls
0x08000386 movw r1, 0xed18
0x0800038a movt r1, 0xe000
0x0800038e movs r2, 0xff
0x08000390 strb r2, [r0, r1]
0x08000392 it ls
0x08000394 pop {r7, pc}
0x08000396 movw r2, 0x1dc0
0x0800039a movs r1, 0xc
0x0800039c movt r2, 0x800
0x080003a0 bl core::panicking::panic_bounds_check::h5181f47ae17a04a5 ; sym.core::panicking::panic_bounds_check::h5181f47ae17a04a5
0x080003a4 trap
```
This PR fix it.
Co-authored-by: Роман Кривенков <qwerty19106@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/peripheral/scb.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/peripheral/scb.rs b/src/peripheral/scb.rs index faebea1..940809e 100644 --- a/src/peripheral/scb.rs +++ b/src/peripheral/scb.rs @@ -949,13 +949,23 @@ impl SCB { #[cfg(not(armv6m))] { // NOTE(unsafe) atomic read with no side effects - unsafe { (*Self::ptr()).shpr[usize::from(index - 4)].read() } + + // NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design. + // TODO: Review it after rust-lang/rust/issues/13926 will be fixed. + let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from(index - 4))}; + + priority_ref.read() } #[cfg(armv6m)] { // NOTE(unsafe) atomic read with no side effects - let shpr = unsafe { (*Self::ptr()).shpr[usize::from((index - 8) / 4)].read() }; + + // NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design. + // TODO: Review it after rust-lang/rust/issues/13926 will be fixed. + let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4))}; + + let shpr = priority_ref.read(); let prio = (shpr >> (8 * (index % 4))) & 0x0000_00ff; prio as u8 } @@ -979,12 +989,20 @@ impl SCB { #[cfg(not(armv6m))] { - self.shpr[usize::from(index - 4)].write(prio) + // NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design. + // TODO: Review it after rust-lang/rust/issues/13926 will be fixed. + let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from(index - 4)); + + priority_ref.write(prio) } #[cfg(armv6m)] { - self.shpr[usize::from((index - 8) / 4)].modify(|value| { + // NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design. + // TODO: Review it after rust-lang/rust/issues/13926 will be fixed. + let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4)); + + priority_ref.modify(|value| { let shift = 8 * (index % 4); let mask = 0x0000_00ff << shift; let prio = u32::from(prio) << shift; |