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 
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
21 #include "llvm/MC/MCFixedLenDisassembler.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/Support/TargetRegistry.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "avr-disassembler"
28 
29 typedef MCDisassembler::DecodeStatus DecodeStatus;
30 
31 namespace {
32 
33 /// A disassembler class for AVR.
34 class AVRDisassembler : public MCDisassembler {
35 public:
36   AVRDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
37       : MCDisassembler(STI, Ctx) {}
38   virtual ~AVRDisassembler() {}
39 
40   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
41                               ArrayRef<uint8_t> Bytes, uint64_t Address,
42                               raw_ostream &VStream,
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" void LLVMInitializeAVRDisassembler() {
55   // Register the disassembler.
56   TargetRegistry::RegisterMCDisassembler(getTheAVRTarget(),
57                                          createAVRDisassembler);
58 }
59 
60 static DecodeStatus DecodeGPR8RegisterClass(MCInst &Inst, unsigned RegNo,
61                                             uint64_t Address, const void *Decoder) {
62   return MCDisassembler::Success;
63 }
64 
65 static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo,
66                                            uint64_t Address, const void *Decoder) {
67   return MCDisassembler::Success;
68 }
69 
70 static DecodeStatus DecodePTRREGSRegisterClass(MCInst &Inst, unsigned RegNo,
71                                                uint64_t Address, const void *Decoder) {
72   return MCDisassembler::Success;
73 }
74 
75 #include "AVRGenDisassemblerTables.inc"
76 
77 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
78                                       uint64_t &Size, uint32_t &Insn) {
79   if (Bytes.size() < 2) {
80     Size = 0;
81     return MCDisassembler::Fail;
82   }
83 
84   Size = 2;
85   Insn = (Bytes[0] << 0) | (Bytes[1] << 8);
86 
87   return MCDisassembler::Success;
88 }
89 
90 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
91                                       uint64_t &Size, uint32_t &Insn) {
92 
93   if (Bytes.size() < 4) {
94     Size = 0;
95     return MCDisassembler::Fail;
96   }
97 
98   Size = 4;
99   Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) | (Bytes[3] << 24);
100 
101   return MCDisassembler::Success;
102 }
103 
104 static const uint8_t *getDecoderTable(uint64_t Size) {
105 
106   switch (Size) {
107     case 2: return DecoderTable16;
108     case 4: return DecoderTable32;
109     default: llvm_unreachable("instructions must be 16 or 32-bits");
110   }
111 }
112 
113 DecodeStatus AVRDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
114                                              ArrayRef<uint8_t> Bytes,
115                                              uint64_t Address,
116                                              raw_ostream &VStream,
117                                              raw_ostream &CStream) const {
118   uint32_t Insn;
119 
120   DecodeStatus Result;
121 
122   // Try decode a 16-bit instruction.
123   {
124     Result = readInstruction16(Bytes, Address, Size, Insn);
125 
126     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
127 
128     // Try to auto-decode a 16-bit instruction.
129     Result = decodeInstruction(getDecoderTable(Size), Instr,
130                                Insn, Address, this, STI);
131 
132     if (Result != MCDisassembler::Fail)
133       return Result;
134   }
135 
136   // Try decode a 32-bit instruction.
137   {
138     Result = readInstruction32(Bytes, Address, Size, Insn);
139 
140     if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
141 
142     Result = decodeInstruction(getDecoderTable(Size), Instr, Insn,
143                                Address, this, STI);
144 
145     if (Result != MCDisassembler::Fail) {
146       return Result;
147     }
148 
149     return MCDisassembler::Fail;
150   }
151 }
152 
153 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
154                                    const void *Decoder);
155 
156