blob: 9d7fedb97c27ce74089071066f2fbf005f559479 [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;
Sam Kolton3381d7a2016-10-06 13:46:08 +000044 mutable ArrayRef<uint8_t> Bytes;
Dmitry Preobrazhenskyce941c92017-05-19 14:27:52 +000045 mutable uint32_t Literal;
46 mutable bool HasLiteral;
Tom Stellarde1818af2016-02-18 03:42:32 +000047
Sam Kolton3381d7a2016-10-06 13:46:08 +000048public:
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000049 AMDGPUDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
50 MCInstrInfo const *MCII) :
51 MCDisassembler(STI, Ctx), MCII(MCII), MRI(*Ctx.getRegisterInfo()) {}
Tom Stellarde1818af2016-02-18 03:42:32 +000052
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000053 ~AMDGPUDisassembler() override = default;
Tom Stellarde1818af2016-02-18 03:42:32 +000054
Sam Kolton3381d7a2016-10-06 13:46:08 +000055 DecodeStatus getInstruction(MCInst &MI, uint64_t &Size,
56 ArrayRef<uint8_t> Bytes, uint64_t Address,
57 raw_ostream &WS, raw_ostream &CS) const override;
Tom Stellarde1818af2016-02-18 03:42:32 +000058
Sam Kolton3381d7a2016-10-06 13:46:08 +000059 const char* getRegClassName(unsigned RegClassID) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000060
Sam Kolton3381d7a2016-10-06 13:46:08 +000061 MCOperand createRegOperand(unsigned int RegId) const;
62 MCOperand createRegOperand(unsigned RegClassID, unsigned Val) const;
63 MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000064
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000065 MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000066
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000067 DecodeStatus tryDecodeInst(const uint8_t* Table, MCInst &MI, uint64_t Inst,
68 uint64_t Address) const;
Tom Stellarde1818af2016-02-18 03:42:32 +000069
Sam Kolton549c89d2017-06-21 08:53:38 +000070 DecodeStatus convertSDWAInst(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;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000085
Sam Kolton3381d7a2016-10-06 13:46:08 +000086 MCOperand decodeOperand_SReg_32(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000087 MCOperand decodeOperand_SReg_32_XM0_XEXEC(unsigned Val) const;
Matt Arsenaultca7b0a12017-07-21 15:36:16 +000088 MCOperand decodeOperand_SReg_32_XEXEC_HI(unsigned Val) const;
Dmitry Preobrazhensky6023d592019-03-04 12:48:32 +000089 MCOperand decodeOperand_SRegOrLds_32(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000090 MCOperand decodeOperand_SReg_64(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000091 MCOperand decodeOperand_SReg_64_XEXEC(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000092 MCOperand decodeOperand_SReg_128(unsigned Val) const;
93 MCOperand decodeOperand_SReg_256(unsigned Val) const;
94 MCOperand decodeOperand_SReg_512(unsigned Val) const;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000095
Sam Kolton3381d7a2016-10-06 13:46:08 +000096 enum OpWidthTy {
97 OPW32,
98 OPW64,
99 OPW128,
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000100 OPW256,
101 OPW512,
Matt Arsenault4bd72362016-12-10 00:39:12 +0000102 OPW16,
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000103 OPWV216,
Sam Kolton3381d7a2016-10-06 13:46:08 +0000104 OPW_LAST_,
105 OPW_FIRST_ = OPW32
Tom Stellarde1818af2016-02-18 03:42:32 +0000106 };
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000107
Sam Kolton3381d7a2016-10-06 13:46:08 +0000108 unsigned getVgprClassId(const OpWidthTy Width) const;
109 unsigned getSgprClassId(const OpWidthTy Width) const;
110 unsigned getTtmpClassId(const OpWidthTy Width) const;
111
112 static MCOperand decodeIntImmed(unsigned Imm);
Matt Arsenault4bd72362016-12-10 00:39:12 +0000113 static MCOperand decodeFPImmed(OpWidthTy Width, unsigned Imm);
Sam Kolton3381d7a2016-10-06 13:46:08 +0000114 MCOperand decodeLiteralConstant() const;
115
116 MCOperand decodeSrcOp(const OpWidthTy Width, unsigned Val) const;
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000117 MCOperand decodeDstOp(const OpWidthTy Width, unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000118 MCOperand decodeSpecialReg32(unsigned Val) const;
119 MCOperand decodeSpecialReg64(unsigned Val) const;
Sam Kolton363f47a2017-05-26 15:52:00 +0000120
Sam Kolton549c89d2017-06-21 08:53:38 +0000121 MCOperand decodeSDWASrc(const OpWidthTy Width, unsigned Val) const;
122 MCOperand decodeSDWASrc16(unsigned Val) const;
123 MCOperand decodeSDWASrc32(unsigned Val) const;
124 MCOperand decodeSDWAVopcDst(unsigned Val) const;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000125
126 int getTTmpIdx(unsigned Val) const;
127
128 bool isVI() const;
129 bool isGFX9() const;
130 };
Sam Kolton3381d7a2016-10-06 13:46:08 +0000131
132//===----------------------------------------------------------------------===//
133// AMDGPUSymbolizer
134//===----------------------------------------------------------------------===//
135
136class AMDGPUSymbolizer : public MCSymbolizer {
137private:
138 void *DisInfo;
139
140public:
141 AMDGPUSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &&RelInfo,
Matt Arsenaultf3dd8632016-11-01 00:55:14 +0000142 void *disInfo)
Sam Kolton3381d7a2016-10-06 13:46:08 +0000143 : MCSymbolizer(Ctx, std::move(RelInfo)), DisInfo(disInfo) {}
144
145 bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
146 int64_t Value, uint64_t Address,
147 bool IsBranch, uint64_t Offset,
148 uint64_t InstSize) override;
149
150 void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
151 int64_t Value,
Matt Arsenault92b355b2016-11-15 19:34:37 +0000152 uint64_t Address) override;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000153};
154
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000155} // end namespace llvm
Tom Stellarde1818af2016-02-18 03:42:32 +0000156
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000157#endif // LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H