Path: blob/main/contrib/llvm-project/llvm/lib/Target/Hexagon/Disassembler/HexagonDisassembler.cpp
35293 views
//===- HexagonDisassembler.cpp - Disassembler for Hexagon ISA -------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "MCTargetDesc/HexagonBaseInfo.h"9#include "MCTargetDesc/HexagonMCChecker.h"10#include "MCTargetDesc/HexagonMCInstrInfo.h"11#include "MCTargetDesc/HexagonMCTargetDesc.h"12#include "TargetInfo/HexagonTargetInfo.h"13#include "llvm/ADT/ArrayRef.h"14#include "llvm/ADT/STLExtras.h"15#include "llvm/MC/MCContext.h"16#include "llvm/MC/MCDecoderOps.h"17#include "llvm/MC/MCDisassembler/MCDisassembler.h"18#include "llvm/MC/MCExpr.h"19#include "llvm/MC/MCInst.h"20#include "llvm/MC/MCInstrInfo.h"21#include "llvm/MC/MCRegisterInfo.h"22#include "llvm/MC/MCSubtargetInfo.h"23#include "llvm/MC/TargetRegistry.h"24#include "llvm/Support/Endian.h"25#include "llvm/Support/MathExtras.h"26#include "llvm/Support/raw_ostream.h"27#include <cassert>28#include <cstddef>29#include <cstdint>30#include <memory>3132#define DEBUG_TYPE "hexagon-disassembler"3334using namespace llvm;35using namespace Hexagon;3637using DecodeStatus = MCDisassembler::DecodeStatus;3839namespace {4041/// Hexagon disassembler for all Hexagon platforms.42class HexagonDisassembler : public MCDisassembler {43public:44std::unique_ptr<MCInstrInfo const> const MCII;45std::unique_ptr<MCInst *> CurrentBundle;46mutable MCInst const *CurrentExtender;4748HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,49MCInstrInfo const *MCII)50: MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),51CurrentExtender(nullptr) {}5253DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,54ArrayRef<uint8_t> Bytes, uint64_t Address,55raw_ostream &CStream, bool &Complete) const;56DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,57ArrayRef<uint8_t> Bytes, uint64_t Address,58raw_ostream &CStream) const override;59void remapInstruction(MCInst &Instr) const;60};6162static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,63int64_t Value) {64MCInstrInfo MCII = *Disassembler.MCII;65if (!Disassembler.CurrentExtender ||66MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))67return Value;68unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);69uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;70int64_t Bits;71bool Success =72Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(73Bits);74assert(Success);75(void)Success;76uint64_t Upper26 = static_cast<uint64_t>(Bits);77uint64_t Operand = Upper26 | Lower6;78return Operand;79}80static HexagonDisassembler const &disassembler(const MCDisassembler *Decoder) {81return *static_cast<HexagonDisassembler const *>(Decoder);82}83template <size_t T>84static void signedDecoder(MCInst &MI, unsigned tmp,85const MCDisassembler *Decoder) {86HexagonDisassembler const &Disassembler = disassembler(Decoder);87int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));88int64_t Extended = SignExtend64<32>(FullValue);89HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());90}91}9293// Forward declare these because the auto-generated code will reference them.94// Definitions are further down.9596static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,97uint64_t Address,98const MCDisassembler *Decoder);99static DecodeStatus100DecodeGeneralSubRegsRegisterClass(MCInst &Inst, unsigned RegNo,101uint64_t Address,102const MCDisassembler *Decoder);103static DecodeStatus104DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,105const MCDisassembler *Decoder);106static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,107uint64_t Address,108const MCDisassembler *Decoder);109static DecodeStatus110DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,111const MCDisassembler *Decoder);112static DecodeStatus113DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,114uint64_t Address,115const MCDisassembler *Decoder);116static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,117uint64_t Address,118const MCDisassembler *Decoder);119static DecodeStatus DecodeHvxVQRRegisterClass(MCInst &Inst, unsigned RegNo,120uint64_t Address,121const MCDisassembler *Decoder);122static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,123uint64_t Address,124const MCDisassembler *Decoder);125static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,126uint64_t Address,127const MCDisassembler *Decoder);128static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,129uint64_t Address,130const MCDisassembler *Decoder);131static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,132uint64_t Address,133const MCDisassembler *Decoder);134static DecodeStatus DecodeSysRegsRegisterClass(MCInst &Inst, unsigned RegNo,135uint64_t Address,136const MCDisassembler *Decoder);137static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,138uint64_t Address,139const MCDisassembler *Decoder);140static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,141uint64_t Address,142const MCDisassembler *Decoder);143static DecodeStatus144DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,145const MCDisassembler *Decoder);146static DecodeStatus DecodeSysRegs64RegisterClass(MCInst &Inst, unsigned RegNo,147uint64_t Address,148const MCDisassembler *Decoder);149150static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,151uint64_t Address,152const MCDisassembler *Decoder);153static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,154uint64_t /*Address*/,155const MCDisassembler *Decoder);156static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,157const MCDisassembler *Decoder);158#include "HexagonDepDecoders.inc"159#include "HexagonGenDisassemblerTables.inc"160161static MCDisassembler *createHexagonDisassembler(const Target &T,162const MCSubtargetInfo &STI,163MCContext &Ctx) {164return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());165}166167extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeHexagonDisassembler() {168TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),169createHexagonDisassembler);170}171172DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,173ArrayRef<uint8_t> Bytes,174uint64_t Address,175raw_ostream &cs) const {176DecodeStatus Result = DecodeStatus::Success;177bool Complete = false;178Size = 0;179180*CurrentBundle = &MI;181MI.setOpcode(Hexagon::BUNDLE);182MI.addOperand(MCOperand::createImm(0));183while (Result == Success && !Complete) {184if (Bytes.size() < HEXAGON_INSTR_SIZE)185return MCDisassembler::Fail;186MCInst *Inst = getContext().createMCInst();187Result = getSingleInstruction(*Inst, MI, Bytes, Address, cs, Complete);188MI.addOperand(MCOperand::createInst(Inst));189Size += HEXAGON_INSTR_SIZE;190Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);191}192if (Result == MCDisassembler::Fail)193return Result;194if (Size > HEXAGON_MAX_PACKET_SIZE)195return MCDisassembler::Fail;196197const auto ArchSTI = Hexagon_MC::getArchSubtarget(&STI);198const auto STI_ = (ArchSTI != nullptr) ? *ArchSTI : STI;199HexagonMCChecker Checker(getContext(), *MCII, STI_, MI,200*getContext().getRegisterInfo(), false);201if (!Checker.check())202return MCDisassembler::Fail;203remapInstruction(MI);204return MCDisassembler::Success;205}206207void HexagonDisassembler::remapInstruction(MCInst &Instr) const {208for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {209auto &MI = const_cast<MCInst &>(*I.getInst());210switch (MI.getOpcode()) {211case Hexagon::S2_allocframe:212if (MI.getOperand(0).getReg() == Hexagon::R29) {213MI.setOpcode(Hexagon::S6_allocframe_to_raw);214MI.erase(MI.begin () + 1);215MI.erase(MI.begin ());216}217break;218case Hexagon::L2_deallocframe:219if (MI.getOperand(0).getReg() == Hexagon::D15 &&220MI.getOperand(1).getReg() == Hexagon::R30) {221MI.setOpcode(L6_deallocframe_map_to_raw);222MI.erase(MI.begin () + 1);223MI.erase(MI.begin ());224}225break;226case Hexagon::L4_return:227if (MI.getOperand(0).getReg() == Hexagon::D15 &&228MI.getOperand(1).getReg() == Hexagon::R30) {229MI.setOpcode(L6_return_map_to_raw);230MI.erase(MI.begin () + 1);231MI.erase(MI.begin ());232}233break;234case Hexagon::L4_return_t:235if (MI.getOperand(0).getReg() == Hexagon::D15 &&236MI.getOperand(2).getReg() == Hexagon::R30) {237MI.setOpcode(L4_return_map_to_raw_t);238MI.erase(MI.begin () + 2);239MI.erase(MI.begin ());240}241break;242case Hexagon::L4_return_f:243if (MI.getOperand(0).getReg() == Hexagon::D15 &&244MI.getOperand(2).getReg() == Hexagon::R30) {245MI.setOpcode(L4_return_map_to_raw_f);246MI.erase(MI.begin () + 2);247MI.erase(MI.begin ());248}249break;250case Hexagon::L4_return_tnew_pt:251if (MI.getOperand(0).getReg() == Hexagon::D15 &&252MI.getOperand(2).getReg() == Hexagon::R30) {253MI.setOpcode(L4_return_map_to_raw_tnew_pt);254MI.erase(MI.begin () + 2);255MI.erase(MI.begin ());256}257break;258case Hexagon::L4_return_fnew_pt:259if (MI.getOperand(0).getReg() == Hexagon::D15 &&260MI.getOperand(2).getReg() == Hexagon::R30) {261MI.setOpcode(L4_return_map_to_raw_fnew_pt);262MI.erase(MI.begin () + 2);263MI.erase(MI.begin ());264}265break;266case Hexagon::L4_return_tnew_pnt:267if (MI.getOperand(0).getReg() == Hexagon::D15 &&268MI.getOperand(2).getReg() == Hexagon::R30) {269MI.setOpcode(L4_return_map_to_raw_tnew_pnt);270MI.erase(MI.begin () + 2);271MI.erase(MI.begin ());272}273break;274case Hexagon::L4_return_fnew_pnt:275if (MI.getOperand(0).getReg() == Hexagon::D15 &&276MI.getOperand(2).getReg() == Hexagon::R30) {277MI.setOpcode(L4_return_map_to_raw_fnew_pnt);278MI.erase(MI.begin () + 2);279MI.erase(MI.begin ());280}281break;282}283}284}285286static void adjustDuplex(MCInst &MI, MCContext &Context) {287switch (MI.getOpcode()) {288case Hexagon::SA1_setin1:289MI.insert(MI.begin() + 1,290MCOperand::createExpr(MCConstantExpr::create(-1, Context)));291break;292case Hexagon::SA1_dec:293MI.insert(MI.begin() + 2,294MCOperand::createExpr(MCConstantExpr::create(-1, Context)));295break;296default:297break;298}299}300301DecodeStatus HexagonDisassembler::getSingleInstruction(MCInst &MI, MCInst &MCB,302ArrayRef<uint8_t> Bytes,303uint64_t Address,304raw_ostream &cs,305bool &Complete) const {306assert(Bytes.size() >= HEXAGON_INSTR_SIZE);307308uint32_t Instruction = support::endian::read32le(Bytes.data());309310auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);311if ((Instruction & HexagonII::INST_PARSE_MASK) ==312HexagonII::INST_PARSE_LOOP_END) {313if (BundleSize == 0)314HexagonMCInstrInfo::setInnerLoop(MCB);315else if (BundleSize == 1)316HexagonMCInstrInfo::setOuterLoop(MCB);317else318return DecodeStatus::Fail;319}320321CurrentExtender = HexagonMCInstrInfo::extenderForIndex(322MCB, HexagonMCInstrInfo::bundleSize(MCB));323324DecodeStatus Result = DecodeStatus::Fail;325if ((Instruction & HexagonII::INST_PARSE_MASK) ==326HexagonII::INST_PARSE_DUPLEX) {327unsigned duplexIClass;328uint8_t const *DecodeLow, *DecodeHigh;329duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);330switch (duplexIClass) {331default:332return MCDisassembler::Fail;333case 0:334DecodeLow = DecoderTableSUBINSN_L132;335DecodeHigh = DecoderTableSUBINSN_L132;336break;337case 1:338DecodeLow = DecoderTableSUBINSN_L232;339DecodeHigh = DecoderTableSUBINSN_L132;340break;341case 2:342DecodeLow = DecoderTableSUBINSN_L232;343DecodeHigh = DecoderTableSUBINSN_L232;344break;345case 3:346DecodeLow = DecoderTableSUBINSN_A32;347DecodeHigh = DecoderTableSUBINSN_A32;348break;349case 4:350DecodeLow = DecoderTableSUBINSN_L132;351DecodeHigh = DecoderTableSUBINSN_A32;352break;353case 5:354DecodeLow = DecoderTableSUBINSN_L232;355DecodeHigh = DecoderTableSUBINSN_A32;356break;357case 6:358DecodeLow = DecoderTableSUBINSN_S132;359DecodeHigh = DecoderTableSUBINSN_A32;360break;361case 7:362DecodeLow = DecoderTableSUBINSN_S232;363DecodeHigh = DecoderTableSUBINSN_A32;364break;365case 8:366DecodeLow = DecoderTableSUBINSN_S132;367DecodeHigh = DecoderTableSUBINSN_L132;368break;369case 9:370DecodeLow = DecoderTableSUBINSN_S132;371DecodeHigh = DecoderTableSUBINSN_L232;372break;373case 10:374DecodeLow = DecoderTableSUBINSN_S132;375DecodeHigh = DecoderTableSUBINSN_S132;376break;377case 11:378DecodeLow = DecoderTableSUBINSN_S232;379DecodeHigh = DecoderTableSUBINSN_S132;380break;381case 12:382DecodeLow = DecoderTableSUBINSN_S232;383DecodeHigh = DecoderTableSUBINSN_L132;384break;385case 13:386DecodeLow = DecoderTableSUBINSN_S232;387DecodeHigh = DecoderTableSUBINSN_L232;388break;389case 14:390DecodeLow = DecoderTableSUBINSN_S232;391DecodeHigh = DecoderTableSUBINSN_S232;392break;393}394MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);395MCInst *MILow = getContext().createMCInst();396MCInst *MIHigh = getContext().createMCInst();397auto TmpExtender = CurrentExtender;398CurrentExtender =399nullptr; // constant extenders in duplex must always be in slot 1400Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,401this, STI);402CurrentExtender = TmpExtender;403if (Result != DecodeStatus::Success)404return DecodeStatus::Fail;405adjustDuplex(*MILow, getContext());406Result = decodeInstruction(407DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);408if (Result != DecodeStatus::Success)409return DecodeStatus::Fail;410adjustDuplex(*MIHigh, getContext());411MCOperand OPLow = MCOperand::createInst(MILow);412MCOperand OPHigh = MCOperand::createInst(MIHigh);413MI.addOperand(OPLow);414MI.addOperand(OPHigh);415Complete = true;416} else {417if ((Instruction & HexagonII::INST_PARSE_MASK) ==418HexagonII::INST_PARSE_PACKET_END)419Complete = true;420421if (CurrentExtender != nullptr)422Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,423Address, this, STI);424425if (Result != MCDisassembler::Success)426Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,427STI);428429if (Result != MCDisassembler::Success &&430STI.hasFeature(Hexagon::ExtensionHVX))431Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,432Address, this, STI);433434}435436switch (MI.getOpcode()) {437case Hexagon::J4_cmpeqn1_f_jumpnv_nt:438case Hexagon::J4_cmpeqn1_f_jumpnv_t:439case Hexagon::J4_cmpeqn1_fp0_jump_nt:440case Hexagon::J4_cmpeqn1_fp0_jump_t:441case Hexagon::J4_cmpeqn1_fp1_jump_nt:442case Hexagon::J4_cmpeqn1_fp1_jump_t:443case Hexagon::J4_cmpeqn1_t_jumpnv_nt:444case Hexagon::J4_cmpeqn1_t_jumpnv_t:445case Hexagon::J4_cmpeqn1_tp0_jump_nt:446case Hexagon::J4_cmpeqn1_tp0_jump_t:447case Hexagon::J4_cmpeqn1_tp1_jump_nt:448case Hexagon::J4_cmpeqn1_tp1_jump_t:449case Hexagon::J4_cmpgtn1_f_jumpnv_nt:450case Hexagon::J4_cmpgtn1_f_jumpnv_t:451case Hexagon::J4_cmpgtn1_fp0_jump_nt:452case Hexagon::J4_cmpgtn1_fp0_jump_t:453case Hexagon::J4_cmpgtn1_fp1_jump_nt:454case Hexagon::J4_cmpgtn1_fp1_jump_t:455case Hexagon::J4_cmpgtn1_t_jumpnv_nt:456case Hexagon::J4_cmpgtn1_t_jumpnv_t:457case Hexagon::J4_cmpgtn1_tp0_jump_nt:458case Hexagon::J4_cmpgtn1_tp0_jump_t:459case Hexagon::J4_cmpgtn1_tp1_jump_nt:460case Hexagon::J4_cmpgtn1_tp1_jump_t:461MI.insert(MI.begin() + 1,462MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));463break;464default:465break;466}467468if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {469unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);470MCOperand &MCO = MI.getOperand(OpIndex);471assert(MCO.isReg() && "New value consumers must be registers");472unsigned Register =473getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());474if ((Register & 0x6) == 0)475// HexagonPRM 10.11 Bit 1-2 == 0 is reserved476return MCDisassembler::Fail;477unsigned Lookback = (Register & 0x6) >> 1;478unsigned Offset = 1;479bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);480bool PrevVector = false;481auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);482auto i = Instructions.end() - 1;483for (auto n = Instructions.begin() - 1;; --i, ++Offset) {484if (i == n)485// Couldn't find producer486return MCDisassembler::Fail;487bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());488if (Vector && !CurrentVector)489// Skip scalars when calculating distances for vectors490++Lookback;491if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))492++Lookback;493PrevVector = CurrentVector;494if (Offset == Lookback)495break;496}497auto const &Inst = *i->getInst();498bool SubregBit = (Register & 0x1) != 0;499if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {500// If subreg bit is set we're selecting the second produced newvalue501unsigned Producer = SubregBit ?502HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :503HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();504assert(Producer != Hexagon::NoRegister);505MCO.setReg(Producer);506} else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {507unsigned Producer =508HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();509510if (HexagonMCInstrInfo::IsVecRegPair(Producer)) {511const bool Rev = HexagonMCInstrInfo::IsReverseVecRegPair(Producer);512const unsigned ProdPairIndex =513Rev ? Producer - Hexagon::WR0 : Producer - Hexagon::W0;514if (Rev)515SubregBit = !SubregBit;516Producer = (ProdPairIndex << 1) + SubregBit + Hexagon::V0;517} else if (SubregBit)518// Hexagon PRM 10.11 New-value operands519// Nt[0] is reserved and should always be encoded as zero.520return MCDisassembler::Fail;521assert(Producer != Hexagon::NoRegister);522MCO.setReg(Producer);523} else524return MCDisassembler::Fail;525}526527if (CurrentExtender != nullptr) {528MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)529? *MI.getOperand(1).getInst()530: MI;531if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&532!HexagonMCInstrInfo::isExtended(*MCII, Inst))533return MCDisassembler::Fail;534}535return Result;536}537538static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,539ArrayRef<MCPhysReg> Table) {540if (RegNo < Table.size()) {541Inst.addOperand(MCOperand::createReg(Table[RegNo]));542return MCDisassembler::Success;543}544545return MCDisassembler::Fail;546}547548static DecodeStatus549DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,550const MCDisassembler *Decoder) {551return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);552}553554static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,555uint64_t Address,556const MCDisassembler *Decoder) {557static const MCPhysReg IntRegDecoderTable[] = {558Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,559Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,560Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,561Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,562Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,563Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,564Hexagon::R30, Hexagon::R31};565566return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);567}568569static DecodeStatus570DecodeGeneralSubRegsRegisterClass(MCInst &Inst, unsigned RegNo,571uint64_t Address,572const MCDisassembler *Decoder) {573static const MCPhysReg GeneralSubRegDecoderTable[] = {574Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,575Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,576Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,577Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,578};579580return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);581}582583static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,584uint64_t /*Address*/,585const MCDisassembler *Decoder) {586static const MCPhysReg HvxVRDecoderTable[] = {587Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,588Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,589Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,590Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,591Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,592Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,593Hexagon::V30, Hexagon::V31};594595return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);596}597598static DecodeStatus599DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,600uint64_t /*Address*/,601const MCDisassembler *Decoder) {602static const MCPhysReg DoubleRegDecoderTable[] = {603Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,604Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,605Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,606Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};607608return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);609}610611static DecodeStatus612DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,613uint64_t /*Address*/,614const MCDisassembler *Decoder) {615static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {616Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,617Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};618619return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);620}621622static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,623uint64_t /*Address*/,624const MCDisassembler *Decoder) {625static const MCPhysReg HvxWRDecoderTable[] = {626Hexagon::W0, Hexagon::WR0, Hexagon::W1, Hexagon::WR1, Hexagon::W2,627Hexagon::WR2, Hexagon::W3, Hexagon::WR3, Hexagon::W4, Hexagon::WR4,628Hexagon::W5, Hexagon::WR5, Hexagon::W6, Hexagon::WR6, Hexagon::W7,629Hexagon::WR7, Hexagon::W8, Hexagon::WR8, Hexagon::W9, Hexagon::WR9,630Hexagon::W10, Hexagon::WR10, Hexagon::W11, Hexagon::WR11, Hexagon::W12,631Hexagon::WR12, Hexagon::W13, Hexagon::WR13, Hexagon::W14, Hexagon::WR14,632Hexagon::W15, Hexagon::WR15,633};634635return DecodeRegisterClass(Inst, RegNo, HvxWRDecoderTable);636}637638LLVM_ATTRIBUTE_UNUSED // Suppress warning temporarily.639static DecodeStatus640DecodeHvxVQRRegisterClass(MCInst &Inst, unsigned RegNo,641uint64_t /*Address*/,642const MCDisassembler *Decoder) {643static const MCPhysReg HvxVQRDecoderTable[] = {644Hexagon::VQ0, Hexagon::VQ1, Hexagon::VQ2, Hexagon::VQ3,645Hexagon::VQ4, Hexagon::VQ5, Hexagon::VQ6, Hexagon::VQ7};646647return DecodeRegisterClass(Inst, RegNo >> 2, HvxVQRDecoderTable);648}649650static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,651uint64_t /*Address*/,652const MCDisassembler *Decoder) {653static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,654Hexagon::P2, Hexagon::P3};655656return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);657}658659static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,660uint64_t /*Address*/,661const MCDisassembler *Decoder) {662static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,663Hexagon::Q2, Hexagon::Q3};664665return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);666}667668static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,669uint64_t /*Address*/,670const MCDisassembler *Decoder) {671using namespace Hexagon;672673static const MCPhysReg CtrlRegDecoderTable[] = {674/* 0 */ SA0, LC0, SA1, LC1,675/* 4 */ P3_0, C5, M0, M1,676/* 8 */ USR, PC, UGP, GP,677/* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,678/* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,679/* 20 */ 0, 0, 0, 0,680/* 24 */ 0, 0, 0, 0,681/* 28 */ 0, 0, UTIMERLO, UTIMERHI682};683684if (RegNo >= std::size(CtrlRegDecoderTable))685return MCDisassembler::Fail;686687static_assert(NoRegister == 0, "Expecting NoRegister to be 0");688if (CtrlRegDecoderTable[RegNo] == NoRegister)689return MCDisassembler::Fail;690691unsigned Register = CtrlRegDecoderTable[RegNo];692Inst.addOperand(MCOperand::createReg(Register));693return MCDisassembler::Success;694}695696static DecodeStatus697DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/,698const MCDisassembler *Decoder) {699using namespace Hexagon;700701static const MCPhysReg CtrlReg64DecoderTable[] = {702/* 0 */ C1_0, 0, C3_2, 0,703/* 4 */ C5_4, 0, C7_6, 0,704/* 8 */ C9_8, 0, C11_10, 0,705/* 12 */ CS, 0, UPCYCLE, 0,706/* 16 */ C17_16, 0, PKTCOUNT, 0,707/* 20 */ 0, 0, 0, 0,708/* 24 */ 0, 0, 0, 0,709/* 28 */ 0, 0, UTIMER, 0710};711712if (RegNo >= std::size(CtrlReg64DecoderTable))713return MCDisassembler::Fail;714715static_assert(NoRegister == 0, "Expecting NoRegister to be 0");716if (CtrlReg64DecoderTable[RegNo] == NoRegister)717return MCDisassembler::Fail;718719unsigned Register = CtrlReg64DecoderTable[RegNo];720Inst.addOperand(MCOperand::createReg(Register));721return MCDisassembler::Success;722}723724static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,725uint64_t /*Address*/,726const MCDisassembler *Decoder) {727unsigned Register = 0;728switch (RegNo) {729case 0:730Register = Hexagon::M0;731break;732case 1:733Register = Hexagon::M1;734break;735default:736return MCDisassembler::Fail;737}738Inst.addOperand(MCOperand::createReg(Register));739return MCDisassembler::Success;740}741742static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,743uint64_t /*Address*/,744const MCDisassembler *Decoder) {745HexagonDisassembler const &Disassembler = disassembler(Decoder);746int64_t FullValue = fullValue(Disassembler, MI, tmp);747assert(FullValue >= 0 && "Negative in unsigned decoder");748HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());749return MCDisassembler::Success;750}751752static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,753uint64_t /*Address*/,754const MCDisassembler *Decoder) {755HexagonDisassembler const &Disassembler = disassembler(Decoder);756unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);757tmp = SignExtend64(tmp, Bits);758signedDecoder<32>(MI, tmp, Decoder);759return MCDisassembler::Success;760}761762// custom decoder for various jump/call immediates763static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,764const MCDisassembler *Decoder) {765HexagonDisassembler const &Disassembler = disassembler(Decoder);766unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);767// r13_2 is not extendable, so if there are no extent bits, it's r13_2768if (Bits == 0)769Bits = 15;770uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));771uint32_t Extended = FullValue + Address;772if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 0,7734))774HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());775return MCDisassembler::Success;776}777778static const uint16_t SysRegDecoderTable[] = {779Hexagon::SGP0, Hexagon::SGP1, Hexagon::STID,780Hexagon::ELR, Hexagon::BADVA0, Hexagon::BADVA1,781Hexagon::SSR, Hexagon::CCR, Hexagon::HTID,782Hexagon::BADVA, Hexagon::IMASK, Hexagon::S11,783Hexagon::S12, Hexagon::S13, Hexagon::S14,784Hexagon::S15, Hexagon::EVB, Hexagon::MODECTL,785Hexagon::SYSCFG, Hexagon::S19, Hexagon::S20,786Hexagon::VID, Hexagon::S22, Hexagon::S23,787Hexagon::S24, Hexagon::S25, Hexagon::S26,788Hexagon::CFGBASE, Hexagon::DIAG, Hexagon::REV,789Hexagon::PCYCLELO, Hexagon::PCYCLEHI, Hexagon::ISDBST,790Hexagon::ISDBCFG0, Hexagon::ISDBCFG1, Hexagon::S35,791Hexagon::BRKPTPC0, Hexagon::BRKPTCFG0, Hexagon::BRKPTPC1,792Hexagon::BRKPTCFG1, Hexagon::ISDBMBXIN, Hexagon::ISDBMBXOUT,793Hexagon::ISDBEN, Hexagon::ISDBGPR, Hexagon::S44,794Hexagon::S45, Hexagon::S46, Hexagon::S47,795Hexagon::PMUCNT0, Hexagon::PMUCNT1, Hexagon::PMUCNT2,796Hexagon::PMUCNT3, Hexagon::PMUEVTCFG, Hexagon::PMUCFG,797Hexagon::S54, Hexagon::S55, Hexagon::S56,798Hexagon::S57, Hexagon::S58, Hexagon::S59,799Hexagon::S60, Hexagon::S61, Hexagon::S62,800Hexagon::S63, Hexagon::S64, Hexagon::S65,801Hexagon::S66, Hexagon::S67, Hexagon::S68,802Hexagon::S69, Hexagon::S70, Hexagon::S71,803Hexagon::S72, Hexagon::S73, Hexagon::S74,804Hexagon::S75, Hexagon::S76, Hexagon::S77,805Hexagon::S78, Hexagon::S79, Hexagon::S80,806};807808static DecodeStatus DecodeSysRegsRegisterClass(MCInst &Inst, unsigned RegNo,809uint64_t /*Address*/,810const MCDisassembler *Decoder) {811if (RegNo >= std::size(SysRegDecoderTable))812return MCDisassembler::Fail;813814if (SysRegDecoderTable[RegNo] == Hexagon::NoRegister)815return MCDisassembler::Fail;816817unsigned Register = SysRegDecoderTable[RegNo];818Inst.addOperand(MCOperand::createReg(Register));819return MCDisassembler::Success;820}821822static const uint16_t SysReg64DecoderTable[] = {823Hexagon::SGP1_0, Hexagon::S3_2, Hexagon::S5_4, Hexagon::S7_6,824Hexagon::S9_8, Hexagon::S11_10, Hexagon::S13_12, Hexagon::S15_14,825Hexagon::S17_16, Hexagon::S19_18, Hexagon::S21_20, Hexagon::S23_22,826Hexagon::S25_24, Hexagon::S27_26, Hexagon::S29_28, Hexagon::S31_30,827Hexagon::S33_32, Hexagon::S35_34, Hexagon::S37_36, Hexagon::S39_38,828Hexagon::S41_40, Hexagon::S43_42, Hexagon::S45_44, Hexagon::S47_46,829Hexagon::S49_48, Hexagon::S51_50, Hexagon::S53_52, Hexagon::S55_54,830Hexagon::S57_56, Hexagon::S59_58, Hexagon::S61_60, Hexagon::S63_62,831Hexagon::S65_64, Hexagon::S67_66, Hexagon::S69_68, Hexagon::S71_70,832Hexagon::S73_72, Hexagon::S75_74, Hexagon::S77_76, Hexagon::S79_78,833};834835static DecodeStatus836DecodeSysRegs64RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/,837const MCDisassembler *Decoder) {838RegNo = RegNo >> 1;839if (RegNo >= std::size(SysReg64DecoderTable))840return MCDisassembler::Fail;841842if (SysReg64DecoderTable[RegNo] == Hexagon::NoRegister)843return MCDisassembler::Fail;844845unsigned Register = SysReg64DecoderTable[RegNo];846Inst.addOperand(MCOperand::createReg(Register));847return MCDisassembler::Success;848}849850static DecodeStatus851DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t /*Address*/,852const MCDisassembler *Decoder) {853using namespace Hexagon;854855static const MCPhysReg GuestRegDecoderTable[] = {856/* 0 */ GELR, GSR, GOSP, G3,857/* 4 */ G4, G5, G6, G7,858/* 8 */ G8, G9, G10, G11,859/* 12 */ G12, G13, G14, G15,860/* 16 */ GPMUCNT4, GPMUCNT5, GPMUCNT6, GPMUCNT7,861/* 20 */ G20, G21, G22, G23,862/* 24 */ GPCYCLELO, GPCYCLEHI, GPMUCNT0, GPMUCNT1,863/* 28 */ GPMUCNT2, GPMUCNT3, G30, G31864};865866if (RegNo >= std::size(GuestRegDecoderTable))867return MCDisassembler::Fail;868if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)869return MCDisassembler::Fail;870871unsigned Register = GuestRegDecoderTable[RegNo];872Inst.addOperand(MCOperand::createReg(Register));873return MCDisassembler::Success;874}875876static DecodeStatus877DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,878uint64_t /*Address*/,879const MCDisassembler *Decoder) {880using namespace Hexagon;881882static const MCPhysReg GuestReg64DecoderTable[] = {883/* 0 */ G1_0, 0, G3_2, 0,884/* 4 */ G5_4, 0, G7_6, 0,885/* 8 */ G9_8, 0, G11_10, 0,886/* 12 */ G13_12, 0, G15_14, 0,887/* 16 */ G17_16, 0, G19_18, 0,888/* 20 */ G21_20, 0, G23_22, 0,889/* 24 */ G25_24, 0, G27_26, 0,890/* 28 */ G29_28, 0, G31_30, 0891};892893if (RegNo >= std::size(GuestReg64DecoderTable))894return MCDisassembler::Fail;895if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)896return MCDisassembler::Fail;897898unsigned Register = GuestReg64DecoderTable[RegNo];899Inst.addOperand(MCOperand::createReg(Register));900return MCDisassembler::Success;901}902903904