Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline asm: add output constraint, add volatile #15

Closed
bombela opened this issue Jul 22, 2021 · 1 comment
Closed

Inline asm: add output constraint, add volatile #15

bombela opened this issue Jul 22, 2021 · 1 comment

Comments

@bombela
Copy link

bombela commented Jul 22, 2021

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:

: "w" (0)

https://llvm.org/docs/LangRef.html#input-constraints

@stappersg
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants