avr-rust / delay

arduino-like delay routines based on busy-wait loops
Apache License 2.0
15 stars 11 forks source link

Inline asm: add output constraint, add volatile #15

Closed bombela closed 2 years ago

bombela commented 3 years ago

It is not permitted for the asm to write to any input register or memory location (unless that input is tied to an output). This is because the compiler will assume that you haven't mutated the input. And can therefore reuse the register with its value.

Additionally, you must add the option volatile. Otherwise the compiler is free to remove the piece of inline asm since you do not care about the output value!

Here is an example that I believe to be correct:

fn busy_loop_4_cycles(count: u16) {
    let mut _count = count; // Quiet the linter with underscore.
    unsafe {
        llvm_asm!(r#"
            1: sbiw $0,1
            brne 1b
            "#
            :"=w" (_count) // output
            :"0" (_count) // input
            : // clobbers
            : "volatile" // options
        )
    }
}

links: https://github.com/avr-rust/delay/blob/7925367d8320ac970fbd9a62de72848860443049/src/lib.rs#L37 https://llvm.org/docs/LangRef.html#input-constraints

stappersg commented 2 years ago

llvm_asm!() has been replaced by asm!(), please help reviewing #17