Skip to content

[Support] Add decodeULEB128AndInc/decodeSLEB128AndInc #85739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/include/llvm/Support/LEB128.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
return Value;
}

inline uint64_t decodeULEB128AndInc(const uint8_t *&p) {
unsigned n;
auto ret = decodeULEB128(p, &n);
p += n;
return ret;
}

inline int64_t decodeSLEB128AndInc(const uint8_t *&p) {
unsigned n;
auto ret = decodeSLEB128(p, &n);
p += n;
return ret;
}

/// Utility function to get the size of the ULEB128-encoded value.
extern unsigned getULEB128Size(uint64_t Value);

Expand Down
15 changes: 15 additions & 0 deletions llvm/unittests/Support/LEB128Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ TEST(LEB128Test, DecodeInvalidSLEB128) {
#undef EXPECT_INVALID_SLEB128
}

TEST(LEB128Test, DecodeAndInc) {
#define EXPECT_LEB128(FUN, VALUE, SIZE) \
do { \
const uint8_t *V = reinterpret_cast<const uint8_t *>(VALUE), *P = V; \
auto Expected = FUN(P), Actual = FUN##AndInc(P); \
EXPECT_EQ(Actual, Expected); \
EXPECT_EQ(P - V, SIZE); \
} while (0)
EXPECT_LEB128(decodeULEB128, "\x7f", 1);
EXPECT_LEB128(decodeULEB128, "\x80\x01", 2);
EXPECT_LEB128(decodeSLEB128, "\x7f", 1);
EXPECT_LEB128(decodeSLEB128, "\x80\x01", 2);
#undef EXPECT_LEB128
}

TEST(LEB128Test, SLEB128Size) {
// Positive Value Testing Plan:
// (1) 128 ^ n - 1 ........ need (n+1) bytes
Expand Down
33 changes: 10 additions & 23 deletions llvm/utils/TableGen/DecoderEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2283,10 +2283,8 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
}
case MCD::OPC_CheckField: {
// Decode the start value.
unsigned Len;
unsigned Start = decodeULEB128(++Ptr, &Len);
Ptr += Len;
Len = *Ptr;)";
unsigned Start = decodeULEB128AndInc(++Ptr);
unsigned Len = *Ptr;)";
if (IsVarLenInst)
OS << "\n makeUp(insn, Start + Len);";
OS << R"(
Expand All @@ -2311,10 +2309,8 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
break;
}
case MCD::OPC_CheckPredicate: {
unsigned Len;
// Decode the Predicate Index value.
unsigned PIdx = decodeULEB128(++Ptr, &Len);
Ptr += Len;
unsigned PIdx = decodeULEB128AndInc(++Ptr);
// NumToSkip is a plain 24-bit integer.
unsigned NumToSkip = *Ptr++;
NumToSkip |= (*Ptr++) << 8;
Expand All @@ -2330,18 +2326,15 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
break;
}
case MCD::OPC_Decode: {
unsigned Len;
// Decode the Opcode value.
unsigned Opc = decodeULEB128(++Ptr, &Len);
Ptr += Len;
unsigned DecodeIdx = decodeULEB128(Ptr, &Len);
Ptr += Len;
unsigned Opc = decodeULEB128AndInc(++Ptr);
unsigned DecodeIdx = decodeULEB128AndInc(Ptr);

MI.clear();
MI.setOpcode(Opc);
bool DecodeComplete;)";
if (IsVarLenInst) {
OS << "\n Len = InstrLenTable[Opc];\n"
OS << "\n unsigned Len = InstrLenTable[Opc];\n"
<< " makeUp(insn, Len);";
}
OS << R"(
Expand All @@ -2354,12 +2347,9 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
return S;
}
case MCD::OPC_TryDecode: {
unsigned Len;
// Decode the Opcode value.
unsigned Opc = decodeULEB128(++Ptr, &Len);
Ptr += Len;
unsigned DecodeIdx = decodeULEB128(Ptr, &Len);
Ptr += Len;
unsigned Opc = decodeULEB128AndInc(++Ptr);
unsigned DecodeIdx = decodeULEB128AndInc(Ptr);
// NumToSkip is a plain 24-bit integer.
unsigned NumToSkip = *Ptr++;
NumToSkip |= (*Ptr++) << 8;
Expand Down Expand Up @@ -2391,11 +2381,8 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
}
case MCD::OPC_SoftFail: {
// Decode the mask values.
unsigned Len;
uint64_t PositiveMask = decodeULEB128(++Ptr, &Len);
Ptr += Len;
uint64_t NegativeMask = decodeULEB128(Ptr, &Len);
Ptr += Len;
uint64_t PositiveMask = decodeULEB128AndInc(++Ptr);
uint64_t NegativeMask = decodeULEB128AndInc(Ptr);
bool Fail = (insn & PositiveMask) != 0 || (~insn & NegativeMask) != 0;
if (Fail)
S = MCDisassembler::SoftFail;
Expand Down