Skip to content

Commit

Permalink
Add options to asm
Browse files Browse the repository at this point in the history
- Use `nomem` when neither memory access nor reordering prevention is needed.
- Use `nostack` when not pushing data to the stack.
- Use `preserves_flags` when not modifying the status register.
  refs: https://doc.rust-lang.org/nightly/unstable-book/language-features/asm-experimental-arch.html#flags-covered-by-preserves_flags
- Add comments explaining why specific options are not used.
  • Loading branch information
taiki-e committed Aug 4, 2022
1 parent 4ee7a8b commit a0e2046
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::asm;
#[inline(always)]
pub fn nop() {
unsafe {
asm!("nop");
asm!("nop", options(nostack, preserves_flags));
}
}

/// A compiler fence, prevents instruction reordering.
#[inline(always)]
pub fn barrier() {
unsafe {
asm!("");
// Do not use `nomem` and `readonly` because prevent preceding and subsequent memory accesses from being reordered.
asm!("", options(nostack, preserves_flags));
}
}
8 changes: 6 additions & 2 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn disable() {
match () {
#[cfg(target_arch = "msp430")]
() => unsafe {
asm!("dint {{ nop");
// Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled.
// Do not use `preserves_flags` because dint modifies the GIE (global interrupt enable) bit of the status register.
asm!("dint {{ nop", options(nostack));
},
#[cfg(not(target_arch = "msp430"))]
() => {}
Expand All @@ -29,7 +31,9 @@ pub unsafe fn enable() {
match () {
#[cfg(target_arch = "msp430")]
() => {
asm!("nop {{ eint {{ nop");
// Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled.
// Do not use `preserves_flags` because eint modifies the GIE (global interrupt enable) bit of the status register.
asm!("nop {{ eint {{ nop", options(nostack));
}
#[cfg(not(target_arch = "msp430"))]
() => {}
Expand Down
2 changes: 1 addition & 1 deletion src/register/pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::asm;
pub fn read() -> u16 {
let r;
unsafe {
asm!("mov R0, {0}", out(reg) r);
asm!("mov R0, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
r
}
2 changes: 1 addition & 1 deletion src/register/sp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::asm;
pub fn read() -> u16 {
let r;
unsafe {
asm!("mov R1, {0}", out(reg) r);
asm!("mov R1, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
r
}
2 changes: 1 addition & 1 deletion src/register/sr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Sr {
pub fn read() -> Sr {
let r: u16;
unsafe {
asm!("mov R2, {0}", out(reg) r);
asm!("mov R2, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
Sr { bits: r }
}

0 comments on commit a0e2046

Please sign in to comment.