Path: blob/master/libs/capstone/arch/AArch64/AArch64Disassembler.c
4389 views
//===- AArch64Disassembler.cpp - Disassembler for AArch64 ISA -------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is distributed under the University of Illinois Open Source5// License. See LICENSE.TXT for details.6//7//===----------------------------------------------------------------------===//8//9// This file contains the functions necessary to decode AArch64 instruction10// bitpatterns into MCInsts (with the help of TableGenerated information from11// the instruction definitions).12//13//===----------------------------------------------------------------------===//1415/* Capstone Disassembly Engine */16/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */1718#ifdef CAPSTONE_HAS_ARM641920#include <stdio.h> // DEBUG21#include <stdlib.h>2223#include "../../cs_priv.h"24#include "../../utils.h"2526#include "AArch64Disassembler.h"2728#include "../../MCDisassembler.h"29#include "../../MCFixedLenDisassembler.h"30#include "../../MCInst.h"31#include "../../MCInstrDesc.h"32#include "../../MCRegisterInfo.h"3334#include "AArch64AddressingModes.h"35#include "AArch64BaseInfo.h"3637// Forward declare these because the autogenerated code will reference them.38// Definitions are further down.39static DecodeStatus DecodeFPR128RegisterClass(MCInst *Inst,40unsigned RegNo, uint64_t Address, const void *Decoder);41static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, unsigned RegNo,42uint64_t Address, const void *Decoder);43static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,44uint64_t Address, const void *Decoder);45static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,46uint64_t Address, const void *Decoder);47static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,48uint64_t Address, const void *Decoder);49static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,50uint64_t Address, const void *Decoder);51static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst, unsigned RegNo,52uint64_t Address, const void *Decoder);53static DecodeStatus DecodeGPR64spRegisterClass(MCInst *Inst,54unsigned RegNo, uint64_t Address, const void *Decoder);55static DecodeStatus DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst *Inst,56unsigned RegNo, uint64_t Address, const void *Decoder);57static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,58uint64_t Address, const void *Decoder);59static DecodeStatus DecodeGPR32spRegisterClass(MCInst *Inst,60unsigned RegNo, uint64_t Address, const void *Decoder);61static DecodeStatus DecodeQQRegisterClass(MCInst *Inst, unsigned RegNo,62uint64_t Address, const void *Decoder);63static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,64uint64_t Address, const void *Decoder);65static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,66uint64_t Address, const void *Decoder);67static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,68uint64_t Address, const void *Decoder);69static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,70uint64_t Address, const void *Decoder);71static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,72uint64_t Address, const void *Decoder);73static DecodeStatus DecodeZPRRegisterClass(MCInst *Inst, unsigned RegNo,74uint64_t Address, const void *Decoder);75static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,76uint64_t Address, const void *Decoder);77static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,78uint64_t Address, const void *Decoder);79static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,80uint64_t Address, const void *Decoder);81static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,82uint64_t Address, const void *Decoder);83static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,84uint64_t Address, const void *Decoder);85static DecodeStatus DecodeMatrixTile(MCInst *Inst, unsigned RegNo,86uint64_t Address, const void *Decoder, unsigned NumBitsForTile);87static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst *Inst,88unsigned RegMask, uint64_t Address, const void *Decoder);89static DecodeStatus DecodePPRRegisterClass(MCInst *Inst, unsigned RegNo,90uint64_t Address, const void *Decoder);91static DecodeStatus DecodePPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,92uint64_t Address, const void *Decoder);93static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,94uint64_t Address, const void *Decoder);95static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,96uint64_t Address, const void *Decoder);97static DecodeStatus DecodePCRelLabel19(MCInst *Inst, unsigned Imm,98uint64_t Address, const void *Decoder);99static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,100uint64_t Address, const void *Decoder);101static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,102uint64_t Address, const void *Decoder);103static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,104uint64_t Address, const void *Decoder);105static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,106uint64_t Address, const void *Decoder);107static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst,108uint32_t insn, uint64_t Address, const void *Decoder);109static DecodeStatus DecodeSignedLdStInstruction(MCInst *Inst,110uint32_t insn, uint64_t Address, const void *Decoder);111static DecodeStatus DecodeExclusiveLdStInstruction(MCInst *Inst,112uint32_t insn, uint64_t Address, const void *Decoder);113static DecodeStatus DecodePairLdStInstruction(MCInst *Inst, uint32_t insn,114uint64_t Address, const void *Decoder);115static DecodeStatus DecodeAuthLoadInstruction(MCInst *Inst, uint32_t insn,116uint64_t Address, const void *Decoder);117static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst,118uint32_t insn, uint64_t Address, const void *Decoder);119static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst,120uint32_t insn, uint64_t Address, const void *Decoder);121static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,122uint64_t Address, const void *Decoder);123static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst,124uint32_t insn, uint64_t Address, const void *Decoder);125static DecodeStatus DecodeAdrInstruction(MCInst *Inst, uint32_t insn,126uint64_t Address, const void *Decoder);127static DecodeStatus DecodeAddSubImmShift(MCInst *Inst, uint32_t insn,128uint64_t Address, const void *Decoder);129static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,130uint64_t Address, const void *Decoder);131static DecodeStatus DecodeSystemPStateInstruction(MCInst *Inst,132uint32_t insn, uint64_t Address, const void *Decoder);133static DecodeStatus DecodeTestAndBranch(MCInst *Inst, uint32_t insn,134uint64_t Address, const void *Decoder);135static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,136uint64_t Address, const void *Decoder);137static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,138uint64_t Addr, const void *Decoder);139static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,140uint64_t Addr, const void *Decoder);141static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,142uint64_t Addr, const void *Decoder);143static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,144uint64_t Addr, const void *Decoder);145static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,146uint64_t Addr, const void *Decoder);147static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,148uint64_t Addr, const void *Decoder);149static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,150uint64_t Addr, const void *Decoder);151static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,152uint64_t Addr, const void *Decoder);153static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,154uint64_t Addr, const void *Decoder);155static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,156uint64_t Addr, const void *Decoder);157static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,158uint64_t Addr, const void *Decoder);159static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,160unsigned RegNo, uint64_t Addr, const void *Decoder);161static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,162unsigned RegNo, uint64_t Addr, const void *Decoder);163static DecodeStatus DecodeSVELogicalImmInstruction(MCInst *Inst, uint32_t insn,164uint64_t Address, const void *Decoder);165static DecodeStatus DecodeSImm(MCInst *Inst, uint64_t Imm, uint64_t Address,166const void *Decoder, int Bits);167static DecodeStatus DecodeImm8OptLsl(MCInst *Inst, unsigned Imm, uint64_t Addr,168const void *Decoder, int ElementWidth);169static DecodeStatus DecodeSVEIncDecImm(MCInst *Inst, unsigned Imm,170uint64_t Addr, const void *Decoder);171static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst,172uint32_t insn, uint64_t Addr, const void *Decoder);173static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,174uint64_t Addr, const void *Decoder);175static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,176uint64_t Addr, const void *Decoder);177static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,178const void *Decoder);179static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,180uint64_t Addr, const void *Decoder);181static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,182uint64_t Addr, const void *Decoder);183184185static bool Check(DecodeStatus *Out, DecodeStatus In)186{187switch (In) {188default: // never reach189return true;190191case MCDisassembler_Success:192// Out stays the same.193return true;194195case MCDisassembler_SoftFail:196*Out = In;197return true;198199case MCDisassembler_Fail:200*Out = In;201return false;202}203// llvm_unreachable("Invalid DecodeStatus!");204}205206// Hacky: enable all features for disassembler207uint64_t AArch64_getFeatureBits(int feature)208{209// enable all features210return (uint64_t)-1;211}212213#define GET_SUBTARGETINFO_ENUM214#include "AArch64GenSubtargetInfo.inc"215216#include "AArch64GenDisassemblerTables.inc"217218#define GET_INSTRINFO_ENUM219#include "AArch64GenInstrInfo.inc"220221#define GET_REGINFO_ENUM222#define GET_REGINFO_MC_DESC223#include "AArch64GenRegisterInfo.inc"224225#define Success MCDisassembler_Success226#define Fail MCDisassembler_Fail227#define SoftFail MCDisassembler_SoftFail228229static DecodeStatus _getInstruction(cs_struct *ud, MCInst *MI,230const uint8_t *code, size_t code_len,231uint16_t *Size,232uint64_t Address, MCRegisterInfo *MRI)233{234uint32_t insn;235DecodeStatus result;236size_t i;237238if (code_len < 4) {239// not enough data240*Size = 0;241return MCDisassembler_Fail;242}243244if (MI->flat_insn->detail) {245memset(MI->flat_insn->detail, 0, offsetof(cs_detail, arm64)+sizeof(cs_arm64));246for (i = 0; i < ARR_SIZE(MI->flat_insn->detail->arm64.operands); i++)247MI->flat_insn->detail->arm64.operands[i].vector_index = -1;248}249250if (MODE_IS_BIG_ENDIAN(ud->mode))251insn = (code[3] << 0) | (code[2] << 8) |252(code[1] << 16) | ((uint32_t) code[0] << 24);253else254insn = ((uint32_t) code[3] << 24) | (code[2] << 16) |255(code[1] << 8) | (code[0] << 0);256257// Calling the auto-generated decoder function.258result = decodeInstruction_4(DecoderTable32, MI, insn, Address);259// If Decoding fails initially, try Fallback table.260if(result == MCDisassembler_Fail){261result = decodeInstruction_4(DecoderTableFallback32, MI, insn, Address);262}263264// Init new MCOperand to be used in switch below.265// Kind RegVal set inside a case when needed.266MCOperand op_storage;267MCOperand *Op = &op_storage;268switch (MCInst_getOpcode(MI)) {269default:270break;271// For Scalable Matrix Extension (SME) instructions that have an implicit272// operand for the accumulator (ZA) which isn't encoded, manually insert273// operand.274case AArch64_LDR_ZA:275case AArch64_STR_ZA: {276Op->Kind = kRegister;277Op->RegVal = AArch64_ZA;278MCInst_insert0(MI, 0, Op);279// Spill and fill instructions have a single immediate used for both the280// vector select offset and optional memory offset. Replicate the decoded281// immediate.282MCOperand *Imm4Op = MCInst_getOperand(MI, 2);283// assert(MCOperand_isImm(Imm4Op) && "Unexpected operand type!");284MCInst_addOperand2(MI, Imm4Op);285break;286}287case AArch64_LD1_MXIPXX_H_B:288case AArch64_LD1_MXIPXX_V_B:289case AArch64_ST1_MXIPXX_H_B:290case AArch64_ST1_MXIPXX_V_B:291case AArch64_INSERT_MXIPZ_H_B:292case AArch64_INSERT_MXIPZ_V_B:293// e.g.294// MOVA ZA0<HV>.B[<Ws>, <imm>], <Pg>/M, <Zn>.B295// ^ insert implicit 8-bit element tile296Op->Kind = kRegister;297Op->RegVal = AArch64_ZAB0;298MCInst_insert0(MI, 0, Op);299break;300case AArch64_EXTRACT_ZPMXI_H_B:301case AArch64_EXTRACT_ZPMXI_V_B:302// MOVA <Zd>.B, <Pg>/M, ZA0<HV>.B[<Ws>, <imm>]303// ^ insert implicit 8-bit element tile304Op->Kind = kRegister;305Op->RegVal = AArch64_ZAB0;306MCInst_insert0(MI, 2, Op);307break;308case AArch64_LD1_MXIPXX_H_Q:309case AArch64_LD1_MXIPXX_V_Q:310case AArch64_ST1_MXIPXX_H_Q:311case AArch64_ST1_MXIPXX_V_Q:312// 128-bit load/store have implicit zero vector index.313Op->Kind = kImmediate;314Op->ImmVal = 0;315MCInst_insert0(MI, 2, Op);316break;317// 128-bit mova have implicit zero vector index.318case AArch64_INSERT_MXIPZ_H_Q:319case AArch64_INSERT_MXIPZ_V_Q:320Op->Kind = kImmediate;321Op->ImmVal = 0;322MCInst_insert0(MI, 2, Op);323break;324case AArch64_EXTRACT_ZPMXI_H_Q:325case AArch64_EXTRACT_ZPMXI_V_Q:326Op->Kind = kImmediate;327Op->ImmVal = 0;328MCInst_addOperand2(MI, Op);329break;330case AArch64_SMOVvi8to32_idx0:331case AArch64_SMOVvi8to64_idx0:332case AArch64_SMOVvi16to32_idx0:333case AArch64_SMOVvi16to64_idx0:334case AArch64_SMOVvi32to64_idx0:335case AArch64_UMOVvi8_idx0:336case AArch64_UMOVvi16_idx0:337case AArch64_UMOVvi32_idx0:338case AArch64_UMOVvi64_idx0:339Op->Kind = kImmediate;340Op->ImmVal = 0;341MCInst_addOperand2(MI, Op);342break;343}344345if (result != MCDisassembler_Fail) {346*Size = 4;347348return result;349}350351// invalid code352MCInst_clear(MI);353*Size = 0;354355return MCDisassembler_Fail;356}357358bool AArch64_getInstruction(csh ud, const uint8_t *code, size_t code_len,359MCInst *instr, uint16_t *size, uint64_t address, void *info)360{361DecodeStatus status = _getInstruction((cs_struct *)ud, instr,362code, code_len,363size,364address, (MCRegisterInfo *)info);365366return status == MCDisassembler_Success;367}368369static const unsigned FPR128DecoderTable[] = {370AArch64_Q0, AArch64_Q1, AArch64_Q2, AArch64_Q3, AArch64_Q4,371AArch64_Q5, AArch64_Q6, AArch64_Q7, AArch64_Q8, AArch64_Q9,372AArch64_Q10, AArch64_Q11, AArch64_Q12, AArch64_Q13, AArch64_Q14,373AArch64_Q15, AArch64_Q16, AArch64_Q17, AArch64_Q18, AArch64_Q19,374AArch64_Q20, AArch64_Q21, AArch64_Q22, AArch64_Q23, AArch64_Q24,375AArch64_Q25, AArch64_Q26, AArch64_Q27, AArch64_Q28, AArch64_Q29,376AArch64_Q30, AArch64_Q31377};378379static DecodeStatus DecodeFPR128RegisterClass(MCInst *Inst, unsigned RegNo,380uint64_t Addr, const void *Decoder)381{382unsigned Register;383384if (RegNo > 31)385return Fail;386387Register = FPR128DecoderTable[RegNo];388MCOperand_CreateReg0(Inst, Register);389390return Success;391}392393static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,394uint64_t Addr, const void *Decoder)395{396if (RegNo > 15)397return Fail;398399return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);400}401402static const unsigned FPR64DecoderTable[] = {403AArch64_D0, AArch64_D1, AArch64_D2, AArch64_D3, AArch64_D4,404AArch64_D5, AArch64_D6, AArch64_D7, AArch64_D8, AArch64_D9,405AArch64_D10, AArch64_D11, AArch64_D12, AArch64_D13, AArch64_D14,406AArch64_D15, AArch64_D16, AArch64_D17, AArch64_D18, AArch64_D19,407AArch64_D20, AArch64_D21, AArch64_D22, AArch64_D23, AArch64_D24,408AArch64_D25, AArch64_D26, AArch64_D27, AArch64_D28, AArch64_D29,409AArch64_D30, AArch64_D31410};411412static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, unsigned RegNo,413uint64_t Addr, const void *Decoder)414{415unsigned Register;416417if (RegNo > 31)418return Fail;419420Register = FPR64DecoderTable[RegNo];421MCOperand_CreateReg0(Inst, Register);422423return Success;424}425426static const unsigned FPR32DecoderTable[] = {427AArch64_S0, AArch64_S1, AArch64_S2, AArch64_S3, AArch64_S4,428AArch64_S5, AArch64_S6, AArch64_S7, AArch64_S8, AArch64_S9,429AArch64_S10, AArch64_S11, AArch64_S12, AArch64_S13, AArch64_S14,430AArch64_S15, AArch64_S16, AArch64_S17, AArch64_S18, AArch64_S19,431AArch64_S20, AArch64_S21, AArch64_S22, AArch64_S23, AArch64_S24,432AArch64_S25, AArch64_S26, AArch64_S27, AArch64_S28, AArch64_S29,433AArch64_S30, AArch64_S31434};435436static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,437uint64_t Addr, const void *Decoder)438{439unsigned Register;440441if (RegNo > 31)442return Fail;443444Register = FPR32DecoderTable[RegNo];445MCOperand_CreateReg0(Inst, Register);446447return Success;448}449450static const unsigned FPR16DecoderTable[] = {451AArch64_H0, AArch64_H1, AArch64_H2, AArch64_H3, AArch64_H4,452AArch64_H5, AArch64_H6, AArch64_H7, AArch64_H8, AArch64_H9,453AArch64_H10, AArch64_H11, AArch64_H12, AArch64_H13, AArch64_H14,454AArch64_H15, AArch64_H16, AArch64_H17, AArch64_H18, AArch64_H19,455AArch64_H20, AArch64_H21, AArch64_H22, AArch64_H23, AArch64_H24,456AArch64_H25, AArch64_H26, AArch64_H27, AArch64_H28, AArch64_H29,457AArch64_H30, AArch64_H31458};459460static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,461uint64_t Addr, const void *Decoder)462{463unsigned Register;464465if (RegNo > 31)466return Fail;467468Register = FPR16DecoderTable[RegNo];469MCOperand_CreateReg0(Inst, Register);470471return Success;472}473474static const unsigned FPR8DecoderTable[] = {475AArch64_B0, AArch64_B1, AArch64_B2, AArch64_B3, AArch64_B4,476AArch64_B5, AArch64_B6, AArch64_B7, AArch64_B8, AArch64_B9,477AArch64_B10, AArch64_B11, AArch64_B12, AArch64_B13, AArch64_B14,478AArch64_B15, AArch64_B16, AArch64_B17, AArch64_B18, AArch64_B19,479AArch64_B20, AArch64_B21, AArch64_B22, AArch64_B23, AArch64_B24,480AArch64_B25, AArch64_B26, AArch64_B27, AArch64_B28, AArch64_B29,481AArch64_B30, AArch64_B31482};483484static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,485uint64_t Addr, const void *Decoder)486{487unsigned Register;488489if (RegNo > 31)490return Fail;491492Register = FPR8DecoderTable[RegNo];493MCOperand_CreateReg0(Inst, Register);494495return Success;496}497498static const unsigned GPR64DecoderTable[] = {499AArch64_X0, AArch64_X1, AArch64_X2, AArch64_X3, AArch64_X4,500AArch64_X5, AArch64_X6, AArch64_X7, AArch64_X8, AArch64_X9,501AArch64_X10, AArch64_X11, AArch64_X12, AArch64_X13, AArch64_X14,502AArch64_X15, AArch64_X16, AArch64_X17, AArch64_X18, AArch64_X19,503AArch64_X20, AArch64_X21, AArch64_X22, AArch64_X23, AArch64_X24,504AArch64_X25, AArch64_X26, AArch64_X27, AArch64_X28, AArch64_FP,505AArch64_LR, AArch64_XZR506};507508static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,509uint64_t Addr, const void *Decoder)510{511unsigned Register;512513if (RegNo > 30)514return Fail;515516Register = GPR64DecoderTable[RegNo];517MCOperand_CreateReg0(Inst, Register);518519return Success;520}521522static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,523uint64_t Addr, const void *Decoder)524{525unsigned Register;526527if (RegNo > 31)528return Fail;529530Register = GPR64DecoderTable[RegNo];531MCOperand_CreateReg0(Inst, Register);532533return Success;534}535536static const unsigned GPR64x8DecoderTable[] = {537AArch64_X0_X1_X2_X3_X4_X5_X6_X7, AArch64_X2_X3_X4_X5_X6_X7_X8_X9,538AArch64_X4_X5_X6_X7_X8_X9_X10_X11, AArch64_X6_X7_X8_X9_X10_X11_X12_X13,539AArch64_X8_X9_X10_X11_X12_X13_X14_X15, AArch64_X10_X11_X12_X13_X14_X15_X16_X17,540AArch64_X12_X13_X14_X15_X16_X17_X18_X19, AArch64_X14_X15_X16_X17_X18_X19_X20_X21,541AArch64_X16_X17_X18_X19_X20_X21_X22_X23, AArch64_X18_X19_X20_X21_X22_X23_X24_X25,542AArch64_X20_X21_X22_X23_X24_X25_X26_X27, AArch64_X22_X23_X24_X25_X26_X27_X28_FP543};544545static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst, unsigned RegNo,546uint64_t Address, const void *Decoder)547{548if (RegNo > 22)549return Fail;550if (RegNo & 1)551return Fail;552553unsigned Register = GPR64x8DecoderTable[RegNo >> 1];554MCOperand_CreateReg0(Inst, Register);555556return Success;557}558559static DecodeStatus DecodeGPR64spRegisterClass(MCInst *Inst, unsigned RegNo,560uint64_t Addr, const void *Decoder)561{562unsigned Register;563564if (RegNo > 31)565return Fail;566567Register = GPR64DecoderTable[RegNo];568if (Register == AArch64_XZR)569Register = AArch64_SP;570571MCOperand_CreateReg0(Inst, Register);572573return Success;574}575576577static const unsigned MatrixIndexGPR32_12_15DecoderTable[] = {578AArch64_W12, AArch64_W13, AArch64_W14, AArch64_W15579};580581static DecodeStatus DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst *Inst,582unsigned RegNo, uint64_t Addr, const void *Decoder)583{584unsigned Register;585586if (RegNo > 3)587return Fail;588589Register = MatrixIndexGPR32_12_15DecoderTable[RegNo];590MCOperand_CreateReg0(Inst, Register);591592return Success;593}594595static const unsigned GPR32DecoderTable[] = {596AArch64_W0, AArch64_W1, AArch64_W2, AArch64_W3, AArch64_W4,597AArch64_W5, AArch64_W6, AArch64_W7, AArch64_W8, AArch64_W9,598AArch64_W10, AArch64_W11, AArch64_W12, AArch64_W13, AArch64_W14,599AArch64_W15, AArch64_W16, AArch64_W17, AArch64_W18, AArch64_W19,600AArch64_W20, AArch64_W21, AArch64_W22, AArch64_W23, AArch64_W24,601AArch64_W25, AArch64_W26, AArch64_W27, AArch64_W28, AArch64_W29,602AArch64_W30, AArch64_WZR603};604605static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,606uint64_t Addr, const void *Decoder)607{608unsigned Register;609610if (RegNo > 31)611return Fail;612613Register = GPR32DecoderTable[RegNo];614MCOperand_CreateReg0(Inst, Register);615616return Success;617}618619static DecodeStatus DecodeGPR32spRegisterClass(MCInst *Inst, unsigned RegNo,620uint64_t Addr, const void *Decoder)621{622unsigned Register;623624if (RegNo > 31)625return Fail;626627Register = GPR32DecoderTable[RegNo];628if (Register == AArch64_WZR)629Register = AArch64_WSP;630631MCOperand_CreateReg0(Inst, Register);632633return Success;634}635636static const unsigned ZPRDecoderTable[] = {637AArch64_Z0, AArch64_Z1, AArch64_Z2, AArch64_Z3,638AArch64_Z4, AArch64_Z5, AArch64_Z6, AArch64_Z7,639AArch64_Z8, AArch64_Z9, AArch64_Z10, AArch64_Z11,640AArch64_Z12, AArch64_Z13, AArch64_Z14, AArch64_Z15,641AArch64_Z16, AArch64_Z17, AArch64_Z18, AArch64_Z19,642AArch64_Z20, AArch64_Z21, AArch64_Z22, AArch64_Z23,643AArch64_Z24, AArch64_Z25, AArch64_Z26, AArch64_Z27,644AArch64_Z28, AArch64_Z29, AArch64_Z30, AArch64_Z31645};646647static DecodeStatus DecodeZPRRegisterClass(MCInst *Inst, unsigned RegNo,648uint64_t Address, const void *Decoder)649{650unsigned Register;651652if (RegNo > 31)653return Fail;654655Register = ZPRDecoderTable[RegNo];656MCOperand_CreateReg0(Inst, Register);657658return Success;659}660661static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,662uint64_t Address, const void *Decoder)663{664if (RegNo > 15)665return Fail;666667return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);668}669670static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,671uint64_t Address, const void *Decoder)672{673if (RegNo > 7)674return Fail;675676return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);677}678679static const unsigned ZZDecoderTable[] = {680AArch64_Z0_Z1, AArch64_Z1_Z2, AArch64_Z2_Z3, AArch64_Z3_Z4,681AArch64_Z4_Z5, AArch64_Z5_Z6, AArch64_Z6_Z7, AArch64_Z7_Z8,682AArch64_Z8_Z9, AArch64_Z9_Z10, AArch64_Z10_Z11, AArch64_Z11_Z12,683AArch64_Z12_Z13, AArch64_Z13_Z14, AArch64_Z14_Z15, AArch64_Z15_Z16,684AArch64_Z16_Z17, AArch64_Z17_Z18, AArch64_Z18_Z19, AArch64_Z19_Z20,685AArch64_Z20_Z21, AArch64_Z21_Z22, AArch64_Z22_Z23, AArch64_Z23_Z24,686AArch64_Z24_Z25, AArch64_Z25_Z26, AArch64_Z26_Z27, AArch64_Z27_Z28,687AArch64_Z28_Z29, AArch64_Z29_Z30, AArch64_Z30_Z31, AArch64_Z31_Z0688};689690static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,691uint64_t Address, const void *Decoder)692{693unsigned Register;694695if (RegNo > 31)696return Fail;697698Register = ZZDecoderTable[RegNo];699MCOperand_CreateReg0(Inst, Register);700701return Success;702}703704static const unsigned ZZZDecoderTable[] = {705AArch64_Z0_Z1_Z2, AArch64_Z1_Z2_Z3, AArch64_Z2_Z3_Z4,706AArch64_Z3_Z4_Z5, AArch64_Z4_Z5_Z6, AArch64_Z5_Z6_Z7,707AArch64_Z6_Z7_Z8, AArch64_Z7_Z8_Z9, AArch64_Z8_Z9_Z10,708AArch64_Z9_Z10_Z11, AArch64_Z10_Z11_Z12, AArch64_Z11_Z12_Z13,709AArch64_Z12_Z13_Z14, AArch64_Z13_Z14_Z15, AArch64_Z14_Z15_Z16,710AArch64_Z15_Z16_Z17, AArch64_Z16_Z17_Z18, AArch64_Z17_Z18_Z19,711AArch64_Z18_Z19_Z20, AArch64_Z19_Z20_Z21, AArch64_Z20_Z21_Z22,712AArch64_Z21_Z22_Z23, AArch64_Z22_Z23_Z24, AArch64_Z23_Z24_Z25,713AArch64_Z24_Z25_Z26, AArch64_Z25_Z26_Z27, AArch64_Z26_Z27_Z28,714AArch64_Z27_Z28_Z29, AArch64_Z28_Z29_Z30, AArch64_Z29_Z30_Z31,715AArch64_Z30_Z31_Z0, AArch64_Z31_Z0_Z1716};717718static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,719uint64_t Address, const void *Decoder)720{721unsigned Register;722723if (RegNo > 31)724return Fail;725726Register = ZZZDecoderTable[RegNo];727MCOperand_CreateReg0(Inst, Register);728729return Success;730}731732static const unsigned ZZZZDecoderTable[] = {733AArch64_Z0_Z1_Z2_Z3, AArch64_Z1_Z2_Z3_Z4, AArch64_Z2_Z3_Z4_Z5,734AArch64_Z3_Z4_Z5_Z6, AArch64_Z4_Z5_Z6_Z7, AArch64_Z5_Z6_Z7_Z8,735AArch64_Z6_Z7_Z8_Z9, AArch64_Z7_Z8_Z9_Z10, AArch64_Z8_Z9_Z10_Z11,736AArch64_Z9_Z10_Z11_Z12, AArch64_Z10_Z11_Z12_Z13, AArch64_Z11_Z12_Z13_Z14,737AArch64_Z12_Z13_Z14_Z15, AArch64_Z13_Z14_Z15_Z16, AArch64_Z14_Z15_Z16_Z17,738AArch64_Z15_Z16_Z17_Z18, AArch64_Z16_Z17_Z18_Z19, AArch64_Z17_Z18_Z19_Z20,739AArch64_Z18_Z19_Z20_Z21, AArch64_Z19_Z20_Z21_Z22, AArch64_Z20_Z21_Z22_Z23,740AArch64_Z21_Z22_Z23_Z24, AArch64_Z22_Z23_Z24_Z25, AArch64_Z23_Z24_Z25_Z26,741AArch64_Z24_Z25_Z26_Z27, AArch64_Z25_Z26_Z27_Z28, AArch64_Z26_Z27_Z28_Z29,742AArch64_Z27_Z28_Z29_Z30, AArch64_Z28_Z29_Z30_Z31, AArch64_Z29_Z30_Z31_Z0,743AArch64_Z30_Z31_Z0_Z1, AArch64_Z31_Z0_Z1_Z2744};745746static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,747uint64_t Address, const void *Decoder)748{749unsigned Register;750751if (RegNo > 31)752return Fail;753754Register = ZZZZDecoderTable[RegNo];755MCOperand_CreateReg0(Inst, Register);756757return Success;758}759760static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst *Inst,761unsigned RegMask, uint64_t Address, const void *Decoder) {762if (RegMask > 0xFF)763return Fail;764765MCOperand_CreateImm0(Inst, RegMask);766return Success;767}768769static const unsigned MatrixZATileDecoderTable[] = {770AArch64_ZAB0,771AArch64_ZAH0, AArch64_ZAH1,772AArch64_ZAS0, AArch64_ZAS1, AArch64_ZAS2, AArch64_ZAS3,773AArch64_ZAD0, AArch64_ZAD1, AArch64_ZAD2, AArch64_ZAD3,774AArch64_ZAD4, AArch64_ZAD5, AArch64_ZAD6, AArch64_ZAD7,775AArch64_ZAQ0, AArch64_ZAQ1, AArch64_ZAQ2, AArch64_ZAQ3,776AArch64_ZAQ4, AArch64_ZAQ5, AArch64_ZAQ6, AArch64_ZAQ7,777AArch64_ZAQ8, AArch64_ZAQ9, AArch64_ZAQ10, AArch64_ZAQ11,778AArch64_ZAQ12, AArch64_ZAQ13, AArch64_ZAQ14, AArch64_ZAQ15779};780781static DecodeStatus DecodeMatrixTile(MCInst *Inst, unsigned RegNo,782uint64_t Address, const void *Decoder, unsigned NumBitsForTile) {783unsigned LastReg = (1 << NumBitsForTile) - 1;784if (RegNo > LastReg)785return Fail;786787// Convert original 2D indexes into 1D table index788unsigned index = 0;789switch (NumBitsForTile)790{791case 0:792// Only a single Byte tile at beginning of list so index = 0793break;794case 1:795index = 1 + RegNo;796break;797case 2:798index = 3 + RegNo;799break;800case 3:801index = 7 + RegNo;802break;803case 4:804index = 15 + RegNo;805break;806default:807break;808}809810MCOperand_CreateReg0(Inst, MatrixZATileDecoderTable[index]);811return Success;812}813814815static const unsigned PPRDecoderTable[] = {816AArch64_P0, AArch64_P1, AArch64_P2, AArch64_P3,817AArch64_P4, AArch64_P5, AArch64_P6, AArch64_P7,818AArch64_P8, AArch64_P9, AArch64_P10, AArch64_P11,819AArch64_P12, AArch64_P13, AArch64_P14, AArch64_P15820};821822static DecodeStatus DecodePPRRegisterClass(MCInst *Inst, unsigned RegNo,823uint64_t Addr, const void *Decoder)824{825unsigned Register;826827if (RegNo > 15)828return Fail;829830Register = PPRDecoderTable[RegNo];831MCOperand_CreateReg0(Inst, Register);832833return Success;834}835836static DecodeStatus DecodePPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,837uint64_t Addr, const void *Decoder)838{839if (RegNo > 7)840return Fail;841842// Just reuse the PPR decode table843return DecodePPRRegisterClass(Inst, RegNo, Addr, Decoder);844}845846static const unsigned VectorDecoderTable[] = {847AArch64_Q0, AArch64_Q1, AArch64_Q2, AArch64_Q3, AArch64_Q4,848AArch64_Q5, AArch64_Q6, AArch64_Q7, AArch64_Q8, AArch64_Q9,849AArch64_Q10, AArch64_Q11, AArch64_Q12, AArch64_Q13, AArch64_Q14,850AArch64_Q15, AArch64_Q16, AArch64_Q17, AArch64_Q18, AArch64_Q19,851AArch64_Q20, AArch64_Q21, AArch64_Q22, AArch64_Q23, AArch64_Q24,852AArch64_Q25, AArch64_Q26, AArch64_Q27, AArch64_Q28, AArch64_Q29,853AArch64_Q30, AArch64_Q31854};855856static DecodeStatus DecodeVectorRegisterClass(MCInst *Inst, unsigned RegNo,857uint64_t Addr, const void *Decoder)858{859unsigned Register;860861if (RegNo > 31)862return Fail;863864Register = VectorDecoderTable[RegNo];865MCOperand_CreateReg0(Inst, Register);866867return Success;868}869870static const unsigned QQDecoderTable[] = {871AArch64_Q0_Q1, AArch64_Q1_Q2, AArch64_Q2_Q3, AArch64_Q3_Q4,872AArch64_Q4_Q5, AArch64_Q5_Q6, AArch64_Q6_Q7, AArch64_Q7_Q8,873AArch64_Q8_Q9, AArch64_Q9_Q10, AArch64_Q10_Q11, AArch64_Q11_Q12,874AArch64_Q12_Q13, AArch64_Q13_Q14, AArch64_Q14_Q15, AArch64_Q15_Q16,875AArch64_Q16_Q17, AArch64_Q17_Q18, AArch64_Q18_Q19, AArch64_Q19_Q20,876AArch64_Q20_Q21, AArch64_Q21_Q22, AArch64_Q22_Q23, AArch64_Q23_Q24,877AArch64_Q24_Q25, AArch64_Q25_Q26, AArch64_Q26_Q27, AArch64_Q27_Q28,878AArch64_Q28_Q29, AArch64_Q29_Q30, AArch64_Q30_Q31, AArch64_Q31_Q0879};880881static DecodeStatus DecodeQQRegisterClass(MCInst *Inst, unsigned RegNo,882uint64_t Addr, const void *Decoder)883{884unsigned Register;885886if (RegNo > 31)887return Fail;888889Register = QQDecoderTable[RegNo];890MCOperand_CreateReg0(Inst, Register);891892return Success;893}894895static const unsigned QQQDecoderTable[] = {896AArch64_Q0_Q1_Q2, AArch64_Q1_Q2_Q3, AArch64_Q2_Q3_Q4,897AArch64_Q3_Q4_Q5, AArch64_Q4_Q5_Q6, AArch64_Q5_Q6_Q7,898AArch64_Q6_Q7_Q8, AArch64_Q7_Q8_Q9, AArch64_Q8_Q9_Q10,899AArch64_Q9_Q10_Q11, AArch64_Q10_Q11_Q12, AArch64_Q11_Q12_Q13,900AArch64_Q12_Q13_Q14, AArch64_Q13_Q14_Q15, AArch64_Q14_Q15_Q16,901AArch64_Q15_Q16_Q17, AArch64_Q16_Q17_Q18, AArch64_Q17_Q18_Q19,902AArch64_Q18_Q19_Q20, AArch64_Q19_Q20_Q21, AArch64_Q20_Q21_Q22,903AArch64_Q21_Q22_Q23, AArch64_Q22_Q23_Q24, AArch64_Q23_Q24_Q25,904AArch64_Q24_Q25_Q26, AArch64_Q25_Q26_Q27, AArch64_Q26_Q27_Q28,905AArch64_Q27_Q28_Q29, AArch64_Q28_Q29_Q30, AArch64_Q29_Q30_Q31,906AArch64_Q30_Q31_Q0, AArch64_Q31_Q0_Q1907};908909static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,910uint64_t Addr, const void *Decoder)911{912unsigned Register;913914if (RegNo > 31)915return Fail;916917Register = QQQDecoderTable[RegNo];918MCOperand_CreateReg0(Inst, Register);919920return Success;921}922923static const unsigned QQQQDecoderTable[] = {924AArch64_Q0_Q1_Q2_Q3, AArch64_Q1_Q2_Q3_Q4, AArch64_Q2_Q3_Q4_Q5,925AArch64_Q3_Q4_Q5_Q6, AArch64_Q4_Q5_Q6_Q7, AArch64_Q5_Q6_Q7_Q8,926AArch64_Q6_Q7_Q8_Q9, AArch64_Q7_Q8_Q9_Q10, AArch64_Q8_Q9_Q10_Q11,927AArch64_Q9_Q10_Q11_Q12, AArch64_Q10_Q11_Q12_Q13, AArch64_Q11_Q12_Q13_Q14,928AArch64_Q12_Q13_Q14_Q15, AArch64_Q13_Q14_Q15_Q16, AArch64_Q14_Q15_Q16_Q17,929AArch64_Q15_Q16_Q17_Q18, AArch64_Q16_Q17_Q18_Q19, AArch64_Q17_Q18_Q19_Q20,930AArch64_Q18_Q19_Q20_Q21, AArch64_Q19_Q20_Q21_Q22, AArch64_Q20_Q21_Q22_Q23,931AArch64_Q21_Q22_Q23_Q24, AArch64_Q22_Q23_Q24_Q25, AArch64_Q23_Q24_Q25_Q26,932AArch64_Q24_Q25_Q26_Q27, AArch64_Q25_Q26_Q27_Q28, AArch64_Q26_Q27_Q28_Q29,933AArch64_Q27_Q28_Q29_Q30, AArch64_Q28_Q29_Q30_Q31, AArch64_Q29_Q30_Q31_Q0,934AArch64_Q30_Q31_Q0_Q1, AArch64_Q31_Q0_Q1_Q2935};936937static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,938uint64_t Addr, const void *Decoder)939{940unsigned Register;941942if (RegNo > 31)943return Fail;944945Register = QQQQDecoderTable[RegNo];946MCOperand_CreateReg0(Inst, Register);947948return Success;949}950951static const unsigned DDDecoderTable[] = {952AArch64_D0_D1, AArch64_D1_D2, AArch64_D2_D3, AArch64_D3_D4,953AArch64_D4_D5, AArch64_D5_D6, AArch64_D6_D7, AArch64_D7_D8,954AArch64_D8_D9, AArch64_D9_D10, AArch64_D10_D11, AArch64_D11_D12,955AArch64_D12_D13, AArch64_D13_D14, AArch64_D14_D15, AArch64_D15_D16,956AArch64_D16_D17, AArch64_D17_D18, AArch64_D18_D19, AArch64_D19_D20,957AArch64_D20_D21, AArch64_D21_D22, AArch64_D22_D23, AArch64_D23_D24,958AArch64_D24_D25, AArch64_D25_D26, AArch64_D26_D27, AArch64_D27_D28,959AArch64_D28_D29, AArch64_D29_D30, AArch64_D30_D31, AArch64_D31_D0960};961962static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,963uint64_t Addr, const void *Decoder)964{965unsigned Register;966967if (RegNo > 31)968return Fail;969970Register = DDDecoderTable[RegNo];971MCOperand_CreateReg0(Inst, Register);972973return Success;974}975976static const unsigned DDDDecoderTable[] = {977AArch64_D0_D1_D2, AArch64_D1_D2_D3, AArch64_D2_D3_D4,978AArch64_D3_D4_D5, AArch64_D4_D5_D6, AArch64_D5_D6_D7,979AArch64_D6_D7_D8, AArch64_D7_D8_D9, AArch64_D8_D9_D10,980AArch64_D9_D10_D11, AArch64_D10_D11_D12, AArch64_D11_D12_D13,981AArch64_D12_D13_D14, AArch64_D13_D14_D15, AArch64_D14_D15_D16,982AArch64_D15_D16_D17, AArch64_D16_D17_D18, AArch64_D17_D18_D19,983AArch64_D18_D19_D20, AArch64_D19_D20_D21, AArch64_D20_D21_D22,984AArch64_D21_D22_D23, AArch64_D22_D23_D24, AArch64_D23_D24_D25,985AArch64_D24_D25_D26, AArch64_D25_D26_D27, AArch64_D26_D27_D28,986AArch64_D27_D28_D29, AArch64_D28_D29_D30, AArch64_D29_D30_D31,987AArch64_D30_D31_D0, AArch64_D31_D0_D1988};989990static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,991uint64_t Addr, const void *Decoder)992{993unsigned Register;994995if (RegNo > 31)996return Fail;997998Register = DDDDecoderTable[RegNo];999MCOperand_CreateReg0(Inst, Register);10001001return Success;1002}10031004static const unsigned DDDDDecoderTable[] = {1005AArch64_D0_D1_D2_D3, AArch64_D1_D2_D3_D4, AArch64_D2_D3_D4_D5,1006AArch64_D3_D4_D5_D6, AArch64_D4_D5_D6_D7, AArch64_D5_D6_D7_D8,1007AArch64_D6_D7_D8_D9, AArch64_D7_D8_D9_D10, AArch64_D8_D9_D10_D11,1008AArch64_D9_D10_D11_D12, AArch64_D10_D11_D12_D13, AArch64_D11_D12_D13_D14,1009AArch64_D12_D13_D14_D15, AArch64_D13_D14_D15_D16, AArch64_D14_D15_D16_D17,1010AArch64_D15_D16_D17_D18, AArch64_D16_D17_D18_D19, AArch64_D17_D18_D19_D20,1011AArch64_D18_D19_D20_D21, AArch64_D19_D20_D21_D22, AArch64_D20_D21_D22_D23,1012AArch64_D21_D22_D23_D24, AArch64_D22_D23_D24_D25, AArch64_D23_D24_D25_D26,1013AArch64_D24_D25_D26_D27, AArch64_D25_D26_D27_D28, AArch64_D26_D27_D28_D29,1014AArch64_D27_D28_D29_D30, AArch64_D28_D29_D30_D31, AArch64_D29_D30_D31_D0,1015AArch64_D30_D31_D0_D1, AArch64_D31_D0_D1_D21016};10171018static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,1019uint64_t Addr, const void *Decoder)1020{1021unsigned Register;10221023if (RegNo > 31)1024return Fail;10251026Register = DDDDDecoderTable[RegNo];1027MCOperand_CreateReg0(Inst, Register);10281029return Success;1030}10311032static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,1033uint64_t Addr, const void *Decoder)1034{1035// scale{5} is asserted as 1 in tblgen.1036Imm |= 0x20;1037MCOperand_CreateImm0(Inst, 64 - Imm);10381039return Success;1040}10411042static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,1043uint64_t Addr, const void *Decoder)1044{1045MCOperand_CreateImm0(Inst, 64 - Imm);10461047return Success;1048}10491050static DecodeStatus DecodePCRelLabel19(MCInst *Inst, unsigned Imm,1051uint64_t Addr, const void *Decoder)1052{1053int64_t ImmVal = Imm;10541055// Sign-extend 19-bit immediate.1056if (ImmVal & (1 << (19 - 1)))1057ImmVal |= ~((1LL << 19) - 1);10581059MCOperand_CreateImm0(Inst, ImmVal);10601061return Success;1062}10631064static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,1065uint64_t Address, const void *Decoder)1066{1067MCOperand_CreateImm0(Inst, (Imm >> 1) & 1);1068MCOperand_CreateImm0(Inst, Imm & 1);10691070return Success;1071}10721073static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,1074uint64_t Address, const void *Decoder)1075{1076MCOperand_CreateImm0(Inst, Imm);10771078// Every system register in the encoding space is valid with the syntax1079// S<op0>_<op1>_<Cn>_<Cm>_<op2>, so decoding system registers always succeeds.1080return Success;1081}10821083static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,1084uint64_t Address, const void *Decoder)1085{1086MCOperand_CreateImm0(Inst, Imm);10871088return Success;1089}10901091static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,1092uint64_t Address, const void *Decoder)1093{1094// This decoder exists to add the dummy Lane operand to the MCInst, which must1095// be 1 in assembly but has no other real manifestation.1096unsigned Rd = fieldFromInstruction_4(Insn, 0, 5);1097unsigned Rn = fieldFromInstruction_4(Insn, 5, 5);1098unsigned IsToVec = fieldFromInstruction_4(Insn, 16, 1);10991100if (IsToVec) {1101DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);1102DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);1103} else {1104DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);1105DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);1106}11071108// Add the lane1109MCOperand_CreateImm0(Inst, 1);11101111return Success;1112}11131114static DecodeStatus DecodeVecShiftRImm(MCInst *Inst, unsigned Imm,1115unsigned Add)1116{1117MCOperand_CreateImm0(Inst, Add - Imm);11181119return Success;1120}11211122static DecodeStatus DecodeVecShiftLImm(MCInst *Inst, unsigned Imm,1123unsigned Add)1124{1125MCOperand_CreateImm0(Inst, (Imm + Add) & (Add - 1));11261127return Success;1128}11291130static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,1131uint64_t Addr, const void *Decoder)1132{1133return DecodeVecShiftRImm(Inst, Imm, 64);1134}11351136static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,1137uint64_t Addr, const void *Decoder)1138{1139return DecodeVecShiftRImm(Inst, Imm | 0x20, 64);1140}11411142static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,1143uint64_t Addr, const void *Decoder)1144{1145return DecodeVecShiftRImm(Inst, Imm, 32);1146}11471148static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,1149uint64_t Addr, const void *Decoder)1150{1151return DecodeVecShiftRImm(Inst, Imm | 0x10, 32);1152}11531154static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,1155uint64_t Addr, const void *Decoder)1156{1157return DecodeVecShiftRImm(Inst, Imm, 16);1158}11591160static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,1161uint64_t Addr, const void *Decoder)1162{1163return DecodeVecShiftRImm(Inst, Imm | 0x8, 16);1164}11651166static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,1167uint64_t Addr, const void *Decoder)1168{1169return DecodeVecShiftRImm(Inst, Imm, 8);1170}11711172static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,1173uint64_t Addr, const void *Decoder)1174{1175return DecodeVecShiftLImm(Inst, Imm, 64);1176}11771178static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,1179uint64_t Addr, const void *Decoder)1180{1181return DecodeVecShiftLImm(Inst, Imm, 32);1182}11831184static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,1185uint64_t Addr, const void *Decoder)1186{1187return DecodeVecShiftLImm(Inst, Imm, 16);1188}11891190static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,1191uint64_t Addr, const void *Decoder)1192{1193return DecodeVecShiftLImm(Inst, Imm, 8);1194}11951196static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst,1197uint32_t insn, uint64_t Addr, const void *Decoder)1198{1199unsigned Rd = fieldFromInstruction_4(insn, 0, 5);1200unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1201unsigned Rm = fieldFromInstruction_4(insn, 16, 5);1202unsigned shiftHi = fieldFromInstruction_4(insn, 22, 2);1203unsigned shiftLo = fieldFromInstruction_4(insn, 10, 6);1204unsigned shift = (shiftHi << 6) | shiftLo;12051206switch (MCInst_getOpcode(Inst)) {1207default:1208return Fail;12091210case AArch64_ADDWrs:1211case AArch64_ADDSWrs:1212case AArch64_SUBWrs:1213case AArch64_SUBSWrs:1214// if shift == '11' then ReservedValue()1215if (shiftHi == 0x3)1216return Fail;1217// Deliberate fallthrough12181219case AArch64_ANDWrs:1220case AArch64_ANDSWrs:1221case AArch64_BICWrs:1222case AArch64_BICSWrs:1223case AArch64_ORRWrs:1224case AArch64_ORNWrs:1225case AArch64_EORWrs:1226case AArch64_EONWrs: {1227// if sf == '0' and imm6<5> == '1' then ReservedValue()1228if (shiftLo >> 5 == 1)1229return Fail;12301231DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);1232DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);1233DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);1234break;1235}12361237case AArch64_ADDXrs:1238case AArch64_ADDSXrs:1239case AArch64_SUBXrs:1240case AArch64_SUBSXrs:1241// if shift == '11' then ReservedValue()1242if (shiftHi == 0x3)1243return Fail;1244// Deliberate fallthrough12451246case AArch64_ANDXrs:1247case AArch64_ANDSXrs:1248case AArch64_BICXrs:1249case AArch64_BICSXrs:1250case AArch64_ORRXrs:1251case AArch64_ORNXrs:1252case AArch64_EORXrs:1253case AArch64_EONXrs:1254DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);1255DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);1256DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);1257break;1258}12591260MCOperand_CreateImm0(Inst, shift);12611262return Success;1263}12641265static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,1266uint64_t Addr, const void *Decoder)1267{1268unsigned Rd = fieldFromInstruction_4(insn, 0, 5);1269unsigned imm = fieldFromInstruction_4(insn, 5, 16);1270unsigned shift = fieldFromInstruction_4(insn, 21, 2);12711272shift <<= 4;12731274switch (MCInst_getOpcode(Inst)) {1275default:1276return Fail;12771278case AArch64_MOVZWi:1279case AArch64_MOVNWi:1280case AArch64_MOVKWi:1281if (shift & (1U << 5))1282return Fail;1283DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);1284break;12851286case AArch64_MOVZXi:1287case AArch64_MOVNXi:1288case AArch64_MOVKXi:1289DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);1290break;1291}12921293if (MCInst_getOpcode(Inst) == AArch64_MOVKWi ||1294MCInst_getOpcode(Inst) == AArch64_MOVKXi)1295MCInst_addOperand2(Inst, MCInst_getOperand(Inst, 0));12961297MCOperand_CreateImm0(Inst, imm);1298MCOperand_CreateImm0(Inst, shift);12991300return Success;1301}13021303static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst,1304uint32_t insn, uint64_t Addr, const void *Decoder)1305{1306unsigned Rt = fieldFromInstruction_4(insn, 0, 5);1307unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1308unsigned offset = fieldFromInstruction_4(insn, 10, 12);13091310switch (MCInst_getOpcode(Inst)) {1311default:1312return Fail;13131314case AArch64_PRFMui:1315// Rt is an immediate in prefetch.1316MCOperand_CreateImm0(Inst, Rt);1317break;13181319case AArch64_STRBBui:1320case AArch64_LDRBBui:1321case AArch64_LDRSBWui:1322case AArch64_STRHHui:1323case AArch64_LDRHHui:1324case AArch64_LDRSHWui:1325case AArch64_STRWui:1326case AArch64_LDRWui:1327DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);1328break;13291330case AArch64_LDRSBXui:1331case AArch64_LDRSHXui:1332case AArch64_LDRSWui:1333case AArch64_STRXui:1334case AArch64_LDRXui:1335DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1336break;13371338case AArch64_LDRQui:1339case AArch64_STRQui:1340DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);1341break;13421343case AArch64_LDRDui:1344case AArch64_STRDui:1345DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);1346break;13471348case AArch64_LDRSui:1349case AArch64_STRSui:1350DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);1351break;13521353case AArch64_LDRHui:1354case AArch64_STRHui:1355DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);1356break;13571358case AArch64_LDRBui:1359case AArch64_STRBui:1360DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);1361break;1362}13631364DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);13651366//if (!Dis->tryAddingSymbolicOperand(Inst, offset, Addr, Fail, 0, 4))1367MCOperand_CreateImm0(Inst, offset);13681369return Success;1370}13711372static DecodeStatus DecodeSignedLdStInstruction(MCInst *Inst,1373uint32_t insn, uint64_t Addr, const void *Decoder)1374{1375bool IsLoad, IsIndexed, IsFP;1376unsigned Rt = fieldFromInstruction_4(insn, 0, 5);1377unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1378int64_t offset = fieldFromInstruction_4(insn, 12, 9);13791380// offset is a 9-bit signed immediate, so sign extend it to1381// fill the unsigned.1382if (offset & (1 << (9 - 1)))1383offset |= ~((1LL << 9) - 1);13841385// First operand is always the writeback to the address register, if needed.1386switch (MCInst_getOpcode(Inst)) {1387default:1388break;13891390case AArch64_LDRSBWpre:1391case AArch64_LDRSHWpre:1392case AArch64_STRBBpre:1393case AArch64_LDRBBpre:1394case AArch64_STRHHpre:1395case AArch64_LDRHHpre:1396case AArch64_STRWpre:1397case AArch64_LDRWpre:1398case AArch64_LDRSBWpost:1399case AArch64_LDRSHWpost:1400case AArch64_STRBBpost:1401case AArch64_LDRBBpost:1402case AArch64_STRHHpost:1403case AArch64_LDRHHpost:1404case AArch64_STRWpost:1405case AArch64_LDRWpost:1406case AArch64_LDRSBXpre:1407case AArch64_LDRSHXpre:1408case AArch64_STRXpre:1409case AArch64_LDRSWpre:1410case AArch64_LDRXpre:1411case AArch64_LDRSBXpost:1412case AArch64_LDRSHXpost:1413case AArch64_STRXpost:1414case AArch64_LDRSWpost:1415case AArch64_LDRXpost:1416case AArch64_LDRQpre:1417case AArch64_STRQpre:1418case AArch64_LDRQpost:1419case AArch64_STRQpost:1420case AArch64_LDRDpre:1421case AArch64_STRDpre:1422case AArch64_LDRDpost:1423case AArch64_STRDpost:1424case AArch64_LDRSpre:1425case AArch64_STRSpre:1426case AArch64_LDRSpost:1427case AArch64_STRSpost:1428case AArch64_LDRHpre:1429case AArch64_STRHpre:1430case AArch64_LDRHpost:1431case AArch64_STRHpost:1432case AArch64_LDRBpre:1433case AArch64_STRBpre:1434case AArch64_LDRBpost:1435case AArch64_STRBpost:1436DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1437break;1438}14391440switch (MCInst_getOpcode(Inst)) {1441default:1442return Fail;14431444case AArch64_PRFUMi:1445// Rt is an immediate in prefetch.1446MCOperand_CreateImm0(Inst, Rt);1447break;14481449case AArch64_STURBBi:1450case AArch64_LDURBBi:1451case AArch64_LDURSBWi:1452case AArch64_STURHHi:1453case AArch64_LDURHHi:1454case AArch64_LDURSHWi:1455case AArch64_STURWi:1456case AArch64_LDURWi:1457case AArch64_LDTRSBWi:1458case AArch64_LDTRSHWi:1459case AArch64_STTRWi:1460case AArch64_LDTRWi:1461case AArch64_STTRHi:1462case AArch64_LDTRHi:1463case AArch64_LDTRBi:1464case AArch64_STTRBi:1465case AArch64_LDRSBWpre:1466case AArch64_LDRSHWpre:1467case AArch64_STRBBpre:1468case AArch64_LDRBBpre:1469case AArch64_STRHHpre:1470case AArch64_LDRHHpre:1471case AArch64_STRWpre:1472case AArch64_LDRWpre:1473case AArch64_LDRSBWpost:1474case AArch64_LDRSHWpost:1475case AArch64_STRBBpost:1476case AArch64_LDRBBpost:1477case AArch64_STRHHpost:1478case AArch64_LDRHHpost:1479case AArch64_STRWpost:1480case AArch64_LDRWpost:1481case AArch64_STLURBi:1482case AArch64_STLURHi:1483case AArch64_STLURWi:1484case AArch64_LDAPURBi:1485case AArch64_LDAPURSBWi:1486case AArch64_LDAPURHi:1487case AArch64_LDAPURSHWi:1488case AArch64_LDAPURi:1489DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);1490break;14911492case AArch64_LDURSBXi:1493case AArch64_LDURSHXi:1494case AArch64_LDURSWi:1495case AArch64_STURXi:1496case AArch64_LDURXi:1497case AArch64_LDTRSBXi:1498case AArch64_LDTRSHXi:1499case AArch64_LDTRSWi:1500case AArch64_STTRXi:1501case AArch64_LDTRXi:1502case AArch64_LDRSBXpre:1503case AArch64_LDRSHXpre:1504case AArch64_STRXpre:1505case AArch64_LDRSWpre:1506case AArch64_LDRXpre:1507case AArch64_LDRSBXpost:1508case AArch64_LDRSHXpost:1509case AArch64_STRXpost:1510case AArch64_LDRSWpost:1511case AArch64_LDRXpost:1512case AArch64_LDAPURSWi:1513case AArch64_LDAPURSHXi:1514case AArch64_LDAPURSBXi:1515case AArch64_STLURXi:1516case AArch64_LDAPURXi:1517DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1518break;15191520case AArch64_LDURQi:1521case AArch64_STURQi:1522case AArch64_LDRQpre:1523case AArch64_STRQpre:1524case AArch64_LDRQpost:1525case AArch64_STRQpost:1526DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);1527break;15281529case AArch64_LDURDi:1530case AArch64_STURDi:1531case AArch64_LDRDpre:1532case AArch64_STRDpre:1533case AArch64_LDRDpost:1534case AArch64_STRDpost:1535DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);1536break;15371538case AArch64_LDURSi:1539case AArch64_STURSi:1540case AArch64_LDRSpre:1541case AArch64_STRSpre:1542case AArch64_LDRSpost:1543case AArch64_STRSpost:1544DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);1545break;15461547case AArch64_LDURHi:1548case AArch64_STURHi:1549case AArch64_LDRHpre:1550case AArch64_STRHpre:1551case AArch64_LDRHpost:1552case AArch64_STRHpost:1553DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);1554break;15551556case AArch64_LDURBi:1557case AArch64_STURBi:1558case AArch64_LDRBpre:1559case AArch64_STRBpre:1560case AArch64_LDRBpost:1561case AArch64_STRBpost:1562DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);1563break;1564}15651566DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1567MCOperand_CreateImm0(Inst, offset);15681569IsLoad = fieldFromInstruction_4(insn, 22, 1) != 0;1570IsIndexed = fieldFromInstruction_4(insn, 10, 2) != 0;1571IsFP = fieldFromInstruction_4(insn, 26, 1) != 0;15721573// Cannot write back to a transfer register (but xzr != sp).1574if (IsLoad && IsIndexed && !IsFP && Rn != 31 && Rt == Rn)1575return SoftFail;15761577return Success;1578}15791580static DecodeStatus DecodeExclusiveLdStInstruction(MCInst *Inst,1581uint32_t insn, uint64_t Addr, const void *Decoder)1582{1583unsigned Rt = fieldFromInstruction_4(insn, 0, 5);1584unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1585unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);1586unsigned Rs = fieldFromInstruction_4(insn, 16, 5);1587unsigned Opcode = MCInst_getOpcode(Inst);15881589switch (Opcode) {1590default:1591return Fail;15921593case AArch64_STLXRW:1594case AArch64_STLXRB:1595case AArch64_STLXRH:1596case AArch64_STXRW:1597case AArch64_STXRB:1598case AArch64_STXRH:1599DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);1600// FALLTHROUGH1601case AArch64_LDARW:1602case AArch64_LDARB:1603case AArch64_LDARH:1604case AArch64_LDAXRW:1605case AArch64_LDAXRB:1606case AArch64_LDAXRH:1607case AArch64_LDXRW:1608case AArch64_LDXRB:1609case AArch64_LDXRH:1610case AArch64_STLRW:1611case AArch64_STLRB:1612case AArch64_STLRH:1613case AArch64_STLLRW:1614case AArch64_STLLRB:1615case AArch64_STLLRH:1616case AArch64_LDLARW:1617case AArch64_LDLARB:1618case AArch64_LDLARH:1619DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);1620break;16211622case AArch64_STLXRX:1623case AArch64_STXRX:1624DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);1625// FALLTHROUGH1626case AArch64_LDARX:1627case AArch64_LDAXRX:1628case AArch64_LDXRX:1629case AArch64_STLRX:1630case AArch64_LDLARX:1631case AArch64_STLLRX:1632DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1633break;16341635case AArch64_STLXPW:1636case AArch64_STXPW:1637DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);1638// FALLTHROUGH1639case AArch64_LDAXPW:1640case AArch64_LDXPW:1641DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);1642DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);1643break;16441645case AArch64_STLXPX:1646case AArch64_STXPX:1647DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);1648// FALLTHROUGH1649case AArch64_LDAXPX:1650case AArch64_LDXPX:1651DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1652DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);1653break;1654}16551656DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);16571658// You shouldn't load to the same register twice in an instruction...1659if ((Opcode == AArch64_LDAXPW || Opcode == AArch64_LDXPW ||1660Opcode == AArch64_LDAXPX || Opcode == AArch64_LDXPX) &&1661Rt == Rt2)1662return SoftFail;16631664return Success;1665}16661667static DecodeStatus DecodePairLdStInstruction(MCInst *Inst, uint32_t insn,1668uint64_t Addr, const void *Decoder)1669{1670unsigned Rt = fieldFromInstruction_4(insn, 0, 5);1671unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1672unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);1673int32_t offset = fieldFromInstruction_4(insn, 15, 7);1674bool IsLoad = fieldFromInstruction_4(insn, 22, 1) != 0;1675unsigned Opcode = MCInst_getOpcode(Inst);1676bool NeedsDisjointWritebackTransfer = false;16771678// offset is a 7-bit signed immediate, so sign extend it to1679// fill the unsigned.1680if (offset & (1 << (7 - 1)))1681offset |= ~((1LL << 7) - 1);16821683// First operand is always writeback of base register.1684switch (Opcode) {1685default:1686break;16871688case AArch64_LDPXpost:1689case AArch64_STPXpost:1690case AArch64_LDPSWpost:1691case AArch64_LDPXpre:1692case AArch64_STPXpre:1693case AArch64_LDPSWpre:1694case AArch64_LDPWpost:1695case AArch64_STPWpost:1696case AArch64_LDPWpre:1697case AArch64_STPWpre:1698case AArch64_LDPQpost:1699case AArch64_STPQpost:1700case AArch64_LDPQpre:1701case AArch64_STPQpre:1702case AArch64_LDPDpost:1703case AArch64_STPDpost:1704case AArch64_LDPDpre:1705case AArch64_STPDpre:1706case AArch64_LDPSpost:1707case AArch64_STPSpost:1708case AArch64_LDPSpre:1709case AArch64_STPSpre:1710DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1711break;1712}17131714switch (Opcode) {1715default:1716return Fail;17171718case AArch64_LDPXpost:1719case AArch64_STPXpost:1720case AArch64_LDPSWpost:1721case AArch64_LDPXpre:1722case AArch64_STPXpre:1723case AArch64_LDPSWpre:1724NeedsDisjointWritebackTransfer = true;1725// Fallthrough1726case AArch64_LDNPXi:1727case AArch64_STNPXi:1728case AArch64_LDPXi:1729case AArch64_STPXi:1730case AArch64_LDPSWi:1731DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1732DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);1733break;17341735case AArch64_LDPWpost:1736case AArch64_STPWpost:1737case AArch64_LDPWpre:1738case AArch64_STPWpre:1739NeedsDisjointWritebackTransfer = true;1740// Fallthrough1741case AArch64_LDNPWi:1742case AArch64_STNPWi:1743case AArch64_LDPWi:1744case AArch64_STPWi:1745DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);1746DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);1747break;17481749case AArch64_LDNPQi:1750case AArch64_STNPQi:1751case AArch64_LDPQpost:1752case AArch64_STPQpost:1753case AArch64_LDPQi:1754case AArch64_STPQi:1755case AArch64_LDPQpre:1756case AArch64_STPQpre:1757DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);1758DecodeFPR128RegisterClass(Inst, Rt2, Addr, Decoder);1759break;17601761case AArch64_LDNPDi:1762case AArch64_STNPDi:1763case AArch64_LDPDpost:1764case AArch64_STPDpost:1765case AArch64_LDPDi:1766case AArch64_STPDi:1767case AArch64_LDPDpre:1768case AArch64_STPDpre:1769DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);1770DecodeFPR64RegisterClass(Inst, Rt2, Addr, Decoder);1771break;17721773case AArch64_LDNPSi:1774case AArch64_STNPSi:1775case AArch64_LDPSpost:1776case AArch64_STPSpost:1777case AArch64_LDPSi:1778case AArch64_STPSi:1779case AArch64_LDPSpre:1780case AArch64_STPSpre:1781DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);1782DecodeFPR32RegisterClass(Inst, Rt2, Addr, Decoder);1783break;1784}17851786DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1787MCOperand_CreateImm0(Inst, offset);17881789// You shouldn't load to the same register twice in an instruction...1790if (IsLoad && Rt == Rt2)1791return SoftFail;17921793// ... or do any operation that writes-back to a transfer register. But note1794// that "stp xzr, xzr, [sp], #4" is fine because xzr and sp are different.1795if (NeedsDisjointWritebackTransfer && Rn != 31 && (Rt == Rn || Rt2 == Rn))1796return SoftFail;17971798return Success;1799}18001801static DecodeStatus DecodeAuthLoadInstruction(MCInst *Inst, uint32_t insn,1802uint64_t Addr, const void *Decoder)1803{1804unsigned Rt = fieldFromInstruction_4(insn, 0, 5);1805unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1806uint64_t offset = fieldFromInstruction_4(insn, 22, 1) << 9 |1807fieldFromInstruction_4(insn, 12, 9);1808unsigned writeback = fieldFromInstruction_4(insn, 11, 1);18091810switch (MCInst_getOpcode(Inst)) {1811default:1812return Fail;1813case AArch64_LDRAAwriteback:1814case AArch64_LDRABwriteback:1815DecodeGPR64spRegisterClass(Inst, Rn /* writeback register */, Addr,1816Decoder);1817break;1818case AArch64_LDRAAindexed:1819case AArch64_LDRABindexed:1820break;1821}18221823DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);1824DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1825DecodeSImm(Inst, offset, Addr, Decoder, 10);18261827if (writeback && Rt == Rn && Rn != 31) {1828return SoftFail;1829}18301831return Success;1832}18331834static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst,1835uint32_t insn, uint64_t Addr, const void *Decoder)1836{1837unsigned Rd, Rn, Rm;1838unsigned extend = fieldFromInstruction_4(insn, 10, 6);1839unsigned shift = extend & 0x7;18401841if (shift > 4)1842return Fail;18431844Rd = fieldFromInstruction_4(insn, 0, 5);1845Rn = fieldFromInstruction_4(insn, 5, 5);1846Rm = fieldFromInstruction_4(insn, 16, 5);18471848switch (MCInst_getOpcode(Inst)) {1849default:1850return Fail;18511852case AArch64_ADDWrx:1853case AArch64_SUBWrx:1854DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);1855DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);1856DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);1857break;18581859case AArch64_ADDSWrx:1860case AArch64_SUBSWrx:1861DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);1862DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);1863DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);1864break;18651866case AArch64_ADDXrx:1867case AArch64_SUBXrx:1868DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);1869DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1870DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);1871break;18721873case AArch64_ADDSXrx:1874case AArch64_SUBSXrx:1875DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);1876DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1877DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);1878break;18791880case AArch64_ADDXrx64:1881case AArch64_SUBXrx64:1882DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);1883DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1884DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);1885break;18861887case AArch64_SUBSXrx64:1888case AArch64_ADDSXrx64:1889DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);1890DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);1891DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);1892break;1893}18941895MCOperand_CreateImm0(Inst, extend);18961897return Success;1898}18991900static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst,1901uint32_t insn, uint64_t Addr, const void *Decoder)1902{1903unsigned Rd = fieldFromInstruction_4(insn, 0, 5);1904unsigned Rn = fieldFromInstruction_4(insn, 5, 5);1905unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);1906unsigned imm;19071908if (Datasize) {1909if (MCInst_getOpcode(Inst) == AArch64_ANDSXri)1910DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);1911else1912DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);19131914DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);19151916imm = fieldFromInstruction_4(insn, 10, 13);1917if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))1918return Fail;1919} else {1920if (MCInst_getOpcode(Inst) == AArch64_ANDSWri)1921DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);1922else1923DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);19241925DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);19261927imm = fieldFromInstruction_4(insn, 10, 12);1928if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 32))1929return Fail;1930}19311932MCOperand_CreateImm0(Inst, imm);19331934return Success;1935}19361937static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,1938uint64_t Addr, const void *Decoder)1939{1940unsigned Rd = fieldFromInstruction_4(insn, 0, 5);1941unsigned cmode = fieldFromInstruction_4(insn, 12, 4);1942unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;1943imm |= fieldFromInstruction_4(insn, 5, 5);19441945if (MCInst_getOpcode(Inst) == AArch64_MOVID)1946DecodeFPR64RegisterClass(Inst, Rd, Addr, Decoder);1947else1948DecodeVectorRegisterClass(Inst, Rd, Addr, Decoder);19491950MCOperand_CreateImm0(Inst, imm);19511952switch (MCInst_getOpcode(Inst)) {1953default:1954break;19551956case AArch64_MOVIv4i16:1957case AArch64_MOVIv8i16:1958case AArch64_MVNIv4i16:1959case AArch64_MVNIv8i16:1960case AArch64_MOVIv2i32:1961case AArch64_MOVIv4i32:1962case AArch64_MVNIv2i32:1963case AArch64_MVNIv4i32:1964MCOperand_CreateImm0(Inst, (cmode & 6) << 2);1965break;19661967case AArch64_MOVIv2s_msl:1968case AArch64_MOVIv4s_msl:1969case AArch64_MVNIv2s_msl:1970case AArch64_MVNIv4s_msl:1971MCOperand_CreateImm0(Inst, cmode & 1 ? 0x110 : 0x108);1972break;1973}19741975return Success;1976}19771978static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst,1979uint32_t insn, uint64_t Addr, const void *Decoder)1980{1981unsigned Rd = fieldFromInstruction_4(insn, 0, 5);1982unsigned cmode = fieldFromInstruction_4(insn, 12, 4);1983unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;1984imm |= fieldFromInstruction_4(insn, 5, 5);19851986// Tied operands added twice.1987DecodeVectorRegisterClass(Inst, Rd, Addr, Decoder);1988DecodeVectorRegisterClass(Inst, Rd, Addr, Decoder);19891990MCOperand_CreateImm0(Inst, imm);1991MCOperand_CreateImm0(Inst, (cmode & 6) << 2);19921993return Success;1994}19951996static DecodeStatus DecodeAdrInstruction(MCInst *Inst, uint32_t insn,1997uint64_t Addr, const void *Decoder)1998{1999unsigned Rd = fieldFromInstruction_4(insn, 0, 5);2000int64_t imm = fieldFromInstruction_4(insn, 5, 19) << 2;2001imm |= fieldFromInstruction_4(insn, 29, 2);20022003// Sign-extend the 21-bit immediate.2004if (imm & (1 << (21 - 1)))2005imm |= ~((1LL << 21) - 1);20062007DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);2008//if (!Dis->tryAddingSymbolicOperand(Inst, imm, Addr, Fail, 0, 4))2009MCOperand_CreateImm0(Inst, imm);20102011return Success;2012}20132014static DecodeStatus DecodeAddSubImmShift(MCInst *Inst, uint32_t insn,2015uint64_t Addr, const void *Decoder)2016{2017unsigned Rd = fieldFromInstruction_4(insn, 0, 5);2018unsigned Rn = fieldFromInstruction_4(insn, 5, 5);2019unsigned Imm = fieldFromInstruction_4(insn, 10, 14);2020unsigned S = fieldFromInstruction_4(insn, 29, 1);2021unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);20222023unsigned ShifterVal = (Imm >> 12) & 3;2024unsigned ImmVal = Imm & 0xFFF;2025// const AArch64Disassembler *Dis =2026// static_cast<const AArch64Disassembler *>(Decoder);20272028if (ShifterVal != 0 && ShifterVal != 1)2029return Fail;20302031if (Datasize) {2032if (Rd == 31 && !S)2033DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);2034else2035DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);2036DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);2037} else {2038if (Rd == 31 && !S)2039DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);2040else2041DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);2042DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);2043}20442045// if (!Dis->tryAddingSymbolicOperand(Inst, Imm, Addr, Fail, 0, 4))2046MCOperand_CreateImm0(Inst, ImmVal);20472048MCOperand_CreateImm0(Inst, (12 * ShifterVal));2049return Success;2050}20512052static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,2053uint64_t Addr, const void *Decoder)2054{2055int64_t imm = fieldFromInstruction_4(insn, 0, 26);20562057// Sign-extend the 26-bit immediate.2058if (imm & (1 << (26 - 1)))2059imm |= ~((1LL << 26) - 1);20602061// if (!Dis->tryAddingSymbolicOperand(Inst, imm << 2, Addr, true, 0, 4))2062MCOperand_CreateImm0(Inst, imm);20632064return Success;2065}20662067static DecodeStatus DecodeSystemPStateInstruction(MCInst *Inst,2068uint32_t insn, uint64_t Addr, const void *Decoder)2069{2070uint32_t op1 = fieldFromInstruction_4(insn, 16, 3);2071uint32_t op2 = fieldFromInstruction_4(insn, 5, 3);2072uint32_t crm = fieldFromInstruction_4(insn, 8, 4);2073uint32_t pstate_field = (op1 << 3) | op2;20742075if ((pstate_field == AArch64PState_PAN ||2076pstate_field == AArch64PState_UAO) && crm > 1)2077return Fail;20782079MCOperand_CreateImm0(Inst, pstate_field);2080MCOperand_CreateImm0(Inst, crm);20812082if (lookupPStateByEncoding(pstate_field))2083return Success;20842085return Fail;2086}20872088static DecodeStatus DecodeTestAndBranch(MCInst *Inst, uint32_t insn,2089uint64_t Addr, const void *Decoder)2090{2091uint32_t Rt = fieldFromInstruction_4(insn, 0, 5);2092uint32_t bit = fieldFromInstruction_4(insn, 31, 1) << 5;2093uint64_t dst = fieldFromInstruction_4(insn, 5, 14);20942095bit |= fieldFromInstruction_4(insn, 19, 5);20962097// Sign-extend 14-bit immediate.2098if (dst & (1 << (14 - 1)))2099dst |= ~((1LL << 14) - 1);21002101if (fieldFromInstruction_4(insn, 31, 1) == 0)2102DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);2103else2104DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);21052106MCOperand_CreateImm0(Inst, bit);21072108//if (!Dis->tryAddingSymbolicOperand(Inst, dst << 2, Addr, true, 0, 4))2109MCOperand_CreateImm0(Inst, dst);21102111return Success;2112}21132114static DecodeStatus DecodeGPRSeqPairsClassRegisterClass(MCInst *Inst,2115unsigned RegClassID, unsigned RegNo, uint64_t Addr, const void *Decoder)2116{2117unsigned Register;21182119// Register number must be even (see CASP instruction)2120if (RegNo & 0x1)2121return Fail;21222123Register = AArch64MCRegisterClasses[RegClassID].RegsBegin[RegNo / 2];2124MCOperand_CreateReg0(Inst, Register);21252126return Success;2127}21282129static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,2130unsigned RegNo, uint64_t Addr, const void *Decoder)2131{2132return DecodeGPRSeqPairsClassRegisterClass(Inst,2133AArch64_WSeqPairsClassRegClassID, RegNo, Addr, Decoder);2134}21352136static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,2137unsigned RegNo, uint64_t Addr, const void *Decoder)2138{2139return DecodeGPRSeqPairsClassRegisterClass(Inst,2140AArch64_XSeqPairsClassRegClassID, RegNo, Addr, Decoder);2141}21422143static DecodeStatus DecodeSVELogicalImmInstruction(MCInst *Inst, uint32_t insn,2144uint64_t Addr, const void *Decoder)2145{2146unsigned Zdn = fieldFromInstruction_4(insn, 0, 5);2147unsigned imm = fieldFromInstruction_4(insn, 5, 13);21482149if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))2150return Fail;21512152// The same (tied) operand is added twice to the instruction.2153DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);2154if (MCInst_getOpcode(Inst) != AArch64_DUPM_ZI)2155DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);21562157MCOperand_CreateImm0(Inst, imm);21582159return Success;2160}21612162static DecodeStatus DecodeSImm(MCInst *Inst, uint64_t Imm, uint64_t Address,2163const void *Decoder, int Bits)2164{2165if (Imm & ~((1LL << Bits) - 1))2166return Fail;21672168// Imm is a signed immediate, so sign extend it.2169if (Imm & (1 << (Bits - 1)))2170Imm |= ~((1LL << Bits) - 1);21712172MCOperand_CreateImm0(Inst, Imm);21732174return Success;2175}21762177// Decode 8-bit signed/unsigned immediate for a given element width.2178static DecodeStatus DecodeImm8OptLsl(MCInst *Inst, unsigned Imm, uint64_t Addr,2179const void *Decoder, int ElementWidth)2180{2181unsigned Val = (uint8_t)Imm;2182unsigned Shift = (Imm & 0x100) ? 8 : 0;21832184if (ElementWidth == 8 && Shift)2185return Fail;21862187MCOperand_CreateImm0(Inst, Val);2188MCOperand_CreateImm0(Inst, Shift);21892190return Success;2191}21922193// Decode uimm4 ranged from 1-16.2194static DecodeStatus DecodeSVEIncDecImm(MCInst *Inst, unsigned Imm,2195uint64_t Addr, const void *Decoder)2196{2197MCOperand_CreateImm0(Inst, Imm + 1);21982199return Success;2200}22012202static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,2203const void *Decoder) {2204if (lookupSVCRByEncoding(Imm)) {2205MCOperand_CreateImm0(Inst, Imm);2206return Success;2207}2208return Fail;2209}22102211static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,2212uint64_t Addr, const void *Decoder) {2213unsigned Rd = fieldFromInstruction_4(insn, 0, 5);2214unsigned Rs = fieldFromInstruction_4(insn, 16, 5);2215unsigned Rn = fieldFromInstruction_4(insn, 5, 5);22162217// None of the registers may alias: if they do, then the instruction is not2218// merely unpredictable but actually entirely unallocated.2219if (Rd == Rs || Rs == Rn || Rd == Rn)2220return Fail;22212222// All three register operands are written back, so they all appear2223// twice in the operand list, once as outputs and once as inputs.2224if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||2225!DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||2226!DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||2227!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||2228!DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||2229!DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder))2230return Fail;22312232return Success;2233}22342235static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,2236uint64_t Addr, const void *Decoder) {2237unsigned Rd = fieldFromInstruction_4(insn, 0, 5);2238unsigned Rm = fieldFromInstruction_4(insn, 16, 5);2239unsigned Rn = fieldFromInstruction_4(insn, 5, 5);22402241// None of the registers may alias: if they do, then the instruction is not2242// merely unpredictable but actually entirely unallocated.2243if (Rd == Rm || Rm == Rn || Rd == Rn)2244return Fail;22452246// Rd and Rn (not Rm) register operands are written back, so they appear2247// twice in the operand list, once as outputs and once as inputs.2248if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||2249!DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||2250!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||2251!DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||2252!DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder))2253return Fail;22542255return Success;2256}22572258void AArch64_init(MCRegisterInfo *MRI)2259{2260/*2261InitMCRegisterInfo(AArch64RegDesc, 661,2262RA, PC,2263AArch64MCRegisterClasses, 100,2264AArch64RegUnitRoots, 115, AArch64RegDiffLists,2265AArch64LaneMaskLists, AArch64RegStrings, AArch64RegClassStrings,2266AArch64SubRegIdxLists, 100,2267AArch64SubRegIdxRanges, AArch64RegEncodingTable);2268*/22692270MCRegisterInfo_InitMCRegisterInfo(MRI, AArch64RegDesc, 674,22710, 0,2272AArch64MCRegisterClasses, 202,22730, 0, AArch64RegDiffLists,22740,2275AArch64SubRegIdxLists, 100,22760);2277}22782279#endif228022812282