blob: 015efea34377970c33999cd69f82c1fb2f5bbf93 [file] [log] [blame]
NAKAMURA Takumi729be142014-10-27 12:37:26 +00001//===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===//
2//
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#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +000011#include "MCTargetDesc/HexagonMCInst.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000012#include "MCTargetDesc/HexagonMCTargetDesc.h"
13
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCDisassembler.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCFixedLenDisassembler.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrDesc.h"
20#include "llvm/MC/MCSubtargetInfo.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/LEB128.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Support/Endian.h"
27
28#include <vector>
29#include <array>
30
31using namespace llvm;
32
33#define DEBUG_TYPE "hexagon-disassembler"
34
35// Pull DecodeStatus and its enum values into the global namespace.
36typedef llvm::MCDisassembler::DecodeStatus DecodeStatus;
37
38namespace {
39/// \brief Hexagon disassembler for all Hexagon platforms.
40class HexagonDisassembler : public MCDisassembler {
41public:
42 HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx)
43 : MCDisassembler(STI, Ctx) {}
44
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000045 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000046 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000047 raw_ostream &VStream,
48 raw_ostream &CStream) const override;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000049};
50}
51
Colin LeMahieuff370ed2014-12-26 20:30:58 +000052static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
53 uint64_t Address, const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +000054static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
55 uint64_t Address, const void *Decoder);
56
Colin LeMahieuefa74e02014-11-18 20:28:11 +000057static const uint16_t IntRegDecoderTable[] = {
58 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
59 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
60 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
61 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
62 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
63 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
64 Hexagon::R30, Hexagon::R31 };
65
66static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1,
67Hexagon::P2, Hexagon::P3 };
68
Colin LeMahieu383c36e2014-12-05 18:24:06 +000069static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
70 const uint16_t Table[], size_t Size) {
71 if (RegNo < Size) {
72 Inst.addOperand(MCOperand::CreateReg(Table[RegNo]));
73 return MCDisassembler::Success;
74 }
75 else
76 return MCDisassembler::Fail;
77}
78
Colin LeMahieuefa74e02014-11-18 20:28:11 +000079static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
80 uint64_t /*Address*/,
81 void const *Decoder) {
82 if (RegNo > 31)
83 return MCDisassembler::Fail;
84
85 unsigned Register = IntRegDecoderTable[RegNo];
86 Inst.addOperand(MCOperand::CreateReg(Register));
87 return MCDisassembler::Success;
88}
89
Colin LeMahieuf3db8842014-12-19 19:06:32 +000090static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
91 uint64_t /*Address*/, const void *Decoder) {
92 static const uint16_t CtrlRegDecoderTable[] = {
93 Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1,
94 Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7,
95 Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP,
96 Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH
97 };
98
99 if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0]))
100 return MCDisassembler::Fail;
101
102 if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister)
103 return MCDisassembler::Fail;
104
105 unsigned Register = CtrlRegDecoderTable[RegNo];
106 Inst.addOperand(MCOperand::CreateReg(Register));
107 return MCDisassembler::Success;
108}
109
Colin LeMahieuff370ed2014-12-26 20:30:58 +0000110static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
111 uint64_t /*Address*/, const void *Decoder) {
112 unsigned Register = 0;
113 switch (RegNo) {
114 case 0:
115 Register = Hexagon::M0;
116 break;
117 case 1:
118 Register = Hexagon::M1;
119 break;
120 default:
121 return MCDisassembler::Fail;
122 }
123 Inst.addOperand(MCOperand::CreateReg(Register));
124 return MCDisassembler::Success;
125}
126
Colin LeMahieu383c36e2014-12-05 18:24:06 +0000127static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
128 uint64_t /*Address*/, const void *Decoder) {
129 static const uint16_t DoubleRegDecoderTable[] = {
130 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
131 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
132 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
133 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15
134 };
135
136 return (DecodeRegisterClass(Inst, RegNo >> 1,
137 DoubleRegDecoderTable,
138 sizeof (DoubleRegDecoderTable)));
139}
140
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000141static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
142 uint64_t /*Address*/,
143 void const *Decoder) {
144 if (RegNo > 3)
145 return MCDisassembler::Fail;
146
147 unsigned Register = PredRegDecoderTable[RegNo];
148 Inst.addOperand(MCOperand::CreateReg(Register));
149 return MCDisassembler::Success;
150}
151
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000152#include "HexagonGenDisassemblerTables.inc"
153
154static MCDisassembler *createHexagonDisassembler(Target const &T,
155 MCSubtargetInfo const &STI,
156 MCContext &Ctx) {
157 return new HexagonDisassembler(STI, Ctx);
158}
159
160extern "C" void LLVMInitializeHexagonDisassembler() {
161 TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,
162 createHexagonDisassembler);
163}
164
165DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000166 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000167 uint64_t Address,
168 raw_ostream &os,
169 raw_ostream &cs) const {
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000170 Size = 4;
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000171 if (Bytes.size() < 4)
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000172 return MCDisassembler::Fail;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000173
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000174 uint32_t insn =
175 llvm::support::endian::read<uint32_t, llvm::support::little,
176 llvm::support::unaligned>(Bytes.data());
177
178 // Remove parse bits.
179 insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000180 DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);
181 HexagonMCInst::AppendImplicitOperands(MI);
182 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000183}