1 //===- AVRDisassembler.cpp - Disassembler for AVR ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is part of the AVR Disassembler.
11 //===----------------------------------------------------------------------===//
14 #include "AVRRegisterInfo.h"
15 #include "AVRSubtarget.h"
16 #include "MCTargetDesc/AVRMCTargetDesc.h"
17 #include "TargetInfo/AVRTargetInfo.h"
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"
28 #define DEBUG_TYPE "avr-disassembler"
30 typedef MCDisassembler::DecodeStatus DecodeStatus;
34 /// A disassembler class for AVR.
35 class AVRDisassembler : public MCDisassembler {
37 AVRDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
38 : MCDisassembler(STI, Ctx) {}
39 virtual ~AVRDisassembler() {}
41 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
42 ArrayRef<uint8_t> Bytes, uint64_t Address,
43 raw_ostream &CStream) const override;
47 static MCDisassembler *createAVRDisassembler(const Target &T,
48 const MCSubtargetInfo &STI,
50 return new AVRDisassembler(STI, Ctx);
54 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRDisassembler() {
55 // Register the disassembler.
56 TargetRegistry::RegisterMCDisassembler(getTheAVRTarget(),
57 createAVRDisassembler);
60 static DecodeStatus DecodeGPR8RegisterClass(MCInst &Inst, unsigned RegNo,
61 uint64_t Address, const void *Decoder) {
62 return MCDisassembler::Success;
65 static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo,
66 uint64_t Address, const void *Decoder) {
67 return MCDisassembler::Success;
70 static DecodeStatus DecodePTRREGSRegisterClass(MCInst &Inst, unsigned RegNo,
71 uint64_t Address, const void *Decoder) {
72 return MCDisassembler::Success;
75 #include "AVRGenDisassemblerTables.inc"
77 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
78 uint64_t &Size, uint32_t &Insn) {
79 if (Bytes.size() < 2) {
81 return MCDisassembler::Fail;
85 Insn = (Bytes[0] << 0) | (Bytes[1] << 8);
87 return MCDisassembler::Success;
90 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
91 uint64_t &Size, uint32_t &Insn) {
93 if (Bytes.size() < 4) {
95 return MCDisassembler::Fail;
99 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) | (Bytes[3] << 24);
101 return MCDisassembler::Success;
104 static const uint8_t *getDecoderTable(uint64_t Size) {
107 case 2: return DecoderTable16;
108 case 4: return DecoderTable32;
109 default: llvm_unreachable("instructions must be 16 or 32-bits");
113 DecodeStatus AVRDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
114 ArrayRef<uint8_t> Bytes,
116 raw_ostream &CStream) const {
121 // Try decode a 16-bit instruction.
123 Result = readInstruction16(Bytes, Address, Size, Insn);
125 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
127 // Try to auto-decode a 16-bit instruction.
128 Result = decodeInstruction(getDecoderTable(Size), Instr,
129 Insn, Address, this, STI);
131 if (Result != MCDisassembler::Fail)
135 // Try decode a 32-bit instruction.
137 Result = readInstruction32(Bytes, Address, Size, Insn);
139 if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
141 Result = decodeInstruction(getDecoderTable(Size), Instr, Insn,
144 if (Result != MCDisassembler::Fail) {
148 return MCDisassembler::Fail;
152 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
153 const void *Decoder);