NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 1 | //===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCTargetDesc/HexagonBaseInfo.h" |
| 11 | #include "MCTargetDesc/HexagonMCTargetDesc.h" |
| 12 | |
| 13 | #include "llvm/MC/MCContext.h" |
| 14 | #include "llvm/MC/MCDisassembler.h" |
| 15 | #include "llvm/MC/MCExpr.h" |
| 16 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 17 | #include "llvm/MC/MCInst.h" |
| 18 | #include "llvm/MC/MCInstrDesc.h" |
| 19 | #include "llvm/MC/MCSubtargetInfo.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/LEB128.h" |
| 23 | #include "llvm/Support/MemoryObject.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | #include "llvm/Support/Endian.h" |
| 27 | |
| 28 | #include <vector> |
| 29 | #include <array> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "hexagon-disassembler" |
| 34 | |
| 35 | // Pull DecodeStatus and its enum values into the global namespace. |
| 36 | typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; |
| 37 | |
| 38 | namespace { |
| 39 | /// \brief Hexagon disassembler for all Hexagon platforms. |
| 40 | class HexagonDisassembler : public MCDisassembler { |
| 41 | public: |
| 42 | HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) |
| 43 | : MCDisassembler(STI, Ctx) {} |
| 44 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame^] | 45 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
| 46 | MemoryObject const &Region, uint64_t Address, |
| 47 | raw_ostream &VStream, |
| 48 | raw_ostream &CStream) const override; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 49 | }; |
| 50 | } |
| 51 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 52 | #include "HexagonGenDisassemblerTables.inc" |
| 53 | |
| 54 | static MCDisassembler *createHexagonDisassembler(Target const &T, |
| 55 | MCSubtargetInfo const &STI, |
| 56 | MCContext &Ctx) { |
| 57 | return new HexagonDisassembler(STI, Ctx); |
| 58 | } |
| 59 | |
| 60 | extern "C" void LLVMInitializeHexagonDisassembler() { |
| 61 | TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, |
| 62 | createHexagonDisassembler); |
| 63 | } |
| 64 | |
| 65 | DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
| 66 | MemoryObject const &Region, |
| 67 | uint64_t Address, |
| 68 | raw_ostream &os, |
| 69 | raw_ostream &cs) const { |
| 70 | std::array<uint8_t, 4> Bytes; |
| 71 | Size = 4; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame^] | 72 | if (Region.readBytes(Address, Bytes.size(), Bytes.data()) == -1) |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 73 | return MCDisassembler::Fail; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame^] | 74 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 75 | uint32_t insn = |
| 76 | llvm::support::endian::read<uint32_t, llvm::support::little, |
| 77 | llvm::support::unaligned>(Bytes.data()); |
| 78 | |
| 79 | // Remove parse bits. |
| 80 | insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); |
| 81 | return decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); |
| 82 | } |