[llvm] r217830 - [Support] add decodeSLEB128()

Alexey Samsonov vonosmas at gmail.com
Wed Sep 17 11:33:31 PDT 2014


On Mon, Sep 15, 2014 at 2:51 PM, Nick Kledzik <kledzik at apple.com> wrote:

> Author: kledzik
> Date: Mon Sep 15 16:51:49 2014
> New Revision: 217830
>
> URL: http://llvm.org/viewvc/llvm-project?rev=217830&view=rev
> Log:
> [Support] add decodeSLEB128()
>
> We already have routines to encode SLEB128 as well as encode/decode
> ULEB128.
> This last function fills out the matrix.  I'll need this for some
> llvm-objdump
> work I am doing.
>
>
> Modified:
>     llvm/trunk/include/llvm/Support/LEB128.h
>     llvm/trunk/unittests/Support/LEB128Test.cpp
>
> Modified: llvm/trunk/include/llvm/Support/LEB128.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/LEB128.h?rev=217830&r1=217829&r2=217830&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/LEB128.h (original)
> +++ llvm/trunk/include/llvm/Support/LEB128.h Mon Sep 15 16:51:49 2014
> @@ -90,6 +90,26 @@ inline uint64_t decodeULEB128(const uint
>    return Value;
>  }
>
> +/// Utility function to decode a SLEB128 value.
> +inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr) {
> +  const uint8_t *orig_p = p;
> +  int64_t Value = 0;
> +  unsigned Shift = 0;
> +  uint8_t Byte;
> +  do {
> +    Byte = *p++;
> +    Value |= ((Byte & 0x7f) << Shift);
> +    Shift += 7;
> +  } while (Byte >= 128);
> +  // Sign extend negative numbers.
> +  if (Byte & 0x40)
> +    Value |= (-1LL) << Shift;
>

^^
UBSan marks left shifts of negative values as undefined behavior. I've
changed this to -1ULL in r217974.


> +  if (n)
> +    *n = (unsigned)(p - orig_p);
> +  return Value;
> +}
> +
> +
>  /// Utility function to get the size of the ULEB128-encoded value.
>  extern unsigned getULEB128Size(uint64_t Value);
>
>
> Modified: llvm/trunk/unittests/Support/LEB128Test.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/LEB128Test.cpp?rev=217830&r1=217829&r2=217830&view=diff
>
> ==============================================================================
> --- llvm/trunk/unittests/Support/LEB128Test.cpp (original)
> +++ llvm/trunk/unittests/Support/LEB128Test.cpp Mon Sep 15 16:51:49 2014
> @@ -119,6 +119,42 @@ TEST(LEB128Test, DecodeULEB128) {
>  #undef EXPECT_DECODE_ULEB128_EQ
>  }
>
> +TEST(LEB128Test, DecodeSLEB128) {
> +#define EXPECT_DECODE_SLEB128_EQ(EXPECTED, VALUE) \
> +  do { \
> +    unsigned ActualSize = 0; \
> +    int64_t Actual = decodeSLEB128(reinterpret_cast<const uint8_t
> *>(VALUE), \
> +                                    &ActualSize); \
> +    EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \
> +    EXPECT_EQ(EXPECTED, Actual); \
> +  } while (0)
> +
> +  // Decode SLEB128
> +  EXPECT_DECODE_SLEB128_EQ(0L, "\x00");
> +  EXPECT_DECODE_SLEB128_EQ(1L, "\x01");
> +  EXPECT_DECODE_SLEB128_EQ(63L, "\x3f");
> +  EXPECT_DECODE_SLEB128_EQ(-64L, "\x40");
> +  EXPECT_DECODE_SLEB128_EQ(-63L, "\x41");
> +  EXPECT_DECODE_SLEB128_EQ(-1L, "\x7f");
> +  EXPECT_DECODE_SLEB128_EQ(128L, "\x80\x01");
> +  EXPECT_DECODE_SLEB128_EQ(129L, "\x81\x01");
> +  EXPECT_DECODE_SLEB128_EQ(-129L, "\xff\x7e");
> +  EXPECT_DECODE_SLEB128_EQ(-128L, "\x80\x7f");
> +  EXPECT_DECODE_SLEB128_EQ(-127L, "\x81\x7f");
> +  EXPECT_DECODE_SLEB128_EQ(64L, "\xc0\x00");
> +  EXPECT_DECODE_SLEB128_EQ(-12345L, "\xc7\x9f\x7f");
> +
> +  // Decode unnormalized SLEB128 with extra padding bytes.
> +  EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x00");
> +  EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x80\x00");
> +  EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x00");
> +  EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x80\x00");
> +  EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x00");
> +  EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x80\x00");
> +
> +#undef EXPECT_DECODE_SLEB128_EQ
> +}
> +
>  TEST(LEB128Test, SLEB128Size) {
>    // Positive Value Testing Plan:
>    // (1) 128 ^ n - 1 ........ need (n+1) bytes
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>



-- 
Alexey Samsonov
vonosmas at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140917/41c1a5f7/attachment.html>


More information about the llvm-commits mailing list