LLVM 22.0.0git
LoongArchDisassembler.cpp
Go to the documentation of this file.
1//===-- LoongArchDisassembler.cpp - Disassembler for LoongArch ------------===//
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 implements the LoongArchDisassembler class.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDecoder.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/Support/Endian.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "loongarch-disassembler"
29
31
32namespace {
33class LoongArchDisassembler : public MCDisassembler {
34public:
35 LoongArchDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
36 : MCDisassembler(STI, Ctx) {}
37
38 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
39 ArrayRef<uint8_t> Bytes, uint64_t Address,
40 raw_ostream &CStream) const override;
41};
42} // end namespace
43
45 const MCSubtargetInfo &STI,
46 MCContext &Ctx) {
47 return new LoongArchDisassembler(STI, Ctx);
48}
49
58
60 uint64_t Address,
61 const MCDisassembler *Decoder) {
62 if (RegNo >= 32)
64 Inst.addOperand(MCOperand::createReg(LoongArch::R0 + RegNo));
66}
67
68static DecodeStatus
70 const MCDisassembler *Decoder) {
71 if (RegNo <= 1)
73 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
74}
75
77 uint64_t Address,
78 const MCDisassembler *Decoder) {
79 if (RegNo >= 32)
81 Inst.addOperand(MCOperand::createReg(LoongArch::F0 + RegNo));
83}
84
86 uint64_t Address,
87 const MCDisassembler *Decoder) {
88 if (RegNo >= 32)
90 Inst.addOperand(MCOperand::createReg(LoongArch::F0_64 + RegNo));
92}
93
95 uint64_t Address,
96 const MCDisassembler *Decoder) {
97 if (RegNo >= 8)
99 Inst.addOperand(MCOperand::createReg(LoongArch::FCC0 + RegNo));
101}
102
104 uint64_t Address,
105 const MCDisassembler *Decoder) {
106 if (RegNo >= 4)
108 Inst.addOperand(MCOperand::createReg(LoongArch::FCSR0 + RegNo));
110}
111
113 uint64_t Address,
114 const MCDisassembler *Decoder) {
115 if (RegNo >= 32)
117 Inst.addOperand(MCOperand::createReg(LoongArch::VR0 + RegNo));
119}
120
122 uint64_t Address,
123 const MCDisassembler *Decoder) {
124 if (RegNo >= 32)
126 Inst.addOperand(MCOperand::createReg(LoongArch::XR0 + RegNo));
128}
129
131 uint64_t Address,
132 const MCDisassembler *Decoder) {
133 if (RegNo >= 4)
135 Inst.addOperand(MCOperand::createReg(LoongArch::SCR0 + RegNo));
137}
138
139template <unsigned N, int P = 0>
141 int64_t Address,
142 const MCDisassembler *Decoder) {
143 assert(isUInt<N>(Imm) && "Invalid immediate");
144 Inst.addOperand(MCOperand::createImm(Imm + P));
146}
147
148template <unsigned N, unsigned S = 0>
150 int64_t Address,
151 const MCDisassembler *Decoder) {
152 assert(isUInt<N>(Imm) && "Invalid immediate");
153 // Shift left Imm <S> bits, then sign-extend the number in the bottom <N+S>
154 // bits.
157}
158
159#include "LoongArchGenDisassemblerTables.inc"
160
161DecodeStatus LoongArchDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
162 ArrayRef<uint8_t> Bytes,
163 uint64_t Address,
164 raw_ostream &CS) const {
165 uint32_t Insn;
167
168 // We want to read exactly 4 bytes of data because all LoongArch instructions
169 // are fixed 32 bits.
170 if (Bytes.size() < 4) {
171 Size = 0;
173 }
174
175 Insn = support::endian::read32le(Bytes.data());
176 // Calling the auto-generated decoder function.
177 Result = decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
178 Size = 4;
179
180 return Result;
181}
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchDisassembler()
static DecodeStatus DecodeSCRRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeCFRRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeLASX256RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeLSX128RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRNoR0R1RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createLoongArchDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFCSRRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
#define T
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
const T * data() const
Definition ArrayRef.h:144
Context object for machine code objects.
Definition MCContext.h:83
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
uint32_t read32le(const void *P)
Definition Endian.h:429
This is an optimization pass for GlobalISel generic memory operations.
Target & getTheLoongArch64Target()
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
Target & getTheLoongArch32Target()
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:577
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.