10b57cec5SDimitry Andric //===- AMDGPUDisassembler.cpp - Disassembler for AMDGPU ISA ---------------===//
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 //===----------------------------------------------------------------------===//
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric /// \file
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric /// This file contains definition for AMDGPU ISA disassembler
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric // ToDo: What to do with instruction suffixes (v_mov_b32 vs v_mov_b32_e32)?
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric #include "Disassembler/AMDGPUDisassembler.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
210b57cec5SDimitry Andric #include "TargetInfo/AMDGPUTargetInfo.h"
220b57cec5SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
23af732203SDimitry Andric #include "llvm-c/DisassemblerTypes.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCFixedLenDisassembler.h"
28af732203SDimitry Andric #include "llvm/Support/AMDHSAKernelDescriptor.h"
290b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric #define DEBUG_TYPE "amdgpu-disassembler"
340b57cec5SDimitry Andric
35af732203SDimitry Andric #define SGPR_MAX \
36af732203SDimitry Andric (isGFX10Plus() ? AMDGPU::EncValues::SGPR_MAX_GFX10 \
370b57cec5SDimitry Andric : AMDGPU::EncValues::SGPR_MAX_SI)
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric using DecodeStatus = llvm::MCDisassembler::DecodeStatus;
400b57cec5SDimitry Andric
AMDGPUDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx,MCInstrInfo const * MCII)410b57cec5SDimitry Andric AMDGPUDisassembler::AMDGPUDisassembler(const MCSubtargetInfo &STI,
420b57cec5SDimitry Andric MCContext &Ctx,
430b57cec5SDimitry Andric MCInstrInfo const *MCII) :
440b57cec5SDimitry Andric MCDisassembler(STI, Ctx), MCII(MCII), MRI(*Ctx.getRegisterInfo()),
450b57cec5SDimitry Andric TargetMaxInstBytes(Ctx.getAsmInfo()->getMaxInstLength(&STI)) {
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric // ToDo: AMDGPUDisassembler supports only VI ISA.
48af732203SDimitry Andric if (!STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding] && !isGFX10Plus())
490b57cec5SDimitry Andric report_fatal_error("Disassembly not yet supported for subtarget");
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric inline static MCDisassembler::DecodeStatus
addOperand(MCInst & Inst,const MCOperand & Opnd)530b57cec5SDimitry Andric addOperand(MCInst &Inst, const MCOperand& Opnd) {
540b57cec5SDimitry Andric Inst.addOperand(Opnd);
550b57cec5SDimitry Andric return Opnd.isValid() ?
560b57cec5SDimitry Andric MCDisassembler::Success :
57480093f4SDimitry Andric MCDisassembler::Fail;
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric
insertNamedMCOperand(MCInst & MI,const MCOperand & Op,uint16_t NameIdx)600b57cec5SDimitry Andric static int insertNamedMCOperand(MCInst &MI, const MCOperand &Op,
610b57cec5SDimitry Andric uint16_t NameIdx) {
620b57cec5SDimitry Andric int OpIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), NameIdx);
630b57cec5SDimitry Andric if (OpIdx != -1) {
640b57cec5SDimitry Andric auto I = MI.begin();
650b57cec5SDimitry Andric std::advance(I, OpIdx);
660b57cec5SDimitry Andric MI.insert(I, Op);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric return OpIdx;
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
decodeSoppBrTarget(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)710b57cec5SDimitry Andric static DecodeStatus decodeSoppBrTarget(MCInst &Inst, unsigned Imm,
720b57cec5SDimitry Andric uint64_t Addr, const void *Decoder) {
730b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric // Our branches take a simm16, but we need two extra bits to account for the
760b57cec5SDimitry Andric // factor of 4.
770b57cec5SDimitry Andric APInt SignedOffset(18, Imm * 4, true);
780b57cec5SDimitry Andric int64_t Offset = (SignedOffset.sext(64) + 4 + Addr).getSExtValue();
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric if (DAsm->tryAddingSymbolicOperand(Inst, Offset, Addr, true, 2, 2))
810b57cec5SDimitry Andric return MCDisassembler::Success;
820b57cec5SDimitry Andric return addOperand(Inst, MCOperand::createImm(Imm));
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
decodeSMEMOffset(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)855ffd83dbSDimitry Andric static DecodeStatus decodeSMEMOffset(MCInst &Inst, unsigned Imm,
865ffd83dbSDimitry Andric uint64_t Addr, const void *Decoder) {
875ffd83dbSDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
885ffd83dbSDimitry Andric int64_t Offset;
895ffd83dbSDimitry Andric if (DAsm->isVI()) { // VI supports 20-bit unsigned offsets.
905ffd83dbSDimitry Andric Offset = Imm & 0xFFFFF;
915ffd83dbSDimitry Andric } else { // GFX9+ supports 21-bit signed offsets.
925ffd83dbSDimitry Andric Offset = SignExtend64<21>(Imm);
935ffd83dbSDimitry Andric }
945ffd83dbSDimitry Andric return addOperand(Inst, MCOperand::createImm(Offset));
955ffd83dbSDimitry Andric }
965ffd83dbSDimitry Andric
decodeBoolReg(MCInst & Inst,unsigned Val,uint64_t Addr,const void * Decoder)970b57cec5SDimitry Andric static DecodeStatus decodeBoolReg(MCInst &Inst, unsigned Val,
980b57cec5SDimitry Andric uint64_t Addr, const void *Decoder) {
990b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
1000b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeBoolReg(Val));
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric #define DECODE_OPERAND(StaticDecoderName, DecoderName) \
1040b57cec5SDimitry Andric static DecodeStatus StaticDecoderName(MCInst &Inst, \
1050b57cec5SDimitry Andric unsigned Imm, \
1060b57cec5SDimitry Andric uint64_t /*Addr*/, \
1070b57cec5SDimitry Andric const void *Decoder) { \
1080b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder); \
1090b57cec5SDimitry Andric return addOperand(Inst, DAsm->DecoderName(Imm)); \
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric #define DECODE_OPERAND_REG(RegClass) \
1130b57cec5SDimitry Andric DECODE_OPERAND(Decode##RegClass##RegisterClass, decodeOperand_##RegClass)
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric DECODE_OPERAND_REG(VGPR_32)
DECODE_OPERAND_REG(VRegOrLds_32)1160b57cec5SDimitry Andric DECODE_OPERAND_REG(VRegOrLds_32)
1170b57cec5SDimitry Andric DECODE_OPERAND_REG(VS_32)
1180b57cec5SDimitry Andric DECODE_OPERAND_REG(VS_64)
1190b57cec5SDimitry Andric DECODE_OPERAND_REG(VS_128)
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric DECODE_OPERAND_REG(VReg_64)
1220b57cec5SDimitry Andric DECODE_OPERAND_REG(VReg_96)
1230b57cec5SDimitry Andric DECODE_OPERAND_REG(VReg_128)
124af732203SDimitry Andric DECODE_OPERAND_REG(VReg_256)
125af732203SDimitry Andric DECODE_OPERAND_REG(VReg_512)
126*5f7ddb14SDimitry Andric DECODE_OPERAND_REG(VReg_1024)
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_32)
1290b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_32_XM0_XEXEC)
1300b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_32_XEXEC_HI)
1310b57cec5SDimitry Andric DECODE_OPERAND_REG(SRegOrLds_32)
1320b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_64)
1330b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_64_XEXEC)
1340b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_128)
1350b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_256)
1360b57cec5SDimitry Andric DECODE_OPERAND_REG(SReg_512)
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric DECODE_OPERAND_REG(AGPR_32)
139*5f7ddb14SDimitry Andric DECODE_OPERAND_REG(AReg_64)
1400b57cec5SDimitry Andric DECODE_OPERAND_REG(AReg_128)
141*5f7ddb14SDimitry Andric DECODE_OPERAND_REG(AReg_256)
1420b57cec5SDimitry Andric DECODE_OPERAND_REG(AReg_512)
1430b57cec5SDimitry Andric DECODE_OPERAND_REG(AReg_1024)
1440b57cec5SDimitry Andric DECODE_OPERAND_REG(AV_32)
1450b57cec5SDimitry Andric DECODE_OPERAND_REG(AV_64)
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric static DecodeStatus decodeOperand_VSrc16(MCInst &Inst,
1480b57cec5SDimitry Andric unsigned Imm,
1490b57cec5SDimitry Andric uint64_t Addr,
1500b57cec5SDimitry Andric const void *Decoder) {
1510b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
1520b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_VSrc16(Imm));
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric
decodeOperand_VSrcV216(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)1550b57cec5SDimitry Andric static DecodeStatus decodeOperand_VSrcV216(MCInst &Inst,
1560b57cec5SDimitry Andric unsigned Imm,
1570b57cec5SDimitry Andric uint64_t Addr,
1580b57cec5SDimitry Andric const void *Decoder) {
1590b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
1600b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_VSrcV216(Imm));
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric
decodeOperand_VSrcV232(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)163*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VSrcV232(MCInst &Inst,
164*5f7ddb14SDimitry Andric unsigned Imm,
165*5f7ddb14SDimitry Andric uint64_t Addr,
166*5f7ddb14SDimitry Andric const void *Decoder) {
167*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
168*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_VSrcV232(Imm));
169*5f7ddb14SDimitry Andric }
170*5f7ddb14SDimitry Andric
decodeOperand_VS_16(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)1710b57cec5SDimitry Andric static DecodeStatus decodeOperand_VS_16(MCInst &Inst,
1720b57cec5SDimitry Andric unsigned Imm,
1730b57cec5SDimitry Andric uint64_t Addr,
1740b57cec5SDimitry Andric const void *Decoder) {
1750b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
1760b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_VSrc16(Imm));
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric
decodeOperand_VS_32(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)1790b57cec5SDimitry Andric static DecodeStatus decodeOperand_VS_32(MCInst &Inst,
1800b57cec5SDimitry Andric unsigned Imm,
1810b57cec5SDimitry Andric uint64_t Addr,
1820b57cec5SDimitry Andric const void *Decoder) {
1830b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
1840b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_VS_32(Imm));
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
decodeOperand_AReg_64(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)187*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_AReg_64(MCInst &Inst,
188*5f7ddb14SDimitry Andric unsigned Imm,
189*5f7ddb14SDimitry Andric uint64_t Addr,
190*5f7ddb14SDimitry Andric const void *Decoder) {
191*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
192*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW64, Imm | 512));
193*5f7ddb14SDimitry Andric }
194*5f7ddb14SDimitry Andric
decodeOperand_AReg_128(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)1950b57cec5SDimitry Andric static DecodeStatus decodeOperand_AReg_128(MCInst &Inst,
1960b57cec5SDimitry Andric unsigned Imm,
1970b57cec5SDimitry Andric uint64_t Addr,
1980b57cec5SDimitry Andric const void *Decoder) {
1990b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
2000b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW128, Imm | 512));
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric
decodeOperand_AReg_256(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)203*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_AReg_256(MCInst &Inst,
204*5f7ddb14SDimitry Andric unsigned Imm,
205*5f7ddb14SDimitry Andric uint64_t Addr,
206*5f7ddb14SDimitry Andric const void *Decoder) {
207*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
208*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW256, Imm | 512));
209*5f7ddb14SDimitry Andric }
210*5f7ddb14SDimitry Andric
decodeOperand_AReg_512(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)2110b57cec5SDimitry Andric static DecodeStatus decodeOperand_AReg_512(MCInst &Inst,
2120b57cec5SDimitry Andric unsigned Imm,
2130b57cec5SDimitry Andric uint64_t Addr,
2140b57cec5SDimitry Andric const void *Decoder) {
2150b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
2160b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW512, Imm | 512));
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
decodeOperand_AReg_1024(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)2190b57cec5SDimitry Andric static DecodeStatus decodeOperand_AReg_1024(MCInst &Inst,
2200b57cec5SDimitry Andric unsigned Imm,
2210b57cec5SDimitry Andric uint64_t Addr,
2220b57cec5SDimitry Andric const void *Decoder) {
2230b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
2240b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW1024, Imm | 512));
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
decodeOperand_VReg_64(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)227*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VReg_64(MCInst &Inst,
228*5f7ddb14SDimitry Andric unsigned Imm,
229*5f7ddb14SDimitry Andric uint64_t Addr,
230*5f7ddb14SDimitry Andric const void *Decoder) {
231*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
232*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW64, Imm));
233*5f7ddb14SDimitry Andric }
234*5f7ddb14SDimitry Andric
decodeOperand_VReg_128(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)235*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VReg_128(MCInst &Inst,
236*5f7ddb14SDimitry Andric unsigned Imm,
237*5f7ddb14SDimitry Andric uint64_t Addr,
238*5f7ddb14SDimitry Andric const void *Decoder) {
239*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
240*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW128, Imm));
241*5f7ddb14SDimitry Andric }
242*5f7ddb14SDimitry Andric
decodeOperand_VReg_256(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)243*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VReg_256(MCInst &Inst,
244*5f7ddb14SDimitry Andric unsigned Imm,
245*5f7ddb14SDimitry Andric uint64_t Addr,
246*5f7ddb14SDimitry Andric const void *Decoder) {
247*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
248*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW256, Imm));
249*5f7ddb14SDimitry Andric }
250*5f7ddb14SDimitry Andric
decodeOperand_VReg_512(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)251*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VReg_512(MCInst &Inst,
252*5f7ddb14SDimitry Andric unsigned Imm,
253*5f7ddb14SDimitry Andric uint64_t Addr,
254*5f7ddb14SDimitry Andric const void *Decoder) {
255*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
256*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW512, Imm));
257*5f7ddb14SDimitry Andric }
258*5f7ddb14SDimitry Andric
decodeOperand_VReg_1024(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)259*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_VReg_1024(MCInst &Inst,
260*5f7ddb14SDimitry Andric unsigned Imm,
261*5f7ddb14SDimitry Andric uint64_t Addr,
262*5f7ddb14SDimitry Andric const void *Decoder) {
263*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
264*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW1024, Imm));
265*5f7ddb14SDimitry Andric }
266*5f7ddb14SDimitry Andric
IsAGPROperand(const MCInst & Inst,int OpIdx,const MCRegisterInfo * MRI)267*5f7ddb14SDimitry Andric static bool IsAGPROperand(const MCInst &Inst, int OpIdx,
268*5f7ddb14SDimitry Andric const MCRegisterInfo *MRI) {
269*5f7ddb14SDimitry Andric if (OpIdx < 0)
270*5f7ddb14SDimitry Andric return false;
271*5f7ddb14SDimitry Andric
272*5f7ddb14SDimitry Andric const MCOperand &Op = Inst.getOperand(OpIdx);
273*5f7ddb14SDimitry Andric if (!Op.isReg())
274*5f7ddb14SDimitry Andric return false;
275*5f7ddb14SDimitry Andric
276*5f7ddb14SDimitry Andric unsigned Sub = MRI->getSubReg(Op.getReg(), AMDGPU::sub0);
277*5f7ddb14SDimitry Andric auto Reg = Sub ? Sub : Op.getReg();
278*5f7ddb14SDimitry Andric return Reg >= AMDGPU::AGPR0 && Reg <= AMDGPU::AGPR255;
279*5f7ddb14SDimitry Andric }
280*5f7ddb14SDimitry Andric
decodeOperand_AVLdSt_Any(MCInst & Inst,unsigned Imm,AMDGPUDisassembler::OpWidthTy Opw,const void * Decoder)281*5f7ddb14SDimitry Andric static DecodeStatus decodeOperand_AVLdSt_Any(MCInst &Inst,
282*5f7ddb14SDimitry Andric unsigned Imm,
283*5f7ddb14SDimitry Andric AMDGPUDisassembler::OpWidthTy Opw,
284*5f7ddb14SDimitry Andric const void *Decoder) {
285*5f7ddb14SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
286*5f7ddb14SDimitry Andric if (!DAsm->isGFX90A()) {
287*5f7ddb14SDimitry Andric Imm &= 511;
288*5f7ddb14SDimitry Andric } else {
289*5f7ddb14SDimitry Andric // If atomic has both vdata and vdst their register classes are tied.
290*5f7ddb14SDimitry Andric // The bit is decoded along with the vdst, first operand. We need to
291*5f7ddb14SDimitry Andric // change register class to AGPR if vdst was AGPR.
292*5f7ddb14SDimitry Andric // If a DS instruction has both data0 and data1 their register classes
293*5f7ddb14SDimitry Andric // are also tied.
294*5f7ddb14SDimitry Andric unsigned Opc = Inst.getOpcode();
295*5f7ddb14SDimitry Andric uint64_t TSFlags = DAsm->getMCII()->get(Opc).TSFlags;
296*5f7ddb14SDimitry Andric uint16_t DataNameIdx = (TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0
297*5f7ddb14SDimitry Andric : AMDGPU::OpName::vdata;
298*5f7ddb14SDimitry Andric const MCRegisterInfo *MRI = DAsm->getContext().getRegisterInfo();
299*5f7ddb14SDimitry Andric int DataIdx = AMDGPU::getNamedOperandIdx(Opc, DataNameIdx);
300*5f7ddb14SDimitry Andric if ((int)Inst.getNumOperands() == DataIdx) {
301*5f7ddb14SDimitry Andric int DstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
302*5f7ddb14SDimitry Andric if (IsAGPROperand(Inst, DstIdx, MRI))
303*5f7ddb14SDimitry Andric Imm |= 512;
304*5f7ddb14SDimitry Andric }
305*5f7ddb14SDimitry Andric
306*5f7ddb14SDimitry Andric if (TSFlags & SIInstrFlags::DS) {
307*5f7ddb14SDimitry Andric int Data2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1);
308*5f7ddb14SDimitry Andric if ((int)Inst.getNumOperands() == Data2Idx &&
309*5f7ddb14SDimitry Andric IsAGPROperand(Inst, DataIdx, MRI))
310*5f7ddb14SDimitry Andric Imm |= 512;
311*5f7ddb14SDimitry Andric }
312*5f7ddb14SDimitry Andric }
313*5f7ddb14SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(Opw, Imm | 256));
314*5f7ddb14SDimitry Andric }
315*5f7ddb14SDimitry Andric
DecodeAVLdSt_32RegisterClass(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)316*5f7ddb14SDimitry Andric static DecodeStatus DecodeAVLdSt_32RegisterClass(MCInst &Inst,
317*5f7ddb14SDimitry Andric unsigned Imm,
318*5f7ddb14SDimitry Andric uint64_t Addr,
319*5f7ddb14SDimitry Andric const void *Decoder) {
320*5f7ddb14SDimitry Andric return decodeOperand_AVLdSt_Any(Inst, Imm,
321*5f7ddb14SDimitry Andric AMDGPUDisassembler::OPW32, Decoder);
322*5f7ddb14SDimitry Andric }
323*5f7ddb14SDimitry Andric
DecodeAVLdSt_64RegisterClass(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)324*5f7ddb14SDimitry Andric static DecodeStatus DecodeAVLdSt_64RegisterClass(MCInst &Inst,
325*5f7ddb14SDimitry Andric unsigned Imm,
326*5f7ddb14SDimitry Andric uint64_t Addr,
327*5f7ddb14SDimitry Andric const void *Decoder) {
328*5f7ddb14SDimitry Andric return decodeOperand_AVLdSt_Any(Inst, Imm,
329*5f7ddb14SDimitry Andric AMDGPUDisassembler::OPW64, Decoder);
330*5f7ddb14SDimitry Andric }
331*5f7ddb14SDimitry Andric
DecodeAVLdSt_96RegisterClass(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)332*5f7ddb14SDimitry Andric static DecodeStatus DecodeAVLdSt_96RegisterClass(MCInst &Inst,
333*5f7ddb14SDimitry Andric unsigned Imm,
334*5f7ddb14SDimitry Andric uint64_t Addr,
335*5f7ddb14SDimitry Andric const void *Decoder) {
336*5f7ddb14SDimitry Andric return decodeOperand_AVLdSt_Any(Inst, Imm,
337*5f7ddb14SDimitry Andric AMDGPUDisassembler::OPW96, Decoder);
338*5f7ddb14SDimitry Andric }
339*5f7ddb14SDimitry Andric
DecodeAVLdSt_128RegisterClass(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)340*5f7ddb14SDimitry Andric static DecodeStatus DecodeAVLdSt_128RegisterClass(MCInst &Inst,
341*5f7ddb14SDimitry Andric unsigned Imm,
342*5f7ddb14SDimitry Andric uint64_t Addr,
343*5f7ddb14SDimitry Andric const void *Decoder) {
344*5f7ddb14SDimitry Andric return decodeOperand_AVLdSt_Any(Inst, Imm,
345*5f7ddb14SDimitry Andric AMDGPUDisassembler::OPW128, Decoder);
346*5f7ddb14SDimitry Andric }
347*5f7ddb14SDimitry Andric
decodeOperand_SReg_32(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)3480b57cec5SDimitry Andric static DecodeStatus decodeOperand_SReg_32(MCInst &Inst,
3490b57cec5SDimitry Andric unsigned Imm,
3500b57cec5SDimitry Andric uint64_t Addr,
3510b57cec5SDimitry Andric const void *Decoder) {
3520b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
3530b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeOperand_SReg_32(Imm));
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
decodeOperand_VGPR_32(MCInst & Inst,unsigned Imm,uint64_t Addr,const void * Decoder)3560b57cec5SDimitry Andric static DecodeStatus decodeOperand_VGPR_32(MCInst &Inst,
3570b57cec5SDimitry Andric unsigned Imm,
3580b57cec5SDimitry Andric uint64_t Addr,
3590b57cec5SDimitry Andric const void *Decoder) {
3600b57cec5SDimitry Andric auto DAsm = static_cast<const AMDGPUDisassembler*>(Decoder);
3610b57cec5SDimitry Andric return addOperand(Inst, DAsm->decodeSrcOp(AMDGPUDisassembler::OPW32, Imm));
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric #define DECODE_SDWA(DecName) \
3650b57cec5SDimitry Andric DECODE_OPERAND(decodeSDWA##DecName, decodeSDWA##DecName)
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric DECODE_SDWA(Src32)
DECODE_SDWA(Src16)3680b57cec5SDimitry Andric DECODE_SDWA(Src16)
3690b57cec5SDimitry Andric DECODE_SDWA(VopcDst)
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric #include "AMDGPUGenDisassemblerTables.inc"
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3740b57cec5SDimitry Andric //
3750b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andric template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
3780b57cec5SDimitry Andric assert(Bytes.size() >= sizeof(T));
3790b57cec5SDimitry Andric const auto Res = support::endian::read<T, support::endianness::little>(Bytes.data());
3800b57cec5SDimitry Andric Bytes = Bytes.slice(sizeof(T));
3810b57cec5SDimitry Andric return Res;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric
tryDecodeInst(const uint8_t * Table,MCInst & MI,uint64_t Inst,uint64_t Address) const3840b57cec5SDimitry Andric DecodeStatus AMDGPUDisassembler::tryDecodeInst(const uint8_t* Table,
3850b57cec5SDimitry Andric MCInst &MI,
3860b57cec5SDimitry Andric uint64_t Inst,
3870b57cec5SDimitry Andric uint64_t Address) const {
3880b57cec5SDimitry Andric assert(MI.getOpcode() == 0);
3890b57cec5SDimitry Andric assert(MI.getNumOperands() == 0);
3900b57cec5SDimitry Andric MCInst TmpInst;
3910b57cec5SDimitry Andric HasLiteral = false;
3920b57cec5SDimitry Andric const auto SavedBytes = Bytes;
3930b57cec5SDimitry Andric if (decodeInstruction(Table, TmpInst, Inst, Address, this, STI)) {
3940b57cec5SDimitry Andric MI = TmpInst;
3950b57cec5SDimitry Andric return MCDisassembler::Success;
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric Bytes = SavedBytes;
3980b57cec5SDimitry Andric return MCDisassembler::Fail;
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric
401*5f7ddb14SDimitry Andric // The disassembler is greedy, so we need to check FI operand value to
402*5f7ddb14SDimitry Andric // not parse a dpp if the correct literal is not set. For dpp16 the
403*5f7ddb14SDimitry Andric // autogenerated decoder checks the dpp literal
isValidDPP8(const MCInst & MI)4040b57cec5SDimitry Andric static bool isValidDPP8(const MCInst &MI) {
4050b57cec5SDimitry Andric using namespace llvm::AMDGPU::DPP;
4060b57cec5SDimitry Andric int FiIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::fi);
4070b57cec5SDimitry Andric assert(FiIdx != -1);
4080b57cec5SDimitry Andric if ((unsigned)FiIdx >= MI.getNumOperands())
4090b57cec5SDimitry Andric return false;
4100b57cec5SDimitry Andric unsigned Fi = MI.getOperand(FiIdx).getImm();
4110b57cec5SDimitry Andric return Fi == DPP8_FI_0 || Fi == DPP8_FI_1;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
getInstruction(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes_,uint64_t Address,raw_ostream & CS) const4140b57cec5SDimitry Andric DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
4150b57cec5SDimitry Andric ArrayRef<uint8_t> Bytes_,
4160b57cec5SDimitry Andric uint64_t Address,
4170b57cec5SDimitry Andric raw_ostream &CS) const {
4180b57cec5SDimitry Andric CommentStream = &CS;
4190b57cec5SDimitry Andric bool IsSDWA = false;
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric unsigned MaxInstBytesNum = std::min((size_t)TargetMaxInstBytes, Bytes_.size());
4220b57cec5SDimitry Andric Bytes = Bytes_.slice(0, MaxInstBytesNum);
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric DecodeStatus Res = MCDisassembler::Fail;
4250b57cec5SDimitry Andric do {
4260b57cec5SDimitry Andric // ToDo: better to switch encoding length using some bit predicate
4270b57cec5SDimitry Andric // but it is unknown yet, so try all we can
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andric // Try to decode DPP and SDWA first to solve conflict with VOP1 and VOP2
4300b57cec5SDimitry Andric // encodings
4310b57cec5SDimitry Andric if (Bytes.size() >= 8) {
4320b57cec5SDimitry Andric const uint64_t QW = eatBytes<uint64_t>(Bytes);
4330b57cec5SDimitry Andric
4345ffd83dbSDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX10_BEncoding]) {
4355ffd83dbSDimitry Andric Res = tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address);
4365ffd83dbSDimitry Andric if (Res) {
4375ffd83dbSDimitry Andric if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dpp8)
4385ffd83dbSDimitry Andric == -1)
4395ffd83dbSDimitry Andric break;
4405ffd83dbSDimitry Andric if (convertDPP8Inst(MI) == MCDisassembler::Success)
4415ffd83dbSDimitry Andric break;
4425ffd83dbSDimitry Andric MI = MCInst(); // clear
4435ffd83dbSDimitry Andric }
4445ffd83dbSDimitry Andric }
4455ffd83dbSDimitry Andric
4460b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableDPP864, MI, QW, Address);
4470b57cec5SDimitry Andric if (Res && convertDPP8Inst(MI) == MCDisassembler::Success)
4480b57cec5SDimitry Andric break;
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric MI = MCInst(); // clear
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableDPP64, MI, QW, Address);
4530b57cec5SDimitry Andric if (Res) break;
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableSDWA64, MI, QW, Address);
4560b57cec5SDimitry Andric if (Res) { IsSDWA = true; break; }
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableSDWA964, MI, QW, Address);
4590b57cec5SDimitry Andric if (Res) { IsSDWA = true; break; }
4600b57cec5SDimitry Andric
4610b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableSDWA1064, MI, QW, Address);
4620b57cec5SDimitry Andric if (Res) { IsSDWA = true; break; }
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem]) {
4650b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address);
4660b57cec5SDimitry Andric if (Res)
4670b57cec5SDimitry Andric break;
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric // Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
4710b57cec5SDimitry Andric // v_mad_mixhi_f16 for FMA variants. Try to decode using this special
4720b57cec5SDimitry Andric // table first so we print the correct name.
4730b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureFmaMixInsts]) {
4740b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address);
4750b57cec5SDimitry Andric if (Res)
4760b57cec5SDimitry Andric break;
4770b57cec5SDimitry Andric }
4780b57cec5SDimitry Andric }
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric // Reinitialize Bytes as DPP64 could have eaten too much
4810b57cec5SDimitry Andric Bytes = Bytes_.slice(0, MaxInstBytesNum);
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric // Try decode 32-bit instruction
4840b57cec5SDimitry Andric if (Bytes.size() < 4) break;
4850b57cec5SDimitry Andric const uint32_t DW = eatBytes<uint32_t>(Bytes);
4860b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX832, MI, DW, Address);
4870b57cec5SDimitry Andric if (Res) break;
4880b57cec5SDimitry Andric
4890b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address);
4900b57cec5SDimitry Andric if (Res) break;
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX932, MI, DW, Address);
4930b57cec5SDimitry Andric if (Res) break;
4940b57cec5SDimitry Andric
495*5f7ddb14SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts]) {
496*5f7ddb14SDimitry Andric Res = tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address);
497*5f7ddb14SDimitry Andric if (Res)
498*5f7ddb14SDimitry Andric break;
499*5f7ddb14SDimitry Andric }
500*5f7ddb14SDimitry Andric
5015ffd83dbSDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX10_BEncoding]) {
5025ffd83dbSDimitry Andric Res = tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address);
5035ffd83dbSDimitry Andric if (Res) break;
5045ffd83dbSDimitry Andric }
5055ffd83dbSDimitry Andric
5060b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX1032, MI, DW, Address);
5070b57cec5SDimitry Andric if (Res) break;
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric if (Bytes.size() < 4) break;
5100b57cec5SDimitry Andric const uint64_t QW = ((uint64_t)eatBytes<uint32_t>(Bytes) << 32) | DW;
511*5f7ddb14SDimitry Andric
512*5f7ddb14SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts]) {
513*5f7ddb14SDimitry Andric Res = tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address);
514*5f7ddb14SDimitry Andric if (Res)
515*5f7ddb14SDimitry Andric break;
516*5f7ddb14SDimitry Andric }
517*5f7ddb14SDimitry Andric
5180b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX864, MI, QW, Address);
5190b57cec5SDimitry Andric if (Res) break;
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableAMDGPU64, MI, QW, Address);
5220b57cec5SDimitry Andric if (Res) break;
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX964, MI, QW, Address);
5250b57cec5SDimitry Andric if (Res) break;
5260b57cec5SDimitry Andric
5270b57cec5SDimitry Andric Res = tryDecodeInst(DecoderTableGFX1064, MI, QW, Address);
5280b57cec5SDimitry Andric } while (false);
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric if (Res && (MI.getOpcode() == AMDGPU::V_MAC_F32_e64_vi ||
5310b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_MAC_F32_e64_gfx6_gfx7 ||
5320b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_MAC_F32_e64_gfx10 ||
533af732203SDimitry Andric MI.getOpcode() == AMDGPU::V_MAC_LEGACY_F32_e64_gfx6_gfx7 ||
534af732203SDimitry Andric MI.getOpcode() == AMDGPU::V_MAC_LEGACY_F32_e64_gfx10 ||
5350b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_MAC_F16_e64_vi ||
536*5f7ddb14SDimitry Andric MI.getOpcode() == AMDGPU::V_FMAC_F64_e64_gfx90a ||
5370b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_FMAC_F32_e64_vi ||
5380b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_FMAC_F32_e64_gfx10 ||
539af732203SDimitry Andric MI.getOpcode() == AMDGPU::V_FMAC_LEGACY_F32_e64_gfx10 ||
5400b57cec5SDimitry Andric MI.getOpcode() == AMDGPU::V_FMAC_F16_e64_gfx10)) {
5410b57cec5SDimitry Andric // Insert dummy unused src2_modifiers.
5420b57cec5SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(0),
5430b57cec5SDimitry Andric AMDGPU::OpName::src2_modifiers);
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric
546af732203SDimitry Andric if (Res && (MCII->get(MI.getOpcode()).TSFlags &
547*5f7ddb14SDimitry Andric (SIInstrFlags::MUBUF | SIInstrFlags::FLAT | SIInstrFlags::SMRD))) {
548*5f7ddb14SDimitry Andric int CPolPos = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
549*5f7ddb14SDimitry Andric AMDGPU::OpName::cpol);
550*5f7ddb14SDimitry Andric if (CPolPos != -1) {
551*5f7ddb14SDimitry Andric unsigned CPol =
552*5f7ddb14SDimitry Andric (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::IsAtomicRet) ?
553*5f7ddb14SDimitry Andric AMDGPU::CPol::GLC : 0;
554*5f7ddb14SDimitry Andric if (MI.getNumOperands() <= (unsigned)CPolPos) {
555*5f7ddb14SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(CPol),
556*5f7ddb14SDimitry Andric AMDGPU::OpName::cpol);
557*5f7ddb14SDimitry Andric } else if (CPol) {
558*5f7ddb14SDimitry Andric MI.getOperand(CPolPos).setImm(MI.getOperand(CPolPos).getImm() | CPol);
559*5f7ddb14SDimitry Andric }
560*5f7ddb14SDimitry Andric }
561*5f7ddb14SDimitry Andric }
562*5f7ddb14SDimitry Andric
563*5f7ddb14SDimitry Andric if (Res && (MCII->get(MI.getOpcode()).TSFlags &
564*5f7ddb14SDimitry Andric (SIInstrFlags::MTBUF | SIInstrFlags::MUBUF)) &&
565*5f7ddb14SDimitry Andric (STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts])) {
566*5f7ddb14SDimitry Andric // GFX90A lost TFE, its place is occupied by ACC.
567*5f7ddb14SDimitry Andric int TFEOpIdx =
568*5f7ddb14SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
569*5f7ddb14SDimitry Andric if (TFEOpIdx != -1) {
570*5f7ddb14SDimitry Andric auto TFEIter = MI.begin();
571*5f7ddb14SDimitry Andric std::advance(TFEIter, TFEOpIdx);
572*5f7ddb14SDimitry Andric MI.insert(TFEIter, MCOperand::createImm(0));
573*5f7ddb14SDimitry Andric }
574*5f7ddb14SDimitry Andric }
575*5f7ddb14SDimitry Andric
576*5f7ddb14SDimitry Andric if (Res && (MCII->get(MI.getOpcode()).TSFlags &
577*5f7ddb14SDimitry Andric (SIInstrFlags::MTBUF | SIInstrFlags::MUBUF))) {
578*5f7ddb14SDimitry Andric int SWZOpIdx =
579*5f7ddb14SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::swz);
580*5f7ddb14SDimitry Andric if (SWZOpIdx != -1) {
581*5f7ddb14SDimitry Andric auto SWZIter = MI.begin();
582*5f7ddb14SDimitry Andric std::advance(SWZIter, SWZOpIdx);
583*5f7ddb14SDimitry Andric MI.insert(SWZIter, MCOperand::createImm(0));
584*5f7ddb14SDimitry Andric }
585af732203SDimitry Andric }
586af732203SDimitry Andric
5870b57cec5SDimitry Andric if (Res && (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::MIMG)) {
5880b57cec5SDimitry Andric int VAddr0Idx =
5890b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
5900b57cec5SDimitry Andric int RsrcIdx =
5910b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
5920b57cec5SDimitry Andric unsigned NSAArgs = RsrcIdx - VAddr0Idx - 1;
5930b57cec5SDimitry Andric if (VAddr0Idx >= 0 && NSAArgs > 0) {
5940b57cec5SDimitry Andric unsigned NSAWords = (NSAArgs + 3) / 4;
5950b57cec5SDimitry Andric if (Bytes.size() < 4 * NSAWords) {
5960b57cec5SDimitry Andric Res = MCDisassembler::Fail;
5970b57cec5SDimitry Andric } else {
5980b57cec5SDimitry Andric for (unsigned i = 0; i < NSAArgs; ++i) {
5990b57cec5SDimitry Andric MI.insert(MI.begin() + VAddr0Idx + 1 + i,
6000b57cec5SDimitry Andric decodeOperand_VGPR_32(Bytes[i]));
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric Bytes = Bytes.slice(4 * NSAWords);
6030b57cec5SDimitry Andric }
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric
6060b57cec5SDimitry Andric if (Res)
6070b57cec5SDimitry Andric Res = convertMIMGInst(MI);
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric
6100b57cec5SDimitry Andric if (Res && IsSDWA)
6110b57cec5SDimitry Andric Res = convertSDWAInst(MI);
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andric int VDstIn_Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6140b57cec5SDimitry Andric AMDGPU::OpName::vdst_in);
6150b57cec5SDimitry Andric if (VDstIn_Idx != -1) {
6160b57cec5SDimitry Andric int Tied = MCII->get(MI.getOpcode()).getOperandConstraint(VDstIn_Idx,
6170b57cec5SDimitry Andric MCOI::OperandConstraint::TIED_TO);
6180b57cec5SDimitry Andric if (Tied != -1 && (MI.getNumOperands() <= (unsigned)VDstIn_Idx ||
6190b57cec5SDimitry Andric !MI.getOperand(VDstIn_Idx).isReg() ||
6200b57cec5SDimitry Andric MI.getOperand(VDstIn_Idx).getReg() != MI.getOperand(Tied).getReg())) {
6210b57cec5SDimitry Andric if (MI.getNumOperands() > (unsigned)VDstIn_Idx)
6220b57cec5SDimitry Andric MI.erase(&MI.getOperand(VDstIn_Idx));
6230b57cec5SDimitry Andric insertNamedMCOperand(MI,
6240b57cec5SDimitry Andric MCOperand::createReg(MI.getOperand(Tied).getReg()),
6250b57cec5SDimitry Andric AMDGPU::OpName::vdst_in);
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric }
6280b57cec5SDimitry Andric
6290b57cec5SDimitry Andric // if the opcode was not recognized we'll assume a Size of 4 bytes
6300b57cec5SDimitry Andric // (unless there are fewer bytes left)
6310b57cec5SDimitry Andric Size = Res ? (MaxInstBytesNum - Bytes.size())
6320b57cec5SDimitry Andric : std::min((size_t)4, Bytes_.size());
6330b57cec5SDimitry Andric return Res;
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric
convertSDWAInst(MCInst & MI) const6360b57cec5SDimitry Andric DecodeStatus AMDGPUDisassembler::convertSDWAInst(MCInst &MI) const {
6370b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX9] ||
6380b57cec5SDimitry Andric STI.getFeatureBits()[AMDGPU::FeatureGFX10]) {
6390b57cec5SDimitry Andric if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sdst) != -1)
6400b57cec5SDimitry Andric // VOPC - insert clamp
6410b57cec5SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::clamp);
6420b57cec5SDimitry Andric } else if (STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands]) {
6430b57cec5SDimitry Andric int SDst = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sdst);
6440b57cec5SDimitry Andric if (SDst != -1) {
6450b57cec5SDimitry Andric // VOPC - insert VCC register as sdst
6460b57cec5SDimitry Andric insertNamedMCOperand(MI, createRegOperand(AMDGPU::VCC),
6470b57cec5SDimitry Andric AMDGPU::OpName::sdst);
6480b57cec5SDimitry Andric } else {
6490b57cec5SDimitry Andric // VOP1/2 - insert omod if present in instruction
6500b57cec5SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::omod);
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric return MCDisassembler::Success;
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric
656*5f7ddb14SDimitry Andric // We must check FI == literal to reject not genuine dpp8 insts, and we must
657*5f7ddb14SDimitry Andric // first add optional MI operands to check FI
convertDPP8Inst(MCInst & MI) const6580b57cec5SDimitry Andric DecodeStatus AMDGPUDisassembler::convertDPP8Inst(MCInst &MI) const {
6590b57cec5SDimitry Andric unsigned Opc = MI.getOpcode();
6600b57cec5SDimitry Andric unsigned DescNumOps = MCII->get(Opc).getNumOperands();
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric // Insert dummy unused src modifiers.
6630b57cec5SDimitry Andric if (MI.getNumOperands() < DescNumOps &&
6640b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0_modifiers) != -1)
6650b57cec5SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(0),
6660b57cec5SDimitry Andric AMDGPU::OpName::src0_modifiers);
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric if (MI.getNumOperands() < DescNumOps &&
6690b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1_modifiers) != -1)
6700b57cec5SDimitry Andric insertNamedMCOperand(MI, MCOperand::createImm(0),
6710b57cec5SDimitry Andric AMDGPU::OpName::src1_modifiers);
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric return isValidDPP8(MI) ? MCDisassembler::Success : MCDisassembler::SoftFail;
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric
6760b57cec5SDimitry Andric // Note that before gfx10, the MIMG encoding provided no information about
6770b57cec5SDimitry Andric // VADDR size. Consequently, decoded instructions always show address as if it
6780b57cec5SDimitry Andric // has 1 dword, which could be not really so.
convertMIMGInst(MCInst & MI) const6790b57cec5SDimitry Andric DecodeStatus AMDGPUDisassembler::convertMIMGInst(MCInst &MI) const {
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric int VDstIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6820b57cec5SDimitry Andric AMDGPU::OpName::vdst);
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andric int VDataIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6850b57cec5SDimitry Andric AMDGPU::OpName::vdata);
6860b57cec5SDimitry Andric int VAddr0Idx =
6870b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
6880b57cec5SDimitry Andric int DMaskIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6890b57cec5SDimitry Andric AMDGPU::OpName::dmask);
6900b57cec5SDimitry Andric
6910b57cec5SDimitry Andric int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6920b57cec5SDimitry Andric AMDGPU::OpName::tfe);
6930b57cec5SDimitry Andric int D16Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
6940b57cec5SDimitry Andric AMDGPU::OpName::d16);
6950b57cec5SDimitry Andric
6960b57cec5SDimitry Andric assert(VDataIdx != -1);
697af732203SDimitry Andric if (DMaskIdx == -1 || TFEIdx == -1) {// intersect_ray
698af732203SDimitry Andric if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::a16) > -1) {
699af732203SDimitry Andric assert(MI.getOpcode() == AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16_sa ||
700af732203SDimitry Andric MI.getOpcode() == AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16_nsa ||
701af732203SDimitry Andric MI.getOpcode() == AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16_sa ||
702af732203SDimitry Andric MI.getOpcode() == AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16_nsa);
703af732203SDimitry Andric addOperand(MI, MCOperand::createImm(1));
704af732203SDimitry Andric }
705af732203SDimitry Andric return MCDisassembler::Success;
706af732203SDimitry Andric }
7070b57cec5SDimitry Andric
7080b57cec5SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
7090b57cec5SDimitry Andric bool IsAtomic = (VDstIdx != -1);
7100b57cec5SDimitry Andric bool IsGather4 = MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::Gather4;
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric bool IsNSA = false;
7130b57cec5SDimitry Andric unsigned AddrSize = Info->VAddrDwords;
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX10]) {
7160b57cec5SDimitry Andric unsigned DimIdx =
7170b57cec5SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dim);
718*5f7ddb14SDimitry Andric int A16Idx =
719*5f7ddb14SDimitry Andric AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::a16);
7200b57cec5SDimitry Andric const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
7210b57cec5SDimitry Andric AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
7220b57cec5SDimitry Andric const AMDGPU::MIMGDimInfo *Dim =
7230b57cec5SDimitry Andric AMDGPU::getMIMGDimInfoByEncoding(MI.getOperand(DimIdx).getImm());
724*5f7ddb14SDimitry Andric const bool IsA16 = (A16Idx != -1 && MI.getOperand(A16Idx).getImm());
7250b57cec5SDimitry Andric
726*5f7ddb14SDimitry Andric AddrSize =
727*5f7ddb14SDimitry Andric AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, AMDGPU::hasG16(STI));
728*5f7ddb14SDimitry Andric
7290b57cec5SDimitry Andric IsNSA = Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA;
7300b57cec5SDimitry Andric if (!IsNSA) {
7310b57cec5SDimitry Andric if (AddrSize > 8)
7320b57cec5SDimitry Andric AddrSize = 16;
7330b57cec5SDimitry Andric } else {
7340b57cec5SDimitry Andric if (AddrSize > Info->VAddrDwords) {
7350b57cec5SDimitry Andric // The NSA encoding does not contain enough operands for the combination
7360b57cec5SDimitry Andric // of base opcode / dimension. Should this be an error?
7370b57cec5SDimitry Andric return MCDisassembler::Success;
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric }
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric unsigned DMask = MI.getOperand(DMaskIdx).getImm() & 0xf;
7430b57cec5SDimitry Andric unsigned DstSize = IsGather4 ? 4 : std::max(countPopulation(DMask), 1u);
7440b57cec5SDimitry Andric
7450b57cec5SDimitry Andric bool D16 = D16Idx >= 0 && MI.getOperand(D16Idx).getImm();
7460b57cec5SDimitry Andric if (D16 && AMDGPU::hasPackedD16(STI)) {
7470b57cec5SDimitry Andric DstSize = (DstSize + 1) / 2;
7480b57cec5SDimitry Andric }
7490b57cec5SDimitry Andric
750*5f7ddb14SDimitry Andric if (TFEIdx != -1 && MI.getOperand(TFEIdx).getImm())
751af732203SDimitry Andric DstSize += 1;
7520b57cec5SDimitry Andric
7530b57cec5SDimitry Andric if (DstSize == Info->VDataDwords && AddrSize == Info->VAddrDwords)
7540b57cec5SDimitry Andric return MCDisassembler::Success;
7550b57cec5SDimitry Andric
7560b57cec5SDimitry Andric int NewOpcode =
7570b57cec5SDimitry Andric AMDGPU::getMIMGOpcode(Info->BaseOpcode, Info->MIMGEncoding, DstSize, AddrSize);
7580b57cec5SDimitry Andric if (NewOpcode == -1)
7590b57cec5SDimitry Andric return MCDisassembler::Success;
7600b57cec5SDimitry Andric
7610b57cec5SDimitry Andric // Widen the register to the correct number of enabled channels.
7620b57cec5SDimitry Andric unsigned NewVdata = AMDGPU::NoRegister;
7630b57cec5SDimitry Andric if (DstSize != Info->VDataDwords) {
7640b57cec5SDimitry Andric auto DataRCID = MCII->get(NewOpcode).OpInfo[VDataIdx].RegClass;
7650b57cec5SDimitry Andric
7660b57cec5SDimitry Andric // Get first subregister of VData
7670b57cec5SDimitry Andric unsigned Vdata0 = MI.getOperand(VDataIdx).getReg();
7680b57cec5SDimitry Andric unsigned VdataSub0 = MRI.getSubReg(Vdata0, AMDGPU::sub0);
7690b57cec5SDimitry Andric Vdata0 = (VdataSub0 != 0)? VdataSub0 : Vdata0;
7700b57cec5SDimitry Andric
7710b57cec5SDimitry Andric NewVdata = MRI.getMatchingSuperReg(Vdata0, AMDGPU::sub0,
7720b57cec5SDimitry Andric &MRI.getRegClass(DataRCID));
7730b57cec5SDimitry Andric if (NewVdata == AMDGPU::NoRegister) {
7740b57cec5SDimitry Andric // It's possible to encode this such that the low register + enabled
7750b57cec5SDimitry Andric // components exceeds the register count.
7760b57cec5SDimitry Andric return MCDisassembler::Success;
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric }
7790b57cec5SDimitry Andric
7800b57cec5SDimitry Andric unsigned NewVAddr0 = AMDGPU::NoRegister;
7810b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX10] && !IsNSA &&
7820b57cec5SDimitry Andric AddrSize != Info->VAddrDwords) {
7830b57cec5SDimitry Andric unsigned VAddr0 = MI.getOperand(VAddr0Idx).getReg();
7840b57cec5SDimitry Andric unsigned VAddrSub0 = MRI.getSubReg(VAddr0, AMDGPU::sub0);
7850b57cec5SDimitry Andric VAddr0 = (VAddrSub0 != 0) ? VAddrSub0 : VAddr0;
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric auto AddrRCID = MCII->get(NewOpcode).OpInfo[VAddr0Idx].RegClass;
7880b57cec5SDimitry Andric NewVAddr0 = MRI.getMatchingSuperReg(VAddr0, AMDGPU::sub0,
7890b57cec5SDimitry Andric &MRI.getRegClass(AddrRCID));
7900b57cec5SDimitry Andric if (NewVAddr0 == AMDGPU::NoRegister)
7910b57cec5SDimitry Andric return MCDisassembler::Success;
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric
7940b57cec5SDimitry Andric MI.setOpcode(NewOpcode);
7950b57cec5SDimitry Andric
7960b57cec5SDimitry Andric if (NewVdata != AMDGPU::NoRegister) {
7970b57cec5SDimitry Andric MI.getOperand(VDataIdx) = MCOperand::createReg(NewVdata);
7980b57cec5SDimitry Andric
7990b57cec5SDimitry Andric if (IsAtomic) {
8000b57cec5SDimitry Andric // Atomic operations have an additional operand (a copy of data)
8010b57cec5SDimitry Andric MI.getOperand(VDstIdx) = MCOperand::createReg(NewVdata);
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric }
8040b57cec5SDimitry Andric
8050b57cec5SDimitry Andric if (NewVAddr0 != AMDGPU::NoRegister) {
8060b57cec5SDimitry Andric MI.getOperand(VAddr0Idx) = MCOperand::createReg(NewVAddr0);
8070b57cec5SDimitry Andric } else if (IsNSA) {
8080b57cec5SDimitry Andric assert(AddrSize <= Info->VAddrDwords);
8090b57cec5SDimitry Andric MI.erase(MI.begin() + VAddr0Idx + AddrSize,
8100b57cec5SDimitry Andric MI.begin() + VAddr0Idx + Info->VAddrDwords);
8110b57cec5SDimitry Andric }
8120b57cec5SDimitry Andric
8130b57cec5SDimitry Andric return MCDisassembler::Success;
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric
getRegClassName(unsigned RegClassID) const8160b57cec5SDimitry Andric const char* AMDGPUDisassembler::getRegClassName(unsigned RegClassID) const {
8170b57cec5SDimitry Andric return getContext().getRegisterInfo()->
8180b57cec5SDimitry Andric getRegClassName(&AMDGPUMCRegisterClasses[RegClassID]);
8190b57cec5SDimitry Andric }
8200b57cec5SDimitry Andric
8210b57cec5SDimitry Andric inline
errOperand(unsigned V,const Twine & ErrMsg) const8220b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::errOperand(unsigned V,
8230b57cec5SDimitry Andric const Twine& ErrMsg) const {
8240b57cec5SDimitry Andric *CommentStream << "Error: " + ErrMsg;
8250b57cec5SDimitry Andric
8260b57cec5SDimitry Andric // ToDo: add support for error operands to MCInst.h
8270b57cec5SDimitry Andric // return MCOperand::createError(V);
8280b57cec5SDimitry Andric return MCOperand();
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andric inline
createRegOperand(unsigned int RegId) const8320b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::createRegOperand(unsigned int RegId) const {
8330b57cec5SDimitry Andric return MCOperand::createReg(AMDGPU::getMCReg(RegId, STI));
8340b57cec5SDimitry Andric }
8350b57cec5SDimitry Andric
8360b57cec5SDimitry Andric inline
createRegOperand(unsigned RegClassID,unsigned Val) const8370b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::createRegOperand(unsigned RegClassID,
8380b57cec5SDimitry Andric unsigned Val) const {
8390b57cec5SDimitry Andric const auto& RegCl = AMDGPUMCRegisterClasses[RegClassID];
8400b57cec5SDimitry Andric if (Val >= RegCl.getNumRegs())
8410b57cec5SDimitry Andric return errOperand(Val, Twine(getRegClassName(RegClassID)) +
8420b57cec5SDimitry Andric ": unknown register " + Twine(Val));
8430b57cec5SDimitry Andric return createRegOperand(RegCl.getRegister(Val));
8440b57cec5SDimitry Andric }
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andric inline
createSRegOperand(unsigned SRegClassID,unsigned Val) const8470b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::createSRegOperand(unsigned SRegClassID,
8480b57cec5SDimitry Andric unsigned Val) const {
8490b57cec5SDimitry Andric // ToDo: SI/CI have 104 SGPRs, VI - 102
8500b57cec5SDimitry Andric // Valery: here we accepting as much as we can, let assembler sort it out
8510b57cec5SDimitry Andric int shift = 0;
8520b57cec5SDimitry Andric switch (SRegClassID) {
8530b57cec5SDimitry Andric case AMDGPU::SGPR_32RegClassID:
8540b57cec5SDimitry Andric case AMDGPU::TTMP_32RegClassID:
8550b57cec5SDimitry Andric break;
8560b57cec5SDimitry Andric case AMDGPU::SGPR_64RegClassID:
8570b57cec5SDimitry Andric case AMDGPU::TTMP_64RegClassID:
8580b57cec5SDimitry Andric shift = 1;
8590b57cec5SDimitry Andric break;
8600b57cec5SDimitry Andric case AMDGPU::SGPR_128RegClassID:
8610b57cec5SDimitry Andric case AMDGPU::TTMP_128RegClassID:
8620b57cec5SDimitry Andric // ToDo: unclear if s[100:104] is available on VI. Can we use VCC as SGPR in
8630b57cec5SDimitry Andric // this bundle?
8640b57cec5SDimitry Andric case AMDGPU::SGPR_256RegClassID:
8650b57cec5SDimitry Andric case AMDGPU::TTMP_256RegClassID:
8660b57cec5SDimitry Andric // ToDo: unclear if s[96:104] is available on VI. Can we use VCC as SGPR in
8670b57cec5SDimitry Andric // this bundle?
8680b57cec5SDimitry Andric case AMDGPU::SGPR_512RegClassID:
8690b57cec5SDimitry Andric case AMDGPU::TTMP_512RegClassID:
8700b57cec5SDimitry Andric shift = 2;
8710b57cec5SDimitry Andric break;
8720b57cec5SDimitry Andric // ToDo: unclear if s[88:104] is available on VI. Can we use VCC as SGPR in
8730b57cec5SDimitry Andric // this bundle?
8740b57cec5SDimitry Andric default:
8750b57cec5SDimitry Andric llvm_unreachable("unhandled register class");
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric
8780b57cec5SDimitry Andric if (Val % (1 << shift)) {
8790b57cec5SDimitry Andric *CommentStream << "Warning: " << getRegClassName(SRegClassID)
8800b57cec5SDimitry Andric << ": scalar reg isn't aligned " << Val;
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric
8830b57cec5SDimitry Andric return createRegOperand(SRegClassID, Val >> shift);
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric
decodeOperand_VS_32(unsigned Val) const8860b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VS_32(unsigned Val) const {
8870b57cec5SDimitry Andric return decodeSrcOp(OPW32, Val);
8880b57cec5SDimitry Andric }
8890b57cec5SDimitry Andric
decodeOperand_VS_64(unsigned Val) const8900b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VS_64(unsigned Val) const {
8910b57cec5SDimitry Andric return decodeSrcOp(OPW64, Val);
8920b57cec5SDimitry Andric }
8930b57cec5SDimitry Andric
decodeOperand_VS_128(unsigned Val) const8940b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VS_128(unsigned Val) const {
8950b57cec5SDimitry Andric return decodeSrcOp(OPW128, Val);
8960b57cec5SDimitry Andric }
8970b57cec5SDimitry Andric
decodeOperand_VSrc16(unsigned Val) const8980b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VSrc16(unsigned Val) const {
8990b57cec5SDimitry Andric return decodeSrcOp(OPW16, Val);
9000b57cec5SDimitry Andric }
9010b57cec5SDimitry Andric
decodeOperand_VSrcV216(unsigned Val) const9020b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VSrcV216(unsigned Val) const {
9030b57cec5SDimitry Andric return decodeSrcOp(OPWV216, Val);
9040b57cec5SDimitry Andric }
9050b57cec5SDimitry Andric
decodeOperand_VSrcV232(unsigned Val) const906*5f7ddb14SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VSrcV232(unsigned Val) const {
907*5f7ddb14SDimitry Andric return decodeSrcOp(OPWV232, Val);
908*5f7ddb14SDimitry Andric }
909*5f7ddb14SDimitry Andric
decodeOperand_VGPR_32(unsigned Val) const9100b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VGPR_32(unsigned Val) const {
9110b57cec5SDimitry Andric // Some instructions have operand restrictions beyond what the encoding
9120b57cec5SDimitry Andric // allows. Some ordinarily VSrc_32 operands are VGPR_32, so clear the extra
9130b57cec5SDimitry Andric // high bit.
9140b57cec5SDimitry Andric Val &= 255;
9150b57cec5SDimitry Andric
9160b57cec5SDimitry Andric return createRegOperand(AMDGPU::VGPR_32RegClassID, Val);
9170b57cec5SDimitry Andric }
9180b57cec5SDimitry Andric
decodeOperand_VRegOrLds_32(unsigned Val) const9190b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VRegOrLds_32(unsigned Val) const {
9200b57cec5SDimitry Andric return decodeSrcOp(OPW32, Val);
9210b57cec5SDimitry Andric }
9220b57cec5SDimitry Andric
decodeOperand_AGPR_32(unsigned Val) const9230b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AGPR_32(unsigned Val) const {
9240b57cec5SDimitry Andric return createRegOperand(AMDGPU::AGPR_32RegClassID, Val & 255);
9250b57cec5SDimitry Andric }
9260b57cec5SDimitry Andric
decodeOperand_AReg_64(unsigned Val) const927*5f7ddb14SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AReg_64(unsigned Val) const {
928*5f7ddb14SDimitry Andric return createRegOperand(AMDGPU::AReg_64RegClassID, Val & 255);
929*5f7ddb14SDimitry Andric }
930*5f7ddb14SDimitry Andric
decodeOperand_AReg_128(unsigned Val) const9310b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AReg_128(unsigned Val) const {
9320b57cec5SDimitry Andric return createRegOperand(AMDGPU::AReg_128RegClassID, Val & 255);
9330b57cec5SDimitry Andric }
9340b57cec5SDimitry Andric
decodeOperand_AReg_256(unsigned Val) const935*5f7ddb14SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AReg_256(unsigned Val) const {
936*5f7ddb14SDimitry Andric return createRegOperand(AMDGPU::AReg_256RegClassID, Val & 255);
937*5f7ddb14SDimitry Andric }
938*5f7ddb14SDimitry Andric
decodeOperand_AReg_512(unsigned Val) const9390b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AReg_512(unsigned Val) const {
9400b57cec5SDimitry Andric return createRegOperand(AMDGPU::AReg_512RegClassID, Val & 255);
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric
decodeOperand_AReg_1024(unsigned Val) const9430b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AReg_1024(unsigned Val) const {
9440b57cec5SDimitry Andric return createRegOperand(AMDGPU::AReg_1024RegClassID, Val & 255);
9450b57cec5SDimitry Andric }
9460b57cec5SDimitry Andric
decodeOperand_AV_32(unsigned Val) const9470b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AV_32(unsigned Val) const {
9480b57cec5SDimitry Andric return decodeSrcOp(OPW32, Val);
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric
decodeOperand_AV_64(unsigned Val) const9510b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_AV_64(unsigned Val) const {
9520b57cec5SDimitry Andric return decodeSrcOp(OPW64, Val);
9530b57cec5SDimitry Andric }
9540b57cec5SDimitry Andric
decodeOperand_VReg_64(unsigned Val) const9550b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_64(unsigned Val) const {
9560b57cec5SDimitry Andric return createRegOperand(AMDGPU::VReg_64RegClassID, Val);
9570b57cec5SDimitry Andric }
9580b57cec5SDimitry Andric
decodeOperand_VReg_96(unsigned Val) const9590b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_96(unsigned Val) const {
9600b57cec5SDimitry Andric return createRegOperand(AMDGPU::VReg_96RegClassID, Val);
9610b57cec5SDimitry Andric }
9620b57cec5SDimitry Andric
decodeOperand_VReg_128(unsigned Val) const9630b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_128(unsigned Val) const {
9640b57cec5SDimitry Andric return createRegOperand(AMDGPU::VReg_128RegClassID, Val);
9650b57cec5SDimitry Andric }
9660b57cec5SDimitry Andric
decodeOperand_VReg_256(unsigned Val) const9670b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_256(unsigned Val) const {
9680b57cec5SDimitry Andric return createRegOperand(AMDGPU::VReg_256RegClassID, Val);
9690b57cec5SDimitry Andric }
9700b57cec5SDimitry Andric
decodeOperand_VReg_512(unsigned Val) const9710b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_512(unsigned Val) const {
9720b57cec5SDimitry Andric return createRegOperand(AMDGPU::VReg_512RegClassID, Val);
9730b57cec5SDimitry Andric }
9740b57cec5SDimitry Andric
decodeOperand_VReg_1024(unsigned Val) const975*5f7ddb14SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_VReg_1024(unsigned Val) const {
976*5f7ddb14SDimitry Andric return createRegOperand(AMDGPU::VReg_1024RegClassID, Val);
977*5f7ddb14SDimitry Andric }
978*5f7ddb14SDimitry Andric
decodeOperand_SReg_32(unsigned Val) const9790b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_32(unsigned Val) const {
9800b57cec5SDimitry Andric // table-gen generated disassembler doesn't care about operand types
9810b57cec5SDimitry Andric // leaving only registry class so SSrc_32 operand turns into SReg_32
9820b57cec5SDimitry Andric // and therefore we accept immediates and literals here as well
9830b57cec5SDimitry Andric return decodeSrcOp(OPW32, Val);
9840b57cec5SDimitry Andric }
9850b57cec5SDimitry Andric
decodeOperand_SReg_32_XM0_XEXEC(unsigned Val) const9860b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_32_XM0_XEXEC(
9870b57cec5SDimitry Andric unsigned Val) const {
9880b57cec5SDimitry Andric // SReg_32_XM0 is SReg_32 without M0 or EXEC_LO/EXEC_HI
9890b57cec5SDimitry Andric return decodeOperand_SReg_32(Val);
9900b57cec5SDimitry Andric }
9910b57cec5SDimitry Andric
decodeOperand_SReg_32_XEXEC_HI(unsigned Val) const9920b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_32_XEXEC_HI(
9930b57cec5SDimitry Andric unsigned Val) const {
9940b57cec5SDimitry Andric // SReg_32_XM0 is SReg_32 without EXEC_HI
9950b57cec5SDimitry Andric return decodeOperand_SReg_32(Val);
9960b57cec5SDimitry Andric }
9970b57cec5SDimitry Andric
decodeOperand_SRegOrLds_32(unsigned Val) const9980b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SRegOrLds_32(unsigned Val) const {
9990b57cec5SDimitry Andric // table-gen generated disassembler doesn't care about operand types
10000b57cec5SDimitry Andric // leaving only registry class so SSrc_32 operand turns into SReg_32
10010b57cec5SDimitry Andric // and therefore we accept immediates and literals here as well
10020b57cec5SDimitry Andric return decodeSrcOp(OPW32, Val);
10030b57cec5SDimitry Andric }
10040b57cec5SDimitry Andric
decodeOperand_SReg_64(unsigned Val) const10050b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_64(unsigned Val) const {
10060b57cec5SDimitry Andric return decodeSrcOp(OPW64, Val);
10070b57cec5SDimitry Andric }
10080b57cec5SDimitry Andric
decodeOperand_SReg_64_XEXEC(unsigned Val) const10090b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_64_XEXEC(unsigned Val) const {
10100b57cec5SDimitry Andric return decodeSrcOp(OPW64, Val);
10110b57cec5SDimitry Andric }
10120b57cec5SDimitry Andric
decodeOperand_SReg_128(unsigned Val) const10130b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_128(unsigned Val) const {
10140b57cec5SDimitry Andric return decodeSrcOp(OPW128, Val);
10150b57cec5SDimitry Andric }
10160b57cec5SDimitry Andric
decodeOperand_SReg_256(unsigned Val) const10170b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_256(unsigned Val) const {
10180b57cec5SDimitry Andric return decodeDstOp(OPW256, Val);
10190b57cec5SDimitry Andric }
10200b57cec5SDimitry Andric
decodeOperand_SReg_512(unsigned Val) const10210b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeOperand_SReg_512(unsigned Val) const {
10220b57cec5SDimitry Andric return decodeDstOp(OPW512, Val);
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric
decodeLiteralConstant() const10250b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeLiteralConstant() const {
10260b57cec5SDimitry Andric // For now all literal constants are supposed to be unsigned integer
10270b57cec5SDimitry Andric // ToDo: deal with signed/unsigned 64-bit integer constants
10280b57cec5SDimitry Andric // ToDo: deal with float/double constants
10290b57cec5SDimitry Andric if (!HasLiteral) {
10300b57cec5SDimitry Andric if (Bytes.size() < 4) {
10310b57cec5SDimitry Andric return errOperand(0, "cannot read literal, inst bytes left " +
10320b57cec5SDimitry Andric Twine(Bytes.size()));
10330b57cec5SDimitry Andric }
10340b57cec5SDimitry Andric HasLiteral = true;
10350b57cec5SDimitry Andric Literal = eatBytes<uint32_t>(Bytes);
10360b57cec5SDimitry Andric }
10370b57cec5SDimitry Andric return MCOperand::createImm(Literal);
10380b57cec5SDimitry Andric }
10390b57cec5SDimitry Andric
decodeIntImmed(unsigned Imm)10400b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeIntImmed(unsigned Imm) {
10410b57cec5SDimitry Andric using namespace AMDGPU::EncValues;
10420b57cec5SDimitry Andric
10430b57cec5SDimitry Andric assert(Imm >= INLINE_INTEGER_C_MIN && Imm <= INLINE_INTEGER_C_MAX);
10440b57cec5SDimitry Andric return MCOperand::createImm((Imm <= INLINE_INTEGER_C_POSITIVE_MAX) ?
10450b57cec5SDimitry Andric (static_cast<int64_t>(Imm) - INLINE_INTEGER_C_MIN) :
10460b57cec5SDimitry Andric (INLINE_INTEGER_C_POSITIVE_MAX - static_cast<int64_t>(Imm)));
10470b57cec5SDimitry Andric // Cast prevents negative overflow.
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric
getInlineImmVal32(unsigned Imm)10500b57cec5SDimitry Andric static int64_t getInlineImmVal32(unsigned Imm) {
10510b57cec5SDimitry Andric switch (Imm) {
10520b57cec5SDimitry Andric case 240:
10530b57cec5SDimitry Andric return FloatToBits(0.5f);
10540b57cec5SDimitry Andric case 241:
10550b57cec5SDimitry Andric return FloatToBits(-0.5f);
10560b57cec5SDimitry Andric case 242:
10570b57cec5SDimitry Andric return FloatToBits(1.0f);
10580b57cec5SDimitry Andric case 243:
10590b57cec5SDimitry Andric return FloatToBits(-1.0f);
10600b57cec5SDimitry Andric case 244:
10610b57cec5SDimitry Andric return FloatToBits(2.0f);
10620b57cec5SDimitry Andric case 245:
10630b57cec5SDimitry Andric return FloatToBits(-2.0f);
10640b57cec5SDimitry Andric case 246:
10650b57cec5SDimitry Andric return FloatToBits(4.0f);
10660b57cec5SDimitry Andric case 247:
10670b57cec5SDimitry Andric return FloatToBits(-4.0f);
10680b57cec5SDimitry Andric case 248: // 1 / (2 * PI)
10690b57cec5SDimitry Andric return 0x3e22f983;
10700b57cec5SDimitry Andric default:
10710b57cec5SDimitry Andric llvm_unreachable("invalid fp inline imm");
10720b57cec5SDimitry Andric }
10730b57cec5SDimitry Andric }
10740b57cec5SDimitry Andric
getInlineImmVal64(unsigned Imm)10750b57cec5SDimitry Andric static int64_t getInlineImmVal64(unsigned Imm) {
10760b57cec5SDimitry Andric switch (Imm) {
10770b57cec5SDimitry Andric case 240:
10780b57cec5SDimitry Andric return DoubleToBits(0.5);
10790b57cec5SDimitry Andric case 241:
10800b57cec5SDimitry Andric return DoubleToBits(-0.5);
10810b57cec5SDimitry Andric case 242:
10820b57cec5SDimitry Andric return DoubleToBits(1.0);
10830b57cec5SDimitry Andric case 243:
10840b57cec5SDimitry Andric return DoubleToBits(-1.0);
10850b57cec5SDimitry Andric case 244:
10860b57cec5SDimitry Andric return DoubleToBits(2.0);
10870b57cec5SDimitry Andric case 245:
10880b57cec5SDimitry Andric return DoubleToBits(-2.0);
10890b57cec5SDimitry Andric case 246:
10900b57cec5SDimitry Andric return DoubleToBits(4.0);
10910b57cec5SDimitry Andric case 247:
10920b57cec5SDimitry Andric return DoubleToBits(-4.0);
10930b57cec5SDimitry Andric case 248: // 1 / (2 * PI)
10940b57cec5SDimitry Andric return 0x3fc45f306dc9c882;
10950b57cec5SDimitry Andric default:
10960b57cec5SDimitry Andric llvm_unreachable("invalid fp inline imm");
10970b57cec5SDimitry Andric }
10980b57cec5SDimitry Andric }
10990b57cec5SDimitry Andric
getInlineImmVal16(unsigned Imm)11000b57cec5SDimitry Andric static int64_t getInlineImmVal16(unsigned Imm) {
11010b57cec5SDimitry Andric switch (Imm) {
11020b57cec5SDimitry Andric case 240:
11030b57cec5SDimitry Andric return 0x3800;
11040b57cec5SDimitry Andric case 241:
11050b57cec5SDimitry Andric return 0xB800;
11060b57cec5SDimitry Andric case 242:
11070b57cec5SDimitry Andric return 0x3C00;
11080b57cec5SDimitry Andric case 243:
11090b57cec5SDimitry Andric return 0xBC00;
11100b57cec5SDimitry Andric case 244:
11110b57cec5SDimitry Andric return 0x4000;
11120b57cec5SDimitry Andric case 245:
11130b57cec5SDimitry Andric return 0xC000;
11140b57cec5SDimitry Andric case 246:
11150b57cec5SDimitry Andric return 0x4400;
11160b57cec5SDimitry Andric case 247:
11170b57cec5SDimitry Andric return 0xC400;
11180b57cec5SDimitry Andric case 248: // 1 / (2 * PI)
11190b57cec5SDimitry Andric return 0x3118;
11200b57cec5SDimitry Andric default:
11210b57cec5SDimitry Andric llvm_unreachable("invalid fp inline imm");
11220b57cec5SDimitry Andric }
11230b57cec5SDimitry Andric }
11240b57cec5SDimitry Andric
decodeFPImmed(OpWidthTy Width,unsigned Imm)11250b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeFPImmed(OpWidthTy Width, unsigned Imm) {
11260b57cec5SDimitry Andric assert(Imm >= AMDGPU::EncValues::INLINE_FLOATING_C_MIN
11270b57cec5SDimitry Andric && Imm <= AMDGPU::EncValues::INLINE_FLOATING_C_MAX);
11280b57cec5SDimitry Andric
11290b57cec5SDimitry Andric // ToDo: case 248: 1/(2*PI) - is allowed only on VI
11300b57cec5SDimitry Andric switch (Width) {
11310b57cec5SDimitry Andric case OPW32:
11320b57cec5SDimitry Andric case OPW128: // splat constants
11330b57cec5SDimitry Andric case OPW512:
11340b57cec5SDimitry Andric case OPW1024:
1135*5f7ddb14SDimitry Andric case OPWV232:
11360b57cec5SDimitry Andric return MCOperand::createImm(getInlineImmVal32(Imm));
11370b57cec5SDimitry Andric case OPW64:
1138*5f7ddb14SDimitry Andric case OPW256:
11390b57cec5SDimitry Andric return MCOperand::createImm(getInlineImmVal64(Imm));
11400b57cec5SDimitry Andric case OPW16:
11410b57cec5SDimitry Andric case OPWV216:
11420b57cec5SDimitry Andric return MCOperand::createImm(getInlineImmVal16(Imm));
11430b57cec5SDimitry Andric default:
11440b57cec5SDimitry Andric llvm_unreachable("implement me");
11450b57cec5SDimitry Andric }
11460b57cec5SDimitry Andric }
11470b57cec5SDimitry Andric
getVgprClassId(const OpWidthTy Width) const11480b57cec5SDimitry Andric unsigned AMDGPUDisassembler::getVgprClassId(const OpWidthTy Width) const {
11490b57cec5SDimitry Andric using namespace AMDGPU;
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andric assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
11520b57cec5SDimitry Andric switch (Width) {
11530b57cec5SDimitry Andric default: // fall
11540b57cec5SDimitry Andric case OPW32:
11550b57cec5SDimitry Andric case OPW16:
11560b57cec5SDimitry Andric case OPWV216:
11570b57cec5SDimitry Andric return VGPR_32RegClassID;
1158*5f7ddb14SDimitry Andric case OPW64:
1159*5f7ddb14SDimitry Andric case OPWV232: return VReg_64RegClassID;
1160*5f7ddb14SDimitry Andric case OPW96: return VReg_96RegClassID;
11610b57cec5SDimitry Andric case OPW128: return VReg_128RegClassID;
1162*5f7ddb14SDimitry Andric case OPW160: return VReg_160RegClassID;
1163*5f7ddb14SDimitry Andric case OPW256: return VReg_256RegClassID;
1164*5f7ddb14SDimitry Andric case OPW512: return VReg_512RegClassID;
1165*5f7ddb14SDimitry Andric case OPW1024: return VReg_1024RegClassID;
11660b57cec5SDimitry Andric }
11670b57cec5SDimitry Andric }
11680b57cec5SDimitry Andric
getAgprClassId(const OpWidthTy Width) const11690b57cec5SDimitry Andric unsigned AMDGPUDisassembler::getAgprClassId(const OpWidthTy Width) const {
11700b57cec5SDimitry Andric using namespace AMDGPU;
11710b57cec5SDimitry Andric
11720b57cec5SDimitry Andric assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
11730b57cec5SDimitry Andric switch (Width) {
11740b57cec5SDimitry Andric default: // fall
11750b57cec5SDimitry Andric case OPW32:
11760b57cec5SDimitry Andric case OPW16:
11770b57cec5SDimitry Andric case OPWV216:
11780b57cec5SDimitry Andric return AGPR_32RegClassID;
1179*5f7ddb14SDimitry Andric case OPW64:
1180*5f7ddb14SDimitry Andric case OPWV232: return AReg_64RegClassID;
1181*5f7ddb14SDimitry Andric case OPW96: return AReg_96RegClassID;
11820b57cec5SDimitry Andric case OPW128: return AReg_128RegClassID;
1183*5f7ddb14SDimitry Andric case OPW160: return AReg_160RegClassID;
11845ffd83dbSDimitry Andric case OPW256: return AReg_256RegClassID;
11850b57cec5SDimitry Andric case OPW512: return AReg_512RegClassID;
11860b57cec5SDimitry Andric case OPW1024: return AReg_1024RegClassID;
11870b57cec5SDimitry Andric }
11880b57cec5SDimitry Andric }
11890b57cec5SDimitry Andric
11900b57cec5SDimitry Andric
getSgprClassId(const OpWidthTy Width) const11910b57cec5SDimitry Andric unsigned AMDGPUDisassembler::getSgprClassId(const OpWidthTy Width) const {
11920b57cec5SDimitry Andric using namespace AMDGPU;
11930b57cec5SDimitry Andric
11940b57cec5SDimitry Andric assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
11950b57cec5SDimitry Andric switch (Width) {
11960b57cec5SDimitry Andric default: // fall
11970b57cec5SDimitry Andric case OPW32:
11980b57cec5SDimitry Andric case OPW16:
11990b57cec5SDimitry Andric case OPWV216:
12000b57cec5SDimitry Andric return SGPR_32RegClassID;
1201*5f7ddb14SDimitry Andric case OPW64:
1202*5f7ddb14SDimitry Andric case OPWV232: return SGPR_64RegClassID;
1203*5f7ddb14SDimitry Andric case OPW96: return SGPR_96RegClassID;
12040b57cec5SDimitry Andric case OPW128: return SGPR_128RegClassID;
1205*5f7ddb14SDimitry Andric case OPW160: return SGPR_160RegClassID;
12060b57cec5SDimitry Andric case OPW256: return SGPR_256RegClassID;
12070b57cec5SDimitry Andric case OPW512: return SGPR_512RegClassID;
12080b57cec5SDimitry Andric }
12090b57cec5SDimitry Andric }
12100b57cec5SDimitry Andric
getTtmpClassId(const OpWidthTy Width) const12110b57cec5SDimitry Andric unsigned AMDGPUDisassembler::getTtmpClassId(const OpWidthTy Width) const {
12120b57cec5SDimitry Andric using namespace AMDGPU;
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andric assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
12150b57cec5SDimitry Andric switch (Width) {
12160b57cec5SDimitry Andric default: // fall
12170b57cec5SDimitry Andric case OPW32:
12180b57cec5SDimitry Andric case OPW16:
12190b57cec5SDimitry Andric case OPWV216:
12200b57cec5SDimitry Andric return TTMP_32RegClassID;
1221*5f7ddb14SDimitry Andric case OPW64:
1222*5f7ddb14SDimitry Andric case OPWV232: return TTMP_64RegClassID;
12230b57cec5SDimitry Andric case OPW128: return TTMP_128RegClassID;
12240b57cec5SDimitry Andric case OPW256: return TTMP_256RegClassID;
12250b57cec5SDimitry Andric case OPW512: return TTMP_512RegClassID;
12260b57cec5SDimitry Andric }
12270b57cec5SDimitry Andric }
12280b57cec5SDimitry Andric
getTTmpIdx(unsigned Val) const12290b57cec5SDimitry Andric int AMDGPUDisassembler::getTTmpIdx(unsigned Val) const {
12300b57cec5SDimitry Andric using namespace AMDGPU::EncValues;
12310b57cec5SDimitry Andric
1232af732203SDimitry Andric unsigned TTmpMin = isGFX9Plus() ? TTMP_GFX9PLUS_MIN : TTMP_VI_MIN;
1233af732203SDimitry Andric unsigned TTmpMax = isGFX9Plus() ? TTMP_GFX9PLUS_MAX : TTMP_VI_MAX;
12340b57cec5SDimitry Andric
12350b57cec5SDimitry Andric return (TTmpMin <= Val && Val <= TTmpMax)? Val - TTmpMin : -1;
12360b57cec5SDimitry Andric }
12370b57cec5SDimitry Andric
decodeSrcOp(const OpWidthTy Width,unsigned Val) const12380b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSrcOp(const OpWidthTy Width, unsigned Val) const {
12390b57cec5SDimitry Andric using namespace AMDGPU::EncValues;
12400b57cec5SDimitry Andric
12410b57cec5SDimitry Andric assert(Val < 1024); // enum10
12420b57cec5SDimitry Andric
12430b57cec5SDimitry Andric bool IsAGPR = Val & 512;
12440b57cec5SDimitry Andric Val &= 511;
12450b57cec5SDimitry Andric
12460b57cec5SDimitry Andric if (VGPR_MIN <= Val && Val <= VGPR_MAX) {
12470b57cec5SDimitry Andric return createRegOperand(IsAGPR ? getAgprClassId(Width)
12480b57cec5SDimitry Andric : getVgprClassId(Width), Val - VGPR_MIN);
12490b57cec5SDimitry Andric }
12500b57cec5SDimitry Andric if (Val <= SGPR_MAX) {
1251af732203SDimitry Andric // "SGPR_MIN <= Val" is always true and causes compilation warning.
1252af732203SDimitry Andric static_assert(SGPR_MIN == 0, "");
12530b57cec5SDimitry Andric return createSRegOperand(getSgprClassId(Width), Val - SGPR_MIN);
12540b57cec5SDimitry Andric }
12550b57cec5SDimitry Andric
12560b57cec5SDimitry Andric int TTmpIdx = getTTmpIdx(Val);
12570b57cec5SDimitry Andric if (TTmpIdx >= 0) {
12580b57cec5SDimitry Andric return createSRegOperand(getTtmpClassId(Width), TTmpIdx);
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric
12610b57cec5SDimitry Andric if (INLINE_INTEGER_C_MIN <= Val && Val <= INLINE_INTEGER_C_MAX)
12620b57cec5SDimitry Andric return decodeIntImmed(Val);
12630b57cec5SDimitry Andric
12640b57cec5SDimitry Andric if (INLINE_FLOATING_C_MIN <= Val && Val <= INLINE_FLOATING_C_MAX)
12650b57cec5SDimitry Andric return decodeFPImmed(Width, Val);
12660b57cec5SDimitry Andric
12670b57cec5SDimitry Andric if (Val == LITERAL_CONST)
12680b57cec5SDimitry Andric return decodeLiteralConstant();
12690b57cec5SDimitry Andric
12700b57cec5SDimitry Andric switch (Width) {
12710b57cec5SDimitry Andric case OPW32:
12720b57cec5SDimitry Andric case OPW16:
12730b57cec5SDimitry Andric case OPWV216:
12740b57cec5SDimitry Andric return decodeSpecialReg32(Val);
12750b57cec5SDimitry Andric case OPW64:
1276*5f7ddb14SDimitry Andric case OPWV232:
12770b57cec5SDimitry Andric return decodeSpecialReg64(Val);
12780b57cec5SDimitry Andric default:
12790b57cec5SDimitry Andric llvm_unreachable("unexpected immediate type");
12800b57cec5SDimitry Andric }
12810b57cec5SDimitry Andric }
12820b57cec5SDimitry Andric
decodeDstOp(const OpWidthTy Width,unsigned Val) const12830b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeDstOp(const OpWidthTy Width, unsigned Val) const {
12840b57cec5SDimitry Andric using namespace AMDGPU::EncValues;
12850b57cec5SDimitry Andric
12860b57cec5SDimitry Andric assert(Val < 128);
12870b57cec5SDimitry Andric assert(Width == OPW256 || Width == OPW512);
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andric if (Val <= SGPR_MAX) {
1290af732203SDimitry Andric // "SGPR_MIN <= Val" is always true and causes compilation warning.
1291af732203SDimitry Andric static_assert(SGPR_MIN == 0, "");
12920b57cec5SDimitry Andric return createSRegOperand(getSgprClassId(Width), Val - SGPR_MIN);
12930b57cec5SDimitry Andric }
12940b57cec5SDimitry Andric
12950b57cec5SDimitry Andric int TTmpIdx = getTTmpIdx(Val);
12960b57cec5SDimitry Andric if (TTmpIdx >= 0) {
12970b57cec5SDimitry Andric return createSRegOperand(getTtmpClassId(Width), TTmpIdx);
12980b57cec5SDimitry Andric }
12990b57cec5SDimitry Andric
13000b57cec5SDimitry Andric llvm_unreachable("unknown dst register");
13010b57cec5SDimitry Andric }
13020b57cec5SDimitry Andric
decodeSpecialReg32(unsigned Val) const13030b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSpecialReg32(unsigned Val) const {
13040b57cec5SDimitry Andric using namespace AMDGPU;
13050b57cec5SDimitry Andric
13060b57cec5SDimitry Andric switch (Val) {
13070b57cec5SDimitry Andric case 102: return createRegOperand(FLAT_SCR_LO);
13080b57cec5SDimitry Andric case 103: return createRegOperand(FLAT_SCR_HI);
13090b57cec5SDimitry Andric case 104: return createRegOperand(XNACK_MASK_LO);
13100b57cec5SDimitry Andric case 105: return createRegOperand(XNACK_MASK_HI);
13110b57cec5SDimitry Andric case 106: return createRegOperand(VCC_LO);
13120b57cec5SDimitry Andric case 107: return createRegOperand(VCC_HI);
13130b57cec5SDimitry Andric case 108: return createRegOperand(TBA_LO);
13140b57cec5SDimitry Andric case 109: return createRegOperand(TBA_HI);
13150b57cec5SDimitry Andric case 110: return createRegOperand(TMA_LO);
13160b57cec5SDimitry Andric case 111: return createRegOperand(TMA_HI);
13170b57cec5SDimitry Andric case 124: return createRegOperand(M0);
13180b57cec5SDimitry Andric case 125: return createRegOperand(SGPR_NULL);
13190b57cec5SDimitry Andric case 126: return createRegOperand(EXEC_LO);
13200b57cec5SDimitry Andric case 127: return createRegOperand(EXEC_HI);
13210b57cec5SDimitry Andric case 235: return createRegOperand(SRC_SHARED_BASE);
13220b57cec5SDimitry Andric case 236: return createRegOperand(SRC_SHARED_LIMIT);
13230b57cec5SDimitry Andric case 237: return createRegOperand(SRC_PRIVATE_BASE);
13240b57cec5SDimitry Andric case 238: return createRegOperand(SRC_PRIVATE_LIMIT);
13250b57cec5SDimitry Andric case 239: return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
13260b57cec5SDimitry Andric case 251: return createRegOperand(SRC_VCCZ);
13270b57cec5SDimitry Andric case 252: return createRegOperand(SRC_EXECZ);
13280b57cec5SDimitry Andric case 253: return createRegOperand(SRC_SCC);
13290b57cec5SDimitry Andric case 254: return createRegOperand(LDS_DIRECT);
13300b57cec5SDimitry Andric default: break;
13310b57cec5SDimitry Andric }
13320b57cec5SDimitry Andric return errOperand(Val, "unknown operand encoding " + Twine(Val));
13330b57cec5SDimitry Andric }
13340b57cec5SDimitry Andric
decodeSpecialReg64(unsigned Val) const13350b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSpecialReg64(unsigned Val) const {
13360b57cec5SDimitry Andric using namespace AMDGPU;
13370b57cec5SDimitry Andric
13380b57cec5SDimitry Andric switch (Val) {
13390b57cec5SDimitry Andric case 102: return createRegOperand(FLAT_SCR);
13400b57cec5SDimitry Andric case 104: return createRegOperand(XNACK_MASK);
13410b57cec5SDimitry Andric case 106: return createRegOperand(VCC);
13420b57cec5SDimitry Andric case 108: return createRegOperand(TBA);
13430b57cec5SDimitry Andric case 110: return createRegOperand(TMA);
13448bcb0991SDimitry Andric case 125: return createRegOperand(SGPR_NULL);
13450b57cec5SDimitry Andric case 126: return createRegOperand(EXEC);
13460b57cec5SDimitry Andric case 235: return createRegOperand(SRC_SHARED_BASE);
13470b57cec5SDimitry Andric case 236: return createRegOperand(SRC_SHARED_LIMIT);
13480b57cec5SDimitry Andric case 237: return createRegOperand(SRC_PRIVATE_BASE);
13490b57cec5SDimitry Andric case 238: return createRegOperand(SRC_PRIVATE_LIMIT);
13500b57cec5SDimitry Andric case 239: return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
13510b57cec5SDimitry Andric case 251: return createRegOperand(SRC_VCCZ);
13520b57cec5SDimitry Andric case 252: return createRegOperand(SRC_EXECZ);
13530b57cec5SDimitry Andric case 253: return createRegOperand(SRC_SCC);
13540b57cec5SDimitry Andric default: break;
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric return errOperand(Val, "unknown operand encoding " + Twine(Val));
13570b57cec5SDimitry Andric }
13580b57cec5SDimitry Andric
decodeSDWASrc(const OpWidthTy Width,const unsigned Val) const13590b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSDWASrc(const OpWidthTy Width,
13600b57cec5SDimitry Andric const unsigned Val) const {
13610b57cec5SDimitry Andric using namespace AMDGPU::SDWA;
13620b57cec5SDimitry Andric using namespace AMDGPU::EncValues;
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric if (STI.getFeatureBits()[AMDGPU::FeatureGFX9] ||
13650b57cec5SDimitry Andric STI.getFeatureBits()[AMDGPU::FeatureGFX10]) {
13660b57cec5SDimitry Andric // XXX: cast to int is needed to avoid stupid warning:
13670b57cec5SDimitry Andric // compare with unsigned is always true
13680b57cec5SDimitry Andric if (int(SDWA9EncValues::SRC_VGPR_MIN) <= int(Val) &&
13690b57cec5SDimitry Andric Val <= SDWA9EncValues::SRC_VGPR_MAX) {
13700b57cec5SDimitry Andric return createRegOperand(getVgprClassId(Width),
13710b57cec5SDimitry Andric Val - SDWA9EncValues::SRC_VGPR_MIN);
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric if (SDWA9EncValues::SRC_SGPR_MIN <= Val &&
1374af732203SDimitry Andric Val <= (isGFX10Plus() ? SDWA9EncValues::SRC_SGPR_MAX_GFX10
13750b57cec5SDimitry Andric : SDWA9EncValues::SRC_SGPR_MAX_SI)) {
13760b57cec5SDimitry Andric return createSRegOperand(getSgprClassId(Width),
13770b57cec5SDimitry Andric Val - SDWA9EncValues::SRC_SGPR_MIN);
13780b57cec5SDimitry Andric }
13790b57cec5SDimitry Andric if (SDWA9EncValues::SRC_TTMP_MIN <= Val &&
13800b57cec5SDimitry Andric Val <= SDWA9EncValues::SRC_TTMP_MAX) {
13810b57cec5SDimitry Andric return createSRegOperand(getTtmpClassId(Width),
13820b57cec5SDimitry Andric Val - SDWA9EncValues::SRC_TTMP_MIN);
13830b57cec5SDimitry Andric }
13840b57cec5SDimitry Andric
13850b57cec5SDimitry Andric const unsigned SVal = Val - SDWA9EncValues::SRC_SGPR_MIN;
13860b57cec5SDimitry Andric
13870b57cec5SDimitry Andric if (INLINE_INTEGER_C_MIN <= SVal && SVal <= INLINE_INTEGER_C_MAX)
13880b57cec5SDimitry Andric return decodeIntImmed(SVal);
13890b57cec5SDimitry Andric
13900b57cec5SDimitry Andric if (INLINE_FLOATING_C_MIN <= SVal && SVal <= INLINE_FLOATING_C_MAX)
13910b57cec5SDimitry Andric return decodeFPImmed(Width, SVal);
13920b57cec5SDimitry Andric
13930b57cec5SDimitry Andric return decodeSpecialReg32(SVal);
13940b57cec5SDimitry Andric } else if (STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands]) {
13950b57cec5SDimitry Andric return createRegOperand(getVgprClassId(Width), Val);
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric llvm_unreachable("unsupported target");
13980b57cec5SDimitry Andric }
13990b57cec5SDimitry Andric
decodeSDWASrc16(unsigned Val) const14000b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSDWASrc16(unsigned Val) const {
14010b57cec5SDimitry Andric return decodeSDWASrc(OPW16, Val);
14020b57cec5SDimitry Andric }
14030b57cec5SDimitry Andric
decodeSDWASrc32(unsigned Val) const14040b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSDWASrc32(unsigned Val) const {
14050b57cec5SDimitry Andric return decodeSDWASrc(OPW32, Val);
14060b57cec5SDimitry Andric }
14070b57cec5SDimitry Andric
decodeSDWAVopcDst(unsigned Val) const14080b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeSDWAVopcDst(unsigned Val) const {
14090b57cec5SDimitry Andric using namespace AMDGPU::SDWA;
14100b57cec5SDimitry Andric
14110b57cec5SDimitry Andric assert((STI.getFeatureBits()[AMDGPU::FeatureGFX9] ||
14120b57cec5SDimitry Andric STI.getFeatureBits()[AMDGPU::FeatureGFX10]) &&
14130b57cec5SDimitry Andric "SDWAVopcDst should be present only on GFX9+");
14140b57cec5SDimitry Andric
14150b57cec5SDimitry Andric bool IsWave64 = STI.getFeatureBits()[AMDGPU::FeatureWavefrontSize64];
14160b57cec5SDimitry Andric
14170b57cec5SDimitry Andric if (Val & SDWA9EncValues::VOPC_DST_VCC_MASK) {
14180b57cec5SDimitry Andric Val &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
14190b57cec5SDimitry Andric
14200b57cec5SDimitry Andric int TTmpIdx = getTTmpIdx(Val);
14210b57cec5SDimitry Andric if (TTmpIdx >= 0) {
14228bcb0991SDimitry Andric auto TTmpClsId = getTtmpClassId(IsWave64 ? OPW64 : OPW32);
14238bcb0991SDimitry Andric return createSRegOperand(TTmpClsId, TTmpIdx);
14240b57cec5SDimitry Andric } else if (Val > SGPR_MAX) {
14250b57cec5SDimitry Andric return IsWave64 ? decodeSpecialReg64(Val)
14260b57cec5SDimitry Andric : decodeSpecialReg32(Val);
14270b57cec5SDimitry Andric } else {
14280b57cec5SDimitry Andric return createSRegOperand(getSgprClassId(IsWave64 ? OPW64 : OPW32), Val);
14290b57cec5SDimitry Andric }
14300b57cec5SDimitry Andric } else {
14310b57cec5SDimitry Andric return createRegOperand(IsWave64 ? AMDGPU::VCC : AMDGPU::VCC_LO);
14320b57cec5SDimitry Andric }
14330b57cec5SDimitry Andric }
14340b57cec5SDimitry Andric
decodeBoolReg(unsigned Val) const14350b57cec5SDimitry Andric MCOperand AMDGPUDisassembler::decodeBoolReg(unsigned Val) const {
14360b57cec5SDimitry Andric return STI.getFeatureBits()[AMDGPU::FeatureWavefrontSize64] ?
14370b57cec5SDimitry Andric decodeOperand_SReg_64(Val) : decodeOperand_SReg_32(Val);
14380b57cec5SDimitry Andric }
14390b57cec5SDimitry Andric
isVI() const14400b57cec5SDimitry Andric bool AMDGPUDisassembler::isVI() const {
14410b57cec5SDimitry Andric return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
14420b57cec5SDimitry Andric }
14430b57cec5SDimitry Andric
isGFX9() const1444af732203SDimitry Andric bool AMDGPUDisassembler::isGFX9() const { return AMDGPU::isGFX9(STI); }
1445af732203SDimitry Andric
isGFX90A() const1446*5f7ddb14SDimitry Andric bool AMDGPUDisassembler::isGFX90A() const {
1447*5f7ddb14SDimitry Andric return STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts];
1448*5f7ddb14SDimitry Andric }
1449*5f7ddb14SDimitry Andric
isGFX9Plus() const1450af732203SDimitry Andric bool AMDGPUDisassembler::isGFX9Plus() const { return AMDGPU::isGFX9Plus(STI); }
1451af732203SDimitry Andric
isGFX10() const1452af732203SDimitry Andric bool AMDGPUDisassembler::isGFX10() const { return AMDGPU::isGFX10(STI); }
1453af732203SDimitry Andric
isGFX10Plus() const1454af732203SDimitry Andric bool AMDGPUDisassembler::isGFX10Plus() const {
1455af732203SDimitry Andric return AMDGPU::isGFX10Plus(STI);
14560b57cec5SDimitry Andric }
14570b57cec5SDimitry Andric
hasArchitectedFlatScratch() const1458*5f7ddb14SDimitry Andric bool AMDGPUDisassembler::hasArchitectedFlatScratch() const {
1459*5f7ddb14SDimitry Andric return STI.getFeatureBits()[AMDGPU::FeatureArchitectedFlatScratch];
1460*5f7ddb14SDimitry Andric }
1461*5f7ddb14SDimitry Andric
1462af732203SDimitry Andric //===----------------------------------------------------------------------===//
1463af732203SDimitry Andric // AMDGPU specific symbol handling
1464af732203SDimitry Andric //===----------------------------------------------------------------------===//
1465af732203SDimitry Andric #define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
1466af732203SDimitry Andric do { \
1467af732203SDimitry Andric KdStream << Indent << DIRECTIVE " " \
1468af732203SDimitry Andric << ((FourByteBuffer & MASK) >> (MASK##_SHIFT)) << '\n'; \
1469af732203SDimitry Andric } while (0)
1470af732203SDimitry Andric
1471af732203SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
decodeCOMPUTE_PGM_RSRC1(uint32_t FourByteBuffer,raw_string_ostream & KdStream) const1472af732203SDimitry Andric MCDisassembler::DecodeStatus AMDGPUDisassembler::decodeCOMPUTE_PGM_RSRC1(
1473af732203SDimitry Andric uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
1474af732203SDimitry Andric using namespace amdhsa;
1475af732203SDimitry Andric StringRef Indent = "\t";
1476af732203SDimitry Andric
1477af732203SDimitry Andric // We cannot accurately backward compute #VGPRs used from
1478af732203SDimitry Andric // GRANULATED_WORKITEM_VGPR_COUNT. But we are concerned with getting the same
1479af732203SDimitry Andric // value of GRANULATED_WORKITEM_VGPR_COUNT in the reassembled binary. So we
1480af732203SDimitry Andric // simply calculate the inverse of what the assembler does.
1481af732203SDimitry Andric
1482af732203SDimitry Andric uint32_t GranulatedWorkitemVGPRCount =
1483af732203SDimitry Andric (FourByteBuffer & COMPUTE_PGM_RSRC1_GRANULATED_WORKITEM_VGPR_COUNT) >>
1484af732203SDimitry Andric COMPUTE_PGM_RSRC1_GRANULATED_WORKITEM_VGPR_COUNT_SHIFT;
1485af732203SDimitry Andric
1486af732203SDimitry Andric uint32_t NextFreeVGPR = (GranulatedWorkitemVGPRCount + 1) *
1487af732203SDimitry Andric AMDGPU::IsaInfo::getVGPREncodingGranule(&STI);
1488af732203SDimitry Andric
1489af732203SDimitry Andric KdStream << Indent << ".amdhsa_next_free_vgpr " << NextFreeVGPR << '\n';
1490af732203SDimitry Andric
1491af732203SDimitry Andric // We cannot backward compute values used to calculate
1492af732203SDimitry Andric // GRANULATED_WAVEFRONT_SGPR_COUNT. Hence the original values for following
1493af732203SDimitry Andric // directives can't be computed:
1494af732203SDimitry Andric // .amdhsa_reserve_vcc
1495af732203SDimitry Andric // .amdhsa_reserve_flat_scratch
1496af732203SDimitry Andric // .amdhsa_reserve_xnack_mask
1497af732203SDimitry Andric // They take their respective default values if not specified in the assembly.
1498af732203SDimitry Andric //
1499af732203SDimitry Andric // GRANULATED_WAVEFRONT_SGPR_COUNT
1500af732203SDimitry Andric // = f(NEXT_FREE_SGPR + VCC + FLAT_SCRATCH + XNACK_MASK)
1501af732203SDimitry Andric //
1502af732203SDimitry Andric // We compute the inverse as though all directives apart from NEXT_FREE_SGPR
1503af732203SDimitry Andric // are set to 0. So while disassembling we consider that:
1504af732203SDimitry Andric //
1505af732203SDimitry Andric // GRANULATED_WAVEFRONT_SGPR_COUNT
1506af732203SDimitry Andric // = f(NEXT_FREE_SGPR + 0 + 0 + 0)
1507af732203SDimitry Andric //
1508af732203SDimitry Andric // The disassembler cannot recover the original values of those 3 directives.
1509af732203SDimitry Andric
1510af732203SDimitry Andric uint32_t GranulatedWavefrontSGPRCount =
1511af732203SDimitry Andric (FourByteBuffer & COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT) >>
1512af732203SDimitry Andric COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT_SHIFT;
1513af732203SDimitry Andric
1514af732203SDimitry Andric if (isGFX10Plus() && GranulatedWavefrontSGPRCount)
1515af732203SDimitry Andric return MCDisassembler::Fail;
1516af732203SDimitry Andric
1517af732203SDimitry Andric uint32_t NextFreeSGPR = (GranulatedWavefrontSGPRCount + 1) *
1518af732203SDimitry Andric AMDGPU::IsaInfo::getSGPREncodingGranule(&STI);
1519af732203SDimitry Andric
1520af732203SDimitry Andric KdStream << Indent << ".amdhsa_reserve_vcc " << 0 << '\n';
1521*5f7ddb14SDimitry Andric if (!hasArchitectedFlatScratch())
1522af732203SDimitry Andric KdStream << Indent << ".amdhsa_reserve_flat_scratch " << 0 << '\n';
1523af732203SDimitry Andric KdStream << Indent << ".amdhsa_reserve_xnack_mask " << 0 << '\n';
1524af732203SDimitry Andric KdStream << Indent << ".amdhsa_next_free_sgpr " << NextFreeSGPR << "\n";
1525af732203SDimitry Andric
1526af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_PRIORITY)
1527af732203SDimitry Andric return MCDisassembler::Fail;
1528af732203SDimitry Andric
1529af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_float_round_mode_32",
1530af732203SDimitry Andric COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_32);
1531af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_float_round_mode_16_64",
1532af732203SDimitry Andric COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_16_64);
1533af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_32",
1534af732203SDimitry Andric COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_32);
1535af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_16_64",
1536af732203SDimitry Andric COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64);
1537af732203SDimitry Andric
1538af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_PRIV)
1539af732203SDimitry Andric return MCDisassembler::Fail;
1540af732203SDimitry Andric
1541af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_dx10_clamp", COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP);
1542af732203SDimitry Andric
1543af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_DEBUG_MODE)
1544af732203SDimitry Andric return MCDisassembler::Fail;
1545af732203SDimitry Andric
1546af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_ieee_mode", COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE);
1547af732203SDimitry Andric
1548af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_BULKY)
1549af732203SDimitry Andric return MCDisassembler::Fail;
1550af732203SDimitry Andric
1551af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_CDBG_USER)
1552af732203SDimitry Andric return MCDisassembler::Fail;
1553af732203SDimitry Andric
1554af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_fp16_overflow", COMPUTE_PGM_RSRC1_FP16_OVFL);
1555af732203SDimitry Andric
1556af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC1_RESERVED0)
1557af732203SDimitry Andric return MCDisassembler::Fail;
1558af732203SDimitry Andric
1559af732203SDimitry Andric if (isGFX10Plus()) {
1560af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_workgroup_processor_mode",
1561af732203SDimitry Andric COMPUTE_PGM_RSRC1_WGP_MODE);
1562af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_memory_ordered", COMPUTE_PGM_RSRC1_MEM_ORDERED);
1563af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_forward_progress", COMPUTE_PGM_RSRC1_FWD_PROGRESS);
1564af732203SDimitry Andric }
1565af732203SDimitry Andric return MCDisassembler::Success;
1566af732203SDimitry Andric }
1567af732203SDimitry Andric
1568af732203SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
decodeCOMPUTE_PGM_RSRC2(uint32_t FourByteBuffer,raw_string_ostream & KdStream) const1569af732203SDimitry Andric MCDisassembler::DecodeStatus AMDGPUDisassembler::decodeCOMPUTE_PGM_RSRC2(
1570af732203SDimitry Andric uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
1571af732203SDimitry Andric using namespace amdhsa;
1572af732203SDimitry Andric StringRef Indent = "\t";
1573*5f7ddb14SDimitry Andric if (hasArchitectedFlatScratch())
1574*5f7ddb14SDimitry Andric PRINT_DIRECTIVE(".amdhsa_enable_private_segment",
1575*5f7ddb14SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
1576*5f7ddb14SDimitry Andric else
1577*5f7ddb14SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_sgpr_private_segment_wavefront_offset",
1578af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
1579af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_x",
1580af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X);
1581af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_y",
1582af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Y);
1583af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_z",
1584af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Z);
1585af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_info",
1586af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_INFO);
1587af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_system_vgpr_workitem_id",
1588af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_VGPR_WORKITEM_ID);
1589af732203SDimitry Andric
1590af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_ADDRESS_WATCH)
1591af732203SDimitry Andric return MCDisassembler::Fail;
1592af732203SDimitry Andric
1593af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_MEMORY)
1594af732203SDimitry Andric return MCDisassembler::Fail;
1595af732203SDimitry Andric
1596af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC2_GRANULATED_LDS_SIZE)
1597af732203SDimitry Andric return MCDisassembler::Fail;
1598af732203SDimitry Andric
1599af732203SDimitry Andric PRINT_DIRECTIVE(
1600af732203SDimitry Andric ".amdhsa_exception_fp_ieee_invalid_op",
1601af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION);
1602af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_exception_fp_denorm_src",
1603af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_FP_DENORMAL_SOURCE);
1604af732203SDimitry Andric PRINT_DIRECTIVE(
1605af732203SDimitry Andric ".amdhsa_exception_fp_ieee_div_zero",
1606af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO);
1607af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_overflow",
1608af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW);
1609af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_underflow",
1610af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW);
1611af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_inexact",
1612af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INEXACT);
1613af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_exception_int_div_zero",
1614af732203SDimitry Andric COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO);
1615af732203SDimitry Andric
1616af732203SDimitry Andric if (FourByteBuffer & COMPUTE_PGM_RSRC2_RESERVED0)
1617af732203SDimitry Andric return MCDisassembler::Fail;
1618af732203SDimitry Andric
1619af732203SDimitry Andric return MCDisassembler::Success;
1620af732203SDimitry Andric }
1621af732203SDimitry Andric
1622af732203SDimitry Andric #undef PRINT_DIRECTIVE
1623af732203SDimitry Andric
1624af732203SDimitry Andric MCDisassembler::DecodeStatus
decodeKernelDescriptorDirective(DataExtractor::Cursor & Cursor,ArrayRef<uint8_t> Bytes,raw_string_ostream & KdStream) const1625af732203SDimitry Andric AMDGPUDisassembler::decodeKernelDescriptorDirective(
1626af732203SDimitry Andric DataExtractor::Cursor &Cursor, ArrayRef<uint8_t> Bytes,
1627af732203SDimitry Andric raw_string_ostream &KdStream) const {
1628af732203SDimitry Andric #define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
1629af732203SDimitry Andric do { \
1630af732203SDimitry Andric KdStream << Indent << DIRECTIVE " " \
1631af732203SDimitry Andric << ((TwoByteBuffer & MASK) >> (MASK##_SHIFT)) << '\n'; \
1632af732203SDimitry Andric } while (0)
1633af732203SDimitry Andric
1634af732203SDimitry Andric uint16_t TwoByteBuffer = 0;
1635af732203SDimitry Andric uint32_t FourByteBuffer = 0;
1636af732203SDimitry Andric
1637af732203SDimitry Andric StringRef ReservedBytes;
1638af732203SDimitry Andric StringRef Indent = "\t";
1639af732203SDimitry Andric
1640af732203SDimitry Andric assert(Bytes.size() == 64);
1641af732203SDimitry Andric DataExtractor DE(Bytes, /*IsLittleEndian=*/true, /*AddressSize=*/8);
1642af732203SDimitry Andric
1643af732203SDimitry Andric switch (Cursor.tell()) {
1644af732203SDimitry Andric case amdhsa::GROUP_SEGMENT_FIXED_SIZE_OFFSET:
1645af732203SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1646af732203SDimitry Andric KdStream << Indent << ".amdhsa_group_segment_fixed_size " << FourByteBuffer
1647af732203SDimitry Andric << '\n';
1648af732203SDimitry Andric return MCDisassembler::Success;
1649af732203SDimitry Andric
1650af732203SDimitry Andric case amdhsa::PRIVATE_SEGMENT_FIXED_SIZE_OFFSET:
1651af732203SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1652af732203SDimitry Andric KdStream << Indent << ".amdhsa_private_segment_fixed_size "
1653af732203SDimitry Andric << FourByteBuffer << '\n';
1654af732203SDimitry Andric return MCDisassembler::Success;
1655af732203SDimitry Andric
1656*5f7ddb14SDimitry Andric case amdhsa::KERNARG_SIZE_OFFSET:
1657*5f7ddb14SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1658*5f7ddb14SDimitry Andric KdStream << Indent << ".amdhsa_kernarg_size "
1659*5f7ddb14SDimitry Andric << FourByteBuffer << '\n';
1660*5f7ddb14SDimitry Andric return MCDisassembler::Success;
1661*5f7ddb14SDimitry Andric
1662af732203SDimitry Andric case amdhsa::RESERVED0_OFFSET:
1663*5f7ddb14SDimitry Andric // 4 reserved bytes, must be 0.
1664*5f7ddb14SDimitry Andric ReservedBytes = DE.getBytes(Cursor, 4);
1665*5f7ddb14SDimitry Andric for (int I = 0; I < 4; ++I) {
1666*5f7ddb14SDimitry Andric if (ReservedBytes[I] != 0) {
1667af732203SDimitry Andric return MCDisassembler::Fail;
1668af732203SDimitry Andric }
1669*5f7ddb14SDimitry Andric }
1670af732203SDimitry Andric return MCDisassembler::Success;
1671af732203SDimitry Andric
1672af732203SDimitry Andric case amdhsa::KERNEL_CODE_ENTRY_BYTE_OFFSET_OFFSET:
1673af732203SDimitry Andric // KERNEL_CODE_ENTRY_BYTE_OFFSET
1674af732203SDimitry Andric // So far no directive controls this for Code Object V3, so simply skip for
1675af732203SDimitry Andric // disassembly.
1676af732203SDimitry Andric DE.skip(Cursor, 8);
1677af732203SDimitry Andric return MCDisassembler::Success;
1678af732203SDimitry Andric
1679af732203SDimitry Andric case amdhsa::RESERVED1_OFFSET:
1680af732203SDimitry Andric // 20 reserved bytes, must be 0.
1681af732203SDimitry Andric ReservedBytes = DE.getBytes(Cursor, 20);
1682af732203SDimitry Andric for (int I = 0; I < 20; ++I) {
1683af732203SDimitry Andric if (ReservedBytes[I] != 0) {
1684af732203SDimitry Andric return MCDisassembler::Fail;
1685af732203SDimitry Andric }
1686af732203SDimitry Andric }
1687af732203SDimitry Andric return MCDisassembler::Success;
1688af732203SDimitry Andric
1689af732203SDimitry Andric case amdhsa::COMPUTE_PGM_RSRC3_OFFSET:
1690af732203SDimitry Andric // COMPUTE_PGM_RSRC3
1691af732203SDimitry Andric // - Only set for GFX10, GFX6-9 have this to be 0.
1692af732203SDimitry Andric // - Currently no directives directly control this.
1693af732203SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1694af732203SDimitry Andric if (!isGFX10Plus() && FourByteBuffer) {
1695af732203SDimitry Andric return MCDisassembler::Fail;
1696af732203SDimitry Andric }
1697af732203SDimitry Andric return MCDisassembler::Success;
1698af732203SDimitry Andric
1699af732203SDimitry Andric case amdhsa::COMPUTE_PGM_RSRC1_OFFSET:
1700af732203SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1701af732203SDimitry Andric if (decodeCOMPUTE_PGM_RSRC1(FourByteBuffer, KdStream) ==
1702af732203SDimitry Andric MCDisassembler::Fail) {
1703af732203SDimitry Andric return MCDisassembler::Fail;
1704af732203SDimitry Andric }
1705af732203SDimitry Andric return MCDisassembler::Success;
1706af732203SDimitry Andric
1707af732203SDimitry Andric case amdhsa::COMPUTE_PGM_RSRC2_OFFSET:
1708af732203SDimitry Andric FourByteBuffer = DE.getU32(Cursor);
1709af732203SDimitry Andric if (decodeCOMPUTE_PGM_RSRC2(FourByteBuffer, KdStream) ==
1710af732203SDimitry Andric MCDisassembler::Fail) {
1711af732203SDimitry Andric return MCDisassembler::Fail;
1712af732203SDimitry Andric }
1713af732203SDimitry Andric return MCDisassembler::Success;
1714af732203SDimitry Andric
1715af732203SDimitry Andric case amdhsa::KERNEL_CODE_PROPERTIES_OFFSET:
1716af732203SDimitry Andric using namespace amdhsa;
1717af732203SDimitry Andric TwoByteBuffer = DE.getU16(Cursor);
1718af732203SDimitry Andric
1719*5f7ddb14SDimitry Andric if (!hasArchitectedFlatScratch())
1720af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_buffer",
1721af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER);
1722af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_ptr",
1723af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR);
1724af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_queue_ptr",
1725af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR);
1726af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_segment_ptr",
1727af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR);
1728af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_id",
1729af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID);
1730*5f7ddb14SDimitry Andric if (!hasArchitectedFlatScratch())
1731af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_flat_scratch_init",
1732af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT);
1733af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_size",
1734af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE);
1735af732203SDimitry Andric
1736af732203SDimitry Andric if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED0)
1737af732203SDimitry Andric return MCDisassembler::Fail;
1738af732203SDimitry Andric
1739af732203SDimitry Andric // Reserved for GFX9
1740af732203SDimitry Andric if (isGFX9() &&
1741af732203SDimitry Andric (TwoByteBuffer & KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32)) {
1742af732203SDimitry Andric return MCDisassembler::Fail;
1743af732203SDimitry Andric } else if (isGFX10Plus()) {
1744af732203SDimitry Andric PRINT_DIRECTIVE(".amdhsa_wavefront_size32",
1745af732203SDimitry Andric KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32);
1746af732203SDimitry Andric }
1747af732203SDimitry Andric
1748af732203SDimitry Andric if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED1)
1749af732203SDimitry Andric return MCDisassembler::Fail;
1750af732203SDimitry Andric
1751af732203SDimitry Andric return MCDisassembler::Success;
1752af732203SDimitry Andric
1753af732203SDimitry Andric case amdhsa::RESERVED2_OFFSET:
1754af732203SDimitry Andric // 6 bytes from here are reserved, must be 0.
1755af732203SDimitry Andric ReservedBytes = DE.getBytes(Cursor, 6);
1756af732203SDimitry Andric for (int I = 0; I < 6; ++I) {
1757af732203SDimitry Andric if (ReservedBytes[I] != 0)
1758af732203SDimitry Andric return MCDisassembler::Fail;
1759af732203SDimitry Andric }
1760af732203SDimitry Andric return MCDisassembler::Success;
1761af732203SDimitry Andric
1762af732203SDimitry Andric default:
1763af732203SDimitry Andric llvm_unreachable("Unhandled index. Case statements cover everything.");
1764af732203SDimitry Andric return MCDisassembler::Fail;
1765af732203SDimitry Andric }
1766af732203SDimitry Andric #undef PRINT_DIRECTIVE
1767af732203SDimitry Andric }
1768af732203SDimitry Andric
decodeKernelDescriptor(StringRef KdName,ArrayRef<uint8_t> Bytes,uint64_t KdAddress) const1769af732203SDimitry Andric MCDisassembler::DecodeStatus AMDGPUDisassembler::decodeKernelDescriptor(
1770af732203SDimitry Andric StringRef KdName, ArrayRef<uint8_t> Bytes, uint64_t KdAddress) const {
1771af732203SDimitry Andric // CP microcode requires the kernel descriptor to be 64 aligned.
1772af732203SDimitry Andric if (Bytes.size() != 64 || KdAddress % 64 != 0)
1773af732203SDimitry Andric return MCDisassembler::Fail;
1774af732203SDimitry Andric
1775af732203SDimitry Andric std::string Kd;
1776af732203SDimitry Andric raw_string_ostream KdStream(Kd);
1777af732203SDimitry Andric KdStream << ".amdhsa_kernel " << KdName << '\n';
1778af732203SDimitry Andric
1779af732203SDimitry Andric DataExtractor::Cursor C(0);
1780af732203SDimitry Andric while (C && C.tell() < Bytes.size()) {
1781af732203SDimitry Andric MCDisassembler::DecodeStatus Status =
1782af732203SDimitry Andric decodeKernelDescriptorDirective(C, Bytes, KdStream);
1783af732203SDimitry Andric
1784af732203SDimitry Andric cantFail(C.takeError());
1785af732203SDimitry Andric
1786af732203SDimitry Andric if (Status == MCDisassembler::Fail)
1787af732203SDimitry Andric return MCDisassembler::Fail;
1788af732203SDimitry Andric }
1789af732203SDimitry Andric KdStream << ".end_amdhsa_kernel\n";
1790af732203SDimitry Andric outs() << KdStream.str();
1791af732203SDimitry Andric return MCDisassembler::Success;
1792af732203SDimitry Andric }
1793af732203SDimitry Andric
1794af732203SDimitry Andric Optional<MCDisassembler::DecodeStatus>
onSymbolStart(SymbolInfoTy & Symbol,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const1795af732203SDimitry Andric AMDGPUDisassembler::onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
1796af732203SDimitry Andric ArrayRef<uint8_t> Bytes, uint64_t Address,
1797af732203SDimitry Andric raw_ostream &CStream) const {
1798af732203SDimitry Andric // Right now only kernel descriptor needs to be handled.
1799af732203SDimitry Andric // We ignore all other symbols for target specific handling.
1800af732203SDimitry Andric // TODO:
1801af732203SDimitry Andric // Fix the spurious symbol issue for AMDGPU kernels. Exists for both Code
1802af732203SDimitry Andric // Object V2 and V3 when symbols are marked protected.
1803af732203SDimitry Andric
1804af732203SDimitry Andric // amd_kernel_code_t for Code Object V2.
1805af732203SDimitry Andric if (Symbol.Type == ELF::STT_AMDGPU_HSA_KERNEL) {
1806af732203SDimitry Andric Size = 256;
1807af732203SDimitry Andric return MCDisassembler::Fail;
1808af732203SDimitry Andric }
1809af732203SDimitry Andric
1810af732203SDimitry Andric // Code Object V3 kernel descriptors.
1811af732203SDimitry Andric StringRef Name = Symbol.Name;
1812af732203SDimitry Andric if (Symbol.Type == ELF::STT_OBJECT && Name.endswith(StringRef(".kd"))) {
1813af732203SDimitry Andric Size = 64; // Size = 64 regardless of success or failure.
1814af732203SDimitry Andric return decodeKernelDescriptor(Name.drop_back(3), Bytes, Address);
1815af732203SDimitry Andric }
1816af732203SDimitry Andric return None;
18170b57cec5SDimitry Andric }
18180b57cec5SDimitry Andric
18190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18200b57cec5SDimitry Andric // AMDGPUSymbolizer
18210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18220b57cec5SDimitry Andric
18230b57cec5SDimitry Andric // Try to find symbol name for specified label
tryAddingSymbolicOperand(MCInst & Inst,raw_ostream &,int64_t Value,uint64_t,bool IsBranch,uint64_t,uint64_t)18240b57cec5SDimitry Andric bool AMDGPUSymbolizer::tryAddingSymbolicOperand(MCInst &Inst,
18250b57cec5SDimitry Andric raw_ostream &/*cStream*/, int64_t Value,
18260b57cec5SDimitry Andric uint64_t /*Address*/, bool IsBranch,
18270b57cec5SDimitry Andric uint64_t /*Offset*/, uint64_t /*InstSize*/) {
18280b57cec5SDimitry Andric
18290b57cec5SDimitry Andric if (!IsBranch) {
18300b57cec5SDimitry Andric return false;
18310b57cec5SDimitry Andric }
18320b57cec5SDimitry Andric
18330b57cec5SDimitry Andric auto *Symbols = static_cast<SectionSymbolsTy *>(DisInfo);
18340b57cec5SDimitry Andric if (!Symbols)
18350b57cec5SDimitry Andric return false;
18360b57cec5SDimitry Andric
1837af732203SDimitry Andric auto Result = llvm::find_if(*Symbols, [Value](const SymbolInfoTy &Val) {
1838af732203SDimitry Andric return Val.Addr == static_cast<uint64_t>(Value) &&
1839af732203SDimitry Andric Val.Type == ELF::STT_NOTYPE;
18400b57cec5SDimitry Andric });
18410b57cec5SDimitry Andric if (Result != Symbols->end()) {
18425ffd83dbSDimitry Andric auto *Sym = Ctx.getOrCreateSymbol(Result->Name);
18430b57cec5SDimitry Andric const auto *Add = MCSymbolRefExpr::create(Sym, Ctx);
18440b57cec5SDimitry Andric Inst.addOperand(MCOperand::createExpr(Add));
18450b57cec5SDimitry Andric return true;
18460b57cec5SDimitry Andric }
1847*5f7ddb14SDimitry Andric // Add to list of referenced addresses, so caller can synthesize a label.
1848*5f7ddb14SDimitry Andric ReferencedAddresses.push_back(static_cast<uint64_t>(Value));
18490b57cec5SDimitry Andric return false;
18500b57cec5SDimitry Andric }
18510b57cec5SDimitry Andric
tryAddingPcLoadReferenceComment(raw_ostream & cStream,int64_t Value,uint64_t Address)18520b57cec5SDimitry Andric void AMDGPUSymbolizer::tryAddingPcLoadReferenceComment(raw_ostream &cStream,
18530b57cec5SDimitry Andric int64_t Value,
18540b57cec5SDimitry Andric uint64_t Address) {
18550b57cec5SDimitry Andric llvm_unreachable("unimplemented");
18560b57cec5SDimitry Andric }
18570b57cec5SDimitry Andric
18580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18590b57cec5SDimitry Andric // Initialization
18600b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18610b57cec5SDimitry Andric
createAMDGPUSymbolizer(const Triple &,LLVMOpInfoCallback,LLVMSymbolLookupCallback,void * DisInfo,MCContext * Ctx,std::unique_ptr<MCRelocationInfo> && RelInfo)18620b57cec5SDimitry Andric static MCSymbolizer *createAMDGPUSymbolizer(const Triple &/*TT*/,
18630b57cec5SDimitry Andric LLVMOpInfoCallback /*GetOpInfo*/,
18640b57cec5SDimitry Andric LLVMSymbolLookupCallback /*SymbolLookUp*/,
18650b57cec5SDimitry Andric void *DisInfo,
18660b57cec5SDimitry Andric MCContext *Ctx,
18670b57cec5SDimitry Andric std::unique_ptr<MCRelocationInfo> &&RelInfo) {
18680b57cec5SDimitry Andric return new AMDGPUSymbolizer(*Ctx, std::move(RelInfo), DisInfo);
18690b57cec5SDimitry Andric }
18700b57cec5SDimitry Andric
createAMDGPUDisassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)18710b57cec5SDimitry Andric static MCDisassembler *createAMDGPUDisassembler(const Target &T,
18720b57cec5SDimitry Andric const MCSubtargetInfo &STI,
18730b57cec5SDimitry Andric MCContext &Ctx) {
18740b57cec5SDimitry Andric return new AMDGPUDisassembler(STI, Ctx, T.createMCInstrInfo());
18750b57cec5SDimitry Andric }
18760b57cec5SDimitry Andric
LLVMInitializeAMDGPUDisassembler()1877480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUDisassembler() {
18780b57cec5SDimitry Andric TargetRegistry::RegisterMCDisassembler(getTheGCNTarget(),
18790b57cec5SDimitry Andric createAMDGPUDisassembler);
18800b57cec5SDimitry Andric TargetRegistry::RegisterMCSymbolizer(getTheGCNTarget(),
18810b57cec5SDimitry Andric createAMDGPUSymbolizer);
18820b57cec5SDimitry Andric }
1883