<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 15, 2014 at 2:51 PM, Nick Kledzik <span dir="ltr"><<a href="mailto:kledzik@apple.com" target="_blank">kledzik@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: kledzik<br>
Date: Mon Sep 15 16:51:49 2014<br>
New Revision: 217830<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=217830&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=217830&view=rev</a><br>
Log:<br>
[Support] add decodeSLEB128()<br>
<br>
We already have routines to encode SLEB128 as well as encode/decode ULEB128.<br>
This last function fills out the matrix.  I'll need this for some llvm-objdump<br>
work I am doing.<br>
<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Support/LEB128.h<br>
    llvm/trunk/unittests/Support/LEB128Test.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Support/LEB128.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/LEB128.h?rev=217830&r1=217829&r2=217830&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/LEB128.h?rev=217830&r1=217829&r2=217830&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/LEB128.h (original)<br>
+++ llvm/trunk/include/llvm/Support/LEB128.h Mon Sep 15 16:51:49 2014<br>
@@ -90,6 +90,26 @@ inline uint64_t decodeULEB128(const uint<br>
   return Value;<br>
 }<br>
<br>
+/// Utility function to decode a SLEB128 value.<br>
+inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr) {<br>
+  const uint8_t *orig_p = p;<br>
+  int64_t Value = 0;<br>
+  unsigned Shift = 0;<br>
+  uint8_t Byte;<br>
+  do {<br>
+    Byte = *p++;<br>
+    Value |= ((Byte & 0x7f) << Shift);<br>
+    Shift += 7;<br>
+  } while (Byte >= 128);<br>
+  // Sign extend negative numbers.<br>
+  if (Byte & 0x40)<br>
+    Value |= (-1LL) << Shift;<br></blockquote><div><br></div><div>^^</div><div>UBSan marks left shifts of negative values as undefined behavior. I've changed this to -1ULL in r217974.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+  if (n)<br>
+    *n = (unsigned)(p - orig_p);<br>
+  return Value;<br>
+}<br>
+<br>
+<br>
 /// Utility function to get the size of the ULEB128-encoded value.<br>
 extern unsigned getULEB128Size(uint64_t Value);<br>
<br>
<br>
Modified: llvm/trunk/unittests/Support/LEB128Test.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/LEB128Test.cpp?rev=217830&r1=217829&r2=217830&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/LEB128Test.cpp?rev=217830&r1=217829&r2=217830&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/Support/LEB128Test.cpp (original)<br>
+++ llvm/trunk/unittests/Support/LEB128Test.cpp Mon Sep 15 16:51:49 2014<br>
@@ -119,6 +119,42 @@ TEST(LEB128Test, DecodeULEB128) {<br>
 #undef EXPECT_DECODE_ULEB128_EQ<br>
 }<br>
<br>
+TEST(LEB128Test, DecodeSLEB128) {<br>
+#define EXPECT_DECODE_SLEB128_EQ(EXPECTED, VALUE) \<br>
+  do { \<br>
+    unsigned ActualSize = 0; \<br>
+    int64_t Actual = decodeSLEB128(reinterpret_cast<const uint8_t *>(VALUE), \<br>
+                                    &ActualSize); \<br>
+    EXPECT_EQ(sizeof(VALUE) - 1, ActualSize); \<br>
+    EXPECT_EQ(EXPECTED, Actual); \<br>
+  } while (0)<br>
+<br>
+  // Decode SLEB128<br>
+  EXPECT_DECODE_SLEB128_EQ(0L, "\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(1L, "\x01");<br>
+  EXPECT_DECODE_SLEB128_EQ(63L, "\x3f");<br>
+  EXPECT_DECODE_SLEB128_EQ(-64L, "\x40");<br>
+  EXPECT_DECODE_SLEB128_EQ(-63L, "\x41");<br>
+  EXPECT_DECODE_SLEB128_EQ(-1L, "\x7f");<br>
+  EXPECT_DECODE_SLEB128_EQ(128L, "\x80\x01");<br>
+  EXPECT_DECODE_SLEB128_EQ(129L, "\x81\x01");<br>
+  EXPECT_DECODE_SLEB128_EQ(-129L, "\xff\x7e");<br>
+  EXPECT_DECODE_SLEB128_EQ(-128L, "\x80\x7f");<br>
+  EXPECT_DECODE_SLEB128_EQ(-127L, "\x81\x7f");<br>
+  EXPECT_DECODE_SLEB128_EQ(64L, "\xc0\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(-12345L, "\xc7\x9f\x7f");<br>
+<br>
+  // Decode unnormalized SLEB128 with extra padding bytes.<br>
+  EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x80\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(0x7fL, "\xff\x80\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x00");<br>
+  EXPECT_DECODE_SLEB128_EQ(0x80L, "\x80\x81\x80\x00");<br>
+<br>
+#undef EXPECT_DECODE_SLEB128_EQ<br>
+}<br>
+<br>
 TEST(LEB128Test, SLEB128Size) {<br>
   // Positive Value Testing Plan:<br>
   // (1) 128 ^ n - 1 ........ need (n+1) bytes<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div>
</div></div>