Skip to content

Import the asm! macro from core::arch #446

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

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ mod c {
file,
"#include \"{}\"",
outlined_atomics_file.canonicalize().unwrap().display()
);
)
.unwrap();
drop(file);
cfg.file(path);

Expand Down
16 changes: 8 additions & 8 deletions src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::intrinsics;
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_uidivmod() {
asm!(
core::arch::asm!(
"push {{lr}}",
"sub sp, sp, #4",
"mov r2, sp",
Expand All @@ -27,7 +27,7 @@ pub unsafe extern "C" fn __aeabi_uidivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_uidivmod() {
asm!(
core::arch::asm!(
"push {{lr}}",
"sub sp, sp, #4",
"mov r2, sp",
Expand All @@ -43,7 +43,7 @@ pub unsafe extern "C" fn __aeabi_uidivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_uldivmod() {
asm!(
core::arch::asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand All @@ -61,7 +61,7 @@ pub unsafe extern "C" fn __aeabi_uldivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_uldivmod() {
asm!(
core::arch::asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand All @@ -79,7 +79,7 @@ pub unsafe extern "C" fn __aeabi_uldivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_idivmod() {
asm!(
core::arch::asm!(
"push {{r0, r1, r4, lr}}",
"bl __aeabi_idiv",
"pop {{r1, r2}}",
Expand All @@ -94,7 +94,7 @@ pub unsafe extern "C" fn __aeabi_idivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_idivmod() {
asm!(
core::arch::asm!(
"push {{r0, r1, r4, lr}}",
"bl ___aeabi_idiv",
"pop {{r1, r2}}",
Expand All @@ -109,7 +109,7 @@ pub unsafe extern "C" fn __aeabi_idivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_ldivmod() {
asm!(
core::arch::asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand All @@ -127,7 +127,7 @@ pub unsafe extern "C" fn __aeabi_ldivmod() {
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe extern "C" fn __aeabi_ldivmod() {
asm!(
core::arch::asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand Down
17 changes: 9 additions & 8 deletions src/float/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ where
// This doubles the number of correct binary digits in the approximation
// with each iteration, so after three iterations, we have about 28 binary
// digits of accuracy.
let mut correction: u32;
correction = negate_u32(((reciprocal as u64).wrapping_mul(q31b as u64) >> 32) as u32);

let mut correction: u32 =
negate_u32(((reciprocal as u64).wrapping_mul(q31b as u64) >> 32) as u32);
reciprocal = ((reciprocal as u64).wrapping_mul(correction as u64) as u64 >> 31) as u32;
correction = negate_u32(((reciprocal as u64).wrapping_mul(q31b as u64) >> 32) as u32);
reciprocal = ((reciprocal as u64).wrapping_mul(correction as u64) as u64 >> 31) as u32;
Expand Down Expand Up @@ -342,8 +343,9 @@ where
// This doubles the number of correct binary digits in the approximation
// with each iteration, so after three iterations, we have about 28 binary
// digits of accuracy.
let mut correction32: u32;
correction32 = negate_u32(((recip32 as u64).wrapping_mul(q31b as u64) >> 32) as u32);

let mut correction32: u32 =
negate_u32(((recip32 as u64).wrapping_mul(q31b as u64) >> 32) as u32);
recip32 = ((recip32 as u64).wrapping_mul(correction32 as u64) >> 31) as u32;
correction32 = negate_u32(((recip32 as u64).wrapping_mul(q31b as u64) >> 32) as u32);
recip32 = ((recip32 as u64).wrapping_mul(correction32 as u64) >> 31) as u32;
Expand All @@ -359,16 +361,15 @@ where
// We need to perform one more iteration to get us to 56 binary digits;
// The last iteration needs to happen with extra precision.
let q63blo = CastInto::<u32>::cast(b_significand << 11.cast());
let correction: u64;
let mut reciprocal: u64;
correction = negate_u64(

let correction: u64 = negate_u64(
(recip32 as u64)
.wrapping_mul(q31b as u64)
.wrapping_add((recip32 as u64).wrapping_mul(q63blo as u64) >> 32),
);
let c_hi = (correction >> 32) as u32;
let c_lo = correction as u32;
reciprocal = (recip32 as u64)
let mut reciprocal: u64 = (recip32 as u64)
.wrapping_mul(c_hi as u64)
.wrapping_add((recip32 as u64).wrapping_mul(c_lo as u64) >> 32);

Expand Down
2 changes: 2 additions & 0 deletions src/int/leading_zeros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public_test_dep! {
/// Returns the number of leading binary zeros in `x`.
#[allow(dead_code)]
pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
// The basic idea is to test if the higher bits of `x` are zero and bisect the number
// of leading zeros. It is possible for all branches of the bisection to use the same
Expand Down Expand Up @@ -78,6 +79,7 @@ pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {

public_test_dep! {
/// Returns the number of leading binary zeros in `x`.
#[allow(dead_code)]
pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
let mut x = x;
// the number of potential leading zeros
Expand Down
4 changes: 2 additions & 2 deletions src/int/specialized_div_rem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ unsafe fn u128_by_u64_div_rem(duo: u128, div: u64) -> (u64, u64) {
// divides the combined registers rdx:rax (`duo` is split into two 64 bit parts to do this)
// by `div`. The quotient is stored in rax and the remainder in rdx.
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"div {0}",
in(reg) div,
inlateout("rax") duo_lo => quo,
Expand Down Expand Up @@ -271,7 +271,7 @@ unsafe fn u64_by_u32_div_rem(duo: u64, div: u32) -> (u32, u32) {
// divides the combined registers rdx:rax (`duo` is split into two 32 bit parts to do this)
// by `div`. The quotient is stored in rax and the remainder in rdx.
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"div {0}",
in(reg) div,
inlateout("rax") duo_lo => quo,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#![allow(improper_ctypes, improper_ctypes_definitions)]
// `mem::swap` cannot be used because it may generate references to memcpy in unoptimized code.
#![allow(clippy::manual_swap)]
// Support compiling on both stage0 and stage1 which may differ in supported stable features.
#![allow(stable_features)]

// We disable #[no_mangle] for tests so that we can verify the test results
// against the native compiler-rt implementations of the builtins.
Expand Down
10 changes: 5 additions & 5 deletions src/mem/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#[cfg(target_feature = "ermsb")]
pub unsafe fn copy_forward(dest: *mut u8, src: *const u8, count: usize) {
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"repe movsb (%rsi), (%rdi)",
inout("rcx") count => _,
inout("rdi") dest => _,
Expand All @@ -35,7 +35,7 @@ pub unsafe fn copy_forward(dest: *mut u8, src: *const u8, count: usize) {
let qword_count = count >> 3;
let byte_count = count & 0b111;
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"repe movsq (%rsi), (%rdi)",
"mov {byte_count:e}, %ecx",
"repe movsb (%rsi), (%rdi)",
Expand All @@ -52,7 +52,7 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, count: usize) {
let qword_count = count >> 3;
let byte_count = count & 0b111;
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"std",
"repe movsq (%rsi), (%rdi)",
"movl {byte_count:e}, %ecx",
Expand All @@ -72,7 +72,7 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, count: usize) {
#[cfg(target_feature = "ermsb")]
pub unsafe fn set_bytes(dest: *mut u8, c: u8, count: usize) {
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"repe stosb %al, (%rdi)",
inout("rcx") count => _,
inout("rdi") dest => _,
Expand All @@ -87,7 +87,7 @@ pub unsafe fn set_bytes(dest: *mut u8, c: u8, count: usize) {
let qword_count = count >> 3;
let byte_count = count & 0b111;
// FIXME: Use the Intel syntax once we drop LLVM 9 support on rust-lang/rust.
asm!(
core::arch::asm!(
"repe stosq %rax, (%rdi)",
"mov {byte_count:e}, %ecx",
"repe stosb %al, (%rdi)",
Expand Down
42 changes: 24 additions & 18 deletions src/probestack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
#![cfg(not(feature = "no-asm"))]
// We only define stack probing for these architectures today.
#![cfg(any(target_arch = "x86_64", target_arch = "x86"))]
// We need to add .att_syntax for bootstraping the new global_asm!
#![allow(unknown_lints, bad_asm_style)]

extern "C" {
pub fn __rust_probestack();
Expand All @@ -65,7 +63,6 @@ macro_rules! define_rust_probestack {
($body: expr) => {
concat!(
"
.att_syntax
.pushsection .text.__rust_probestack
.globl __rust_probestack
.type __rust_probestack, @function
Expand All @@ -86,7 +83,6 @@ macro_rules! define_rust_probestack {
($body: expr) => {
concat!(
"
.att_syntax
.globl __rust_probestack
__rust_probestack:
",
Expand All @@ -102,7 +98,6 @@ macro_rules! define_rust_probestack {
($body: expr) => {
concat!(
"
.att_syntax
.globl ___rust_probestack
___rust_probestack:
",
Expand All @@ -117,7 +112,6 @@ macro_rules! define_rust_probestack {
($body: expr) => {
concat!(
"
.att_syntax
.globl ___rust_probestack
___rust_probestack:
",
Expand All @@ -137,8 +131,9 @@ macro_rules! define_rust_probestack {
target_arch = "x86_64",
not(all(target_env = "sgx", target_vendor = "fortanix"))
))]
global_asm!(define_rust_probestack!(
"
core::arch::global_asm!(
define_rust_probestack!(
"
.cfi_startproc
pushq %rbp
.cfi_adjust_cfa_offset 8
Expand Down Expand Up @@ -188,7 +183,9 @@ global_asm!(define_rust_probestack!(
ret
.cfi_endproc
"
));
),
options(att_syntax)
);

// This function is the same as above, except that some instructions are
// [manually patched for LVI].
Expand All @@ -198,8 +195,9 @@ global_asm!(define_rust_probestack!(
target_arch = "x86_64",
all(target_env = "sgx", target_vendor = "fortanix")
))]
global_asm!(define_rust_probestack!(
"
core::arch::global_asm!(
define_rust_probestack!(
"
.cfi_startproc
pushq %rbp
.cfi_adjust_cfa_offset 8
Expand Down Expand Up @@ -251,16 +249,19 @@ global_asm!(define_rust_probestack!(
jmp *%r11
.cfi_endproc
"
));
),
options(att_syntax)
);

#[cfg(all(target_arch = "x86", not(target_os = "uefi")))]
// This is the same as x86_64 above, only translated for 32-bit sizes. Note
// that on Unix we're expected to restore everything as it was, this
// function basically can't tamper with anything.
//
// The ABI here is the same as x86_64, except everything is 32-bits large.
global_asm!(define_rust_probestack!(
"
core::arch::global_asm!(
define_rust_probestack!(
"
.cfi_startproc
push %ebp
.cfi_adjust_cfa_offset 4
Expand Down Expand Up @@ -291,7 +292,9 @@ global_asm!(define_rust_probestack!(
ret
.cfi_endproc
"
));
),
options(att_syntax)
);

#[cfg(all(target_arch = "x86", target_os = "uefi"))]
// UEFI target is windows like target. LLVM will do _chkstk things like windows.
Expand All @@ -304,8 +307,9 @@ global_asm!(define_rust_probestack!(
// MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves.
// MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
// themselves.
global_asm!(define_rust_probestack!(
"
core::arch::global_asm!(
define_rust_probestack!(
"
.cfi_startproc
push %ebp
.cfi_adjust_cfa_offset 4
Expand Down Expand Up @@ -341,4 +345,6 @@ global_asm!(define_rust_probestack!(
ret
.cfi_endproc
"
));
),
options(att_syntax)
);
6 changes: 3 additions & 3 deletions src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::intrinsics;
#[naked]
#[no_mangle]
pub unsafe extern "C" fn ___chkstk_ms() {
asm!(
core::arch::asm!(
"push %ecx",
"push %eax",
"cmp $0x1000,%eax",
Expand Down Expand Up @@ -49,7 +49,7 @@ pub unsafe extern "C" fn ___chkstk_ms() {
#[naked]
#[no_mangle]
pub unsafe extern "C" fn __alloca() {
asm!(
core::arch::asm!(
"jmp ___chkstk", // Jump to ___chkstk since fallthrough may be unreliable"
options(noreturn, att_syntax)
);
Expand All @@ -64,7 +64,7 @@ pub unsafe extern "C" fn __alloca() {
#[naked]
#[no_mangle]
pub unsafe extern "C" fn ___chkstk() {
asm!(
core::arch::asm!(
"push %ecx",
"cmp $0x1000,%eax",
"lea 8(%esp),%ecx", // esp before calling this routine -> ecx
Expand Down
6 changes: 3 additions & 3 deletions src/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::intrinsics;
#[naked]
#[no_mangle]
pub unsafe extern "C" fn ___chkstk_ms() {
asm!(
core::arch::asm!(
"push %rcx",
"push %rax",
"cmp $0x1000,%rax",
Expand Down Expand Up @@ -48,7 +48,7 @@ pub unsafe extern "C" fn ___chkstk_ms() {
#[naked]
#[no_mangle]
pub unsafe extern "C" fn __alloca() {
asm!(
core::arch::asm!(
"mov %rcx,%rax", // x64 _alloca is a normal function with parameter in rcx
"jmp ___chkstk", // Jump to ___chkstk since fallthrough may be unreliable"
options(noreturn, att_syntax)
Expand All @@ -64,7 +64,7 @@ pub unsafe extern "C" fn __alloca() {
#[naked]
#[no_mangle]
pub unsafe extern "C" fn ___chkstk() {
asm!(
core::arch::asm!(
"push %rcx",
"cmp $0x1000,%rax",
"lea 16(%rsp),%rcx", // rsp before calling this routine -> rcx
Expand Down