1 //===- AArch64Disassembler.cpp - Disassembler for AArch64 ISA -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the functions necessary to decode AArch64 instruction
11 // bitpatterns into MCInsts (with the help of TableGenerated information from
12 // the instruction definitions).
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "arm-disassembler"
19 #include "AArch64RegisterInfo.h"
20 #include "AArch64Subtarget.h"
21 #include "Utils/AArch64BaseInfo.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrDesc.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCDisassembler.h"
27 #include "llvm/MC/MCFixedLenDisassembler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/MemoryObject.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/raw_ostream.h"
36 typedef MCDisassembler::DecodeStatus DecodeStatus;
39 /// AArch64 disassembler for all AArch64 platforms.
40 class AArch64Disassembler : public MCDisassembler {
41 OwningPtr<const MCRegisterInfo> RegInfo;
43 /// Initializes the disassembler.
45 AArch64Disassembler(const MCSubtargetInfo &STI, const MCRegisterInfo *Info)
46 : MCDisassembler(STI), RegInfo(Info) {
49 ~AArch64Disassembler() {}
51 /// See MCDisassembler.
52 DecodeStatus getInstruction(MCInst &instr,
54 const MemoryObject ®ion,
57 raw_ostream &cStream) const;
59 const MCRegisterInfo *getRegInfo() const { return RegInfo.get(); }
64 // Forward-declarations used in the auto-generated files.
65 static DecodeStatus DecodeGPR64RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
66 uint64_t Address, const void *Decoder);
68 DecodeGPR64xspRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
69 uint64_t Address, const void *Decoder);
71 static DecodeStatus DecodeGPR32RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
72 uint64_t Address, const void *Decoder);
74 DecodeGPR32wspRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
75 uint64_t Address, const void *Decoder);
77 static DecodeStatus DecodeFPR8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
78 uint64_t Address, const void *Decoder);
79 static DecodeStatus DecodeFPR16RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
80 uint64_t Address, const void *Decoder);
81 static DecodeStatus DecodeFPR32RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
82 uint64_t Address, const void *Decoder);
83 static DecodeStatus DecodeFPR64RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
84 uint64_t Address, const void *Decoder);
85 static DecodeStatus DecodeFPR128RegisterClass(llvm::MCInst &Inst,
86 unsigned RegNo, uint64_t Address,
89 static DecodeStatus DecodeAddrRegExtendOperand(llvm::MCInst &Inst,
95 static DecodeStatus DecodeBitfield32ImmOperand(llvm::MCInst &Inst,
100 static DecodeStatus DecodeCVT32FixedPosOperand(llvm::MCInst &Inst,
103 const void *Decoder);
105 static DecodeStatus DecodeFPZeroOperand(llvm::MCInst &Inst,
108 const void *Decoder);
110 static DecodeStatus DecodeShiftRightImm8(MCInst &Inst, unsigned Val,
111 uint64_t Address, const void *Decoder);
112 static DecodeStatus DecodeShiftRightImm16(MCInst &Inst, unsigned Val,
114 const void *Decoder);
115 static DecodeStatus DecodeShiftRightImm32(MCInst &Inst, unsigned Val,
117 const void *Decoder);
118 static DecodeStatus DecodeShiftRightImm64(MCInst &Inst, unsigned Val,
120 const void *Decoder);
122 template<int RegWidth>
123 static DecodeStatus DecodeMoveWideImmOperand(llvm::MCInst &Inst,
126 const void *Decoder);
128 template<int RegWidth>
129 static DecodeStatus DecodeLogicalImmOperand(llvm::MCInst &Inst,
132 const void *Decoder);
134 static DecodeStatus DecodeRegExtendOperand(llvm::MCInst &Inst,
135 unsigned ShiftAmount,
137 const void *Decoder);
138 template <A64SE::ShiftExtSpecifiers Ext, bool IsHalf>
140 DecodeNeonMovImmShiftOperand(llvm::MCInst &Inst, unsigned ShiftAmount,
141 uint64_t Address, const void *Decoder);
143 static DecodeStatus Decode32BitShiftOperand(llvm::MCInst &Inst,
144 unsigned ShiftAmount,
146 const void *Decoder);
147 static DecodeStatus DecodeBitfieldInstruction(llvm::MCInst &Inst, unsigned Insn,
149 const void *Decoder);
151 static DecodeStatus DecodeFMOVLaneInstruction(llvm::MCInst &Inst, unsigned Insn,
153 const void *Decoder);
155 static DecodeStatus DecodeLDSTPairInstruction(llvm::MCInst &Inst,
158 const void *Decoder);
160 static DecodeStatus DecodeLoadPairExclusiveInstruction(llvm::MCInst &Inst,
163 const void *Decoder);
165 template<typename SomeNamedImmMapper>
166 static DecodeStatus DecodeNamedImmOperand(llvm::MCInst &Inst,
169 const void *Decoder);
172 DecodeSysRegOperand(const A64SysReg::SysRegMapper &InstMapper,
173 llvm::MCInst &Inst, unsigned Val,
174 uint64_t Address, const void *Decoder);
176 static DecodeStatus DecodeMRSOperand(llvm::MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeMSROperand(llvm::MCInst &Inst,
184 const void *Decoder);
187 static DecodeStatus DecodeSingleIndexedInstruction(llvm::MCInst &Inst,
190 const void *Decoder);
193 static bool Check(DecodeStatus &Out, DecodeStatus In);
195 #include "AArch64GenDisassemblerTables.inc"
196 #include "AArch64GenInstrInfo.inc"
198 static bool Check(DecodeStatus &Out, DecodeStatus In) {
200 case MCDisassembler::Success:
201 // Out stays the same.
203 case MCDisassembler::SoftFail:
206 case MCDisassembler::Fail:
210 llvm_unreachable("Invalid DecodeStatus!");
213 DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
214 const MemoryObject &Region,
217 raw_ostream &cs) const {
222 // We want to read exactly 4 bytes of data.
223 if (Region.readBytes(Address, 4, bytes) == -1) {
225 return MCDisassembler::Fail;
228 // Encoded as a small-endian 32-bit word in the stream.
229 uint32_t insn = (bytes[3] << 24) |
234 // Calling the auto-generated decoder function.
235 DecodeStatus result = decodeInstruction(DecoderTableA6432, MI, insn, Address,
237 if (result != MCDisassembler::Fail) {
244 return MCDisassembler::Fail;
247 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
248 const AArch64Disassembler *Dis = static_cast<const AArch64Disassembler*>(D);
249 return Dis->getRegInfo()->getRegClass(RC).getRegister(RegNo);
252 static DecodeStatus DecodeGPR64RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
253 uint64_t Address, const void *Decoder) {
255 return MCDisassembler::Fail;
257 uint16_t Register = getReg(Decoder, AArch64::GPR64RegClassID, RegNo);
258 Inst.addOperand(MCOperand::CreateReg(Register));
259 return MCDisassembler::Success;
263 DecodeGPR64xspRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
264 uint64_t Address, const void *Decoder) {
266 return MCDisassembler::Fail;
268 uint16_t Register = getReg(Decoder, AArch64::GPR64xspRegClassID, RegNo);
269 Inst.addOperand(MCOperand::CreateReg(Register));
270 return MCDisassembler::Success;
273 static DecodeStatus DecodeGPR32RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
275 const void *Decoder) {
277 return MCDisassembler::Fail;
279 uint16_t Register = getReg(Decoder, AArch64::GPR32RegClassID, RegNo);
280 Inst.addOperand(MCOperand::CreateReg(Register));
281 return MCDisassembler::Success;
285 DecodeGPR32wspRegisterClass(llvm::MCInst &Inst, unsigned RegNo,
286 uint64_t Address, const void *Decoder) {
288 return MCDisassembler::Fail;
290 uint16_t Register = getReg(Decoder, AArch64::GPR32wspRegClassID, RegNo);
291 Inst.addOperand(MCOperand::CreateReg(Register));
292 return MCDisassembler::Success;
296 DecodeFPR8RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
297 uint64_t Address, const void *Decoder) {
299 return MCDisassembler::Fail;
301 uint16_t Register = getReg(Decoder, AArch64::FPR8RegClassID, RegNo);
302 Inst.addOperand(MCOperand::CreateReg(Register));
303 return MCDisassembler::Success;
307 DecodeFPR16RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
308 uint64_t Address, const void *Decoder) {
310 return MCDisassembler::Fail;
312 uint16_t Register = getReg(Decoder, AArch64::FPR16RegClassID, RegNo);
313 Inst.addOperand(MCOperand::CreateReg(Register));
314 return MCDisassembler::Success;
319 DecodeFPR32RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
320 uint64_t Address, const void *Decoder) {
322 return MCDisassembler::Fail;
324 uint16_t Register = getReg(Decoder, AArch64::FPR32RegClassID, RegNo);
325 Inst.addOperand(MCOperand::CreateReg(Register));
326 return MCDisassembler::Success;
330 DecodeFPR64RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
331 uint64_t Address, const void *Decoder) {
333 return MCDisassembler::Fail;
335 uint16_t Register = getReg(Decoder, AArch64::FPR64RegClassID, RegNo);
336 Inst.addOperand(MCOperand::CreateReg(Register));
337 return MCDisassembler::Success;
342 DecodeFPR128RegisterClass(llvm::MCInst &Inst, unsigned RegNo,
343 uint64_t Address, const void *Decoder) {
345 return MCDisassembler::Fail;
347 uint16_t Register = getReg(Decoder, AArch64::FPR128RegClassID, RegNo);
348 Inst.addOperand(MCOperand::CreateReg(Register));
349 return MCDisassembler::Success;
352 static DecodeStatus DecodeAddrRegExtendOperand(llvm::MCInst &Inst,
355 const void *Decoder) {
356 // Option{1} must be 1. OptionHiS is made up of {Option{2}, Option{1},
357 // S}. Hence we want to check bit 1.
358 if (!(OptionHiS & 2))
359 return MCDisassembler::Fail;
361 Inst.addOperand(MCOperand::CreateImm(OptionHiS));
362 return MCDisassembler::Success;
365 static DecodeStatus DecodeBitfield32ImmOperand(llvm::MCInst &Inst,
368 const void *Decoder) {
369 // In the 32-bit variant, bit 6 must be zero. I.e. the immediate must be
372 return MCDisassembler::Fail;
374 Inst.addOperand(MCOperand::CreateImm(Imm6Bits));
375 return MCDisassembler::Success;
378 static DecodeStatus DecodeCVT32FixedPosOperand(llvm::MCInst &Inst,
381 const void *Decoder) {
382 // 1 <= Imm <= 32. Encoded as 64 - Imm so: 63 >= Encoded >= 32.
384 return MCDisassembler::Fail;
386 Inst.addOperand(MCOperand::CreateImm(Imm6Bits));
387 return MCDisassembler::Success;
390 static DecodeStatus DecodeFPZeroOperand(llvm::MCInst &Inst,
393 const void *Decoder) {
394 // Any bits are valid in the instruction (they're architecturally ignored),
395 // but a code generator should insert 0.
396 Inst.addOperand(MCOperand::CreateImm(0));
397 return MCDisassembler::Success;
400 static DecodeStatus DecodeShiftRightImm8(MCInst &Inst, unsigned Val,
402 const void *Decoder) {
403 Inst.addOperand(MCOperand::CreateImm(8 - Val));
404 return MCDisassembler::Success;
407 static DecodeStatus DecodeShiftRightImm16(MCInst &Inst, unsigned Val,
409 const void *Decoder) {
410 Inst.addOperand(MCOperand::CreateImm(16 - Val));
411 return MCDisassembler::Success;
414 static DecodeStatus DecodeShiftRightImm32(MCInst &Inst, unsigned Val,
416 const void *Decoder) {
417 Inst.addOperand(MCOperand::CreateImm(32 - Val));
418 return MCDisassembler::Success;
421 static DecodeStatus DecodeShiftRightImm64(MCInst &Inst, unsigned Val,
423 const void *Decoder) {
424 Inst.addOperand(MCOperand::CreateImm(64 - Val));
425 return MCDisassembler::Success;
428 template<int RegWidth>
429 static DecodeStatus DecodeMoveWideImmOperand(llvm::MCInst &Inst,
432 const void *Decoder) {
433 unsigned Imm16 = FullImm & 0xffff;
434 unsigned Shift = FullImm >> 16;
436 if (RegWidth == 32 && Shift > 1) return MCDisassembler::Fail;
438 Inst.addOperand(MCOperand::CreateImm(Imm16));
439 Inst.addOperand(MCOperand::CreateImm(Shift));
440 return MCDisassembler::Success;
443 template<int RegWidth>
444 static DecodeStatus DecodeLogicalImmOperand(llvm::MCInst &Inst,
447 const void *Decoder) {
449 if (!A64Imms::isLogicalImmBits(RegWidth, Bits, Imm))
450 return MCDisassembler::Fail;
452 Inst.addOperand(MCOperand::CreateImm(Bits));
453 return MCDisassembler::Success;
457 static DecodeStatus DecodeRegExtendOperand(llvm::MCInst &Inst,
458 unsigned ShiftAmount,
460 const void *Decoder) {
461 // Only values 0-4 are valid for this 3-bit field
463 return MCDisassembler::Fail;
465 Inst.addOperand(MCOperand::CreateImm(ShiftAmount));
466 return MCDisassembler::Success;
469 static DecodeStatus Decode32BitShiftOperand(llvm::MCInst &Inst,
470 unsigned ShiftAmount,
472 const void *Decoder) {
473 // Only values below 32 are valid for a 32-bit register
474 if (ShiftAmount > 31)
475 return MCDisassembler::Fail;
477 Inst.addOperand(MCOperand::CreateImm(ShiftAmount));
478 return MCDisassembler::Success;
481 static DecodeStatus DecodeBitfieldInstruction(llvm::MCInst &Inst, unsigned Insn,
483 const void *Decoder) {
484 unsigned Rd = fieldFromInstruction(Insn, 0, 5);
485 unsigned Rn = fieldFromInstruction(Insn, 5, 5);
486 unsigned ImmS = fieldFromInstruction(Insn, 10, 6);
487 unsigned ImmR = fieldFromInstruction(Insn, 16, 6);
488 unsigned SF = fieldFromInstruction(Insn, 31, 1);
490 // Undef for 0b11 just in case it occurs. Don't want the compiler to optimise
491 // out assertions that it thinks should never be hit.
492 enum OpcTypes { SBFM = 0, BFM, UBFM, Undef } Opc;
493 Opc = (OpcTypes)fieldFromInstruction(Insn, 29, 2);
496 // ImmR and ImmS must be between 0 and 31 for 32-bit instructions.
497 if (ImmR > 31 || ImmS > 31)
498 return MCDisassembler::Fail;
502 DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
503 // BFM MCInsts use Rd as a source too.
504 if (Opc == BFM) DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
505 DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
507 DecodeGPR32RegisterClass(Inst, Rd, Address, Decoder);
508 // BFM MCInsts use Rd as a source too.
509 if (Opc == BFM) DecodeGPR32RegisterClass(Inst, Rd, Address, Decoder);
510 DecodeGPR32RegisterClass(Inst, Rn, Address, Decoder);
513 // ASR and LSR have more specific patterns so they won't get here:
514 assert(!(ImmS == 31 && !SF && Opc != BFM)
515 && "shift should have used auto decode");
516 assert(!(ImmS == 63 && SF && Opc != BFM)
517 && "shift should have used auto decode");
519 // Extension instructions similarly:
520 if (Opc == SBFM && ImmR == 0) {
521 assert((ImmS != 7 && ImmS != 15) && "extension got here");
522 assert((ImmS != 31 || SF == 0) && "extension got here");
523 } else if (Opc == UBFM && ImmR == 0) {
524 assert((SF != 0 || (ImmS != 7 && ImmS != 15)) && "extension got here");
528 // It might be a LSL instruction, which actually takes the shift amount
529 // itself as an MCInst operand.
530 if (SF && (ImmS + 1) % 64 == ImmR) {
531 Inst.setOpcode(AArch64::LSLxxi);
532 Inst.addOperand(MCOperand::CreateImm(63 - ImmS));
533 return MCDisassembler::Success;
534 } else if (!SF && (ImmS + 1) % 32 == ImmR) {
535 Inst.setOpcode(AArch64::LSLwwi);
536 Inst.addOperand(MCOperand::CreateImm(31 - ImmS));
537 return MCDisassembler::Success;
541 // Otherwise it's definitely either an extract or an insert depending on which
542 // of ImmR or ImmS is larger.
543 unsigned ExtractOp, InsertOp;
545 default: llvm_unreachable("unexpected instruction trying to decode bitfield");
547 ExtractOp = SF ? AArch64::SBFXxxii : AArch64::SBFXwwii;
548 InsertOp = SF ? AArch64::SBFIZxxii : AArch64::SBFIZwwii;
551 ExtractOp = SF ? AArch64::BFXILxxii : AArch64::BFXILwwii;
552 InsertOp = SF ? AArch64::BFIxxii : AArch64::BFIwwii;
555 ExtractOp = SF ? AArch64::UBFXxxii : AArch64::UBFXwwii;
556 InsertOp = SF ? AArch64::UBFIZxxii : AArch64::UBFIZwwii;
560 // Otherwise it's a boring insert or extract
561 Inst.addOperand(MCOperand::CreateImm(ImmR));
562 Inst.addOperand(MCOperand::CreateImm(ImmS));
566 Inst.setOpcode(InsertOp);
568 Inst.setOpcode(ExtractOp);
570 return MCDisassembler::Success;
573 static DecodeStatus DecodeFMOVLaneInstruction(llvm::MCInst &Inst, unsigned Insn,
575 const void *Decoder) {
576 // This decoder exists to add the dummy Lane operand to the MCInst, which must
577 // be 1 in assembly but has no other real manifestation.
578 unsigned Rd = fieldFromInstruction(Insn, 0, 5);
579 unsigned Rn = fieldFromInstruction(Insn, 5, 5);
580 unsigned IsToVec = fieldFromInstruction(Insn, 16, 1);
583 DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);
584 DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
586 DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
587 DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);
591 Inst.addOperand(MCOperand::CreateImm(1));
593 return MCDisassembler::Success;
597 static DecodeStatus DecodeLDSTPairInstruction(llvm::MCInst &Inst,
600 const void *Decoder) {
601 DecodeStatus Result = MCDisassembler::Success;
602 unsigned Rt = fieldFromInstruction(Insn, 0, 5);
603 unsigned Rn = fieldFromInstruction(Insn, 5, 5);
604 unsigned Rt2 = fieldFromInstruction(Insn, 10, 5);
605 unsigned SImm7 = fieldFromInstruction(Insn, 15, 7);
606 unsigned L = fieldFromInstruction(Insn, 22, 1);
607 unsigned V = fieldFromInstruction(Insn, 26, 1);
608 unsigned Opc = fieldFromInstruction(Insn, 30, 2);
610 // Not an official name, but it turns out that bit 23 distinguishes indexed
611 // from non-indexed operations.
612 unsigned Indexed = fieldFromInstruction(Insn, 23, 1);
614 if (Indexed && L == 0) {
615 // The MCInst for an indexed store has an out operand and 4 ins:
616 // Rn_wb, Rt, Rt2, Rn, Imm
617 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
620 // You shouldn't load to the same register twice in an instruction...
622 Result = MCDisassembler::SoftFail;
624 // ... or do any operation that writes-back to a transfer register. But note
625 // that "stp xzr, xzr, [sp], #4" is fine because xzr and sp are different.
626 if (Indexed && V == 0 && Rn != 31 && (Rt == Rn || Rt2 == Rn))
627 Result = MCDisassembler::SoftFail;
629 // Exactly how we decode the MCInst's registers depends on the Opc and V
630 // fields of the instruction. These also obviously determine the size of the
631 // operation so we can fill in that information while we're at it.
633 // The instruction operates on the FP/SIMD registers
635 default: return MCDisassembler::Fail;
637 DecodeFPR32RegisterClass(Inst, Rt, Address, Decoder);
638 DecodeFPR32RegisterClass(Inst, Rt2, Address, Decoder);
641 DecodeFPR64RegisterClass(Inst, Rt, Address, Decoder);
642 DecodeFPR64RegisterClass(Inst, Rt2, Address, Decoder);
645 DecodeFPR128RegisterClass(Inst, Rt, Address, Decoder);
646 DecodeFPR128RegisterClass(Inst, Rt2, Address, Decoder);
651 default: return MCDisassembler::Fail;
653 DecodeGPR32RegisterClass(Inst, Rt, Address, Decoder);
654 DecodeGPR32RegisterClass(Inst, Rt2, Address, Decoder);
657 assert(L && "unexpected \"store signed\" attempt");
658 DecodeGPR64RegisterClass(Inst, Rt, Address, Decoder);
659 DecodeGPR64RegisterClass(Inst, Rt2, Address, Decoder);
662 DecodeGPR64RegisterClass(Inst, Rt, Address, Decoder);
663 DecodeGPR64RegisterClass(Inst, Rt2, Address, Decoder);
668 if (Indexed && L == 1) {
669 // The MCInst for an indexed load has 3 out operands and an 3 ins:
670 // Rt, Rt2, Rn_wb, Rt2, Rn, Imm
671 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
675 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
676 Inst.addOperand(MCOperand::CreateImm(SImm7));
681 static DecodeStatus DecodeLoadPairExclusiveInstruction(llvm::MCInst &Inst,
684 const void *Decoder) {
685 unsigned Rt = fieldFromInstruction(Val, 0, 5);
686 unsigned Rn = fieldFromInstruction(Val, 5, 5);
687 unsigned Rt2 = fieldFromInstruction(Val, 10, 5);
688 unsigned MemSize = fieldFromInstruction(Val, 30, 2);
690 DecodeStatus S = MCDisassembler::Success;
691 if (Rt == Rt2) S = MCDisassembler::SoftFail;
695 if (!Check(S, DecodeGPR32RegisterClass(Inst, Rt, Address, Decoder)))
696 return MCDisassembler::Fail;
697 if (!Check(S, DecodeGPR32RegisterClass(Inst, Rt2, Address, Decoder)))
698 return MCDisassembler::Fail;
701 if (!Check(S, DecodeGPR64RegisterClass(Inst, Rt, Address, Decoder)))
702 return MCDisassembler::Fail;
703 if (!Check(S, DecodeGPR64RegisterClass(Inst, Rt2, Address, Decoder)))
704 return MCDisassembler::Fail;
707 llvm_unreachable("Invalid MemSize in DecodeLoadPairExclusiveInstruction");
710 if (!Check(S, DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder)))
711 return MCDisassembler::Fail;
716 template<typename SomeNamedImmMapper>
717 static DecodeStatus DecodeNamedImmOperand(llvm::MCInst &Inst,
720 const void *Decoder) {
721 SomeNamedImmMapper Mapper;
723 Mapper.toString(Val, ValidNamed);
724 if (ValidNamed || Mapper.validImm(Val)) {
725 Inst.addOperand(MCOperand::CreateImm(Val));
726 return MCDisassembler::Success;
729 return MCDisassembler::Fail;
732 static DecodeStatus DecodeSysRegOperand(const A64SysReg::SysRegMapper &Mapper,
736 const void *Decoder) {
738 Mapper.toString(Val, ValidNamed);
740 Inst.addOperand(MCOperand::CreateImm(Val));
742 return ValidNamed ? MCDisassembler::Success : MCDisassembler::Fail;
745 static DecodeStatus DecodeMRSOperand(llvm::MCInst &Inst,
748 const void *Decoder) {
749 return DecodeSysRegOperand(A64SysReg::MRSMapper(), Inst, Val, Address,
753 static DecodeStatus DecodeMSROperand(llvm::MCInst &Inst,
756 const void *Decoder) {
757 return DecodeSysRegOperand(A64SysReg::MSRMapper(), Inst, Val, Address,
761 static DecodeStatus DecodeSingleIndexedInstruction(llvm::MCInst &Inst,
764 const void *Decoder) {
765 unsigned Rt = fieldFromInstruction(Insn, 0, 5);
766 unsigned Rn = fieldFromInstruction(Insn, 5, 5);
767 unsigned Imm9 = fieldFromInstruction(Insn, 12, 9);
769 unsigned Opc = fieldFromInstruction(Insn, 22, 2);
770 unsigned V = fieldFromInstruction(Insn, 26, 1);
771 unsigned Size = fieldFromInstruction(Insn, 30, 2);
773 if (Opc == 0 || (V == 1 && Opc == 2)) {
774 // It's a store, the MCInst gets: Rn_wb, Rt, Rn, Imm
775 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
778 if (V == 0 && (Opc == 2 || Size == 3)) {
779 DecodeGPR64RegisterClass(Inst, Rt, Address, Decoder);
781 DecodeGPR32RegisterClass(Inst, Rt, Address, Decoder);
782 } else if (V == 1 && (Opc & 2)) {
783 DecodeFPR128RegisterClass(Inst, Rt, Address, Decoder);
787 DecodeFPR8RegisterClass(Inst, Rt, Address, Decoder);
790 DecodeFPR16RegisterClass(Inst, Rt, Address, Decoder);
793 DecodeFPR32RegisterClass(Inst, Rt, Address, Decoder);
796 DecodeFPR64RegisterClass(Inst, Rt, Address, Decoder);
801 if (Opc != 0 && (V != 1 || Opc != 2)) {
802 // It's a load, the MCInst gets: Rt, Rn_wb, Rn, Imm
803 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
806 DecodeGPR64xspRegisterClass(Inst, Rn, Address, Decoder);
808 Inst.addOperand(MCOperand::CreateImm(Imm9));
810 // N.b. The official documentation says undpredictable if Rt == Rn, but this
811 // takes place at the architectural rather than encoding level:
813 // "STR xzr, [sp], #4" is perfectly valid.
814 if (V == 0 && Rt == Rn && Rn != 31)
815 return MCDisassembler::SoftFail;
817 return MCDisassembler::Success;
820 static MCDisassembler *createAArch64Disassembler(const Target &T,
821 const MCSubtargetInfo &STI) {
822 return new AArch64Disassembler(STI, T.createMCRegInfo(""));
825 extern "C" void LLVMInitializeAArch64Disassembler() {
826 TargetRegistry::RegisterMCDisassembler(TheAArch64Target,
827 createAArch64Disassembler);
830 template <A64SE::ShiftExtSpecifiers Ext, bool IsHalf>
832 DecodeNeonMovImmShiftOperand(llvm::MCInst &Inst, unsigned ShiftAmount,
833 uint64_t Address, const void *Decoder) {
835 if (Ext == A64SE::LSL)
837 else if (Ext != A64SE::MSL)
838 return MCDisassembler::Fail;
840 // MSL and LSLH accepts encoded shift amount 0 or 1.
841 if ((!IsLSL || (IsLSL && IsHalf)) && ShiftAmount != 0 && ShiftAmount != 1)
842 return MCDisassembler::Fail;
844 // LSL accepts encoded shift amount 0, 1, 2 or 3.
845 if (IsLSL && ShiftAmount > 3)
846 return MCDisassembler::Fail;
848 Inst.addOperand(MCOperand::CreateImm(ShiftAmount));
849 return MCDisassembler::Success;