Skip to content

Commit 273efe4

Browse files
committed
Fix AArch64InlineAsmReg::emit
1 parent d858dfe commit 273efe4

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

+5-46
Original file line numberDiff line numberDiff line change
@@ -542,57 +542,16 @@ fn xmm_reg_index(reg: InlineAsmReg) -> Option<u32> {
542542

543543
/// If the register is an AArch64 integer register then return its index.
544544
fn a64_reg_index(reg: InlineAsmReg) -> Option<u32> {
545-
use AArch64InlineAsmReg::*;
546-
// Unlike `a64_vreg_index`, we can't subtract `x0` to get the u32 because
547-
// `x19` and `x29` are missing and the integer constants for the
548-
// `x0`..`x30` enum variants don't all match the register number. E.g. the
549-
// integer constant for `x18` is 18, but the constant for `x20` is 19.
550-
Some(match reg {
551-
InlineAsmReg::AArch64(r) => match r {
552-
x0 => 0,
553-
x1 => 1,
554-
x2 => 2,
555-
x3 => 3,
556-
x4 => 4,
557-
x5 => 5,
558-
x6 => 6,
559-
x7 => 7,
560-
x8 => 8,
561-
x9 => 9,
562-
x10 => 10,
563-
x11 => 11,
564-
x12 => 12,
565-
x13 => 13,
566-
x14 => 14,
567-
x15 => 15,
568-
x16 => 16,
569-
x17 => 17,
570-
x18 => 18,
571-
// x19 is reserved
572-
x20 => 20,
573-
x21 => 21,
574-
x22 => 22,
575-
x23 => 23,
576-
x24 => 24,
577-
x25 => 25,
578-
x26 => 26,
579-
x27 => 27,
580-
x28 => 28,
581-
// x29 is reserved
582-
x30 => 30,
583-
_ => return None,
584-
},
585-
_ => return None,
586-
})
545+
match reg {
546+
InlineAsmReg::AArch64(r) => r.reg_index(),
547+
_ => None,
548+
}
587549
}
588550

589551
/// If the register is an AArch64 vector register then return its index.
590552
fn a64_vreg_index(reg: InlineAsmReg) -> Option<u32> {
591-
use AArch64InlineAsmReg::*;
592553
match reg {
593-
InlineAsmReg::AArch64(reg) if reg as u32 >= v0 as u32 && reg as u32 <= v31 as u32 => {
594-
Some(reg as u32 - v0 as u32)
595-
}
554+
InlineAsmReg::AArch64(reg) => reg.vreg_index(),
596555
_ => None,
597556
}
598557
}

compiler/rustc_target/src/asm/aarch64.rs

+57-3
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,66 @@ impl AArch64InlineAsmReg {
201201
_arch: InlineAsmArch,
202202
modifier: Option<char>,
203203
) -> fmt::Result {
204-
let (prefix, index) = if (self as u32) < Self::v0 as u32 {
205-
(modifier.unwrap_or('x'), self as u32 - Self::x0 as u32)
204+
let (prefix, index) = if let Some(index) = self.reg_index() {
205+
(modifier.unwrap_or('x'), index)
206+
} else if let Some(index) = self.vreg_index() {
207+
(modifier.unwrap_or('v'), index)
206208
} else {
207-
(modifier.unwrap_or('v'), self as u32 - Self::v0 as u32)
209+
return out.write_str(self.name());
208210
};
209211
assert!(index < 32);
210212
write!(out, "{prefix}{index}")
211213
}
214+
215+
/// If the register is an integer register then return its index.
216+
pub fn reg_index(self) -> Option<u32> {
217+
// Unlike `vreg_index`, we can't subtract `x0` to get the u32 because
218+
// `x19` and `x29` are missing and the integer constants for the
219+
// `x0`..`x30` enum variants don't all match the register number. E.g. the
220+
// integer constant for `x18` is 18, but the constant for `x20` is 19.
221+
use AArch64InlineAsmReg::*;
222+
Some(match self {
223+
x0 => 0,
224+
x1 => 1,
225+
x2 => 2,
226+
x3 => 3,
227+
x4 => 4,
228+
x5 => 5,
229+
x6 => 6,
230+
x7 => 7,
231+
x8 => 8,
232+
x9 => 9,
233+
x10 => 10,
234+
x11 => 11,
235+
x12 => 12,
236+
x13 => 13,
237+
x14 => 14,
238+
x15 => 15,
239+
x16 => 16,
240+
x17 => 17,
241+
x18 => 18,
242+
// x19 is reserved
243+
x20 => 20,
244+
x21 => 21,
245+
x22 => 22,
246+
x23 => 23,
247+
x24 => 24,
248+
x25 => 25,
249+
x26 => 26,
250+
x27 => 27,
251+
x28 => 28,
252+
// x29 is reserved
253+
x30 => 30,
254+
_ => return None,
255+
})
256+
}
257+
258+
/// If the register is a vector register then return its index.
259+
pub fn vreg_index(self) -> Option<u32> {
260+
use AArch64InlineAsmReg::*;
261+
if self as u32 >= v0 as u32 && self as u32 <= v31 as u32 {
262+
return Some(self as u32 - v0 as u32);
263+
}
264+
None
265+
}
212266
}

0 commit comments

Comments
 (0)