1212// ===----------------------------------------------------------------------===//
1313
1414#include " Disassembler.h"
15+ #include " llvm/ADT/StringExtras.h"
1516#include " llvm/MC/MCAsmInfo.h"
1617#include " llvm/MC/MCContext.h"
1718#include " llvm/MC/MCDisassembler/MCDisassembler.h"
@@ -94,10 +95,8 @@ static bool SkipToToken(StringRef &Str) {
9495 }
9596}
9697
97-
98- static bool ByteArrayFromString (ByteArrayTy &ByteArray,
99- StringRef &Str,
100- SourceMgr &SM) {
98+ static bool byteArrayFromString (ByteArrayTy &ByteArray, StringRef &Str,
99+ SourceMgr &SM, bool HexPairs) {
101100 while (SkipToToken (Str)) {
102101 // Handled by higher level
103102 if (Str[0 ] == ' [' || Str[0 ] == ' ]' )
@@ -109,7 +108,24 @@ static bool ByteArrayFromString(ByteArrayTy &ByteArray,
109108
110109 // Convert to a byte and add to the byte vector.
111110 unsigned ByteVal;
112- if (Value.getAsInteger (0 , ByteVal) || ByteVal > 255 ) {
111+ if (HexPairs) {
112+ if (Next < 2 ) {
113+ SM.PrintMessage (SMLoc::getFromPointer (Value.data ()),
114+ SourceMgr::DK_Error, " expected two hex digits" );
115+ Str = Str.substr (Next);
116+ return true ;
117+ }
118+ Next = 2 ;
119+ unsigned C0 = hexDigitValue (Value[0 ]);
120+ unsigned C1 = hexDigitValue (Value[1 ]);
121+ if (C0 == -1u || C1 == -1u ) {
122+ SM.PrintMessage (SMLoc::getFromPointer (Value.data ()),
123+ SourceMgr::DK_Error, " invalid input token" );
124+ Str = Str.substr (Next);
125+ return true ;
126+ }
127+ ByteVal = C0 * 16 + C1;
128+ } else if (Value.getAsInteger (0 , ByteVal) || ByteVal > 255 ) {
113129 // If we have an error, print it and skip to the end of line.
114130 SM.PrintMessage (SMLoc::getFromPointer (Value.data ()), SourceMgr::DK_Error,
115131 " invalid input token" );
@@ -130,9 +146,8 @@ static bool ByteArrayFromString(ByteArrayTy &ByteArray,
130146int Disassembler::disassemble (const Target &T, const std::string &Triple,
131147 MCSubtargetInfo &STI, MCStreamer &Streamer,
132148 MemoryBuffer &Buffer, SourceMgr &SM,
133- MCContext &Ctx,
134- const MCTargetOptions &MCOptions) {
135-
149+ MCContext &Ctx, const MCTargetOptions &MCOptions,
150+ bool HexPairs) {
136151 std::unique_ptr<const MCRegisterInfo> MRI (T.createMCRegInfo (Triple));
137152 if (!MRI) {
138153 errs () << " error: no register info for target " << Triple << " \n " ;
@@ -188,7 +203,7 @@ int Disassembler::disassemble(const Target &T, const std::string &Triple,
188203 }
189204
190205 // It's a real token, get the bytes and emit them
191- ErrorOccurred |= ByteArrayFromString (ByteArray, Str, SM);
206+ ErrorOccurred |= byteArrayFromString (ByteArray, Str, SM, HexPairs );
192207
193208 if (!ByteArray.first .empty ())
194209 ErrorOccurred |=
0 commit comments