Skip to content

Commit

Permalink
Auto merge of rust-lang#91728 - Amanieu:stable_asm, r=joshtriplett
Browse files Browse the repository at this point in the history
Stabilize asm! and global_asm!

Tracking issue: rust-lang#72016

It's been almost 2 years since the original [RFC](rust-lang/rfcs#2850) was posted and we're finally ready to stabilize this feature!

The main changes in this PR are:
- Removing `asm!` and `global_asm!` from the prelude as per the decision in rust-lang#87228.
- Stabilizing the `asm` and `global_asm` features.
- Removing the unstable book pages for `asm` and `global_asm`. The contents are moved to the [reference](rust-lang/reference#1105) and [rust by example](rust-lang/rust-by-example#1483).
  - All links to these pages have been removed to satisfy the link checker. In a later PR these will be replaced with links to the reference or rust by example.
- Removing the automatic suggestion for using `llvm_asm!` instead of `asm!` if you're still using the old syntax, since it doesn't work anymore with `asm!` no longer being in the prelude. This only affects code that predates the old LLVM-style `asm!` being renamed to `llvm_asm!`.
- Updating `stdarch` and `compiler-builtins`.
- Updating all the tests.

r? `@joshtriplett`
  • Loading branch information
bors committed Dec 14, 2021
2 parents 404c847 + d6f4da9 commit 2f4da62
Show file tree
Hide file tree
Showing 141 changed files with 753 additions and 1,915 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock
Expand Up @@ -680,9 +680,9 @@ dependencies = [

[[package]]
name = "compiler_builtins"
version = "0.1.55"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9ac60765140c97aaf531dae151a287646b0805ec725805da9e2a3ee31cd501c"
checksum = "ed37ea958309f2451e1cea7fd2b37aa56b1894c9a9fbdbbe6a194f7b78f0362d"
dependencies = [
"cc",
"rustc-std-workspace-core",
Expand Down
13 changes: 0 additions & 13 deletions compiler/rustc_builtin_macros/src/asm.rs
Expand Up @@ -42,19 +42,6 @@ fn parse_args<'a>(
ecx.struct_span_err(sp, "the legacy LLVM-style asm! syntax is no longer supported");
err.note("consider migrating to the new asm! syntax specified in RFC 2873");
err.note("alternatively, switch to llvm_asm! to keep your code working as it is");

// Find the span of the "asm!" so that we can offer an automatic suggestion
let asm_span = sp.from_inner(InnerSpan::new(0, 4));
if let Ok(s) = ecx.source_map().span_to_snippet(asm_span) {
if s == "asm!" {
err.span_suggestion(
asm_span,
"replace with",
"llvm_asm!".into(),
Applicability::MachineApplicable,
);
}
}
return Err(err);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/inline_asm.rs
@@ -1,4 +1,4 @@
//! Codegen of [`asm!`] invocations.
//! Codegen of `asm!` invocations.

use crate::prelude::*;

Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_gcc/tests/run/asm.rs
Expand Up @@ -3,8 +3,6 @@
// Run-time:
// status: 0

#![feature(asm, global_asm)]

global_asm!("
.global add_asm
add_asm:
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/def.rs
Expand Up @@ -113,7 +113,7 @@ pub enum DefKind {
Field,
/// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
LifetimeParam,
/// A use of [`global_asm!`].
/// A use of `global_asm!`.
GlobalAsm,
Impl,
Closure,
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_lint/src/builtin.rs
Expand Up @@ -3147,7 +3147,8 @@ declare_lint! {
/// ### Example
///
/// ```rust,compile_fail
/// #![feature(asm)]
/// use std::arch::asm;
///
/// fn main() {
/// unsafe {
/// asm!("foo: bar");
Expand All @@ -3164,10 +3165,7 @@ declare_lint! {
/// of this, GNU assembler [local labels] *must* be used instead of labels
/// with a name. Using named labels might cause assembler or linker errors.
///
/// See the [unstable book] for more details.
///
/// [local labels]: https://sourceware.org/binutils/docs/as/Symbol-Names.html#Local-Labels
/// [unstable book]: https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels
pub NAMED_ASM_LABELS,
Deny,
"named labels in inline assembly",
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint/src/context.rs
Expand Up @@ -772,7 +772,6 @@ pub trait LintContext: Sized {
}
BuiltinLintDiagnostics::NamedAsmLabel(help) => {
db.help(&help);
db.note("see the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information");
}
}
// Rewrap `db`, and pass control to the user.
Expand Down
48 changes: 11 additions & 37 deletions compiler/rustc_lint_defs/src/builtin.rs
Expand Up @@ -2419,8 +2419,9 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,ignore (fails on system llvm)
/// #![feature(asm)]
/// ```rust,ignore (fails on non-x86_64)
/// #[cfg(target_arch="x86_64")]
/// use std::arch::asm;
///
/// fn main() {
/// #[cfg(target_arch="x86_64")]
Expand All @@ -2430,19 +2431,7 @@ declare_lint! {
/// }
/// ```
///
/// This will produce:
///
/// ```text
/// warning: formatting may not be suitable for sub-register argument
/// --> src/main.rs:6:19
/// |
/// 6 | asm!("mov {0}, {0}", in(reg) 0i16);
/// | ^^^ ^^^ ---- for this argument
/// |
/// = note: `#[warn(asm_sub_register)]` on by default
/// = help: use the `x` modifier to have the register formatted as `ax`
/// = help: or use the `r` modifier to keep the default formatting of `rax`
/// ```
/// {{produces}}
///
/// ### Explanation
///
Expand All @@ -2455,10 +2444,6 @@ declare_lint! {
/// register size, to alert you of possibly using the incorrect width. To
/// fix this, add the suggested modifier to the template, or cast the
/// value to the correct size.
///
/// See [register template modifiers] for more details.
///
/// [register template modifiers]: https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#register-template-modifiers
pub ASM_SUB_REGISTER,
Warn,
"using only a subset of a register for inline asm inputs",
Expand All @@ -2470,34 +2455,22 @@ declare_lint! {
///
/// ### Example
///
/// ```rust,ignore (fails on system llvm)
/// #![feature(asm)]
/// ```rust,ignore (fails on non-x86_64)
/// #[cfg(target_arch="x86_64")]
/// use std::arch::asm;
///
/// fn main() {
/// #[cfg(target_arch="x86_64")]
/// unsafe {
/// asm!(
/// ".att_syntax",
/// "movl {0}, {0}", in(reg) 0usize
/// "movq %{0}, %{0}", in(reg) 0usize
/// );
/// }
/// }
/// ```
///
/// This will produce:
///
/// ```text
/// warning: avoid using `.att_syntax`, prefer using `options(att_syntax)` instead
/// --> test.rs:7:14
/// |
/// 7 | ".att_syntax",
/// | ^^^^^^^^^^^
/// 8 | "movq {0}, {0}", out(reg) _,
/// 9 | );
/// | - help: add option: `, options(att_syntax)`
/// |
/// = note: `#[warn(bad_asm_style)]` on by default
/// ```
/// {{produces}}
///
/// ### Explanation
///
Expand Down Expand Up @@ -2739,7 +2712,8 @@ declare_lint! {
///
/// ```rust
/// #![feature(naked_functions)]
/// #![feature(asm)]
///
/// use std::arch::asm;
///
/// #[naked]
/// pub fn default_abi() -> u32 {
Expand Down
17 changes: 2 additions & 15 deletions library/core/src/lib.rs
Expand Up @@ -153,7 +153,6 @@
#![feature(abi_unadjusted)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![feature(asm)]
#![feature(associated_type_bounds)]
#![feature(auto_traits)]
#![feature(cfg_target_has_atomic)]
Expand Down Expand Up @@ -373,26 +372,14 @@ pub mod arch {
pub use crate::core_arch::arch::*;

/// Inline assembly.
///
/// Read the [unstable book] for the usage.
///
/// [unstable book]: ../../unstable-book/library-features/asm.html
#[unstable(
feature = "asm",
issue = "72016",
reason = "inline assembly is not stable enough for use and is subject to change"
)]
#[stable(feature = "asm", since = "1.59.0")]
#[rustc_builtin_macro]
pub macro asm("assembly template", $(operands,)* $(options($(option),*))?) {
/* compiler built-in */
}
/// Module-level inline assembly.
#[unstable(
feature = "global_asm",
issue = "35119",
reason = "`global_asm!` is not stable enough for use and is subject to change"
)]
#[stable(feature = "global_asm", since = "1.59.0")]
#[rustc_builtin_macro]
pub macro global_asm("assembly template", $(operands,)* $(options($(option),*))?) {
/* compiler built-in */
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/dec2flt/fpu.rs
Expand Up @@ -10,6 +10,7 @@ pub use fpu_precision::set_precision;
// computations are performed in the desired precision.
#[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
mod fpu_precision {
use core::arch::asm;
use core::mem::size_of;

/// A structure used to preserve the original value of the FPU control word, so that it can be
Expand Down
16 changes: 0 additions & 16 deletions library/core/src/prelude/v1.rs
Expand Up @@ -69,22 +69,6 @@ pub use crate::{
#[doc(no_inline)]
pub use crate::concat_bytes;

#[unstable(
feature = "asm",
issue = "72016",
reason = "inline assembly is not stable enough for use and is subject to change"
)]
#[doc(no_inline)]
pub use crate::arch::asm;

#[unstable(
feature = "global_asm",
issue = "35119",
reason = "`global_asm!` is not stable enough for use and is subject to change"
)]
#[doc(no_inline)]
pub use crate::arch::global_asm;

#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow(deprecated, deprecated_in_future)]
#[doc(no_inline)]
Expand Down
7 changes: 3 additions & 4 deletions library/panic_abort/src/lib.rs
Expand Up @@ -14,7 +14,6 @@
#![feature(std_internals)]
#![feature(staged_api)]
#![feature(rustc_attrs)]
#![feature(asm)]
#![feature(c_unwind)]

#[cfg(target_os = "android")]
Expand Down Expand Up @@ -69,11 +68,11 @@ pub unsafe extern "C-unwind" fn __rust_start_panic(_payload: *mut &mut dyn BoxMe
const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
} else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
} else if #[cfg(target_arch = "aarch64")] {
asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
} else {
core::intrinsics::abort();
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Expand Up @@ -16,7 +16,7 @@ panic_unwind = { path = "../panic_unwind", optional = true }
panic_abort = { path = "../panic_abort" }
core = { path = "../core" }
libc = { version = "0.2.108", default-features = false, features = ['rustc-dep-of-std'] }
compiler_builtins = { version = "0.1.55" }
compiler_builtins = { version = "0.1.65" }
profiler_builtins = { path = "../profiler_builtins", optional = true }
unwind = { path = "../unwind" }
hashbrown = { version = "0.11", default-features = false, features = ['rustc-dep-of-std'] }
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/lib.rs
Expand Up @@ -233,7 +233,6 @@
#![feature(allow_internal_unstable)]
#![feature(arbitrary_self_types)]
#![feature(array_error_internals)]
#![feature(asm)]
#![feature(assert_matches)]
#![feature(associated_type_bounds)]
#![feature(async_stream)]
Expand Down Expand Up @@ -287,7 +286,6 @@
#![feature(gen_future)]
#![feature(generator_trait)]
#![feature(get_mut_unchecked)]
#![feature(global_asm)]
#![feature(hashmap_internals)]
#![feature(int_error_internals)]
#![feature(integer_atomics)]
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/fortanix_sgx/arch.rs
Expand Up @@ -5,6 +5,7 @@
#![unstable(feature = "sgx_platform", issue = "56975")]

use crate::mem::MaybeUninit;
use core::arch::asm;

/// Wrapper struct to force 16-byte alignment.
#[repr(align(16))]
Expand Down
16 changes: 0 additions & 16 deletions library/std/src/prelude/v1.rs
Expand Up @@ -54,22 +54,6 @@ pub use core::prelude::v1::{
#[doc(no_inline)]
pub use core::prelude::v1::concat_bytes;

#[unstable(
feature = "asm",
issue = "72016",
reason = "inline assembly is not stable enough for use and is subject to change"
)]
#[doc(no_inline)]
pub use core::prelude::v1::asm;

#[unstable(
feature = "global_asm",
issue = "35119",
reason = "`global_asm!` is not stable enough for use and is subject to change"
)]
#[doc(no_inline)]
pub use core::prelude::v1::global_asm;

// FIXME: Attribute and internal derive macros are not documented because for them rustdoc generates
// dead links which fail link checker testing.
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/sgx/abi/mem.rs
@@ -1,3 +1,5 @@
use core::arch::asm;

// Do not remove inline: will result in relocation failure
#[inline(always)]
pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/sgx/abi/mod.rs
@@ -1,6 +1,7 @@
#![cfg_attr(test, allow(unused))] // RT initialization logic is not compiled for test

use crate::io::Write;
use core::arch::global_asm;
use core::sync::atomic::{AtomicUsize, Ordering};

// runtime features
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sys/solid/abi/mod.rs
Expand Up @@ -10,9 +10,9 @@ pub fn breakpoint_program_exited(tid: usize) {
match () {
// SOLID_BP_PROGRAM_EXITED = 15
#[cfg(target_arch = "arm")]
() => asm!("bkpt #15", in("r0") tid),
() => core::arch::asm!("bkpt #15", in("r0") tid),
#[cfg(target_arch = "aarch64")]
() => asm!("hlt #15", in("x0") tid),
() => core::arch::asm!("hlt #15", in("x0") tid),
}
}
}
Expand All @@ -23,9 +23,9 @@ pub fn breakpoint_abort() {
match () {
// SOLID_BP_CSABORT = 16
#[cfg(target_arch = "arm")]
() => asm!("bkpt #16"),
() => core::arch::asm!("bkpt #16"),
#[cfg(target_arch = "aarch64")]
() => asm!("hlt #16"),
() => core::arch::asm!("hlt #16"),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/windows/mod.rs
Expand Up @@ -288,13 +288,13 @@ pub fn abort_internal() -> ! {
unsafe {
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
crate::intrinsics::unreachable();
} else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
crate::intrinsics::unreachable();
} else if #[cfg(target_arch = "aarch64")] {
asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
crate::intrinsics::unreachable();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/doc/unstable-book/src/compiler-flags/sanitizer.md
Expand Up @@ -199,8 +199,9 @@ LLVM CFI can be enabled with -Zsanitizer=cfi and requires LTO (i.e., -Clto).
## Example
```text
#![feature(asm, naked_functions)]
#![feature(naked_functions)]
use std::arch::asm;
use std::mem;
fn add_one(x: i32) -> i32 {
Expand Down

0 comments on commit 2f4da62

Please sign in to comment.