Skip to content

Commit 92723d5

Browse files
committed
[RISCV][NFC] Simplify decoding code of disassembler
The decoding parts are reduplicative, we add a macro to simplify the code. Reviewed By: craig.topper, kito-cheng Differential Revision: https://reviews.llvm.org/D151309
1 parent cc2d061 commit 92723d5

File tree

1 file changed

+72
-146
lines changed

1 file changed

+72
-146
lines changed

llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp

Lines changed: 72 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,27 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
490490
uint32_t Insn;
491491
DecodeStatus Result;
492492

493+
#define TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, \
494+
DESC, ADDITIONAL_OPERATION) \
495+
do { \
496+
if (FEATURE_CHECKS) { \
497+
LLVM_DEBUG(dbgs() << "Trying " DESC ":\n"); \
498+
Result = decodeInstruction(DECODER_TABLE, MI, Insn, Address, this, STI); \
499+
if (Result != MCDisassembler::Fail) { \
500+
ADDITIONAL_OPERATION; \
501+
return Result; \
502+
} \
503+
} \
504+
} while (false)
505+
#define TRY_TO_DECODE_AND_ADD_SP(FEATURE_CHECKS, DECODER_TABLE, DESC) \
506+
TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
507+
addSPOperands(MI))
508+
#define TRY_TO_DECODE(FEATURE_CHECKS, DECODER_TABLE, DESC) \
509+
TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
510+
(void)nullptr)
511+
#define TRY_TO_DECODE_FEATURE(FEATURE, DECODER_TABLE, DESC) \
512+
TRY_TO_DECODE(STI.hasFeature(FEATURE), DECODER_TABLE, DESC)
513+
493514
// It's a 32 bit instruction if bit 0 and 1 are 1.
494515
if ((Bytes[0] & 0x3) == 0x3) {
495516
if (Bytes.size() < 4) {
@@ -500,116 +521,46 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
500521

501522
Insn = support::endian::read32le(Bytes.data());
502523

503-
if (STI.hasFeature(RISCV::FeatureStdExtZdinx) &&
504-
!STI.hasFeature(RISCV::Feature64Bit)) {
505-
LLVM_DEBUG(dbgs() << "Trying RV32Zdinx table (Double in Integer and"
506-
"rv32)\n");
507-
Result = decodeInstruction(DecoderTableRV32Zdinx32, MI, Insn, Address,
508-
this, STI);
509-
if (Result != MCDisassembler::Fail)
510-
return Result;
511-
}
512-
if (STI.hasFeature(RISCV::FeatureStdExtZfinx)) {
513-
LLVM_DEBUG(dbgs() << "Trying RVZfinx table (Float in Integer):\n");
514-
Result = decodeInstruction(DecoderTableRVZfinx32, MI, Insn, Address, this,
515-
STI);
516-
if (Result != MCDisassembler::Fail)
517-
return Result;
518-
}
519-
if (STI.hasFeature(RISCV::FeatureVendorXVentanaCondOps)) {
520-
LLVM_DEBUG(dbgs() << "Trying Ventana custom opcode table:\n");
521-
Result = decodeInstruction(DecoderTableVentana32, MI, Insn, Address, this,
522-
STI);
523-
if (Result != MCDisassembler::Fail)
524-
return Result;
525-
}
526-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadBa)) {
527-
LLVM_DEBUG(dbgs() << "Trying XTHeadBa custom opcode table:\n");
528-
Result = decodeInstruction(DecoderTableTHeadBa32, MI, Insn, Address, this,
529-
STI);
530-
if (Result != MCDisassembler::Fail)
531-
return Result;
532-
}
533-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadBb)) {
534-
LLVM_DEBUG(dbgs() << "Trying XTHeadBb custom opcode table:\n");
535-
Result = decodeInstruction(DecoderTableTHeadBb32, MI, Insn, Address, this,
536-
STI);
537-
if (Result != MCDisassembler::Fail)
538-
return Result;
539-
}
540-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadBs)) {
541-
LLVM_DEBUG(dbgs() << "Trying XTHeadBs custom opcode table:\n");
542-
Result = decodeInstruction(DecoderTableTHeadBs32, MI, Insn, Address, this,
543-
STI);
544-
if (Result != MCDisassembler::Fail)
545-
return Result;
546-
}
547-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadCondMov)) {
548-
LLVM_DEBUG(dbgs() << "Trying XTHeadCondMov custom opcode table:\n");
549-
Result = decodeInstruction(DecoderTableTHeadCondMov32, MI, Insn, Address,
550-
this, STI);
551-
if (Result != MCDisassembler::Fail)
552-
return Result;
553-
}
554-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadCmo)) {
555-
LLVM_DEBUG(dbgs() << "Trying XTHeadCmo custom opcode table:\n");
556-
Result = decodeInstruction(DecoderTableTHeadCmo32, MI, Insn, Address,
557-
this, STI);
558-
if (Result != MCDisassembler::Fail)
559-
return Result;
560-
}
561-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadFMemIdx)) {
562-
LLVM_DEBUG(dbgs() << "Trying XTHeadFMemIdx custom opcode table:\n");
563-
Result = decodeInstruction(DecoderTableTHeadFMemIdx32, MI, Insn, Address,
564-
this, STI);
565-
if (Result != MCDisassembler::Fail)
566-
return Result;
567-
}
568-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadMac)) {
569-
LLVM_DEBUG(dbgs() << "Trying XTHeadMac custom opcode table:\n");
570-
Result = decodeInstruction(DecoderTableTHeadMac32, MI, Insn, Address,
571-
this, STI);
572-
if (Result != MCDisassembler::Fail)
573-
return Result;
574-
}
575-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadMemIdx)) {
576-
LLVM_DEBUG(dbgs() << "Trying XTHeadMemIdx custom opcode table:\n");
577-
Result = decodeInstruction(DecoderTableTHeadMemIdx32, MI, Insn, Address,
578-
this, STI);
579-
if (Result != MCDisassembler::Fail)
580-
return Result;
581-
}
582-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadMemPair)) {
583-
LLVM_DEBUG(dbgs() << "Trying XTHeadMemPair custom opcode table:\n");
584-
Result = decodeInstruction(DecoderTableTHeadMemPair32, MI, Insn, Address,
585-
this, STI);
586-
if (Result != MCDisassembler::Fail)
587-
return Result;
588-
}
589-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadSync)) {
590-
LLVM_DEBUG(dbgs() << "Trying XTHeadSync custom opcode table:\n");
591-
Result = decodeInstruction(DecoderTableTHeadSync32, MI, Insn, Address,
592-
this, STI);
593-
if (Result != MCDisassembler::Fail)
594-
return Result;
595-
}
596-
if (STI.hasFeature(RISCV::FeatureVendorXTHeadVdot)) {
597-
LLVM_DEBUG(dbgs() << "Trying XTHeadVdot custom opcode table:\n");
598-
Result =
599-
decodeInstruction(DecoderTableTHeadV32, MI, Insn, Address, this, STI);
600-
if (Result != MCDisassembler::Fail)
601-
return Result;
602-
}
603-
if (STI.hasFeature(RISCV::FeatureVendorXSfvcp)) {
604-
LLVM_DEBUG(dbgs() << "Trying SiFive VCIX custom opcode table:\n");
605-
Result = decodeInstruction(DecoderTableXSfvcp32, MI, Insn, Address, this,
606-
STI);
607-
if (Result != MCDisassembler::Fail)
608-
return Result;
609-
}
524+
TRY_TO_DECODE(STI.hasFeature(RISCV::FeatureStdExtZdinx) &&
525+
!STI.hasFeature(RISCV::Feature64Bit),
526+
DecoderTableRV32Zdinx32,
527+
"RV32Zdinx table (Double in Integer and rv32)");
528+
TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZfinx, DecoderTableRVZfinx32,
529+
"RVZfinx table (Float in Integer)");
530+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXVentanaCondOps,
531+
DecoderTableVentana32, "Ventana custom opcode table");
532+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBa, DecoderTableTHeadBa32,
533+
"XTHeadBa custom opcode table");
534+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBb, DecoderTableTHeadBb32,
535+
"XTHeadBb custom opcode table");
536+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBs, DecoderTableTHeadBs32,
537+
"XTHeadBs custom opcode table");
538+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCondMov,
539+
DecoderTableTHeadCondMov32,
540+
"XTHeadCondMov custom opcode table");
541+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCmo, DecoderTableTHeadCmo32,
542+
"XTHeadCmo custom opcode table");
543+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadFMemIdx,
544+
DecoderTableTHeadFMemIdx32,
545+
"XTHeadFMemIdx custom opcode table");
546+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMac, DecoderTableTHeadMac32,
547+
"XTHeadMac custom opcode table");
548+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemIdx,
549+
DecoderTableTHeadMemIdx32,
550+
"XTHeadMemIdx custom opcode table");
551+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemPair,
552+
DecoderTableTHeadMemPair32,
553+
"XTHeadMemPair custom opcode table");
554+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadSync,
555+
DecoderTableTHeadSync32,
556+
"XTHeadSync custom opcode table");
557+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadVdot, DecoderTableTHeadV32,
558+
"XTHeadVdot custom opcode table");
559+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSfvcp, DecoderTableXSfvcp32,
560+
"SiFive VCIX custom opcode table");
561+
TRY_TO_DECODE(true, DecoderTable32, "RISCV32 table");
610562

