This is an archive of the discontinued LLVM Phabricator instance.

[Support] Refactor LEB128 decoding into an output iterator
Needs ReviewPublic

Authored by nlguillemot on Apr 23 2020, 11:28 PM.

Details

Summary

Refactors the logic for LEB128 decoding into a std iterator style output
iterator. This allows LEB128 decoding to be used as the output of std
algorithms. Furthermore, this patch refactors the existing LEB128 decoding
functions to use this new interface.

Diff Detail

Event Timeline

nlguillemot created this revision.Apr 23 2020, 11:28 PM

The end goal of this series of patches is to support encoding/decoding APInt to/from [U|S]LEB128. This patch is an initial step in that direction. It refactors the logic to avoid code duplication and makes the interface to LEB128 decoding more generic.

In a later patch, I'd like to do some template magic to support LEB128OutputIterator<APInt> in a way that tries to share code with the uint64_t/int64_t implementations.

Forgot to include a few changes in the original diff that was posted.

Added a unit test that uses the output iterator in a std algorithm, just as a sanity test that the class can indeed be used as an output iterator. The code in decode[U|S]LEB128 doesn't use the output iterator with std algorithm, so this extra test was added to get coverage for that use case.

Rebased this patch on top of its parent commit.

Replaced assignments of 0 with assignments of ValueT(), as a more generic way to get a zero value.

diff
diff --git a/llvm/include/llvm/Support/LEB128.h b/llvm/include/llvm/Support/LEB128.h
index c9649365bbd..bb92d664603 100644
--- a/llvm/include/llvm/Support/LEB128.h
+++ b/llvm/include/llvm/Support/LEB128.h
@@ -202,7 +202,7 @@ public:
       : Value(Value), IsSigned(IsSigned), Error(nullptr), Shift(0),
         IsComplete(false) {
     // Initially zero before any decoded bits are written into it.
-    this->Value = 0;
+    this->Value = ValueT();
   }

   /// Decodes one byte from a stream of LEB128-encoded bytes, and stores the
@@ -326,7 +326,7 @@ ValueT decodeLEB128(const uint8_t *p, bool IsSigned, unsigned *n = nullptr,
   if (error)
     *error = found_error;
   if (found_error)
-    return 0;
+    return ValueT();
   return Value;
 }
Herald added a project: Restricted Project. · View Herald TranscriptApr 26 2020, 11:36 AM