LLVM  10.0.0
AVRDisassembler.cpp
Go to the documentation of this file.
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"
18 
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCInst.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "avr-disassembler"
29 
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 
48  const MCSubtargetInfo &STI,
49  MCContext &Ctx) {
50  return new AVRDisassembler(STI, Ctx);
51 }
52 
53 
55  // Register the disassembler.
58 }
59 
60 static DecodeStatus DecodeGPR8RegisterClass(MCInst &Inst, unsigned RegNo,
61  uint64_t Address, const void *Decoder) {
63 }
64 
65 static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo,
66  uint64_t Address, const void *Decoder) {
68 }
69 
70 static DecodeStatus DecodePTRREGSRegisterClass(MCInst &Inst, unsigned RegNo,
71  uint64_t Address, const void *Decoder) {
73 }
74 
75 #include "AVRGenDisassemblerTables.inc"
76 
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 
88 }
89 
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 
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 &CStream) const {
117  uint32_t Insn;
118 
119  DecodeStatus Result;
120 
121  // Try decode a 16-bit instruction.
122  {
123  Result = readInstruction16(Bytes, Address, Size, Insn);
124 
125  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
126 
127  // Try to auto-decode a 16-bit instruction.
128  Result = decodeInstruction(getDecoderTable(Size), Instr,
129  Insn, Address, this, STI);
130 
131  if (Result != MCDisassembler::Fail)
132  return Result;
133  }
134 
135  // Try decode a 32-bit instruction.
136  {
137  Result = readInstruction32(Bytes, Address, Size, Insn);
138 
139  if (Result == MCDisassembler::Fail) return MCDisassembler::Fail;
140 
141  Result = decodeInstruction(getDecoderTable(Size), Instr, Insn,
142  Address, this, STI);
143 
144  if (Result != MCDisassembler::Fail) {
145  return Result;
146  }
147 
148  return MCDisassembler::Fail;
149  }
150 }
151 
152 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
153  const void *Decoder);
154 
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
static DecodeStatus readInstruction32(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn)
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
static DecodeStatus DecodePTRREGSRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const void *Decoder)
DecodeStatus(* DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address, const void *Decoder)
Target & getTheAVRTarget()
static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const void *Decoder)
Context object for machine code objects.
Definition: MCContext.h:65
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static MCDisassembler * createAVRDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRDisassembler()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:130
static const uint8_t * getDecoderTable(uint64_t Size)
Target - Wrapper for Target specific information.
static DecodeStatus DecodeGPR8RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus readInstruction16(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn)
Generic base class for all target subtargets.
MCDisassembler::DecodeStatus DecodeStatus
uint32_t Size
Definition: Profile.cpp:46
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI