Skip to content

Remove remaining traces of AT&T assembly syntax #911

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions compiler-builtins/src/int/specialized_div_rem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,12 @@ unsafe fn u128_by_u64_div_rem(duo: u128, div: u64) -> (u64, u64) {
unsafe {
// 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.
core::arch::asm!(
"div {0}",
in(reg) div,
inlateout("rax") duo_lo => quo,
inlateout("rdx") duo_hi => rem,
options(att_syntax, pure, nomem, nostack)
options(pure, nomem, nostack),
);
}
(quo, rem)
Expand Down Expand Up @@ -283,13 +282,12 @@ unsafe fn u64_by_u32_div_rem(duo: u64, div: u32) -> (u32, u32) {
unsafe {
// 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.
core::arch::asm!(
"div {0}",
in(reg) div,
inlateout("rax") duo_lo => quo,
inlateout("rdx") duo_hi => rem,
options(att_syntax, pure, nomem, nostack)
options(pure, nomem, nostack),
);
}
(quo, rem)
Expand Down
52 changes: 25 additions & 27 deletions compiler-builtins/src/mem/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ use core::{intrinsics, mem};
#[inline(always)]
#[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.
core::arch::asm!(
"repe movsb (%rsi), (%rdi)",
asm!(
"repe movsb [rdi], [rsi]",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"repe movsb [rdi], [rsi]",
"rep movsb [rdi], [rsi]",

The repe mnemonic is for "repeat while equal" and only makes sense for the string comparison operations
https://www.felixcloutier.com/x86/rep:repe:repz:repne:repnz
(but the encoding is the same so I guess assemblers don't care)

inout("rcx") count => _,
inout("rdi") dest => _,
inout("rsi") src => _,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
}

Expand All @@ -42,21 +41,21 @@ pub unsafe fn copy_forward(mut dest: *mut u8, mut src: *const u8, count: usize)
inout("ecx") pre_byte_count => _,
inout("rdi") dest => dest,
inout("rsi") src => src,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
asm!(
"rep movsq",
inout("rcx") qword_count => _,
inout("rdi") dest => dest,
inout("rsi") src => src,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
asm!(
"rep movsb",
inout("ecx") byte_count => _,
inout("rdi") dest => _,
inout("rsi") src => _,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
}

Expand All @@ -67,14 +66,14 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, count: usize) {
asm!(
"std",
"rep movsb",
"sub $7, %rsi",
"sub $7, %rdi",
"mov {qword_count}, %rcx",
"sub rsi, 7",
"sub rdi, 7",
"mov rcx, {qword_count}",
"rep movsq",
"test {pre_byte_count:e}, {pre_byte_count:e}",
"add $7, %rsi",
"add $7, %rdi",
"mov {pre_byte_count:e}, %ecx",
"add rsi, 7",
"add rdi, 7",
"mov ecx, {pre_byte_count:e}",
"rep movsb",
"cld",
pre_byte_count = in(reg) pre_byte_count,
Expand All @@ -83,20 +82,19 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, count: usize) {
inout("rdi") dest.add(count - 1) => _,
inout("rsi") src.add(count - 1) => _,
// We modify flags, but we restore it afterwards
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
Comment on lines 84 to +85
Copy link
Contributor

Choose a reason for hiding this comment

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

This definitely shouldn't have preserves_flags. Flags are modified by each of add,sub,test. The comment refers to how std/cld are used to set/clear the direction flag, but that's separate since it must be cleared before exiting the inline assembly.

Actually, what is the point of the test instruction here? It only affects flags, and none of the subsequent instructions depend on any flags.

);
}

#[inline(always)]
#[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.
core::arch::asm!(
"repe stosb %al, (%rdi)",
asm!(
"repe stosb [rdi], al",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"repe stosb [rdi], al",
"rep stosb [rdi], al",

inout("rcx") count => _,
inout("rdi") dest => _,
inout("al") c => _,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
)
}

Expand All @@ -111,21 +109,21 @@ pub unsafe fn set_bytes(mut dest: *mut u8, c: u8, count: usize) {
inout("ecx") pre_byte_count => _,
inout("rdi") dest => dest,
in("rax") c,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
asm!(
"rep stosq",
inout("rcx") qword_count => _,
inout("rdi") dest => dest,
in("rax") c,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
asm!(
"rep stosb",
inout("ecx") byte_count => _,
inout("rdi") dest => _,
in("rax") c,
options(att_syntax, nostack, preserves_flags)
options(nostack, preserves_flags)
);
}

Expand Down Expand Up @@ -212,10 +210,10 @@ pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
let x = {
let r;
asm!(
"movdqa ({addr}), {dest}",
"movdqa {dest}, [{addr}]",
addr = in(reg) s,
dest = out(xmm_reg) r,
options(att_syntax, nostack),
options(nostack, preserves_flags),
);
r
};
Expand All @@ -232,10 +230,10 @@ pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
let x = {
let r;
asm!(
"movdqa ({addr}), {dest}",
"movdqa {dest}, [{addr}]",
addr = in(reg) s,
dest = out(xmm_reg) r,
options(att_syntax, nostack),
options(nostack, preserves_flags),
);
r
};
Expand Down Expand Up @@ -277,10 +275,10 @@ pub unsafe fn c_string_length(mut s: *const core::ffi::c_char) -> usize {
let mut cs = {
let r: u64;
asm!(
"mov ({addr}), {dest}",
"mov {dest}, [{addr}]",
addr = in(reg) s,
dest = out(reg) r,
options(att_syntax, nostack),
options(nostack, preserves_flags),
);
r
};
Expand Down
34 changes: 16 additions & 18 deletions compiler-builtins/src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ intrinsics! {
pub unsafe extern "C" fn __chkstk() {
core::arch::naked_asm!(
"jmp __alloca", // Jump to __alloca since fallthrough may be unreliable"
options(att_syntax)
);
}

Expand All @@ -28,26 +27,25 @@ intrinsics! {
pub unsafe extern "C" fn _alloca() {
// __chkstk and _alloca are the same function
core::arch::naked_asm!(
"push %ecx",
"cmp $0x1000,%eax",
"lea 8(%esp),%ecx", // esp before calling this routine -> ecx
"jb 1f",
"push ecx",
"cmp eax, 0x1000",
"lea ecx, [esp + 8]", // esp before calling this routine -> ecx
"jb 3f",
"2:",
"sub $0x1000,%ecx",
"test %ecx,(%ecx)",
"sub $0x1000,%eax",
"cmp $0x1000,%eax",
"sub ecx, 0x1000",
"test [ecx], ecx",
"sub eax, 0x1000",
"cmp eax, 0x1000",
"ja 2b",
"1:",
"sub %eax,%ecx",
"test %ecx,(%ecx)",
"lea 4(%esp),%eax", // load pointer to the return address into eax
"mov %ecx,%esp", // install the new top of stack pointer into esp
"mov -4(%eax),%ecx", // restore ecx
"push (%eax)", // push return address onto the stack
"sub %esp,%eax", // restore the original value in eax
"3:",
"sub ecx, eax",
"test [ecx], ecx",
"lea eax, [esp + 4]", // load pointer to the return address into eax
"mov esp, ecx", // install the new top of stack pointer into esp
"mov ecx, [eax - 4]", // restore ecx
"push [eax]", // push return address onto the stack
"sub eax, esp", // restore the original value in eax
"ret",
options(att_syntax)
);
}
}
29 changes: 14 additions & 15 deletions compiler-builtins/src/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ intrinsics! {
))]
pub unsafe extern "C" fn ___chkstk_ms() {
core::arch::naked_asm!(
"push %rcx",
"push %rax",
"cmp $0x1000,%rax",
"lea 24(%rsp),%rcx",
"jb 1f",
"push rcx",
"push rax",
"cmp rax, 0x1000",
"lea rcx, [rsp + 24]",
"jb 3f",
"2:",
"sub $0x1000,%rcx",
"test %rcx,(%rcx)",
"sub $0x1000,%rax",
"cmp $0x1000,%rax",
"sub rcx, 0x1000",
"test [rcx], rcx",
"sub rax, 0x1000",
"cmp rax, 0x1000",
"ja 2b",
"1:",
"sub %rax,%rcx",
"test %rcx,(%rcx)",
"pop %rax",
"pop %rcx",
"3:",
"sub rcx, rax",
"test [rcx], rcx",
"pop rax",
"pop rcx",
"ret",
options(att_syntax)
);
}
}
Expand Down