@@ -775,6 +775,33 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
775
775
776
776
Indentation += 2 ;
777
777
778
+ // Emit ULEB128 encoded value to OS, returning the number of bytes emitted.
779
+ auto emitULEB128 = [](DecoderTable::const_iterator I,
780
+ formatted_raw_ostream &OS) {
781
+ unsigned Len = 0 ;
782
+ while (*I >= 128 ) {
783
+ OS << (unsigned )*I++ << " , " ;
784
+ Len++;
785
+ }
786
+ OS << (unsigned )*I++ << " , " ;
787
+ return Len + 1 ;
788
+ };
789
+
790
+ // Emit 24-bit numtoskip value to OS, returning the NumToSkip value.
791
+ auto emitNumToSkip = [](DecoderTable::const_iterator I,
792
+ formatted_raw_ostream &OS) {
793
+ uint8_t Byte = *I++;
794
+ uint32_t NumToSkip = Byte;
795
+ OS << (unsigned )Byte << " , " ;
796
+ Byte = *I++;
797
+ OS << (unsigned )Byte << " , " ;
798
+ NumToSkip |= Byte << 8 ;
799
+ Byte = *I++;
800
+ OS << utostr (Byte) << " , " ;
801
+ NumToSkip |= Byte << 16 ;
802
+ return NumToSkip;
803
+ };
804
+
778
805
// FIXME: We may be able to use the NumToSkip values to recover
779
806
// appropriate indentation levels.
780
807
DecoderTable::const_iterator I = Table.begin ();
@@ -794,14 +821,11 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
794
821
OS.indent (Indentation) << " MCD::OPC_ExtractField, " ;
795
822
796
823
// ULEB128 encoded start value.
797
- uint8_t Buffer[16 ], *P = Buffer;
798
- while ((*P++ = *I++) >= 128 )
799
- assert ((P - Buffer) <= (ptrdiff_t )sizeof (Buffer) &&
800
- " ULEB128 value too large!" );
801
- unsigned Start = decodeULEB128 (Buffer);
802
- for (P = Buffer; *P >= 128 ; ++P)
803
- OS << (unsigned )*P << " , " ;
804
- OS << (unsigned )*P << " , " ;
824
+ const char *ErrMsg = nullptr ;
825
+ unsigned Start = decodeULEB128 (Table.data () + Pos + 1 , nullptr ,
826
+ Table.data () + Table.size (), &ErrMsg);
827
+ assert (ErrMsg == nullptr && " ULEB128 value too large!" );
828
+ I += emitULEB128 (I, OS);
805
829
806
830
unsigned Len = *I++;
807
831
OS << Len << " , // Inst{" ;
@@ -814,91 +838,58 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
814
838
++I;
815
839
OS.indent (Indentation) << " MCD::OPC_FilterValue, " ;
816
840
// The filter value is ULEB128 encoded.
817
- while (*I >= 128 )
818
- OS << (unsigned )*I++ << " , " ;
819
- OS << (unsigned )*I++ << " , " ;
841
+ I += emitULEB128 (I, OS);
820
842
821
843
// 24-bit numtoskip value.
822
- uint8_t Byte = *I++;
823
- uint32_t NumToSkip = Byte;
824
- OS << (unsigned )Byte << " , " ;
825
- Byte = *I++;
826
- OS << (unsigned )Byte << " , " ;
827
- NumToSkip |= Byte << 8 ;
828
- Byte = *I++;
829
- OS << utostr (Byte) << " , " ;
830
- NumToSkip |= Byte << 16 ;
844
+ uint32_t NumToSkip = emitNumToSkip (I, OS);
845
+ I += 3 ;
831
846
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
832
847
break ;
833
848
}
834
849
case MCD::OPC_CheckField: {
835
850
++I;
836
851
OS.indent (Indentation) << " MCD::OPC_CheckField, " ;
837
852
// ULEB128 encoded start value.
838
- for (; *I >= 128 ; ++I)
839
- OS << (unsigned )*I << " , " ;
840
- OS << (unsigned )*I++ << " , " ;
853
+ I += emitULEB128 (I, OS);
841
854
// 8-bit length.
842
855
unsigned Len = *I++;
843
856
OS << Len << " , " ;
844
857
// ULEB128 encoded field value.
845
- for (; *I >= 128 ; ++I)
846
- OS << (unsigned )*I << " , " ;
847
- OS << (unsigned )*I++ << " , " ;
858
+ I += emitULEB128 (I, OS);
859
+
848
860
// 24-bit numtoskip value.
849
- uint8_t Byte = *I++;
850
- uint32_t NumToSkip = Byte;
851
- OS << (unsigned )Byte << " , " ;
852
- Byte = *I++;
853
- OS << (unsigned )Byte << " , " ;
854
- NumToSkip |= Byte << 8 ;
855
- Byte = *I++;
856
- OS << utostr (Byte) << " , " ;
857
- NumToSkip |= Byte << 16 ;
861
+ uint32_t NumToSkip = emitNumToSkip (I, OS);
862
+ I += 3 ;
858
863
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
859
864
break ;
860
865
}
861
866
case MCD::OPC_CheckPredicate: {
862
867
++I;
863
868
OS.indent (Indentation) << " MCD::OPC_CheckPredicate, " ;
864
- for (; *I >= 128 ; ++I)
865
- OS << (unsigned )*I << " , " ;
866
- OS << (unsigned )*I++ << " , " ;
869
+ I += emitULEB128 (I, OS);
867
870
868
871
// 24-bit numtoskip value.
869
- uint8_t Byte = *I++;
870
- uint32_t NumToSkip = Byte;
871
- OS << (unsigned )Byte << " , " ;
872
- Byte = *I++;
873
- OS << (unsigned )Byte << " , " ;
874
- NumToSkip |= Byte << 8 ;
875
- Byte = *I++;
876
- OS << utostr (Byte) << " , " ;
877
- NumToSkip |= Byte << 16 ;
872
+ uint32_t NumToSkip = emitNumToSkip (I, OS);
873
+ I += 3 ;
878
874
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
879
875
break ;
880
876
}
881
877
case MCD::OPC_Decode:
882
878
case MCD::OPC_TryDecode: {
883
879
bool IsTry = *I == MCD::OPC_TryDecode;
884
880
++I;
885
- // Extract the ULEB128 encoded Opcode to a buffer.
886
- uint8_t Buffer[16 ], *p = Buffer;
887
- while ((*p++ = *I++) >= 128 )
888
- assert ((p - Buffer) <= (ptrdiff_t )sizeof (Buffer)
889
- && " ULEB128 value too large!" );
890
881
// Decode the Opcode value.
891
- unsigned Opc = decodeULEB128 (Buffer);
882
+ const char *ErrMsg = nullptr ;
883
+ unsigned Opc = decodeULEB128 (Table.data () + Pos + 1 , nullptr ,
884
+ Table.data () + Table.size (), &ErrMsg);
885
+ assert (ErrMsg == nullptr && " ULEB128 value too large!" );
886
+
892
887
OS.indent (Indentation) << " MCD::OPC_" << (IsTry ? " Try" : " " )
893
888
<< " Decode, " ;
894
- for (p = Buffer; *p >= 128 ; ++p)
895
- OS << (unsigned )*p << " , " ;
896
- OS << (unsigned )*p << " , " ;
889
+ I += emitULEB128 (I, OS);
897
890
898
891
// Decoder index.
899
- for (; *I >= 128 ; ++I)
900
- OS << (unsigned )*I << " , " ;
901
- OS << (unsigned )*I++ << " , " ;
892
+ I += emitULEB128 (I, OS);
902
893
903
894
if (!IsTry) {
904
895
OS << " // Opcode: " << NumberedEncodings[Opc] << " \n " ;
@@ -908,15 +899,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
908
899
// Fallthrough for OPC_TryDecode.
909
900
910
901
// 24-bit numtoskip value.
911
- uint8_t Byte = *I++;
912
- uint32_t NumToSkip = Byte;
913
- OS << (unsigned )Byte << " , " ;
914
- Byte = *I++;
915
- OS << (unsigned )Byte << " , " ;
916
- NumToSkip |= Byte << 8 ;
917
- Byte = *I++;
918
- OS << utostr (Byte) << " , " ;
919
- NumToSkip |= Byte << 16 ;
902
+ uint32_t NumToSkip = emitNumToSkip (I, OS);
903
+ I += 3 ;
920
904
921
905
OS << " // Opcode: " << NumberedEncodings[Opc]
922
906
<< " , skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
0 commit comments