@@ -487,6 +487,46 @@ static DecodeStatus decodeVersionImm(MCInst &Inst, unsigned Imm,
487487//
488488// ===----------------------------------------------------------------------===//
489489
490+ template <typename InsnType>
491+ static DecodeStatus
492+ tryDecodeInst (const AMDGPUDisassembler *Asm, const uint8_t *Table, MCInst &MI,
493+ InsnType Inst, uint64_t Address, raw_ostream &Comments) {
494+ assert (MI.getOpcode () == 0 );
495+ assert (MI.getNumOperands () == 0 );
496+ MCInst TmpInst;
497+ Asm->setHasLiteral (false );
498+ const auto SavedBytes = Asm->getBytes ();
499+
500+ SmallString<64 > LocalComments;
501+ raw_svector_ostream LocalCommentStream (LocalComments);
502+ Asm->CommentStream = &LocalCommentStream;
503+
504+ DecodeStatus Res = decodeInstruction (Table, TmpInst, Inst, Address, Asm,
505+ Asm->getSubtargetInfo ());
506+
507+ Asm->CommentStream = nullptr ;
508+
509+ if (Res != MCDisassembler::Fail) {
510+ MI = TmpInst;
511+ Comments << LocalComments;
512+ return MCDisassembler::Success;
513+ }
514+ Asm->setBytes (SavedBytes);
515+ return MCDisassembler::Fail;
516+ }
517+
518+ template <typename InsnType>
519+ static DecodeStatus tryDecodeInst (const AMDGPUDisassembler *Asm,
520+ const uint8_t *Table1, const uint8_t *Table2,
521+ MCInst &MI, InsnType Inst, uint64_t Address,
522+ raw_ostream &Comments) {
523+ for (const uint8_t *T : {Table1, Table2}) {
524+ if (DecodeStatus Res = tryDecodeInst (Asm, T, MI, Inst, Address, Comments))
525+ return Res;
526+ }
527+ return MCDisassembler::Fail;
528+ }
529+
490530template <typename T> static inline T eatBytes (ArrayRef<uint8_t >& Bytes) {
491531 assert (Bytes.size () >= sizeof (T));
492532 const auto Res =
@@ -538,17 +578,17 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
538578 DecoderUInt128 DecW = eat12Bytes (Bytes);
539579
540580 if (isGFX11 () &&
541- tryDecodeInst (DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI ,
542- DecW, Address, CS))
581+ tryDecodeInst (this , DecoderTableGFX1196, DecoderTableGFX11_FAKE1696 ,
582+ MI, DecW, Address, CS))
543583 break ;
544584
545585 if (isGFX12 () &&
546- tryDecodeInst (DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI ,
547- DecW, Address, CS))
586+ tryDecodeInst (this , DecoderTableGFX1296, DecoderTableGFX12_FAKE1696 ,
587+ MI, DecW, Address, CS))
548588 break ;
549589
550590 if (isGFX12 () &&
551- tryDecodeInst (DecoderTableGFX12W6496, MI, DecW, Address, CS))
591+ tryDecodeInst (this , DecoderTableGFX12W6496, MI, DecW, Address, CS))
552592 break ;
553593
554594 // Reinitialize Bytes
@@ -557,7 +597,7 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
557597 } else if (Bytes.size () >= 16 &&
558598 STI.hasFeature (AMDGPU::FeatureGFX950Insts)) {
559599 DecoderUInt128 DecW = eat16Bytes (Bytes);
560- if (tryDecodeInst (DecoderTableGFX940128, MI, DecW, Address, CS))
600+ if (tryDecodeInst (this , DecoderTableGFX940128, MI, DecW, Address, CS))
561601 break ;
562602
563603 // Reinitialize Bytes
@@ -568,58 +608,61 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
568608 const uint64_t QW = eatBytes<uint64_t >(Bytes);
569609
570610 if (STI.hasFeature (AMDGPU::FeatureGFX10_BEncoding) &&
571- tryDecodeInst (DecoderTableGFX10_B64, MI, QW, Address, CS))
611+ tryDecodeInst (this , DecoderTableGFX10_B64, MI, QW, Address, CS))
572612 break ;
573613
574614 if (STI.hasFeature (AMDGPU::FeatureUnpackedD16VMem) &&
575- tryDecodeInst (DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
615+ tryDecodeInst (this , DecoderTableGFX80_UNPACKED64, MI, QW, Address,
616+ CS))
576617 break ;
577618
578619 if (STI.hasFeature (AMDGPU::FeatureGFX950Insts) &&
579- tryDecodeInst (DecoderTableGFX95064, MI, QW, Address, CS))
620+ tryDecodeInst (this , DecoderTableGFX95064, MI, QW, Address, CS))
580621 break ;
581622
582623 // Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
583624 // v_mad_mixhi_f16 for FMA variants. Try to decode using this special
584625 // table first so we print the correct name.
585626 if (STI.hasFeature (AMDGPU::FeatureFmaMixInsts) &&
586- tryDecodeInst (DecoderTableGFX9_DL64, MI, QW, Address, CS))
627+ tryDecodeInst (this , DecoderTableGFX9_DL64, MI, QW, Address, CS))
587628 break ;
588629
589630 if (STI.hasFeature (AMDGPU::FeatureGFX940Insts) &&
590- tryDecodeInst (DecoderTableGFX94064, MI, QW, Address, CS))
631+ tryDecodeInst (this , DecoderTableGFX94064, MI, QW, Address, CS))
591632 break ;
592633
593634 if (STI.hasFeature (AMDGPU::FeatureGFX90AInsts) &&
594- tryDecodeInst (DecoderTableGFX90A64, MI, QW, Address, CS))
635+ tryDecodeInst (this , DecoderTableGFX90A64, MI, QW, Address, CS))
595636 break ;
596637
597638 if ((isVI () || isGFX9 ()) &&
598- tryDecodeInst (DecoderTableGFX864, MI, QW, Address, CS))
639+ tryDecodeInst (this , DecoderTableGFX864, MI, QW, Address, CS))
599640 break ;
600641
601- if (isGFX9 () && tryDecodeInst (DecoderTableGFX964, MI, QW, Address, CS))
642+ if (isGFX9 () &&
643+ tryDecodeInst (this , DecoderTableGFX964, MI, QW, Address, CS))
602644 break ;
603645
604- if (isGFX10 () && tryDecodeInst (DecoderTableGFX1064, MI, QW, Address, CS))
646+ if (isGFX10 () &&
647+ tryDecodeInst (this , DecoderTableGFX1064, MI, QW, Address, CS))
605648 break ;
606649
607650 if (isGFX12 () &&
608- tryDecodeInst (DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW ,
609- Address, CS))
651+ tryDecodeInst (this , DecoderTableGFX1264, DecoderTableGFX12_FAKE1664 ,
652+ MI, QW, Address, CS))
610653 break ;
611654
612655 if (isGFX11 () &&
613- tryDecodeInst (DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW ,
614- Address, CS))
656+ tryDecodeInst (this , DecoderTableGFX1164, DecoderTableGFX11_FAKE1664 ,
657+ MI, QW, Address, CS))
615658 break ;
616659
617660 if (isGFX11 () &&
618- tryDecodeInst (DecoderTableGFX11W6464, MI, QW, Address, CS))
661+ tryDecodeInst (this , DecoderTableGFX11W6464, MI, QW, Address, CS))
619662 break ;
620663
621664 if (isGFX12 () &&
622- tryDecodeInst (DecoderTableGFX12W6464, MI, QW, Address, CS))
665+ tryDecodeInst (this , DecoderTableGFX12W6464, MI, QW, Address, CS))
623666 break ;
624667
625668 // Reinitialize Bytes
@@ -631,38 +674,40 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
631674 const uint32_t DW = eatBytes<uint32_t >(Bytes);
632675
633676 if ((isVI () || isGFX9 ()) &&
634- tryDecodeInst (DecoderTableGFX832, MI, DW, Address, CS))
677+ tryDecodeInst (this , DecoderTableGFX832, MI, DW, Address, CS))
635678 break ;
636679
637- if (tryDecodeInst (DecoderTableAMDGPU32, MI, DW, Address, CS))
680+ if (tryDecodeInst (this , DecoderTableAMDGPU32, MI, DW, Address, CS))
638681 break ;
639682
640- if (isGFX9 () && tryDecodeInst (DecoderTableGFX932, MI, DW, Address, CS))
683+ if (isGFX9 () &&
684+ tryDecodeInst (this , DecoderTableGFX932, MI, DW, Address, CS))
641685 break ;
642686
643687 if (STI.hasFeature (AMDGPU::FeatureGFX950Insts) &&
644- tryDecodeInst (DecoderTableGFX95032, MI, DW, Address, CS))
688+ tryDecodeInst (this , DecoderTableGFX95032, MI, DW, Address, CS))
645689 break ;
646690
647691 if (STI.hasFeature (AMDGPU::FeatureGFX90AInsts) &&
648- tryDecodeInst (DecoderTableGFX90A32, MI, DW, Address, CS))
692+ tryDecodeInst (this , DecoderTableGFX90A32, MI, DW, Address, CS))
649693 break ;
650694
651695 if (STI.hasFeature (AMDGPU::FeatureGFX10_BEncoding) &&
652- tryDecodeInst (DecoderTableGFX10_B32, MI, DW, Address, CS))
696+ tryDecodeInst (this , DecoderTableGFX10_B32, MI, DW, Address, CS))
653697 break ;
654698
655- if (isGFX10 () && tryDecodeInst (DecoderTableGFX1032, MI, DW, Address, CS))
699+ if (isGFX10 () &&
700+ tryDecodeInst (this , DecoderTableGFX1032, MI, DW, Address, CS))
656701 break ;
657702
658703 if (isGFX11 () &&
659- tryDecodeInst (DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW ,
660- Address, CS))
704+ tryDecodeInst (this , DecoderTableGFX1132, DecoderTableGFX11_FAKE1632 ,
705+ MI, DW, Address, CS))
661706 break ;
662707
663708 if (isGFX12 () &&
664- tryDecodeInst (DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW ,
665- Address, CS))
709+ tryDecodeInst (this , DecoderTableGFX1232, DecoderTableGFX12_FAKE1632 ,
710+ MI, DW, Address, CS))
666711 break ;
667712 }
668713
0 commit comments