Skip to content

emit .att_syntax when global/naked asm use that option #143599

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 1 commit into from
Jul 10, 2025
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
22 changes: 15 additions & 7 deletions compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,19 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
) {
let asm_arch = self.tcx.sess.asm_arch.unwrap();

// Default to Intel syntax on x86
let intel_syntax = matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
&& !options.contains(InlineAsmOptions::ATT_SYNTAX);

// Build the template string
let mut template_str = String::new();
if intel_syntax {
template_str.push_str(".intel_syntax\n");

// On X86 platforms there are two assembly syntaxes. Rust uses intel by default,
// but AT&T can be specified explicitly.
if matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64) {
if options.contains(InlineAsmOptions::ATT_SYNTAX) {
template_str.push_str(".att_syntax\n")
} else {
template_str.push_str(".intel_syntax\n")
}
}

for piece in template {
match *piece {
InlineAsmTemplatePiece::String(ref s) => template_str.push_str(s),
Expand Down Expand Up @@ -431,7 +435,11 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
}
}
}
if intel_syntax {

// Just to play it safe, if intel was used, reset the assembly syntax to att.
if matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
&& !options.contains(InlineAsmOptions::ATT_SYNTAX)
{
template_str.push_str("\n.att_syntax\n");
}

Expand Down
75 changes: 75 additions & 0 deletions tests/assembly/emit-intel-att-syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//@ assembly-output: emit-asm
//@ revisions: att intel
//@ [att] compile-flags: -Cllvm-args=-x86-asm-syntax=att
//@ [intel] compile-flags: -Cllvm-args=-x86-asm-syntax=intel
//@ only-x86_64

#![crate_type = "lib"]

// CHECK-LABEL: naked_att:
// intel-CHECK: mov rax, qword ptr [rdi]
// intel-CHECK: ret
// att-CHECK: movq (%rdi), %rax
// att-CHECK: retq

#[unsafe(naked)]
#[unsafe(no_mangle)]
extern "sysv64" fn naked_att() {
std::arch::naked_asm!(
"
movq (%rdi), %rax
retq
",
options(att_syntax),
);
}

// CHECK-LABEL: naked_intel:
// intel-CHECK: mov rax, rdi
// intel-CHECK: ret
// att-CHECK: movq (%rdi), %rax
// att-CHECK: retq

#[unsafe(naked)]
#[unsafe(no_mangle)]
extern "sysv64" fn naked_intel() {
std::arch::naked_asm!(
"
mov rax, rdi
ret
",
options(),
);
}

// CHECK-LABEL: global_att:
// intel-CHECK: mov rax, rdi
// intel-CHECK: ret
// att-CHECK: movq (%rdi), %rax
// att-CHECK: retq

core::arch::global_asm!(
"
.globl global_att
global_att:
movq (%rdi), %rax
retq
",
options(att_syntax),
);

// CHECK-LABEL: global_intel:
// intel-CHECK: mov rax, rdi
// intel-CHECK: ret
// att-CHECK: movq (%rdi), %rax
// att-CHECK: retq

core::arch::global_asm!(
"
.globl global_intel
global_intel:
mov rax, rdi
ret
",
options(),
);
Loading