blob: 75cfc5e112820aea42928744b18c54bb87b7e98d [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11///
12/// This file contains declaration for AMDGPU ISA disassembler
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
17#define LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H
18
Mehdi Aminib550cb12016-04-18 09:17:29 +000019#include "llvm/ADT/ArrayRef.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCInstrInfo.h"
Tom Stellarde1818af2016-02-18 03:42:32 +000022#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000023#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Sam Kolton3381d7a2016-10-06 13:46:08 +000024#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000025
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000026#include <algorithm>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include <cstdint>
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000028#include <memory>
Tom Stellarde1818af2016-02-18 03:42:32 +000029
30namespace llvm {
31
Sam Kolton3381d7a2016-10-06 13:46:08 +000032class MCInst;
33class MCOperand;
34class MCSubtargetInfo;
35class Twine;
Tom Stellarde1818af2016-02-18 03:42:32 +000036
Sam Kolton3381d7a2016-10-06 13:46:08 +000037//===----------------------------------------------------------------------===//
38// AMDGPUDisassembler
39//===----------------------------------------------------------------------===//
Nikolay Haustov161a1582016-02-25 16:09:14 +000040
Sam Kolton3381d7a2016-10-06 13:46:08 +000041class AMDGPUDisassembler : public MCDisassembler {
42private:
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000043 std::unique_ptr<MCInstrInfo const> const MCII;
44 const MCRegisterInfo &MRI;
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,
51 MCInstrInfo const *MCII) :
52 MCDisassembler(STI, Ctx), MCII(MCII), MRI(*Ctx.getRegisterInfo()) {}
Tom Stellarde1818af2016-02-18 03:42:32 +000053
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000054 ~AMDGPUDisassembler() override = default;
Tom Stellarde1818af2016-02-18 03:42:32 +000055
Sam Kolton3381d7a2016-10-06 13:46:08 +000056 DecodeStatus getInstruction(MCInst &MI, uint64_t &Size,
57 ArrayRef<uint8_t> Bytes, uint64_t Address,
58 raw_ostream &WS, raw_ostream &CS) const override;
Tom Stellarde1818af2016-02-18 03:42:32 +000059
Sam Kolton3381d7a2016-10-06 13:46:08 +000060 const char* getRegClassName(unsigned RegClassID) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000061
Sam Kolton3381d7a2016-10-06 13:46:08 +000062 MCOperand createRegOperand(unsigned int RegId) const;
63 MCOperand createRegOperand(unsigned RegClassID, unsigned Val) const;
64 MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000065
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000066 MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000067
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000068 DecodeStatus tryDecodeInst(const uint8_t* Table, MCInst &MI, uint64_t Inst,
69 uint64_t Address) const;
Tom Stellarde1818af2016-02-18 03:42:32 +000070
Sam Kolton549c89d2017-06-21 08:53:38 +000071 DecodeStatus convertSDWAInst(MCInst &MI) const;
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000072 DecodeStatus convertMIMGInst(MCInst &MI) const;
Sam Kolton549c89d2017-06-21 08:53:38 +000073
Sam Kolton3381d7a2016-10-06 13:46:08 +000074 MCOperand decodeOperand_VGPR_32(unsigned Val) const;
75 MCOperand decodeOperand_VS_32(unsigned Val) const;
76 MCOperand decodeOperand_VS_64(unsigned Val) const;
Dmitry Preobrazhensky30fc5232017-07-18 13:12:48 +000077 MCOperand decodeOperand_VS_128(unsigned Val) const;
Matt Arsenault4bd72362016-12-10 00:39:12 +000078 MCOperand decodeOperand_VSrc16(unsigned Val) const;
Matt Arsenault9be7b0d2017-02-27 18:49:11 +000079 MCOperand decodeOperand_VSrcV216(unsigned Val) const;
Nikolay Haustov161a1582016-02-25 16:09:14 +000080
Sam Kolton3381d7a2016-10-06 13:46:08 +000081 MCOperand decodeOperand_VReg_64(unsigned Val) const;
82 MCOperand decodeOperand_VReg_96(unsigned Val) const;
83 MCOperand decodeOperand_VReg_128(unsigned Val) const;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000084
Sam Kolton3381d7a2016-10-06 13:46:08 +000085 MCOperand decodeOperand_SReg_32(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000086 MCOperand decodeOperand_SReg_32_XM0_XEXEC(unsigned Val) const;
Matt Arsenaultca7b0a12017-07-21 15:36:16 +000087 MCOperand decodeOperand_SReg_32_XEXEC_HI(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000088 MCOperand decodeOperand_SReg_64(unsigned Val) const;
Matt Arsenault640c44b2016-11-29 19:39:53 +000089 MCOperand decodeOperand_SReg_64_XEXEC(unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +000090 MCOperand decodeOperand_SReg_128(unsigned Val) const;
91 MCOperand decodeOperand_SReg_256(unsigned Val) const;
92 MCOperand decodeOperand_SReg_512(unsigned Val) const;
Nikolay Haustovac106ad2016-03-01 13:57:29 +000093
Sam Kolton3381d7a2016-10-06 13:46:08 +000094 enum OpWidthTy {
95 OPW32,
96 OPW64,
97 OPW128,
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +000098 OPW256,
99 OPW512,
Matt Arsenault4bd72362016-12-10 00:39:12 +0000100 OPW16,
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000101 OPWV216,
Sam Kolton3381d7a2016-10-06 13:46:08 +0000102 OPW_LAST_,
103 OPW_FIRST_ = OPW32
Tom Stellarde1818af2016-02-18 03:42:32 +0000104 };
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000105
Sam Kolton3381d7a2016-10-06 13:46:08 +0000106 unsigned getVgprClassId(const OpWidthTy Width) const;
107 unsigned getSgprClassId(const OpWidthTy Width) const;
108 unsigned getTtmpClassId(const OpWidthTy Width) const;
109
110 static MCOperand decodeIntImmed(unsigned Imm);
Matt Arsenault4bd72362016-12-10 00:39:12 +0000111 static MCOperand decodeFPImmed(OpWidthTy Width, unsigned Imm);
Sam Kolton3381d7a2016-10-06 13:46:08 +0000112 MCOperand decodeLiteralConstant() const;
113
114 MCOperand decodeSrcOp(const OpWidthTy Width, unsigned Val) const;
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000115 MCOperand decodeDstOp(const OpWidthTy Width, unsigned Val) const;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000116 MCOperand decodeSpecialReg32(unsigned Val) const;
117 MCOperand decodeSpecialReg64(unsigned Val) const;
Sam Kolton363f47a2017-05-26 15:52:00 +0000118
Sam Kolton549c89d2017-06-21 08:53:38 +0000119 MCOperand decodeSDWASrc(const OpWidthTy Width, unsigned Val) const;
120 MCOperand decodeSDWASrc16(unsigned Val) const;
121 MCOperand decodeSDWASrc32(unsigned Val) const;
122 MCOperand decodeSDWAVopcDst(unsigned Val) const;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000123
124 int getTTmpIdx(unsigned Val) const;
125
126 bool isVI() const;
127 bool isGFX9() const;
128 };
Sam Kolton3381d7a2016-10-06 13:46:08 +0000129
130//===----------------------------------------------------------------------===//
131// AMDGPUSymbolizer
132//===----------------------------------------------------------------------===//
133
134class AMDGPUSymbolizer : public MCSymbolizer {
135private:
136 void *DisInfo;
137
138public:
139 AMDGPUSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &&RelInfo,
Matt Arsenaultf3dd8632016-11-01 00:55:14 +0000140 void *disInfo)
Sam Kolton3381d7a2016-10-06 13:46:08 +0000141 : MCSymbolizer(Ctx, std::move(RelInfo)), DisInfo(disInfo) {}
142
143 bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
144 int64_t Value, uint64_t Address,
145 bool IsBranch, uint64_t Offset,
146 uint64_t InstSize) override;
147
148 void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
149 int64_t Value,
Matt Arsenault92b355b2016-11-15 19:34:37 +0000150 uint64_t Address) override;
Sam Kolton3381d7a2016-10-06 13:46:08 +0000151};
152
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000153} // end namespace llvm
Tom Stellarde1818af2016-02-18 03:42:32 +0000154
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000155#endif // LLVM_LIB_TARGET_AMDGPU_DISASSEMBLER_AMDGPUDISASSEMBLER_H