LLVM  9.0.0svn
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"
17 
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCInst.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "avr-disassembler"
28 
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 
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.
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 &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 
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()
void LLVMInitializeAVRDisassembler()
static DecodeStatus DecodeLD8RegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const void *Decoder)
Context object for machine code objects.
Definition: MCContext.h:62
int decodeInstruction(InternalInstruction *insn, byteReader_t reader, const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg, uint64_t startLoc, DisassemblerMode mode)
Decode one instruction and store the decoding results in a buffer provided by the consumer...
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:148
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:45
IRTranslator LLVM IR MI