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 #include "AVRGenDisassemblerTables.inc"
108 
109 static DecodeStatus decodeFIOARr(MCInst &Inst, unsigned Insn,
110                                  uint64_t Address, const void *Decoder) {
111   unsigned addr = 0;
112   addr |= fieldFromInstruction(Insn, 0, 4);
113   addr |= fieldFromInstruction(Insn, 9, 2) << 4;
114   unsigned reg = fieldFromInstruction(Insn, 4, 5);
115   Inst.addOperand(MCOperand::createImm(addr));
116   if (DecodeGPR8RegisterClass(Inst, reg, Address, Decoder) == MCDisassembler::Fail)
117     return MCDisassembler::Fail;
118   return MCDisassembler::Success;
119 }
120 
121 static DecodeStatus decodeFIORdA(MCInst &Inst, unsigned Insn,
122                                  uint64_t Address, const void *Decoder) {
123   unsigned addr = 0;
124   addr |= fieldFromInstruction(Insn, 0, 4);
125   addr |= fieldFromInstruction(Insn, 9, 2) << 4;
126   unsigned reg = fieldFromInstruction(Insn, 4, 5);
127   if (DecodeGPR8RegisterClass(Inst, reg, Address, Decoder) == MCDisassembler::Fail)
128     return MCDisassembler::Fail;
129   Inst.addOperand(MCOperand::createImm(addr));
130   return MCDisassembler::Success;
131 }
132 
133 static DecodeStatus decodeFIOBIT(MCInst &Inst, unsigned Insn,
134                                  uint64_t Address, const void *Decoder) {
135   unsigned addr = fieldFromInstruction(Insn, 3, 5);
136   unsigned b = fieldFromInstruction(Insn, 0, 3);
137   Inst.addOperand(MCOperand::createImm(addr));
138   Inst.addOperand(MCOperand::createImm(b));
139   return MCDisassembler::Success;
140 }
141 
142 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
143                                       uint64_t &Size, uint32_t &Insn) {
144   if (Bytes.size() < 2) {
145     Size = 0;
146     return MCDisassembler::Fail;
147   }
148 
149   Size = 2;
150   Insn = (Bytes[0] << 0) | (Bytes[1] << 8);
151 
152   return MCDisassembler::Success;
153 }
154 
155 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
156                                       uint64_t &Size, uint32_t &Insn) {
157 
158   if (Bytes.size() < 4) {
159     Size = 0;
160     return MCDisassembler::Fail;
161   }
162 
163   Size = 4;
164   Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) | (Bytes[3] << 24);
165 
166   return MCDisassembler::Success;
167 }
168 
169 static const uint8_t *getDecoderTable(uint64_t Size) {
170 
171   switch (Size) {
172     case 2: return DecoderTable16;
173     case 4: return DecoderTable32;
174     default: llvm_unreachable("instructions must be 16 or 32-bits");
175   }
176 }
177 
178 DecodeStatus AVRDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
179                                              ArrayRef<uint8_t> Bytes,
180                                              uint64_t Address,
181                                              raw_ostream &CStream) const {
182   uint32_t Insn;
183 
184   DecodeStatus Result;
185 
186   // Try decode a 16-bit instruction.
187   {
188     Result = readInstruction16(Bytes, Address, Size, Insn);
189 
190     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
191 
192     // Try to auto-decode a 16-bit instruction.
193     Result = decodeInstruction(getDecoderTable(Size), Instr,
194                                Insn, Address, this, STI);
195 
196     if (Result != MCDisassembler::Fail)
197       return Result;
198   }
199 
200   // Try decode a 32-bit instruction.
201   {
202     Result = readInstruction32(Bytes, Address, Size, Insn);
203 
204     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
205 
206     Result = decodeInstruction(getDecoderTable(Size), Instr, Insn,
207                                Address, this, STI);
208 
209     if (Result != MCDisassembler::Fail) {
210       return Result;
211     }
212 
213     return MCDisassembler::Fail;
214   }
215 }
216 
217 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
218                                    const void *Decoder);
219 
220