611-
LLVM_DEBUG(dbgs() << "Trying RISCV32 table :\n");
612-
return decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
563+
return MCDisassembler::Fail;
613564
}
614565

615566
if (Bytes.size() < 2) {
@@ -619,41 +570,16 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
619570
Size = 2;
620571

621572
Insn = support::endian::read16le(Bytes.data());
622-
623-
if (!STI.hasFeature(RISCV::Feature64Bit)) {
624-
LLVM_DEBUG(
625-
dbgs() << "Trying RISCV32Only_16 table (16-bit Instruction):\n");
626-
// Calling the auto-generated decoder function.
627-
Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Insn, Address,
628-
this, STI);
629-
if (Result != MCDisassembler::Fail) {
630-
addSPOperands(MI);
631-
return Result;
632-
}
633-
}
634-
if (STI.hasFeature(RISCV::FeatureStdExtZcmt)) {
635-
LLVM_DEBUG(
636-
dbgs() << "Trying Zcmt table (16-bit Table Jump Instructions):\n");
637-
Result = decodeInstruction(DecoderTableRVZcmt16, MI, Insn, Address,
638-
this, STI);
639-
if (Result != MCDisassembler::Fail)
640-
return Result;
641-
}
642-
if (STI.hasFeature(RISCV::FeatureStdExtZcmp)) {
643-
LLVM_DEBUG(
644-
dbgs()
645-
<< "Trying Zcmp table (16-bit Push/Pop & Double Move Instructions):\n");
646-
Result =
647-
decodeInstruction(DecoderTableRVZcmp16, MI, Insn, Address, this, STI);
648-
if (Result != MCDisassembler::Fail)
649-
return Result;
650-
}
651-
652-
LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n");
653-
// Calling the auto-generated decoder function.
654-
Result = decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI);
655-
if (Result != MCDisassembler::Fail)
656-
addSPOperands(MI);
657-
658-
return Result;
573+
TRY_TO_DECODE_AND_ADD_SP(!STI.hasFeature(RISCV::Feature64Bit),
574+
DecoderTableRISCV32Only_16,
575+
"RISCV32Only_16 table (16-bit Instruction)");
576+
TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZcmt, DecoderTableRVZcmt16,
577+
"Zcmt table (16-bit Table Jump Instructions)");
578+
TRY_TO_DECODE_FEATURE(
579+
RISCV::FeatureStdExtZcmp, DecoderTableRVZcmp16,
580+
"Zcmp table (16-bit Push/Pop & Double Move Instructions)");
581+
TRY_TO_DECODE_AND_ADD_SP(true, DecoderTable16,
582+
"RISCV_C table (16-bit Instruction)");
583+
584+
return MCDisassembler::Fail;
659585
}

0 commit comments

Comments
 (0)