xref: /src/contrib/llvm-project/llvm/lib/Target/MSP430/Disassembler/MSP430Disassembler.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1d8e91e46SDimitry Andric //===-- MSP430Disassembler.cpp - Disassembler for MSP430 ------------------===//
2d8e91e46SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d8e91e46SDimitry Andric //
7d8e91e46SDimitry Andric //===----------------------------------------------------------------------===//
8d8e91e46SDimitry Andric //
9d8e91e46SDimitry Andric // This file implements the MSP430Disassembler class.
10d8e91e46SDimitry Andric //
11d8e91e46SDimitry Andric //===----------------------------------------------------------------------===//
12d8e91e46SDimitry Andric 
13d8e91e46SDimitry Andric #include "MCTargetDesc/MSP430MCTargetDesc.h"
14c0981da4SDimitry Andric #include "MSP430.h"
15e6d15924SDimitry Andric #include "TargetInfo/MSP430TargetInfo.h"
16d8e91e46SDimitry Andric #include "llvm/MC/MCContext.h"
17145449b1SDimitry Andric #include "llvm/MC/MCDecoderOps.h"
18d8e91e46SDimitry Andric #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19d8e91e46SDimitry Andric #include "llvm/MC/MCInst.h"
20d8e91e46SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
21d8e91e46SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
22c0981da4SDimitry Andric #include "llvm/MC/TargetRegistry.h"
23d8e91e46SDimitry Andric #include "llvm/Support/Endian.h"
24d8e91e46SDimitry Andric 
25d8e91e46SDimitry Andric using namespace llvm;
26d8e91e46SDimitry Andric 
27d8e91e46SDimitry Andric #define DEBUG_TYPE "msp430-disassembler"
28d8e91e46SDimitry Andric 
29d8e91e46SDimitry Andric typedef MCDisassembler::DecodeStatus DecodeStatus;
30d8e91e46SDimitry Andric 
31d8e91e46SDimitry Andric namespace {
32d8e91e46SDimitry Andric class MSP430Disassembler : public MCDisassembler {
33d8e91e46SDimitry Andric   DecodeStatus getInstructionI(MCInst &MI, uint64_t &Size,
34d8e91e46SDimitry Andric                                ArrayRef<uint8_t> Bytes, uint64_t Address,
35d8e91e46SDimitry Andric                                raw_ostream &CStream) const;
36d8e91e46SDimitry Andric 
37d8e91e46SDimitry Andric   DecodeStatus getInstructionII(MCInst &MI, uint64_t &Size,
38d8e91e46SDimitry Andric                                 ArrayRef<uint8_t> Bytes, uint64_t Address,
39d8e91e46SDimitry Andric                                 raw_ostream &CStream) const;
40d8e91e46SDimitry Andric 
41d8e91e46SDimitry Andric   DecodeStatus getInstructionCJ(MCInst &MI, uint64_t &Size,
42d8e91e46SDimitry Andric                                 ArrayRef<uint8_t> Bytes, uint64_t Address,
43d8e91e46SDimitry Andric                                 raw_ostream &CStream) const;
44d8e91e46SDimitry Andric 
45d8e91e46SDimitry Andric public:
MSP430Disassembler(const MCSubtargetInfo & STI,MCContext & Ctx)46d8e91e46SDimitry Andric   MSP430Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
47d8e91e46SDimitry Andric       : MCDisassembler(STI, Ctx) {}
48d8e91e46SDimitry Andric 
49d8e91e46SDimitry Andric   DecodeStatus getInstruction(MCInst &MI, uint64_t &Size,
50d8e91e46SDimitry Andric                               ArrayRef<uint8_t> Bytes, uint64_t Address,
51d8e91e46SDimitry Andric                               raw_ostream &CStream) const override;
52d8e91e46SDimitry Andric };
53d8e91e46SDimitry Andric } // end anonymous namespace
54d8e91e46SDimitry Andric 
createMSP430Disassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)55d8e91e46SDimitry Andric static MCDisassembler *createMSP430Disassembler(const Target &T,
56d8e91e46SDimitry Andric                                                 const MCSubtargetInfo &STI,
57d8e91e46SDimitry Andric                                                 MCContext &Ctx) {
58d8e91e46SDimitry Andric   return new MSP430Disassembler(STI, Ctx);
59d8e91e46SDimitry Andric }
60d8e91e46SDimitry Andric 
LLVMInitializeMSP430Disassembler()61706b4fc4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430Disassembler() {
62d8e91e46SDimitry Andric   TargetRegistry::RegisterMCDisassembler(getTheMSP430Target(),
63d8e91e46SDimitry Andric                                          createMSP430Disassembler);
64d8e91e46SDimitry Andric }
65d8e91e46SDimitry Andric 
66d8e91e46SDimitry Andric static const unsigned GR8DecoderTable[] = {
67d8e91e46SDimitry Andric   MSP430::PCB,  MSP430::SPB,  MSP430::SRB,  MSP430::CGB,
68cfca06d7SDimitry Andric   MSP430::R4B,  MSP430::R5B,  MSP430::R6B,  MSP430::R7B,
69d8e91e46SDimitry Andric   MSP430::R8B,  MSP430::R9B,  MSP430::R10B, MSP430::R11B,
70d8e91e46SDimitry Andric   MSP430::R12B, MSP430::R13B, MSP430::R14B, MSP430::R15B
71d8e91e46SDimitry Andric };
72d8e91e46SDimitry Andric 
DecodeGR8RegisterClass(MCInst & MI,uint64_t RegNo,uint64_t Address,const MCDisassembler * Decoder)73d8e91e46SDimitry Andric static DecodeStatus DecodeGR8RegisterClass(MCInst &MI, uint64_t RegNo,
74d8e91e46SDimitry Andric                                            uint64_t Address,
75145449b1SDimitry Andric                                            const MCDisassembler *Decoder) {
76d8e91e46SDimitry Andric   if (RegNo > 15)
77d8e91e46SDimitry Andric     return MCDisassembler::Fail;
78d8e91e46SDimitry Andric 
79d8e91e46SDimitry Andric   unsigned Reg = GR8DecoderTable[RegNo];
80d8e91e46SDimitry Andric   MI.addOperand(MCOperand::createReg(Reg));
81d8e91e46SDimitry Andric   return MCDisassembler::Success;
82d8e91e46SDimitry Andric }
83d8e91e46SDimitry Andric 
84d8e91e46SDimitry Andric static const unsigned GR16DecoderTable[] = {
85d8e91e46SDimitry Andric   MSP430::PC,  MSP430::SP,  MSP430::SR,  MSP430::CG,
86cfca06d7SDimitry Andric   MSP430::R4,  MSP430::R5,  MSP430::R6,  MSP430::R7,
87d8e91e46SDimitry Andric   MSP430::R8,  MSP430::R9,  MSP430::R10, MSP430::R11,
88d8e91e46SDimitry Andric   MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15
89d8e91e46SDimitry Andric };
90d8e91e46SDimitry Andric 
DecodeGR16RegisterClass(MCInst & MI,uint64_t RegNo,uint64_t Address,const MCDisassembler * Decoder)91d8e91e46SDimitry Andric static DecodeStatus DecodeGR16RegisterClass(MCInst &MI, uint64_t RegNo,
92d8e91e46SDimitry Andric                                             uint64_t Address,
93145449b1SDimitry Andric                                             const MCDisassembler *Decoder) {
94d8e91e46SDimitry Andric   if (RegNo > 15)
95d8e91e46SDimitry Andric     return MCDisassembler::Fail;
96d8e91e46SDimitry Andric 
97d8e91e46SDimitry Andric   unsigned Reg = GR16DecoderTable[RegNo];
98d8e91e46SDimitry Andric   MI.addOperand(MCOperand::createReg(Reg));
99d8e91e46SDimitry Andric   return MCDisassembler::Success;
100d8e91e46SDimitry Andric }
101d8e91e46SDimitry Andric 
102d8e91e46SDimitry Andric static DecodeStatus DecodeCGImm(MCInst &MI, uint64_t Bits, uint64_t Address,
103145449b1SDimitry Andric                                 const MCDisassembler *Decoder);
104d8e91e46SDimitry Andric 
105d8e91e46SDimitry Andric static DecodeStatus DecodeMemOperand(MCInst &MI, uint64_t Bits,
106d8e91e46SDimitry Andric                                      uint64_t Address,
107145449b1SDimitry Andric                                      const MCDisassembler *Decoder);
108d8e91e46SDimitry Andric 
109d8e91e46SDimitry Andric #include "MSP430GenDisassemblerTables.inc"
110d8e91e46SDimitry Andric 
DecodeCGImm(MCInst & MI,uint64_t Bits,uint64_t Address,const MCDisassembler * Decoder)111d8e91e46SDimitry Andric static DecodeStatus DecodeCGImm(MCInst &MI, uint64_t Bits, uint64_t Address,
112145449b1SDimitry Andric                                 const MCDisassembler *Decoder) {
113d8e91e46SDimitry Andric   int64_t Imm;
114d8e91e46SDimitry Andric   switch (Bits) {
115d8e91e46SDimitry Andric   default:
116d8e91e46SDimitry Andric     llvm_unreachable("Invalid immediate value");
117d8e91e46SDimitry Andric   case 0x22: Imm =  4; break;
118d8e91e46SDimitry Andric   case 0x32: Imm =  8; break;
119d8e91e46SDimitry Andric   case 0x03: Imm =  0; break;
120d8e91e46SDimitry Andric   case 0x13: Imm =  1; break;
121d8e91e46SDimitry Andric   case 0x23: Imm =  2; break;
122d8e91e46SDimitry Andric   case 0x33: Imm = -1; break;
123d8e91e46SDimitry Andric   }
124d8e91e46SDimitry Andric   MI.addOperand(MCOperand::createImm(Imm));
125d8e91e46SDimitry Andric   return MCDisassembler::Success;
126d8e91e46SDimitry Andric }
127d8e91e46SDimitry Andric 
DecodeMemOperand(MCInst & MI,uint64_t Bits,uint64_t Address,const MCDisassembler * Decoder)128d8e91e46SDimitry Andric static DecodeStatus DecodeMemOperand(MCInst &MI, uint64_t Bits,
129d8e91e46SDimitry Andric                                      uint64_t Address,
130145449b1SDimitry Andric                                      const MCDisassembler *Decoder) {
131d8e91e46SDimitry Andric   unsigned Reg = Bits & 15;
132d8e91e46SDimitry Andric   unsigned Imm = Bits >> 4;
133d8e91e46SDimitry Andric 
134d8e91e46SDimitry Andric   if (DecodeGR16RegisterClass(MI, Reg, Address, Decoder) !=
135d8e91e46SDimitry Andric       MCDisassembler::Success)
136d8e91e46SDimitry Andric     return MCDisassembler::Fail;
137d8e91e46SDimitry Andric 
138d8e91e46SDimitry Andric   MI.addOperand(MCOperand::createImm((int16_t)Imm));
139d8e91e46SDimitry Andric   return MCDisassembler::Success;
140d8e91e46SDimitry Andric }
141d8e91e46SDimitry Andric 
142d8e91e46SDimitry Andric enum AddrMode {
143d8e91e46SDimitry Andric   amInvalid = 0,
144d8e91e46SDimitry Andric   amRegister,
145d8e91e46SDimitry Andric   amIndexed,
146d8e91e46SDimitry Andric   amIndirect,
147d8e91e46SDimitry Andric   amIndirectPost,
148d8e91e46SDimitry Andric   amSymbolic,
149d8e91e46SDimitry Andric   amImmediate,
150d8e91e46SDimitry Andric   amAbsolute,
151d8e91e46SDimitry Andric   amConstant
152d8e91e46SDimitry Andric };
153d8e91e46SDimitry Andric 
DecodeSrcAddrMode(unsigned Rs,unsigned As)154d8e91e46SDimitry Andric static AddrMode DecodeSrcAddrMode(unsigned Rs, unsigned As) {
155d8e91e46SDimitry Andric   switch (Rs) {
156d8e91e46SDimitry Andric   case 0:
157d8e91e46SDimitry Andric     if (As == 1) return amSymbolic;
158d8e91e46SDimitry Andric     if (As == 2) return amInvalid;
159d8e91e46SDimitry Andric     if (As == 3) return amImmediate;
160d8e91e46SDimitry Andric     break;
161d8e91e46SDimitry Andric   case 2:
162d8e91e46SDimitry Andric     if (As == 1) return amAbsolute;
163d8e91e46SDimitry Andric     if (As == 2) return amConstant;
164d8e91e46SDimitry Andric     if (As == 3) return amConstant;
165d8e91e46SDimitry Andric     break;
166d8e91e46SDimitry Andric   case 3:
167d8e91e46SDimitry Andric     return amConstant;
168d8e91e46SDimitry Andric   default:
169d8e91e46SDimitry Andric     break;
170d8e91e46SDimitry Andric   }
171d8e91e46SDimitry Andric   switch (As) {
172d8e91e46SDimitry Andric   case 0: return amRegister;
173d8e91e46SDimitry Andric   case 1: return amIndexed;
174d8e91e46SDimitry Andric   case 2: return amIndirect;
175d8e91e46SDimitry Andric   case 3: return amIndirectPost;
176d8e91e46SDimitry Andric   default:
177d8e91e46SDimitry Andric     llvm_unreachable("As out of range");
178d8e91e46SDimitry Andric   }
179d8e91e46SDimitry Andric }
180d8e91e46SDimitry Andric 
DecodeSrcAddrModeI(unsigned Insn)181d8e91e46SDimitry Andric static AddrMode DecodeSrcAddrModeI(unsigned Insn) {
182d8e91e46SDimitry Andric   unsigned Rs = fieldFromInstruction(Insn, 8, 4);
183d8e91e46SDimitry Andric   unsigned As = fieldFromInstruction(Insn, 4, 2);
184d8e91e46SDimitry Andric   return DecodeSrcAddrMode(Rs, As);
185d8e91e46SDimitry Andric }
186d8e91e46SDimitry Andric 
DecodeSrcAddrModeII(unsigned Insn)187d8e91e46SDimitry Andric static AddrMode DecodeSrcAddrModeII(unsigned Insn) {
188d8e91e46SDimitry Andric   unsigned Rs = fieldFromInstruction(Insn, 0, 4);
189d8e91e46SDimitry Andric   unsigned As = fieldFromInstruction(Insn, 4, 2);
190d8e91e46SDimitry Andric   return DecodeSrcAddrMode(Rs, As);
191d8e91e46SDimitry Andric }
192d8e91e46SDimitry Andric 
DecodeDstAddrMode(unsigned Insn)193d8e91e46SDimitry Andric static AddrMode DecodeDstAddrMode(unsigned Insn) {
194d8e91e46SDimitry Andric   unsigned Rd = fieldFromInstruction(Insn, 0, 4);
195d8e91e46SDimitry Andric   unsigned Ad = fieldFromInstruction(Insn, 7, 1);
196d8e91e46SDimitry Andric   switch (Rd) {
197d8e91e46SDimitry Andric   case 0: return Ad ? amSymbolic : amRegister;
198d8e91e46SDimitry Andric   case 2: return Ad ? amAbsolute : amRegister;
199d8e91e46SDimitry Andric   default:
200d8e91e46SDimitry Andric     break;
201d8e91e46SDimitry Andric   }
202d8e91e46SDimitry Andric   return Ad ? amIndexed : amRegister;
203d8e91e46SDimitry Andric }
204d8e91e46SDimitry Andric 
getDecoderTable(AddrMode SrcAM,unsigned Words)205d8e91e46SDimitry Andric static const uint8_t *getDecoderTable(AddrMode SrcAM, unsigned Words) {
206d8e91e46SDimitry Andric   assert(0 < Words && Words < 4 && "Incorrect number of words");
207d8e91e46SDimitry Andric   switch (SrcAM) {
208d8e91e46SDimitry Andric   default:
209d8e91e46SDimitry Andric     llvm_unreachable("Invalid addressing mode");
210d8e91e46SDimitry Andric   case amRegister:
211d8e91e46SDimitry Andric     assert(Words < 3 && "Incorrect number of words");
212d8e91e46SDimitry Andric     return Words == 2 ? DecoderTableAlpha32 : DecoderTableAlpha16;
213d8e91e46SDimitry Andric   case amConstant:
214d8e91e46SDimitry Andric     assert(Words < 3 && "Incorrect number of words");
215d8e91e46SDimitry Andric     return Words == 2 ? DecoderTableBeta32 : DecoderTableBeta16;
216d8e91e46SDimitry Andric   case amIndexed:
217d8e91e46SDimitry Andric   case amSymbolic:
218d8e91e46SDimitry Andric   case amImmediate:
219d8e91e46SDimitry Andric   case amAbsolute:
220d8e91e46SDimitry Andric     assert(Words > 1 && "Incorrect number of words");
221d8e91e46SDimitry Andric     return Words == 2 ? DecoderTableGamma32 : DecoderTableGamma48;
222d8e91e46SDimitry Andric   case amIndirect:
223d8e91e46SDimitry Andric   case amIndirectPost:
224d8e91e46SDimitry Andric     assert(Words < 3 && "Incorrect number of words");
225d8e91e46SDimitry Andric     return Words == 2 ? DecoderTableDelta32 : DecoderTableDelta16;
226d8e91e46SDimitry Andric   }
227d8e91e46SDimitry Andric }
228d8e91e46SDimitry Andric 
getInstructionI(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const229d8e91e46SDimitry Andric DecodeStatus MSP430Disassembler::getInstructionI(MCInst &MI, uint64_t &Size,
230d8e91e46SDimitry Andric                                                  ArrayRef<uint8_t> Bytes,
231d8e91e46SDimitry Andric                                                  uint64_t Address,
232d8e91e46SDimitry Andric                                                  raw_ostream &CStream) const {
233d8e91e46SDimitry Andric   uint64_t Insn = support::endian::read16le(Bytes.data());
234d8e91e46SDimitry Andric   AddrMode SrcAM = DecodeSrcAddrModeI(Insn);
235d8e91e46SDimitry Andric   AddrMode DstAM = DecodeDstAddrMode(Insn);
236d8e91e46SDimitry Andric   if (SrcAM == amInvalid || DstAM == amInvalid) {
237d8e91e46SDimitry Andric     Size = 2; // skip one word and let disassembler to try further
238d8e91e46SDimitry Andric     return MCDisassembler::Fail;
239d8e91e46SDimitry Andric   }
240d8e91e46SDimitry Andric 
241d8e91e46SDimitry Andric   unsigned Words = 1;
242d8e91e46SDimitry Andric   switch (SrcAM) {
243d8e91e46SDimitry Andric   case amIndexed:
244d8e91e46SDimitry Andric   case amSymbolic:
245d8e91e46SDimitry Andric   case amImmediate:
246d8e91e46SDimitry Andric   case amAbsolute:
247d8e91e46SDimitry Andric     if (Bytes.size() < (Words + 1) * 2) {
248d8e91e46SDimitry Andric       Size = 2;
249d8e91e46SDimitry Andric       return DecodeStatus::Fail;
250d8e91e46SDimitry Andric     }
251d8e91e46SDimitry Andric     Insn |= (uint64_t)support::endian::read16le(Bytes.data() + 2) << 16;
252d8e91e46SDimitry Andric     ++Words;
253d8e91e46SDimitry Andric     break;
254d8e91e46SDimitry Andric   default:
255d8e91e46SDimitry Andric     break;
256d8e91e46SDimitry Andric   }
257d8e91e46SDimitry Andric   switch (DstAM) {
258d8e91e46SDimitry Andric   case amIndexed:
259d8e91e46SDimitry Andric   case amSymbolic:
260d8e91e46SDimitry Andric   case amAbsolute:
261d8e91e46SDimitry Andric     if (Bytes.size() < (Words + 1) * 2) {
262d8e91e46SDimitry Andric       Size = 2;
263d8e91e46SDimitry Andric       return DecodeStatus::Fail;
264d8e91e46SDimitry Andric     }
265d8e91e46SDimitry Andric     Insn |= (uint64_t)support::endian::read16le(Bytes.data() + Words * 2)
266d8e91e46SDimitry Andric         << (Words * 16);
267d8e91e46SDimitry Andric     ++Words;
268d8e91e46SDimitry Andric     break;
269d8e91e46SDimitry Andric   default:
270d8e91e46SDimitry Andric     break;
271d8e91e46SDimitry Andric   }
272d8e91e46SDimitry Andric 
273d8e91e46SDimitry Andric   DecodeStatus Result = decodeInstruction(getDecoderTable(SrcAM, Words), MI,
274d8e91e46SDimitry Andric                                           Insn, Address, this, STI);
275d8e91e46SDimitry Andric   if (Result != MCDisassembler::Fail) {
276d8e91e46SDimitry Andric     Size = Words * 2;
277d8e91e46SDimitry Andric     return Result;
278d8e91e46SDimitry Andric   }
279d8e91e46SDimitry Andric 
280d8e91e46SDimitry Andric   Size = 2;
281d8e91e46SDimitry Andric   return DecodeStatus::Fail;
282d8e91e46SDimitry Andric }
283d8e91e46SDimitry Andric 
getInstructionII(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const284d8e91e46SDimitry Andric DecodeStatus MSP430Disassembler::getInstructionII(MCInst &MI, uint64_t &Size,
285d8e91e46SDimitry Andric                                                   ArrayRef<uint8_t> Bytes,
286d8e91e46SDimitry Andric                                                   uint64_t Address,
287d8e91e46SDimitry Andric                                                   raw_ostream &CStream) const {
288d8e91e46SDimitry Andric   uint64_t Insn = support::endian::read16le(Bytes.data());
289d8e91e46SDimitry Andric   AddrMode SrcAM = DecodeSrcAddrModeII(Insn);
290d8e91e46SDimitry Andric   if (SrcAM == amInvalid) {
291d8e91e46SDimitry Andric     Size = 2; // skip one word and let disassembler to try further
292d8e91e46SDimitry Andric     return MCDisassembler::Fail;
293d8e91e46SDimitry Andric   }
294d8e91e46SDimitry Andric 
295d8e91e46SDimitry Andric   unsigned Words = 1;
296d8e91e46SDimitry Andric   switch (SrcAM) {
297d8e91e46SDimitry Andric   case amIndexed:
298d8e91e46SDimitry Andric   case amSymbolic:
299d8e91e46SDimitry Andric   case amImmediate:
300d8e91e46SDimitry Andric   case amAbsolute:
301d8e91e46SDimitry Andric     if (Bytes.size() < (Words + 1) * 2) {
302d8e91e46SDimitry Andric       Size = 2;
303d8e91e46SDimitry Andric       return DecodeStatus::Fail;
304d8e91e46SDimitry Andric     }
305d8e91e46SDimitry Andric     Insn |= (uint64_t)support::endian::read16le(Bytes.data() + 2) << 16;
306d8e91e46SDimitry Andric     ++Words;
307d8e91e46SDimitry Andric     break;
308d8e91e46SDimitry Andric   default:
309d8e91e46SDimitry Andric     break;
310d8e91e46SDimitry Andric   }
311d8e91e46SDimitry Andric 
312d8e91e46SDimitry Andric   const uint8_t *DecoderTable = Words == 2 ? DecoderTable32 : DecoderTable16;
313d8e91e46SDimitry Andric   DecodeStatus Result = decodeInstruction(DecoderTable, MI, Insn, Address,
314d8e91e46SDimitry Andric                                           this, STI);
315d8e91e46SDimitry Andric   if (Result != MCDisassembler::Fail) {
316d8e91e46SDimitry Andric     Size = Words * 2;
317d8e91e46SDimitry Andric     return Result;
318d8e91e46SDimitry Andric   }
319d8e91e46SDimitry Andric 
320d8e91e46SDimitry Andric   Size = 2;
321d8e91e46SDimitry Andric   return DecodeStatus::Fail;
322d8e91e46SDimitry Andric }
323d8e91e46SDimitry Andric 
getCondCode(unsigned Cond)324d8e91e46SDimitry Andric static MSP430CC::CondCodes getCondCode(unsigned Cond) {
325d8e91e46SDimitry Andric   switch (Cond) {
326d8e91e46SDimitry Andric   case 0: return MSP430CC::COND_NE;
327d8e91e46SDimitry Andric   case 1: return MSP430CC::COND_E;
328d8e91e46SDimitry Andric   case 2: return MSP430CC::COND_LO;
329d8e91e46SDimitry Andric   case 3: return MSP430CC::COND_HS;
330d8e91e46SDimitry Andric   case 4: return MSP430CC::COND_N;
331d8e91e46SDimitry Andric   case 5: return MSP430CC::COND_GE;
332d8e91e46SDimitry Andric   case 6: return MSP430CC::COND_L;
333d8e91e46SDimitry Andric   case 7: return MSP430CC::COND_NONE;
334d8e91e46SDimitry Andric   default:
335d8e91e46SDimitry Andric     llvm_unreachable("Cond out of range");
336d8e91e46SDimitry Andric   }
337d8e91e46SDimitry Andric }
338d8e91e46SDimitry Andric 
getInstructionCJ(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const339d8e91e46SDimitry Andric DecodeStatus MSP430Disassembler::getInstructionCJ(MCInst &MI, uint64_t &Size,
340d8e91e46SDimitry Andric                                                   ArrayRef<uint8_t> Bytes,
341d8e91e46SDimitry Andric                                                   uint64_t Address,
342d8e91e46SDimitry Andric                                                   raw_ostream &CStream) const {
343d8e91e46SDimitry Andric   uint64_t Insn = support::endian::read16le(Bytes.data());
344d8e91e46SDimitry Andric   unsigned Cond = fieldFromInstruction(Insn, 10, 3);
345d8e91e46SDimitry Andric   unsigned Offset = fieldFromInstruction(Insn, 0, 10);
346d8e91e46SDimitry Andric 
347d8e91e46SDimitry Andric   MI.addOperand(MCOperand::createImm(SignExtend32(Offset, 10)));
348d8e91e46SDimitry Andric 
349d8e91e46SDimitry Andric   if (Cond == 7)
350d8e91e46SDimitry Andric     MI.setOpcode(MSP430::JMP);
351d8e91e46SDimitry Andric   else {
352d8e91e46SDimitry Andric     MI.setOpcode(MSP430::JCC);
353d8e91e46SDimitry Andric     MI.addOperand(MCOperand::createImm(getCondCode(Cond)));
354d8e91e46SDimitry Andric   }
355d8e91e46SDimitry Andric 
356d8e91e46SDimitry Andric   Size = 2;
357d8e91e46SDimitry Andric   return DecodeStatus::Success;
358d8e91e46SDimitry Andric }
359d8e91e46SDimitry Andric 
getInstruction(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const360d8e91e46SDimitry Andric DecodeStatus MSP430Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
361d8e91e46SDimitry Andric                                                 ArrayRef<uint8_t> Bytes,
362d8e91e46SDimitry Andric                                                 uint64_t Address,
363d8e91e46SDimitry Andric                                                 raw_ostream &CStream) const {
364d8e91e46SDimitry Andric   if (Bytes.size() < 2) {
365d8e91e46SDimitry Andric     Size = 0;
366d8e91e46SDimitry Andric     return MCDisassembler::Fail;
367d8e91e46SDimitry Andric   }
368d8e91e46SDimitry Andric 
369d8e91e46SDimitry Andric   uint64_t Insn = support::endian::read16le(Bytes.data());
370d8e91e46SDimitry Andric   unsigned Opc = fieldFromInstruction(Insn, 13, 3);
371d8e91e46SDimitry Andric   switch (Opc) {
372d8e91e46SDimitry Andric   case 0:
373706b4fc4SDimitry Andric     return getInstructionII(MI, Size, Bytes, Address, CStream);
374d8e91e46SDimitry Andric   case 1:
375706b4fc4SDimitry Andric     return getInstructionCJ(MI, Size, Bytes, Address, CStream);
376d8e91e46SDimitry Andric   default:
377706b4fc4SDimitry Andric     return getInstructionI(MI, Size, Bytes, Address, CStream);
378d8e91e46SDimitry Andric   }
379d8e91e46SDimitry Andric }
380