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

Fix inline assembly #12798

Merged
merged 1 commit into from
Mar 14, 2014
Merged

Fix inline assembly #12798

merged 1 commit into from
Mar 14, 2014

Conversation

pczarn
Copy link
Contributor

@pczarn pczarn commented Mar 9, 2014

read+write modifier '+'

This small sugar was left out in the original implementation (#5359).

When an output operand with the '+' modifier is encountered, we store the index of that operand alongside the expression to create and append an input operand later. The following lines are equivalent:

asm!("" : "+m"(expr));
asm!("" : "=m"(expr) : "0"(expr));

misplaced options and clobbers give a warning

It's really annoying when a small typo might change behavior without any warning.

asm!("mov $1, $0" : "=r"(x) : "r"(8u) : "cc" , "volatile");
//~^ WARNING expected a clobber, but found an option

liveness

Fixed incorrect order of propagation.
Sometimes it caused spurious warnings in code: warning: value assigned toiis never read, #[warn(dead_assignment)] on by default

Note: Rebased on top of another PR. (uses other changes)

  • Implement read+write
  • Warn about misplaced options
  • Fix liveness (dead_assignment lint)
  • Add all tests

@alexcrichton
Copy link
Member

Can you rebase out the changes from #12797?

@huonw
Copy link
Member

huonw commented Mar 10, 2014

@alexcrichton it looks like the asm changes use the changes from #12797.

@pczarn
Copy link
Contributor Author

pczarn commented Mar 12, 2014

Done. I don't have more ideas other than refactoring. I.e. the body of while could be written as

state = match (p.token, next_state(state), next_state(next_state(state))) {
    (token::COLON, StateNone, _)   |
    (token::MOD_SEP, _, StateNone) => {
        p.bump();
        break 'statement;
    }
    (token::COLON, st, _)   |
    (token::MOD_SEP, _, st) => {
        p.bump();
        st
    }
    (token::EOF, _) => break 'statement,
    _ => state
}

@alexcrichton
Copy link
Member

While you're at it, do you think you could write some documentation for this feature? There's not really a great place that I know of for the documentation, but docs in asm.rs are better than nothing!

Also, could you edit the title of the PR to not have "WIP" in the name, and also squash the commits together?

@huonw
Copy link
Member

huonw commented Mar 13, 2014

There is a chunk of docs https://github.com/mozilla/rust/wiki/Doc-detailed-release-notes#wiki-inline-assembly but not in a very useful/obvious place... and no code examples are tested.

Maybe we could have a guide-low-level.md or guide-unsafe.md that covers how to write (approximately) safe unsafe code, and various things like intrinsics and asm! (with big warnings that these features are subject to change). A reasonable start to that guide would just be copying the section from those release notes into src/doc/guide-unsafe.md with a # asm!`` heading; updating it for any changes that have happened (and are happening in this one).

That said, I'd personally be happy with this happening in a different PR.

@@ -135,6 +142,10 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
let (s, _str_style) = p.parse_str();
let clob = format!("~\\{{}\\}", s);
clobs.push(clob);

if s.equiv(&("volatile")) || s.equiv(&("alignstack")) || s.equiv(&("intel")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be "volatile" == s || "alignstack" == s || "intel" == s, although maybe it would be even better (aka more extensible) as

static OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];

// ...

if OPTIONS.iter().any(|opt| *opt == *s) { .. .}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huonw Can I write "volatile" == option below?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's an InternedString

error: mismatched types: expected `&str` but found `parse::token::InternedString`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my apologies, thought they were just ~strs. In that case, .equiv is good.

@pczarn
Copy link
Contributor Author

pczarn commented Mar 13, 2014

The problem is these examples can only be tested for x86 and x86_64. I will try to document this feature. GCC inline asm is extensively described here: http://locklessinc.com/articles/gcc_asm/

Should I squash all commits?

@alexcrichton
Copy link
Member

I'm ok if you want to defer documentation for later, but yes, it would be nice to have the commits all squashed.

Read+write modifier
Some documentation in asm.rs
rpass and cfail tests
@pczarn
Copy link
Contributor Author

pczarn commented Mar 13, 2014

Done

bors added a commit that referenced this pull request Mar 14, 2014
## read+write modifier '+'
This small sugar was left out in the original implementation (#5359).
 
When an output operand with the '+' modifier is encountered, we store the index of that operand alongside the expression to create and append an input operand later. The following lines are equivalent:
```
asm!("" : "+m"(expr));
asm!("" : "=m"(expr) : "0"(expr));
```
## misplaced options and clobbers give a warning
It's really annoying when a small typo might change behavior without any warning.
```
asm!("mov $1, $0" : "=r"(x) : "r"(8u) : "cc" , "volatile");
//~^ WARNING expected a clobber, but found an option
```
## liveness
Fixed incorrect order of propagation.
Sometimes it caused spurious warnings in code: `warning: value assigned to `i` is never read, #[warn(dead_assignment)] on by default`

~~Note: Rebased on top of another PR. (uses other changes)~~

* [x] Implement read+write
* [x] Warn about misplaced options
* [x] Fix liveness (`dead_assignment` lint)
* [x] Add all tests
@bors bors closed this Mar 14, 2014
@bors bors merged commit 2a1bd2f into rust-lang:master Mar 14, 2014
pczarn referenced this pull request in lexs/rust-os Mar 14, 2014
@huonw
Copy link
Member

huonw commented Mar 16, 2014

BTW, @pczarn, I created the unsafe guide in #12887, maybe you could update the asm section if you have time?

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
… r=jonas-schievink

internal: Remove outdated proc macro ABIs

Drops support for 1.48 to 1.57. We still support 1.58+, which is 4 versions out of date, so that should be plenty.
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 17, 2024
…r=y21

Don't lint path statements in no_effect

The rustc lint `path_statements` covers this case

Fixes rust-lang#11547

changelog: none
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

Successfully merging this pull request may close these issues.

4 participants