From 275acf27b32339edd264a1a4dffd955c0e758a83 Mon Sep 17 00:00:00 2001 From: Tobias Decking Date: Thu, 8 May 2025 09:12:53 +0200 Subject: [PATCH 1/2] Remove remaining traces of AT&T assembly syntax --- .../src/int/specialized_div_rem/mod.rs | 6 +-- compiler-builtins/src/mem/x86_64.rs | 52 +++++++++---------- compiler-builtins/src/x86.rs | 30 +++++------ compiler-builtins/src/x86_64.rs | 25 +++++---- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/compiler-builtins/src/int/specialized_div_rem/mod.rs b/compiler-builtins/src/int/specialized_div_rem/mod.rs index 43f466e75..14756bc3a 100644 --- a/compiler-builtins/src/int/specialized_div_rem/mod.rs +++ b/compiler-builtins/src/int/specialized_div_rem/mod.rs @@ -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) @@ -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) diff --git a/compiler-builtins/src/mem/x86_64.rs b/compiler-builtins/src/mem/x86_64.rs index 5cbe83ab1..fec282979 100644 --- a/compiler-builtins/src/mem/x86_64.rs +++ b/compiler-builtins/src/mem/x86_64.rs @@ -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]", inout("rcx") count => _, inout("rdi") dest => _, inout("rsi") src => _, - options(att_syntax, nostack, preserves_flags) + options(nostack, preserves_flags) ); } @@ -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) ); } @@ -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, @@ -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) ); } #[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", inout("rcx") count => _, inout("rdi") dest => _, inout("al") c => _, - options(att_syntax, nostack, preserves_flags) + options(nostack, preserves_flags) ) } @@ -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) ); } @@ -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 }; @@ -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 }; @@ -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 }; diff --git a/compiler-builtins/src/x86.rs b/compiler-builtins/src/x86.rs index 01152d9c7..d2dcbdd03 100644 --- a/compiler-builtins/src/x86.rs +++ b/compiler-builtins/src/x86.rs @@ -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) ); } @@ -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 + "push ecx", + "cmp eax, 0x1000", + "lea ecx, [esp + 8]", // esp before calling this routine -> ecx "jb 1f", "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 + "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) ); } } diff --git a/compiler-builtins/src/x86_64.rs b/compiler-builtins/src/x86_64.rs index fc1190f79..2ce4236c8 100644 --- a/compiler-builtins/src/x86_64.rs +++ b/compiler-builtins/src/x86_64.rs @@ -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", + "push rcx", + "push rax", + "cmp rax, 0x1000", + "lea rcx, [rsp + 24]", "jb 1f", "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", + "sub rcx, rax", + "test [rcx], rcx", + "pop rax", + "pop rcx", "ret", - options(att_syntax) ); } } From ee144cf1f585afab2ebfd9adad182f0877532bb2 Mon Sep 17 00:00:00 2001 From: Tobias Decking Date: Thu, 8 May 2025 11:38:07 +0200 Subject: [PATCH 2/2] Fix asm labels --- compiler-builtins/src/x86.rs | 4 ++-- compiler-builtins/src/x86_64.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler-builtins/src/x86.rs b/compiler-builtins/src/x86.rs index d2dcbdd03..afcf10c07 100644 --- a/compiler-builtins/src/x86.rs +++ b/compiler-builtins/src/x86.rs @@ -30,14 +30,14 @@ intrinsics! { "push ecx", "cmp eax, 0x1000", "lea ecx, [esp + 8]", // esp before calling this routine -> ecx - "jb 1f", + "jb 3f", "2:", "sub ecx, 0x1000", "test [ecx], ecx", "sub eax, 0x1000", "cmp eax, 0x1000", "ja 2b", - "1:", + "3:", "sub ecx, eax", "test [ecx], ecx", "lea eax, [esp + 4]", // load pointer to the return address into eax diff --git a/compiler-builtins/src/x86_64.rs b/compiler-builtins/src/x86_64.rs index 2ce4236c8..0cc700424 100644 --- a/compiler-builtins/src/x86_64.rs +++ b/compiler-builtins/src/x86_64.rs @@ -23,14 +23,14 @@ intrinsics! { "push rax", "cmp rax, 0x1000", "lea rcx, [rsp + 24]", - "jb 1f", + "jb 3f", "2:", "sub rcx, 0x1000", "test [rcx], rcx", "sub rax, 0x1000", "cmp rax, 0x1000", "ja 2b", - "1:", + "3:", "sub rcx, rax", "test [rcx], rcx", "pop rax",