diff options
author | Craig Topper <craig.topper@sifive.com> | 2023-05-16 09:43:38 -0700 |
---|---|---|
committer | Craig Topper <craig.topper@sifive.com> | 2023-05-16 09:43:38 -0700 |
commit | 8f43c3f49ecea404458a2fbde126e20c6d404a14 (patch) | |
tree | e76fb37def812af74fd23628273d74cd05d6ae94 /llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp | |
parent | 2f4c96097a3cbf9d07eab699efd25f767bb4fdd5 (diff) | |
download | llvm-8f43c3f49ecea404458a2fbde126e20c6d404a14.zip llvm-8f43c3f49ecea404458a2fbde126e20c6d404a14.tar.gz llvm-8f43c3f49ecea404458a2fbde126e20c6d404a14.tar.bz2 |
[RISCV] Rework how implied SP operands work in the disassembler. NFC
Previously we added the SP operands when an immediate operand was added
to certain opcodes.
This patch moves it to a post processing step using the information
in MCInstrDesc. This avoids an explicit opcode list in RISCVDisassembler.cpp.
In considered using a custom DecoderMethod, but the bit swizzling we
need to do for the immediates on these instructions made that
unattractive.
Reviewed By: asb
Differential Revision: https://reviews.llvm.org/D149931
Diffstat (limited to 'llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp')
-rw-r--r-- | llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp index 03eb101..4f23edb 100644 --- a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp +++ b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp @@ -41,6 +41,9 @@ public: DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address, raw_ostream &CStream) const override; + +private: + void addSPOperands(MCInst &MI) const; }; } // end anonymous namespace @@ -267,30 +270,11 @@ static DecodeStatus decodeVMaskReg(MCInst &Inst, uint64_t RegNo, return MCDisassembler::Success; } -// Add implied SP operand for instructions *SP compressed instructions. The SP -// operand isn't explicitly encoded in the instruction. -static void addImplySP(MCInst &Inst) { - if (Inst.getOpcode() == RISCV::C_LWSP || Inst.getOpcode() == RISCV::C_SWSP || - Inst.getOpcode() == RISCV::C_LDSP || Inst.getOpcode() == RISCV::C_SDSP || - Inst.getOpcode() == RISCV::C_FLWSP || - Inst.getOpcode() == RISCV::C_FSWSP || - Inst.getOpcode() == RISCV::C_FLDSP || - Inst.getOpcode() == RISCV::C_FSDSP || - Inst.getOpcode() == RISCV::C_ADDI4SPN) { - Inst.addOperand(MCOperand::createReg(RISCV::X2)); - } - if (Inst.getOpcode() == RISCV::C_ADDI16SP) { - Inst.addOperand(MCOperand::createReg(RISCV::X2)); - Inst.addOperand(MCOperand::createReg(RISCV::X2)); - } -} - template <unsigned N> static DecodeStatus decodeUImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder) { assert(isUInt<N>(Imm) && "Invalid immediate"); - addImplySP(Inst); Inst.addOperand(MCOperand::createImm(Imm)); return MCDisassembler::Success; } @@ -309,7 +293,6 @@ static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder) { assert(isUInt<N>(Imm) && "Invalid immediate"); - addImplySP(Inst); // Sign-extend the number in the bottom N bits of Imm Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm))); return MCDisassembler::Success; @@ -489,6 +472,15 @@ static DecodeStatus decodeZcmpSpimm(MCInst &Inst, unsigned Imm, return MCDisassembler::Success; } +// Add implied SP operand for C.*SP compressed instructions. The SP operand +// isn't explicitly encoded in the instruction. +void RISCVDisassembler::addSPOperands(MCInst &MI) const { + const MCInstrDesc &MCID = MCII->get(MI.getOpcode()); + for (unsigned i = 0; i < MCID.getNumOperands(); i++) + if (MCID.operands()[i].RegClass == RISCV::SPRegClassID) + MI.insert(MI.begin() + i, MCOperand::createReg(RISCV::X2)); +} + DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address, @@ -634,8 +626,10 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size, // Calling the auto-generated decoder function. Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Insn, Address, this, STI); - if (Result != MCDisassembler::Fail) + if (Result != MCDisassembler::Fail) { + addSPOperands(MI); return Result; + } } if (STI.hasFeature(RISCV::FeatureStdExtZcmt)) { LLVM_DEBUG( @@ -657,5 +651,9 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size, LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n"); // Calling the auto-generated decoder function. - return decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI); + Result = decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI); + if (Result != MCDisassembler::Fail) + addSPOperands(MI); + + return Result; } |