xref: /llvm-project/llvm/lib/Target/AVR/Disassembler/AVRDisassembler.cpp (revision 01c2209d518c7f19de7997ac29385c699a7ccd35)
1 //===- AVRDisassembler.cpp - Disassembler for AVR ---------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is part of the AVR Disassembler.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "AVR.h"
14 #include "AVRRegisterInfo.h"
15 #include "AVRSubtarget.h"
16 #include "MCTargetDesc/AVRMCTargetDesc.h"
17 #include "TargetInfo/AVRTargetInfo.h"
18 
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
22 #include "llvm/MC/MCFixedLenDisassembler.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Support/TargetRegistry.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "avr-disassembler"
29 
30 typedef MCDisassembler::DecodeStatus DecodeStatus;
31 
32 namespace {
33 
34 /// A disassembler class for AVR.
35 class AVRDisassembler : public MCDisassembler {
36 public:
37   AVRDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
38       : MCDisassembler(STI, Ctx) {}
39   virtual ~AVRDisassembler() {}
40 
41   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
42                               ArrayRef<uint8_t> Bytes, uint64_t Address,
43                               raw_ostream &CStream) const override;
44 };
45 }
46 
47 static MCDisassembler *createAVRDisassembler(const Target &T,
48                                              const MCSubtargetInfo &STI,
49                                              MCContext &Ctx) {
50   return new AVRDisassembler(STI, Ctx);
51 }
52 
53 
54 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRDisassembler() {
55   // Register the disassembler.
56   TargetRegistry::RegisterMCDisassembler(getTheAVRTarget(),
57                                          createAVRDisassembler);
58 }
59 
60 static const uint16_t GPRDecoderTable[] = {
61   AVR::R0, AVR::R1, AVR::R2, AVR::R3,
62   AVR::R4, AVR::R5, AVR::R6, AVR::R7,
63   AVR::R8, AVR::R9, AVR::R10, AVR::R11,
64   AVR::R12, AVR::R13, AVR::R14, AVR::R15,
65   AVR::R16, AVR::R17, AVR::R18, AVR::R19,
66   AVR::R20, AVR::R21, AVR::R22, AVR::R23,
67   AVR::R24, AVR::R25, AVR::R26, AVR::R27,
68   AVR::R28, AVR::R29, AVR::R30, AVR::R31,
69 };
70 
71 static DecodeStatus DecodeGPR8RegisterClass(MCInst &Inst, unsigned RegNo,
72                                             uint64_t Address, const void *Decoder) {
73   if (RegNo > 31)
74     return MCDisassembler::Fail;
75 
76   unsigned Register = GPRDecoderTable[RegNo];
77   Inst.addOperand(MCOperand::createReg(Register));
78   return MCDisassembler::Success;
79 }
80 
81 static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo,
82                                            uint64_t Address, const void *Decoder) {
83   if (RegNo > 15)
84     return MCDisassembler::Fail;
85 
86   unsigned Register = GPRDecoderTable[RegNo+16];
87   Inst.addOperand(MCOperand::createReg(Register));
88   return MCDisassembler::Success;
89 }
90 
91 static DecodeStatus DecodePTRREGSRegisterClass(MCInst &Inst, unsigned RegNo,
92                                                uint64_t Address, const void *Decoder) {
93   // Note: this function must be defined but does not seem to be called.
94   assert(false && "unimplemented: PTRREGS register class");
95   return MCDisassembler::Success;
96 }
97 
98 static DecodeStatus decodeFIOARr(MCInst &Inst, unsigned Insn,
99                                  uint64_t Address, const void *Decoder);
100 
101 static DecodeStatus decodeFIORdA(MCInst &Inst, unsigned Insn,
102                                  uint64_t Address, const void *Decoder);
103 
104 static DecodeStatus decodeFIOBIT(MCInst &Inst, unsigned Insn,
105                                  uint64_t Address, const void *Decoder);
106 
107 static DecodeStatus decodeCallTarget(MCInst &Inst, unsigned Insn,
108                                      uint64_t Address, const void *Decoder);
109 
110 static DecodeStatus decodeFRd(MCInst &Inst, unsigned Insn,
111                               uint64_t Address, const void *Decoder);
112 
113 #include "AVRGenDisassemblerTables.inc"
114 
115 static DecodeStatus decodeFIOARr(MCInst &Inst, unsigned Insn,
116                                  uint64_t Address, const void *Decoder) {
117   unsigned addr = 0;
118   addr |= fieldFromInstruction(Insn, 0, 4);
119   addr |= fieldFromInstruction(Insn, 9, 2) << 4;
120   unsigned reg = fieldFromInstruction(Insn, 4, 5);
121   Inst.addOperand(MCOperand::createImm(addr));
122   if (DecodeGPR8RegisterClass(Inst, reg, Address, Decoder) == MCDisassembler::Fail)
123     return MCDisassembler::Fail;
124   return MCDisassembler::Success;
125 }
126 
127 static DecodeStatus decodeFIORdA(MCInst &Inst, unsigned Insn,
128                                  uint64_t Address, const void *Decoder) {
129   unsigned addr = 0;
130   addr |= fieldFromInstruction(Insn, 0, 4);
131   addr |= fieldFromInstruction(Insn, 9, 2) << 4;
132   unsigned reg = fieldFromInstruction(Insn, 4, 5);
133   if (DecodeGPR8RegisterClass(Inst, reg, Address, Decoder) == MCDisassembler::Fail)
134     return MCDisassembler::Fail;
135   Inst.addOperand(MCOperand::createImm(addr));
136   return MCDisassembler::Success;
137 }
138 
139 static DecodeStatus decodeFIOBIT(MCInst &Inst, unsigned Insn,
140                                  uint64_t Address, const void *Decoder) {
141   unsigned addr = fieldFromInstruction(Insn, 3, 5);
142   unsigned b = fieldFromInstruction(Insn, 0, 3);
143   Inst.addOperand(MCOperand::createImm(addr));
144   Inst.addOperand(MCOperand::createImm(b));
145   return MCDisassembler::Success;
146 }
147 
148 static DecodeStatus decodeCallTarget(MCInst &Inst, unsigned Field,
149                                      uint64_t Address, const void *Decoder) {
150   // Call targets need to be shifted left by one so this needs a custom
151   // decoder.
152   Inst.addOperand(MCOperand::createImm(Field << 1));
153   return MCDisassembler::Success;
154 }
155 
156 static DecodeStatus decodeFRd(MCInst &Inst, unsigned Insn,
157                               uint64_t Address, const void *Decoder) {
158   unsigned d = fieldFromInstruction(Insn, 4, 5);
159   if (DecodeGPR8RegisterClass(Inst, d, Address, Decoder) == MCDisassembler::Fail)
160     return MCDisassembler::Fail;
161   return MCDisassembler::Success;
162 }
163 
164 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
165                                       uint64_t &Size, uint32_t &Insn) {
166   if (Bytes.size() < 2) {
167     Size = 0;
168     return MCDisassembler::Fail;
169   }
170 
171   Size = 2;
172   Insn = (Bytes[0] << 0) | (Bytes[1] << 8);
173 
174   return MCDisassembler::Success;
175 }
176 
177 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
178                                       uint64_t &Size, uint32_t &Insn) {
179 
180   if (Bytes.size() < 4) {
181     Size = 0;
182     return MCDisassembler::Fail;
183   }
184 
185   Size = 4;
186   Insn = (Bytes[0] << 16) | (Bytes[1] << 24) | (Bytes[2] << 0) | (Bytes[3] << 8);
187 
188   return MCDisassembler::Success;
189 }
190 
191 static const uint8_t *getDecoderTable(uint64_t Size) {
192 
193   switch (Size) {
194     case 2: return DecoderTable16;
195     case 4: return DecoderTable32;
196     default: llvm_unreachable("instructions must be 16 or 32-bits");
197   }
198 }
199 
200 DecodeStatus AVRDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
201                                              ArrayRef<uint8_t> Bytes,
202                                              uint64_t Address,
203                                              raw_ostream &CStream) const {
204   uint32_t Insn;
205 
206   DecodeStatus Result;
207 
208   // Try decode a 16-bit instruction.
209   {
210     Result = readInstruction16(Bytes, Address, Size, Insn);
211 
212     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
213 
214     // Try to auto-decode a 16-bit instruction.
215     Result = decodeInstruction(getDecoderTable(Size), Instr,
216                                Insn, Address, this, STI);
217 
218     if (Result != MCDisassembler::Fail)
219       return Result;
220   }
221 
222   // Try decode a 32-bit instruction.
223   {
224     Result = readInstruction32(Bytes, Address, Size, Insn);
225 
226     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
227 
228     Result = decodeInstruction(getDecoderTable(Size), Instr, Insn,
229                                Address, this, STI);
230 
231     if (Result != MCDisassembler::Fail) {
232       return Result;
233     }
234 
235     return MCDisassembler::Fail;
236   }
237 }
238 
239 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
240                                    const void *Decoder);
241 
242