Skip to content

Commit f38c83c

Browse files
authored
[AArch64][llvm] Disassemble instructions in SYS alias encoding space more correctly (#153905)
For instructions in the `SYS` alias encoding space which take no register operands, and where the unused 5 register bits are not all set (0x31, 0b11111), then disassemble to a `SYS` alias and not the instruction, since it is not considered valid. This is because it is specified in the Arm ARM in text similar to this (e.g. page C5-1037 of DDI0487L.b for `TLBI ALLE1`, or page C5-1585 for `GCSPOPX`): ``` Rt should be encoded as 0b11111. If the Rt field is not set to 0b11111, it is CONSTRAINED UNPREDICTABLE whether: * The instruction is UNDEFINED. * The instruction behaves as if the Rt field is set to 0b11111. ``` Since we want to follow "should" directives, and not encourage undefined behaviour, only assemble or disassemble instructions considered valid. Add an extra test-case for this, and all existing test-cases are continuing to pass.
1 parent 31d2db2 commit f38c83c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,14 +1017,22 @@ bool AArch64InstPrinter::printSysAlias(const MCInst *MI,
10171017
else
10181018
return false;
10191019

1020+
StringRef Reg = getRegisterName(MI->getOperand(4).getReg());
1021+
bool NotXZR = Reg != "xzr";
1022+
1023+
// If a mandatory is not specified in the TableGen
1024+
// (i.e. no register operand should be present), and the register value
1025+
// is not xzr/x31, then disassemble to a SYS alias instead.
1026+
if (NotXZR && !NeedsReg)
1027+
return false;
1028+
10201029
std::string Str = Ins + Name;
10211030
llvm::transform(Str, Str.begin(), ::tolower);
10221031

10231032
O << '\t' << Str;
1024-
if (NeedsReg) {
1025-
O << ", ";
1026-
printRegName(O, MI->getOperand(4).getReg());
1027-
}
1033+
1034+
if (NeedsReg)
1035+
O << ", " << Reg;
10281036

10291037
return true;
10301038
}

llvm/test/MC/AArch64/arm64-aliases.s

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,20 @@ foo:
512512
sys #4, c8, c3, #6
513513
; CHECK: tlbi vmalls12e1is
514514

515+
; Check that all 5 register bits are set (0x31):
516+
; (from Arm ARM regarding TLBI instructions without operands)
517+
; "Rt should be encoded as 0b11111. If the Rt field is not set to 0b11111,
518+
; it is CONSTRAINED UNPREDICTABLE whether:
519+
; * The instruction is UNDEFINED.
520+
; * The instruction behaves as if the Rt field is set to 0b11111."
521+
;
522+
; Do not disassemble this to `tlbi` but a SYS alias instead
523+
;
524+
sys #4, c8, c7, #6, x30
525+
; CHECK: sys #0x4, c8, c7, #0x6, x30
526+
sys #4, c8, c7, #6, x31
527+
; CHECK: tlbi vmalls12e1
528+
515529
ic ialluis
516530
; CHECK: ic ialluis ; encoding: [0x1f,0x71,0x08,0xd5]
517531
ic iallu

0 commit comments

Comments
 (0)