xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/Sparc/Disassembler/SparcDisassembler.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- SparcDisassembler.cpp - Disassembler for Sparc -----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is part of the Sparc Disassembler.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MCTargetDesc/SparcMCTargetDesc.h"
140b57cec5SDimitry Andric #include "TargetInfo/SparcTargetInfo.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
1781ad6265SDimitry Andric #include "llvm/MC/MCDecoderOps.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCDisassembler/MCDisassembler.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
20349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric #define DEBUG_TYPE "sparc-disassembler"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric typedef MCDisassembler::DecodeStatus DecodeStatus;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric namespace {
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric /// A disassembler class for Sparc.
310b57cec5SDimitry Andric class SparcDisassembler : public MCDisassembler {
320b57cec5SDimitry Andric public:
SparcDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx)330b57cec5SDimitry Andric   SparcDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
340b57cec5SDimitry Andric       : MCDisassembler(STI, Ctx) {}
3581ad6265SDimitry Andric   virtual ~SparcDisassembler() = default;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
380b57cec5SDimitry Andric                               ArrayRef<uint8_t> Bytes, uint64_t Address,
390b57cec5SDimitry Andric                               raw_ostream &CStream) const override;
400b57cec5SDimitry Andric };
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
createSparcDisassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)430b57cec5SDimitry Andric static MCDisassembler *createSparcDisassembler(const Target &T,
440b57cec5SDimitry Andric                                                const MCSubtargetInfo &STI,
450b57cec5SDimitry Andric                                                MCContext &Ctx) {
460b57cec5SDimitry Andric   return new SparcDisassembler(STI, Ctx);
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric 
LLVMInitializeSparcDisassembler()50480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcDisassembler() {
510b57cec5SDimitry Andric   // Register the disassembler.
520b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheSparcTarget(),
530b57cec5SDimitry Andric                                          createSparcDisassembler);
540b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheSparcV9Target(),
550b57cec5SDimitry Andric                                          createSparcDisassembler);
560b57cec5SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheSparcelTarget(),
570b57cec5SDimitry Andric                                          createSparcDisassembler);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric static const unsigned IntRegDecoderTable[] = {
610b57cec5SDimitry Andric   SP::G0,  SP::G1,  SP::G2,  SP::G3,
620b57cec5SDimitry Andric   SP::G4,  SP::G5,  SP::G6,  SP::G7,
630b57cec5SDimitry Andric   SP::O0,  SP::O1,  SP::O2,  SP::O3,
640b57cec5SDimitry Andric   SP::O4,  SP::O5,  SP::O6,  SP::O7,
650b57cec5SDimitry Andric   SP::L0,  SP::L1,  SP::L2,  SP::L3,
660b57cec5SDimitry Andric   SP::L4,  SP::L5,  SP::L6,  SP::L7,
670b57cec5SDimitry Andric   SP::I0,  SP::I1,  SP::I2,  SP::I3,
680b57cec5SDimitry Andric   SP::I4,  SP::I5,  SP::I6,  SP::I7 };
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric static const unsigned FPRegDecoderTable[] = {
710b57cec5SDimitry Andric   SP::F0,   SP::F1,   SP::F2,   SP::F3,
720b57cec5SDimitry Andric   SP::F4,   SP::F5,   SP::F6,   SP::F7,
730b57cec5SDimitry Andric   SP::F8,   SP::F9,   SP::F10,  SP::F11,
740b57cec5SDimitry Andric   SP::F12,  SP::F13,  SP::F14,  SP::F15,
750b57cec5SDimitry Andric   SP::F16,  SP::F17,  SP::F18,  SP::F19,
760b57cec5SDimitry Andric   SP::F20,  SP::F21,  SP::F22,  SP::F23,
770b57cec5SDimitry Andric   SP::F24,  SP::F25,  SP::F26,  SP::F27,
780b57cec5SDimitry Andric   SP::F28,  SP::F29,  SP::F30,  SP::F31 };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric static const unsigned DFPRegDecoderTable[] = {
810b57cec5SDimitry Andric   SP::D0,   SP::D16,  SP::D1,   SP::D17,
820b57cec5SDimitry Andric   SP::D2,   SP::D18,  SP::D3,   SP::D19,
830b57cec5SDimitry Andric   SP::D4,   SP::D20,  SP::D5,   SP::D21,
840b57cec5SDimitry Andric   SP::D6,   SP::D22,  SP::D7,   SP::D23,
850b57cec5SDimitry Andric   SP::D8,   SP::D24,  SP::D9,   SP::D25,
860b57cec5SDimitry Andric   SP::D10,  SP::D26,  SP::D11,  SP::D27,
870b57cec5SDimitry Andric   SP::D12,  SP::D28,  SP::D13,  SP::D29,
880b57cec5SDimitry Andric   SP::D14,  SP::D30,  SP::D15,  SP::D31 };
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric static const unsigned QFPRegDecoderTable[] = {
910b57cec5SDimitry Andric   SP::Q0,  SP::Q8,   ~0U,  ~0U,
920b57cec5SDimitry Andric   SP::Q1,  SP::Q9,   ~0U,  ~0U,
930b57cec5SDimitry Andric   SP::Q2,  SP::Q10,  ~0U,  ~0U,
940b57cec5SDimitry Andric   SP::Q3,  SP::Q11,  ~0U,  ~0U,
950b57cec5SDimitry Andric   SP::Q4,  SP::Q12,  ~0U,  ~0U,
960b57cec5SDimitry Andric   SP::Q5,  SP::Q13,  ~0U,  ~0U,
970b57cec5SDimitry Andric   SP::Q6,  SP::Q14,  ~0U,  ~0U,
980b57cec5SDimitry Andric   SP::Q7,  SP::Q15,  ~0U,  ~0U } ;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric static const unsigned FCCRegDecoderTable[] = {
1010b57cec5SDimitry Andric   SP::FCC0, SP::FCC1, SP::FCC2, SP::FCC3 };
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric static const unsigned ASRRegDecoderTable[] = {
104*5f757f3fSDimitry Andric     SP::Y,     SP::ASR1,  SP::ASR2,  SP::ASR3,  SP::ASR4,  SP::ASR5,  SP::ASR6,
105*5f757f3fSDimitry Andric     SP::ASR7,  SP::ASR8,  SP::ASR9,  SP::ASR10, SP::ASR11, SP::ASR12, SP::ASR13,
106*5f757f3fSDimitry Andric     SP::ASR14, SP::ASR15, SP::ASR16, SP::ASR17, SP::ASR18, SP::ASR19, SP::ASR20,
107*5f757f3fSDimitry Andric     SP::ASR21, SP::ASR22, SP::ASR23, SP::ASR24, SP::ASR25, SP::ASR26, SP::ASR27,
1080b57cec5SDimitry Andric     SP::ASR28, SP::ASR29, SP::ASR30, SP::ASR31};
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric static const unsigned PRRegDecoderTable[] = {
111*5f757f3fSDimitry Andric     SP::TPC,     SP::TNPC,       SP::TSTATE,   SP::TT,       SP::TICK,
112*5f757f3fSDimitry Andric     SP::TBA,     SP::PSTATE,     SP::TL,       SP::PIL,      SP::CWP,
113*5f757f3fSDimitry Andric     SP::CANSAVE, SP::CANRESTORE, SP::CLEANWIN, SP::OTHERWIN, SP::WSTATE};
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric static const uint16_t IntPairDecoderTable[] = {
1160b57cec5SDimitry Andric   SP::G0_G1, SP::G2_G3, SP::G4_G5, SP::G6_G7,
1170b57cec5SDimitry Andric   SP::O0_O1, SP::O2_O3, SP::O4_O5, SP::O6_O7,
1180b57cec5SDimitry Andric   SP::L0_L1, SP::L2_L3, SP::L4_L5, SP::L6_L7,
1190b57cec5SDimitry Andric   SP::I0_I1, SP::I2_I3, SP::I4_I5, SP::I6_I7,
1200b57cec5SDimitry Andric };
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric static const unsigned CPRegDecoderTable[] = {
1230b57cec5SDimitry Andric   SP::C0,  SP::C1,  SP::C2,  SP::C3,
1240b57cec5SDimitry Andric   SP::C4,  SP::C5,  SP::C6,  SP::C7,
1250b57cec5SDimitry Andric   SP::C8,  SP::C9,  SP::C10, SP::C11,
1260b57cec5SDimitry Andric   SP::C12, SP::C13, SP::C14, SP::C15,
1270b57cec5SDimitry Andric   SP::C16, SP::C17, SP::C18, SP::C19,
1280b57cec5SDimitry Andric   SP::C20, SP::C21, SP::C22, SP::C23,
1290b57cec5SDimitry Andric   SP::C24, SP::C25, SP::C26, SP::C27,
1300b57cec5SDimitry Andric   SP::C28, SP::C29, SP::C30, SP::C31
1310b57cec5SDimitry Andric };
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric static const uint16_t CPPairDecoderTable[] = {
1350b57cec5SDimitry Andric   SP::C0_C1,   SP::C2_C3,   SP::C4_C5,   SP::C6_C7,
1360b57cec5SDimitry Andric   SP::C8_C9,   SP::C10_C11, SP::C12_C13, SP::C14_C15,
1370b57cec5SDimitry Andric   SP::C16_C17, SP::C18_C19, SP::C20_C21, SP::C22_C23,
1380b57cec5SDimitry Andric   SP::C24_C25, SP::C26_C27, SP::C28_C29, SP::C30_C31
1390b57cec5SDimitry Andric };
1400b57cec5SDimitry Andric 
DecodeIntRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)14181ad6265SDimitry Andric static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1420b57cec5SDimitry Andric                                                uint64_t Address,
14381ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
1440b57cec5SDimitry Andric   if (RegNo > 31)
1450b57cec5SDimitry Andric     return MCDisassembler::Fail;
1460b57cec5SDimitry Andric   unsigned Reg = IntRegDecoderTable[RegNo];
1470b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1480b57cec5SDimitry Andric   return MCDisassembler::Success;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
DecodeI64RegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)15181ad6265SDimitry Andric static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst, unsigned RegNo,
1520b57cec5SDimitry Andric                                                uint64_t Address,
15381ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
154bdd1243dSDimitry Andric   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
155bdd1243dSDimitry Andric }
156bdd1243dSDimitry Andric 
157bdd1243dSDimitry Andric // This is used for the type "ptr_rc", which is either IntRegs or I64Regs
158bdd1243dSDimitry Andric // depending on SparcRegisterInfo::getPointerRegClass.
DecodePointerLikeRegClass0(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)159bdd1243dSDimitry Andric static DecodeStatus DecodePointerLikeRegClass0(MCInst &Inst, unsigned RegNo,
160bdd1243dSDimitry Andric                                                uint64_t Address,
161bdd1243dSDimitry Andric                                                const MCDisassembler *Decoder) {
162bdd1243dSDimitry Andric   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
DecodeFPRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)16581ad6265SDimitry Andric static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1660b57cec5SDimitry Andric                                               uint64_t Address,
16781ad6265SDimitry Andric                                               const MCDisassembler *Decoder) {
1680b57cec5SDimitry Andric   if (RegNo > 31)
1690b57cec5SDimitry Andric     return MCDisassembler::Fail;
1700b57cec5SDimitry Andric   unsigned Reg = FPRegDecoderTable[RegNo];
1710b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1720b57cec5SDimitry Andric   return MCDisassembler::Success;
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
DecodeDFPRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)17581ad6265SDimitry Andric static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1760b57cec5SDimitry Andric                                                uint64_t Address,
17781ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
1780b57cec5SDimitry Andric   if (RegNo > 31)
1790b57cec5SDimitry Andric     return MCDisassembler::Fail;
1800b57cec5SDimitry Andric   unsigned Reg = DFPRegDecoderTable[RegNo];
1810b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1820b57cec5SDimitry Andric   return MCDisassembler::Success;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
DecodeQFPRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)18581ad6265SDimitry Andric static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1860b57cec5SDimitry Andric                                                uint64_t Address,
18781ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
1880b57cec5SDimitry Andric   if (RegNo > 31)
1890b57cec5SDimitry Andric     return MCDisassembler::Fail;
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   unsigned Reg = QFPRegDecoderTable[RegNo];
1920b57cec5SDimitry Andric   if (Reg == ~0U)
1930b57cec5SDimitry Andric     return MCDisassembler::Fail;
1940b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
1950b57cec5SDimitry Andric   return MCDisassembler::Success;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
198bdd1243dSDimitry Andric static DecodeStatus
DecodeCoprocRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)199bdd1243dSDimitry Andric DecodeCoprocRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
20081ad6265SDimitry Andric                               const MCDisassembler *Decoder) {
2010b57cec5SDimitry Andric   if (RegNo > 31)
2020b57cec5SDimitry Andric     return MCDisassembler::Fail;
2030b57cec5SDimitry Andric   unsigned Reg = CPRegDecoderTable[RegNo];
2040b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(Reg));
2050b57cec5SDimitry Andric   return MCDisassembler::Success;
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric 
DecodeFCCRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)2080b57cec5SDimitry Andric static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
2090b57cec5SDimitry Andric                                                uint64_t Address,
21081ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
2110b57cec5SDimitry Andric   if (RegNo > 3)
2120b57cec5SDimitry Andric     return MCDisassembler::Fail;
2130b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(FCCRegDecoderTable[RegNo]));
2140b57cec5SDimitry Andric   return MCDisassembler::Success;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
DecodeASRRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)2170b57cec5SDimitry Andric static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
2180b57cec5SDimitry Andric                                                uint64_t Address,
21981ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
2200b57cec5SDimitry Andric   if (RegNo > 31)
2210b57cec5SDimitry Andric     return MCDisassembler::Fail;
2220b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(ASRRegDecoderTable[RegNo]));
2230b57cec5SDimitry Andric   return MCDisassembler::Success;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
DecodePRRegsRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)2260b57cec5SDimitry Andric static DecodeStatus DecodePRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
2270b57cec5SDimitry Andric                                               uint64_t Address,
22881ad6265SDimitry Andric                                               const MCDisassembler *Decoder) {
229bdd1243dSDimitry Andric   if (RegNo >= std::size(PRRegDecoderTable))
2300b57cec5SDimitry Andric     return MCDisassembler::Fail;
2310b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(PRRegDecoderTable[RegNo]));
2320b57cec5SDimitry Andric   return MCDisassembler::Success;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric 
DecodeIntPairRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)2350b57cec5SDimitry Andric static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo,
23681ad6265SDimitry Andric                                                uint64_t Address,
23781ad6265SDimitry Andric                                                const MCDisassembler *Decoder) {
2380b57cec5SDimitry Andric   DecodeStatus S = MCDisassembler::Success;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   if (RegNo > 31)
2410b57cec5SDimitry Andric     return MCDisassembler::Fail;
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   if ((RegNo & 1))
2440b57cec5SDimitry Andric     S = MCDisassembler::SoftFail;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   unsigned RegisterPair = IntPairDecoderTable[RegNo/2];
2470b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(RegisterPair));
2480b57cec5SDimitry Andric   return S;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
251bdd1243dSDimitry Andric static DecodeStatus
DecodeCoprocPairRegisterClass(MCInst & Inst,unsigned RegNo,uint64_t Address,const MCDisassembler * Decoder)252bdd1243dSDimitry Andric DecodeCoprocPairRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
25381ad6265SDimitry Andric                               const MCDisassembler *Decoder) {
2540b57cec5SDimitry Andric   if (RegNo > 31)
2550b57cec5SDimitry Andric     return MCDisassembler::Fail;
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   unsigned RegisterPair = CPPairDecoderTable[RegNo/2];
2580b57cec5SDimitry Andric   Inst.addOperand(MCOperand::createReg(RegisterPair));
2590b57cec5SDimitry Andric   return MCDisassembler::Success;
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
26281ad6265SDimitry Andric static DecodeStatus DecodeCall(MCInst &Inst, unsigned insn, uint64_t Address,
26381ad6265SDimitry Andric                                const MCDisassembler *Decoder);
26481ad6265SDimitry Andric static DecodeStatus DecodeSIMM13(MCInst &Inst, unsigned insn, uint64_t Address,
26581ad6265SDimitry Andric                                  const MCDisassembler *Decoder);
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric #include "SparcGenDisassemblerTables.inc"
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric /// Read four bytes from the ArrayRef and return 32 bit word.
readInstruction32(ArrayRef<uint8_t> Bytes,uint64_t Address,uint64_t & Size,uint32_t & Insn,bool IsLittleEndian)2700b57cec5SDimitry Andric static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
2710b57cec5SDimitry Andric                                       uint64_t &Size, uint32_t &Insn,
2720b57cec5SDimitry Andric                                       bool IsLittleEndian) {
2730b57cec5SDimitry Andric   // We want to read exactly 4 Bytes of data.
2740b57cec5SDimitry Andric   if (Bytes.size() < 4) {
2750b57cec5SDimitry Andric     Size = 0;
2760b57cec5SDimitry Andric     return MCDisassembler::Fail;
2770b57cec5SDimitry Andric   }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   Insn = IsLittleEndian
2800b57cec5SDimitry Andric              ? (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
2810b57cec5SDimitry Andric                    (Bytes[3] << 24)
2820b57cec5SDimitry Andric              : (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) |
2830b57cec5SDimitry Andric                    (Bytes[0] << 24);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   return MCDisassembler::Success;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
getInstruction(MCInst & Instr,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const2880b57cec5SDimitry Andric DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
2890b57cec5SDimitry Andric                                                ArrayRef<uint8_t> Bytes,
2900b57cec5SDimitry Andric                                                uint64_t Address,
2910b57cec5SDimitry Andric                                                raw_ostream &CStream) const {
2920b57cec5SDimitry Andric   uint32_t Insn;
2930b57cec5SDimitry Andric   bool isLittleEndian = getContext().getAsmInfo()->isLittleEndian();
2940b57cec5SDimitry Andric   DecodeStatus Result =
2950b57cec5SDimitry Andric       readInstruction32(Bytes, Address, Size, Insn, isLittleEndian);
2960b57cec5SDimitry Andric   if (Result == MCDisassembler::Fail)
2970b57cec5SDimitry Andric     return MCDisassembler::Fail;
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   // Calling the auto-generated decoder function.
3000b57cec5SDimitry Andric 
30106c3fb27SDimitry Andric   if (STI.hasFeature(Sparc::FeatureV9))
3020b57cec5SDimitry Andric   {
3030b57cec5SDimitry Andric     Result = decodeInstruction(DecoderTableSparcV932, Instr, Insn, Address, this, STI);
3040b57cec5SDimitry Andric   }
3050b57cec5SDimitry Andric   else
3060b57cec5SDimitry Andric   {
3070b57cec5SDimitry Andric     Result = decodeInstruction(DecoderTableSparcV832, Instr, Insn, Address, this, STI);
3080b57cec5SDimitry Andric   }
309*5f757f3fSDimitry Andric   if (Result != MCDisassembler::Fail) {
310*5f757f3fSDimitry Andric     Size = 4;
3110b57cec5SDimitry Andric     return Result;
312*5f757f3fSDimitry Andric   }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   Result =
3150b57cec5SDimitry Andric       decodeInstruction(DecoderTableSparc32, Instr, Insn, Address, this, STI);
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   if (Result != MCDisassembler::Fail) {
3180b57cec5SDimitry Andric     Size = 4;
3190b57cec5SDimitry Andric     return Result;
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   return MCDisassembler::Fail;
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric 
tryAddingSymbolicOperand(int64_t Value,bool isBranch,uint64_t Address,uint64_t Offset,uint64_t Width,MCInst & MI,const MCDisassembler * Decoder)3250b57cec5SDimitry Andric static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
3260b57cec5SDimitry Andric                                      uint64_t Address, uint64_t Offset,
3270b57cec5SDimitry Andric                                      uint64_t Width, MCInst &MI,
32881ad6265SDimitry Andric                                      const MCDisassembler *Decoder) {
32981ad6265SDimitry Andric   return Decoder->tryAddingSymbolicOperand(MI, Value, Address, isBranch, Offset,
33081ad6265SDimitry Andric                                            Width, /*InstSize=*/4);
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
DecodeCall(MCInst & MI,unsigned insn,uint64_t Address,const MCDisassembler * Decoder)33381ad6265SDimitry Andric static DecodeStatus DecodeCall(MCInst &MI, unsigned insn, uint64_t Address,
33481ad6265SDimitry Andric                                const MCDisassembler *Decoder) {
3350b57cec5SDimitry Andric   unsigned tgt = fieldFromInstruction(insn, 0, 30);
3360b57cec5SDimitry Andric   tgt <<= 2;
3370b57cec5SDimitry Andric   if (!tryAddingSymbolicOperand(tgt+Address, false, Address,
3380b57cec5SDimitry Andric                                 0, 30, MI, Decoder))
3390b57cec5SDimitry Andric     MI.addOperand(MCOperand::createImm(tgt));
3400b57cec5SDimitry Andric   return MCDisassembler::Success;
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric 
DecodeSIMM13(MCInst & MI,unsigned insn,uint64_t Address,const MCDisassembler * Decoder)34381ad6265SDimitry Andric static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn, uint64_t Address,
34481ad6265SDimitry Andric                                  const MCDisassembler *Decoder) {
345*5f757f3fSDimitry Andric   assert(isUInt<13>(insn));
346*5f757f3fSDimitry Andric   MI.addOperand(MCOperand::createImm(SignExtend64<13>(insn)));
3470b57cec5SDimitry Andric   return MCDisassembler::Success;
3480b57cec5SDimitry Andric }
349