LLVM 22.0.0git
LanaiDisassembler.cpp
Go to the documentation of this file.
1//===- LanaiDisassembler.cpp - Disassembler for Lanai -----------*- 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 Lanai Disassembler.
10//
11//===----------------------------------------------------------------------===//
12
13#include "LanaiDisassembler.h"
14
15#include "LanaiAluCode.h"
16#include "LanaiCondCode.h"
17#include "LanaiInstrInfo.h"
19#include "llvm/MC/MCDecoder.h"
21#include "llvm/MC/MCInst.h"
25#include "llvm/Support/Debug.h"
27
28#define DEBUG_TYPE "lanai-disassembler"
29
30using namespace llvm;
31using namespace llvm::MCD;
32
34
36 const MCSubtargetInfo &STI,
37 MCContext &Ctx) {
38 return new LanaiDisassembler(STI, Ctx);
39}
40
47
50
51// clang-format off
52static const unsigned GPRDecoderTable[] = {
53 Lanai::R0, Lanai::R1, Lanai::PC, Lanai::R3, Lanai::SP, Lanai::FP,
54 Lanai::R6, Lanai::R7, Lanai::RV, Lanai::R9, Lanai::RR1, Lanai::RR2,
55 Lanai::R12, Lanai::R13, Lanai::R14, Lanai::RCA, Lanai::R16, Lanai::R17,
56 Lanai::R18, Lanai::R19, Lanai::R20, Lanai::R21, Lanai::R22, Lanai::R23,
57 Lanai::R24, Lanai::R25, Lanai::R26, Lanai::R27, Lanai::R28, Lanai::R29,
58 Lanai::R30, Lanai::R31
59};
60// clang-format on
61
63 uint64_t /*Address*/,
64 const MCDisassembler * /*Decoder*/) {
65 if (RegNo > 31)
67
68 unsigned Reg = GPRDecoderTable[RegNo];
71}
72
73static DecodeStatus decodeRiMemoryValue(MCInst &Inst, unsigned Insn,
74 uint64_t Address,
75 const MCDisassembler *Decoder) {
76 // RI memory values encoded using 23 bits:
77 // 5 bit register, 16 bit constant
78 unsigned Register = (Insn >> 18) & 0x1f;
80 unsigned Offset = (Insn & 0xffff);
82
84}
85
86static DecodeStatus decodeRrMemoryValue(MCInst &Inst, unsigned Insn,
87 uint64_t Address,
88 const MCDisassembler *Decoder) {
89 // RR memory values encoded using 20 bits:
90 // 5 bit register, 5 bit register, 2 bit PQ, 3 bit ALU operator, 5 bit JJJJJ
91 unsigned Register = (Insn >> 15) & 0x1f;
93 Register = (Insn >> 10) & 0x1f;
95
97}
98
99static DecodeStatus decodeSplsValue(MCInst &Inst, unsigned Insn,
100 uint64_t Address,
101 const MCDisassembler *Decoder) {
102 // RI memory values encoded using 17 bits:
103 // 5 bit register, 10 bit constant
104 unsigned Register = (Insn >> 12) & 0x1f;
106 unsigned Offset = (Insn & 0x3ff);
108
110}
111
112static bool tryAddingSymbolicOperand(int64_t Value, bool IsBranch,
113 uint64_t Address, uint64_t Offset,
114 uint64_t Width, MCInst &MI,
115 const MCDisassembler *Decoder) {
116 return Decoder->tryAddingSymbolicOperand(MI, Value, Address, IsBranch, Offset,
117 Width, /*InstSize=*/0);
118}
119
120static DecodeStatus decodeBranch(MCInst &MI, unsigned Insn, uint64_t Address,
121 const MCDisassembler *Decoder) {
122 if (!tryAddingSymbolicOperand(Insn + Address, false, Address, 2, 23, MI,
123 Decoder))
124 MI.addOperand(MCOperand::createImm(Insn));
126}
127
128static DecodeStatus decodeShiftImm(MCInst &Inst, unsigned Insn,
129 uint64_t Address,
130 const MCDisassembler *Decoder) {
131 unsigned Offset = (Insn & 0xffff);
133
135}
136
137static DecodeStatus decodePredicateOperand(MCInst &Inst, unsigned Val,
138 uint64_t Address,
139 const MCDisassembler *Decoder) {
140 if (Val >= LPCC::UNKNOWN)
144}
145
146#include "LanaiGenDisassemblerTables.inc"
147
149 uint32_t &Insn) {
150 // We want to read exactly 4 bytes of data.
151 if (Bytes.size() < 4) {
152 Size = 0;
154 }
155
156 // Encoded as big-endian 32-bit word in the stream.
157 Insn =
158 (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 8) | (Bytes[3] << 0);
159
161}
162
163static void PostOperandDecodeAdjust(MCInst &Instr, uint32_t Insn) {
164 unsigned AluOp = LPAC::ADD;
165 // Fix up for pre and post operations.
166 int PqShift = -1;
167 if (isRMOpcode(Instr.getOpcode()))
168 PqShift = 16;
169 else if (isSPLSOpcode(Instr.getOpcode()))
170 PqShift = 10;
171 else if (isRRMOpcode(Instr.getOpcode())) {
172 PqShift = 16;
173 // Determine RRM ALU op.
174 AluOp = (Insn >> 8) & 0x7;
175 if (AluOp == 7)
176 // Handle JJJJJ
177 // 0b10000 or 0b11000
178 AluOp |= 0x20 | (((Insn >> 3) & 0xf) << 1);
179 }
180
181 if (PqShift != -1) {
182 unsigned PQ = (Insn >> PqShift) & 0x3;
183 switch (PQ) {
184 case 0x0:
185 if (Instr.getOperand(2).isReg()) {
186 Instr.getOperand(2).setReg(Lanai::R0);
187 }
188 if (Instr.getOperand(2).isImm())
189 Instr.getOperand(2).setImm(0);
190 break;
191 case 0x1:
192 AluOp = LPAC::makePostOp(AluOp);
193 break;
194 case 0x2:
195 break;
196 case 0x3:
197 AluOp = LPAC::makePreOp(AluOp);
198 break;
199 }
200 Instr.addOperand(MCOperand::createImm(AluOp));
201 }
202}
203
207 raw_ostream & /*CStream*/) const {
208 uint32_t Insn;
209
210 DecodeStatus Result = readInstruction32(Bytes, Size, Insn);
211
212 if (Result == MCDisassembler::Fail)
214
215 // Call auto-generated decoder function
216 Result =
217 decodeInstruction(DecoderTableLanai32, Instr, Insn, Address, this, STI);
218
219 if (Result != MCDisassembler::Fail) {
220 PostOperandDecodeAdjust(Instr, Insn);
221 Size = 4;
222 return Result;
223 }
224
226}
MCDisassembler::DecodeStatus DecodeStatus
static bool readInstruction32(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn)
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static bool tryAddingSymbolicOperand(uint64_t Address, int32_t Value, bool isBranch, uint64_t InstSize, MCInst &MI, const MCDisassembler *Decoder)
tryAddingSymbolicOperand - trys to add a symbolic operand in place of the immediate Value in the MCIn...
static const uint16_t GPRDecoderTable[]
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
static DecodeStatus decodeRrMemoryValue(MCInst &Inst, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeSplsValue(MCInst &Inst, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
static void PostOperandDecodeAdjust(MCInst &Instr, uint32_t Insn)
static DecodeStatus decodeRiMemoryValue(MCInst &Inst, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createLanaiDisassembler(const Target &, const MCSubtargetInfo &STI, MCContext &Ctx)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiDisassembler()
static DecodeStatus decodeShiftImm(MCInst &Inst, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodePredicateOperand(MCInst &Inst, unsigned Val, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeBranch(MCInst &MI, unsigned Insn, uint64_t Address, const MCDisassembler *Decoder)
Register Reg
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
LanaiDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
MCDisassembler::DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const override
Returns the disassembly of a single instruction.
Context object for machine code objects.
Definition MCContext.h:83
Superclass for all disassemblers.
MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) const
const MCSubtargetInfo & STI
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.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static unsigned makePostOp(unsigned AluOp)
static unsigned makePreOp(unsigned AluOp)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
static bool isRMOpcode(unsigned Opcode)
Target & getTheLanaiTarget()
static bool isRRMOpcode(unsigned Opcode)
static bool isSPLSOpcode(unsigned Opcode)
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition MathExtras.h:565
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.