blob: c5eaba615c2a6bd2a8d722530ff2628e56339cba [file] [log] [blame]
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +00001//===- AMDGPUDisassembler.hpp - Disassembler for AMDGPU ISA -----*- C++ -*-===//
Tom Stellarde1818af2016-02-18 03:42:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellarde1818af2016-02-18 03:42:32 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10///
11/// This file contains declaration for AMDGPU ISA disassembler
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
16#define LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
17
Mehdi Aminib550cb12016-04-18 09:17:29 +000018#include "llvm/ADT/ArrayRef.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCInstrInfo.h"
Tom Stellarde1818af2016-02-18 03:42:32 +000021#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000022#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Sam Kolton3381d7a2016-10-06 13:46:08 +000023#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000024
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000025#include <algorithm>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000026#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000027#include <memory>
Tom Stellarde1818af2016-02-18 03:42:32 +000028
29namespace llvm {
30
Sam Kolton3381d7a2016-10-06 13:46:08 +000031class MCInst;
32class MCOperand;
33class MCSubtargetInfo;
34class Twine;
Tom Stellarde1818af2016-02-18 03:42:32 +000035
Sam Kolton3381d7a2016-10-06 13:46:08 +000036//===----------------------------------------------------------------------===//
37// AMDGPUDisassembler
38//===----------------------------------------------------------------------===//
Nikolay Haustov161a1582016-02-25 16:09:14 +000039
Sam Kolton3381d7a2016-10-06 13:46:08 +000040class AMDGPUDisassembler : public MCDisassembler {
41private:
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000042 std::unique_ptr<MCInstrInfo const> const MCII;
43 const MCRegisterInfo &MRI;
Matt Arsenaultca64ef22019-05-22 16:28:41 +000044 const unsigned TargetMaxInstBytes;
Sam Kolton3381d7a2016-10-06 13:46:08 +000045 mutable ArrayRef<uint8_t> Bytes;
Dmitry Preobrazhenskyce941c92017-05-19 14:27:52 +000046 mutable uint32_t Literal;
47 mutable bool HasLiteral;
Tom Stellarde1818af2016-02-18 03:42:32 +000048
Sam Kolton3381d7a2016-10-06 13:46:08 +000049public:
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000050 AMDGPUDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
Matt Arsenaultca64ef22019-05-22 16:28:41 +000051 MCInstrInfo const *MCII);
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000052 ~AMDGPUDisassembler() override = default;
Tom Stellarde1818af2016-02-18 03:42:32 +000053
Sam Kolton3381d7a2016-10-06 13:46:08 +000054 DecodeStatus getInstruction(MCInst &MI, uint64_t &Size,
55 ArrayRef<uint8_t> Bytes, uint64_t Address,
56 raw_ostream &WS, raw_ostream &CS) const override;
Tom Stellarde1818af2016-02-18 03:42:32 +000057
Sam Kolton3381d7a2016-10-06 13:46:08 +000058 const char* getRegClassName(unsigned RegClassID) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000059
Sam Kolton3381d7a2016-10-06 13:46:08 +000060 MCOperand createRegOperand(unsigned int RegId) const;
61 MCOperand createRegOperand(unsigned RegClassID, unsigned Val) const;
62 MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000063
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000064 MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000065
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000066 DecodeStatus tryDecodeInst(const uint8_t* Table, MCInst &MI, uint64_t Inst,
67 uint64_t Address) const;
Tom Stellarde1818af2016-02-18 03:42:32 +000068
Sam Kolton549c89d2017-06-21 08:53:38 +000069 DecodeStatus convertSDWAInst(MCInst &MI) const;
Stanislav Mekhanoshin245b5ba2019-06-12 18:02:41 +000070 DecodeStatus convertDPP8Inst(MCInst &MI) const;
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000071 DecodeStatus convertMIMGInst(MCInst &MI) const;
Sam Kolton549c89d2017-06-21 08:53:38 +000072
Sam Kolton3381d7a2016-10-06 13:46:08 +000073 MCOperand decodeOperand_VGPR_32(unsigned Val) const;
Dmitry Preobrazhensky6023d592019-03-04 12:48:32 +000074 MCOperand decodeOperand_VRegOrLds_32(unsigned Val) const;
75
Sam Kolton3381d7a2016-10-06 13:46:08 +000076 MCOperand decodeOperand_VS_32(unsigned Val) const;
77 MCOperand decodeOperand_VS_64(unsigned Val) const;
Dmitry Preobrazhensky30fc5232017-07-18 13:12:48 +000078 MCOperand decodeOperand_VS_128(unsigned Val) const;
Matt Arsenault4bd72362016-12-10 00:39:12 +000079 MCOperand decodeOperand_VSrc16(unsigned Val) const;
Matt Arsenault9be7b0d2017-02-27 18:49:11 +000080 MCOperand decodeOperand_VSrcV216(unsigned Val) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000081
Sam Kolton3381d7a2016-10-06 13:46:08 +000082 MCOperand decodeOperand_VReg_64(unsigned Val) const;
83 MCOperand decodeOperand_VReg_96(unsigned Val) const;
84 MCOperand decodeOperand_VReg_128(unsigned Val) const;
Stanislav Mekhanoshin9e77d0c2019-07-09 19:41:51 +000085 MCOperand decodeOperand_VReg_256(unsigned Val) const;
86 MCOperand decodeOperand_VReg_512(unsigned Val) const;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000087
Sam Kolton3381d7a2016-10-06 13:46:08 +000088 MCOperand decodeOperand_SReg_32(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000089 MCOperand decodeOperand_SReg_32_XM0_XEXEC(unsigned Val) const;
Matt Arsenaultca7b0a12017-07-21 15:36:16 +000090 MCOperand decodeOperand_SReg_32_XEXEC_HI(unsigned Val) const;
Dmitry Preobrazhensky6023d592019-03-04 12:48:32 +000091 MCOperand decodeOperand_SRegOrLds_32(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000092 MCOperand decodeOperand_SReg_64(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000093 MCOperand decodeOperand_SReg_64_XEXEC(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000094 MCOperand decodeOperand_SReg_128(unsigned Val) const;
95 MCOperand decodeOperand_SReg_256(unsigned Val) const;
96 MCOperand decodeOperand_SReg_512(unsigned Val) const;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000097
Stanislav Mekhanoshin9e77d0c2019-07-09 19:41:51 +000098 MCOperand decodeOperand_AGPR_32(unsigned Val) const;
99 MCOperand decodeOperand_AReg_128(unsigned Val) const;
100 MCOperand decodeOperand_AReg_512(unsigned Val) const;
101 MCOperand decodeOperand_AReg_1024(unsigned Val) const;
102 MCOperand decodeOperand_AV_32(unsigned Val) const;
103 MCOperand decodeOperand_AV_64(unsigned Val) const;
104
Sam Kolton3381d7a2016-10-06 13:46:08 +0000105 enum OpWidthTy {
106 OPW32,
107 OPW64,
108 OPW128,
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000109 OPW256,
110 OPW512,
Stanislav Mekhanoshin9e77d0c2019-07-09 19:41:51 +0000111 OPW1024,
Matt Arsenault4bd72362016-12-10 00:39:12 +0000112 OPW16,
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000113 OPWV216,
Sam Kolton3381d7a2016-10-06 13:46:08 +0000114 OPW_LAST_,
115 OPW_FIRST_ = OPW32
Tom Stellarde1818af2016-02-18 03:42:32 +0000116 };
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000117
Sam Kolton3381d7a2016-10-06 13:46:08 +0000118 unsigned getVgprClassId(const OpWidthTy Width) const;
Stanislav Mekhanoshin9e77d0c2019-07-09 19:41:51 +0000119 unsigned getAgprClassId(const OpWidthTy Width) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000120 unsigned getSgprClassId(const OpWidthTy Width) const;
121 unsigned getTtmpClassId(const OpWidthTy Width) const;
122
123 static MCOperand decodeIntImmed(unsigned Imm);
Matt Arsenault4bd72362016-12-10 00:39:12 +0000124 static MCOperand decodeFPImmed(OpWidthTy Width, unsigned Imm);
Sam Kolton3381d7a2016-10-06 13:46:08 +0000125 MCOperand decodeLiteralConstant() const;
126
127 MCOperand decodeSrcOp(const OpWidthTy Width, unsigned Val) const;
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000128 MCOperand decodeDstOp(const OpWidthTy Width, unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000129 MCOperand decodeSpecialReg32(unsigned Val) const;
130 MCOperand decodeSpecialReg64(unsigned Val) const;
Sam Kolton363f47a2017-05-26 15:52:00 +0000131
Sam Kolton549c89d2017-06-21 08:53:38 +0000132 MCOperand decodeSDWASrc(const OpWidthTy Width, unsigned Val) const;
133 MCOperand decodeSDWASrc16(unsigned Val) const;
134 MCOperand decodeSDWASrc32(unsigned Val) const;
135 MCOperand decodeSDWAVopcDst(unsigned Val) const;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000136
Stanislav Mekhanoshinab4f2ea2019-06-18 19:10:59 +0000137 MCOperand decodeBoolReg(unsigned Val) const;
138
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000139 int getTTmpIdx(unsigned Val) const;
140
141 bool isVI() const;
142 bool isGFX9() const;
Stanislav Mekhanoshin33d806a2019-04-24 17:28:30 +0000143 bool isGFX10() const;
Stanislav Mekhanoshin245b5ba2019-06-12 18:02:41 +0000144};
Sam Kolton3381d7a2016-10-06 13:46:08 +0000145
146//===----------------------------------------------------------------------===//
147// AMDGPUSymbolizer
148//===----------------------------------------------------------------------===//
149
150class AMDGPUSymbolizer : public MCSymbolizer {
151private:
152 void *DisInfo;
153
154public:
155 AMDGPUSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &&RelInfo,
Matt Arsenaultf3dd8632016-11-01 00:55:14 +0000156 void *disInfo)
Sam Kolton3381d7a2016-10-06 13:46:08 +0000157 : MCSymbolizer(Ctx, std::move(RelInfo)), DisInfo(disInfo) {}
158
159 bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
160 int64_t Value, uint64_t Address,
161 bool IsBranch, uint64_t Offset,
162 uint64_t InstSize) override;
163
164 void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
165 int64_t Value,
Matt Arsenault92b355b2016-11-15 19:34:37 +0000166 uint64_t Address) override;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000167};
168
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000169} // end namespace llvm
Tom Stellarde1818af2016-02-18 03:42:32 +0000170
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000171#endif // LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H