Skip to content

Commit 9e6af56

Browse files
authored
Rollup merge of #143599 - folkertdev:x86-asm-syntax-global-naked-asm, r=Amanieu
emit `.att_syntax` when global/naked asm use that option fixes #143542 LLVM would error when using `-Cllvm-args=-x86-asm-syntax=intel` in combination with global/naked assembly with `att_syntax`. It turns out that for LLVM you do in this case need to emit `.att_syntax`. r? `@Amanieu`
2 parents 05c68f3 + 6fc5d4e commit 9e6af56

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,19 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
384384
) {
385385
let asm_arch = self.tcx.sess.asm_arch.unwrap();
386386

387-
// Default to Intel syntax on x86
388-
let intel_syntax = matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
389-
&& !options.contains(InlineAsmOptions::ATT_SYNTAX);
390-
391387
// Build the template string
392388
let mut template_str = String::new();
393-
if intel_syntax {
394-
template_str.push_str(".intel_syntax\n");
389+
390+
// On X86 platforms there are two assembly syntaxes. Rust uses intel by default,
391+
// but AT&T can be specified explicitly.
392+
if matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64) {
393+
if options.contains(InlineAsmOptions::ATT_SYNTAX) {
394+
template_str.push_str(".att_syntax\n")
395+
} else {
396+
template_str.push_str(".intel_syntax\n")
397+
}
395398
}
399+
396400
for piece in template {
397401
match *piece {
398402
InlineAsmTemplatePiece::String(ref s) => template_str.push_str(s),
@@ -431,7 +435,11 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
431435
}
432436
}
433437
}
434-
if intel_syntax {
438+
439+
// Just to play it safe, if intel was used, reset the assembly syntax to att.
440+
if matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
441+
&& !options.contains(InlineAsmOptions::ATT_SYNTAX)
442+
{
435443
template_str.push_str("\n.att_syntax\n");
436444
}
437445

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//@ assembly-output: emit-asm
2+
//@ revisions: att intel
3+
//@ [att] compile-flags: -Cllvm-args=-x86-asm-syntax=att
4+
//@ [intel] compile-flags: -Cllvm-args=-x86-asm-syntax=intel
5+
//@ only-x86_64
6+
7+
#![crate_type = "lib"]
8+
9+
// CHECK-LABEL: naked_att:
10+
// intel-CHECK: mov rax, qword ptr [rdi]
11+
// intel-CHECK: ret
12+
// att-CHECK: movq (%rdi), %rax
13+
// att-CHECK: retq
14+
15+
#[unsafe(naked)]
16+
#[unsafe(no_mangle)]
17+
extern "sysv64" fn naked_att() {
18+
std::arch::naked_asm!(
19+
"
20+
movq (%rdi), %rax
21+
retq
22+
",
23+
options(att_syntax),
24+
);
25+
}
26+
27+
// CHECK-LABEL: naked_intel:
28+
// intel-CHECK: mov rax, rdi
29+
// intel-CHECK: ret
30+
// att-CHECK: movq (%rdi), %rax
31+
// att-CHECK: retq
32+
33+
#[unsafe(naked)]
34+
#[unsafe(no_mangle)]
35+
extern "sysv64" fn naked_intel() {
36+
std::arch::naked_asm!(
37+
"
38+
mov rax, rdi
39+
ret
40+
",
41+
options(),
42+
);
43+
}
44+
45+
// CHECK-LABEL: global_att:
46+
// intel-CHECK: mov rax, rdi
47+
// intel-CHECK: ret
48+
// att-CHECK: movq (%rdi), %rax
49+
// att-CHECK: retq
50+
51+
core::arch::global_asm!(
52+
"
53+
.globl global_att
54+
global_att:
55+
movq (%rdi), %rax
56+
retq
57+
",
58+
options(att_syntax),
59+
);
60+
61+
// CHECK-LABEL: global_intel:
62+
// intel-CHECK: mov rax, rdi
63+
// intel-CHECK: ret
64+
// att-CHECK: movq (%rdi), %rax
65+
// att-CHECK: retq
66+
67+
core::arch::global_asm!(
68+
"
69+
.globl global_intel
70+
global_intel:
71+
mov rax, rdi
72+
ret
73+
",
74+
options(),
75+
);

0 commit comments

Comments
 (0)