Skip to content

Commit 67ebb6c

Browse files
committed
Fix AArch64InlineAsmReg::emit
1 parent 3678036 commit 67ebb6c

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

Diff for: 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
}

Diff for: compiler/rustc_target/src/asm/aarch64.rs

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

0 commit comments

Comments
 (0)