1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//===-- M68kDisassembler.cpp - Disassembler for M68k ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is part of the M68k Disassembler.
//
//===----------------------------------------------------------------------===//
#include "M68k.h"
#include "M68kRegisterInfo.h"
#include "M68kSubtarget.h"
#include "MCTargetDesc/M68kMCCodeEmitter.h"
#include "MCTargetDesc/M68kMCTargetDesc.h"
#include "TargetInfo/M68kTargetInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCDecoderOps.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
#define DEBUG_TYPE "m68k-disassembler"
typedef MCDisassembler::DecodeStatus DecodeStatus;
static const unsigned RegisterDecode[] = {
M68k::D0, M68k::D1, M68k::D2, M68k::D3, M68k::D4, M68k::D5,
M68k::D6, M68k::D7, M68k::A0, M68k::A1, M68k::A2, M68k::A3,
M68k::A4, M68k::A5, M68k::A6, M68k::SP, M68k::FP0, M68k::FP1,
M68k::FP2, M68k::FP3, M68k::FP4, M68k::FP5, M68k::FP6, M68k::FP7};
static DecodeStatus DecodeRegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address, const void *Decoder) {
if (RegNo >= 24)
return DecodeStatus::Fail;
Inst.addOperand(MCOperand::createReg(RegisterDecode[RegNo]));
return DecodeStatus::Success;
}
static DecodeStatus DecodeDR32RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo, Address, Decoder);
}
static DecodeStatus DecodeDR16RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo, Address, Decoder);
}
static DecodeStatus DecodeDR8RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo, Address, Decoder);
}
static DecodeStatus DecodeAR32RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo | 8ULL, Address, Decoder);
}
static DecodeStatus DecodeAR16RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo | 8ULL, Address, Decoder);
}
static DecodeStatus DecodeXR32RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo, Address, Decoder);
}
static DecodeStatus DecodeXR16RegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo, Address, Decoder);
}
static DecodeStatus DecodeFPDRRegisterClass(MCInst &Inst, uint64_t RegNo,
uint64_t Address,
const void *Decoder) {
return DecodeRegisterClass(Inst, RegNo | 16ULL, Address, Decoder);
}
#define DecodeFPDR32RegisterClass DecodeFPDRRegisterClass
#define DecodeFPDR64RegisterClass DecodeFPDRRegisterClass
#define DecodeFPDR80RegisterClass DecodeFPDRRegisterClass
static DecodeStatus DecodeCCRCRegisterClass(MCInst &Inst, APInt &Insn,
uint64_t Address,
const void *Decoder) {
llvm_unreachable("unimplemented");
}
static DecodeStatus DecodeImm32(MCInst &Inst, uint64_t Imm, uint64_t Address,
const void *Decoder) {
Inst.addOperand(MCOperand::createImm(M68k::swapWord<uint32_t>(Imm)));
return DecodeStatus::Success;
}
#include "M68kGenDisassemblerTable.inc"
#undef DecodeFPDR32RegisterClass
#undef DecodeFPDR64RegisterClass
#undef DecodeFPDR80RegisterClass
/// A disassembler class for M68k.
struct M68kDisassembler : public MCDisassembler {
M68kDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
: MCDisassembler(STI, Ctx) {}
virtual ~M68kDisassembler() {}
DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &CStream) const override;
};
DecodeStatus M68kDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
ArrayRef<uint8_t> Bytes,
uint64_t Address,
raw_ostream &CStream) const {
DecodeStatus Result;
auto MakeUp = [&](APInt &Insn, unsigned InstrBits) {
unsigned Idx = Insn.getBitWidth() >> 3;
unsigned RoundUp = alignTo(InstrBits, Align(16));
if (RoundUp > Insn.getBitWidth())
Insn = Insn.zext(RoundUp);
RoundUp = RoundUp >> 3;
for (; Idx < RoundUp; Idx += 2) {
Insn.insertBits(support::endian::read16be(&Bytes[Idx]), Idx * 8, 16);
}
};
APInt Insn(16, support::endian::read16be(Bytes.data()));
// 2 bytes of data are consumed, so set Size to 2
// If we don't do this, disassembler may generate result even
// the encoding is invalid. We need to let it fail correctly.
Size = 2;
Result = decodeInstruction(DecoderTable80, Instr, Insn, Address, this, STI,
MakeUp);
if (Result == DecodeStatus::Success)
Size = InstrLenTable[Instr.getOpcode()] >> 3;
return Result;
}
static MCDisassembler *createM68kDisassembler(const Target &T,
const MCSubtargetInfo &STI,
MCContext &Ctx) {
return new M68kDisassembler(STI, Ctx);
}
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kDisassembler() {
// Register the disassembler.
TargetRegistry::RegisterMCDisassembler(getTheM68kTarget(),
createM68kDisassembler);
}
|