[PATCH] D78796: [Support] Refactor LEB128 encoding into an input iterator

Nicolas Guillemot via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 24 14:05:49 PDT 2020


nlguillemot updated this revision to Diff 259982.
nlguillemot added a comment.

Refactored the encode functions more aggressively by factoring out the common logic for writing to an array or to a raw_ostream:

  diff
  diff --git a/llvm/include/llvm/Support/LEB128.h b/llvm/include/llvm/Support/LEB128.h
  index 729ee5ca745..f65dc919e39 100644
  --- a/llvm/include/llvm/Support/LEB128.h
  +++ b/llvm/include/llvm/Support/LEB128.h
  @@ -168,43 +168,52 @@ public:
     ///@}
   };
   
  +/// Utility function to encode a SLEB128 or ULEB128 value to a buffer. Returns
  +/// the length in bytes of the encoded value.
  +template <class ValueT>
  +unsigned encodeLEB128(const ValueT &Value, bool IsSigned, uint8_t *p,
  +                      unsigned PadTo = 0) {
  +  uint8_t *orig_p = p;
  +  p = std::copy(LEB128InputIterator<ValueT>(Value, IsSigned, PadTo),
  +                LEB128InputIterator<ValueT>(), p);
  +  return (unsigned)(p - orig_p);
  +}
  +
  +/// Utility function to encode a SLEB128 or ULEB128 value to an output stream.
  +/// Returns the length in bytes of the encoded value.
  +template <class ValueT>
  +inline unsigned encodeLEB128(const ValueT &Value, bool IsSigned,
  +                             raw_ostream &OS, unsigned PadTo = 0) {
  +  uint64_t TellBefore = OS.tell();
  +  std::copy(LEB128InputIterator<ValueT>(Value, IsSigned, PadTo),
  +            LEB128InputIterator<ValueT>(), raw_ostream_iterator<uint8_t>(OS));
  +  return (unsigned)(OS.tell() - TellBefore);
  +}
  +
   /// Utility function to encode a SLEB128 value to an output stream. Returns
   /// the length in bytes of the encoded value.
   inline unsigned encodeSLEB128(int64_t Value, raw_ostream &OS,
                                 unsigned PadTo = 0) {
  -  uint64_t TellBefore = OS.tell();
  -  std::copy(LEB128InputIterator<int64_t>(Value, /* IsSigned */ true, PadTo),
  -            LEB128InputIterator<int64_t>(), raw_ostream_iterator<uint8_t>(OS));
  -  return (unsigned)(OS.tell() - TellBefore);
  +  return encodeLEB128(Value, /* IsSigned */ true, OS, PadTo);
   }
  
   /// Utility function to encode a SLEB128 value to a buffer. Returns
   /// the length in bytes of the encoded value.
   inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
  -  uint8_t *orig_p = p;
  -  p = std::copy(LEB128InputIterator<int64_t>(Value, /* IsSigned */ true, PadTo),
  -                LEB128InputIterator<int64_t>(), p);
  -  return (unsigned)(p - orig_p);
  +  return encodeLEB128(Value, /* IsSigned */ true, p, PadTo);
   }
  
   /// Utility function to encode a ULEB128 value to an output stream. Returns
   /// the length in bytes of the encoded value.
   inline unsigned encodeULEB128(uint64_t Value, raw_ostream &OS,
                                 unsigned PadTo = 0) {
  -  uint64_t TellBefore = OS.tell();
  -  std::copy(LEB128InputIterator<uint64_t>(Value, /* IsSigned */ false, PadTo),
  -            LEB128InputIterator<uint64_t>(), raw_ostream_iterator<uint8_t>(OS));
  -  return (unsigned)(OS.tell() - TellBefore);
  +  return encodeLEB128(Value, /* IsSigned */ false, OS, PadTo);
   }
  
   /// Utility function to encode a ULEB128 value to a buffer. Returns
   /// the length in bytes of the encoded value.
   inline unsigned encodeULEB128(uint64_t Value, uint8_t *p, unsigned PadTo = 0) {
  -  uint8_t *orig_p = p;
  -  p = std::copy(
  -      LEB128InputIterator<uint64_t>(Value, /* IsSigned */ false, PadTo),
  -      LEB128InputIterator<uint64_t>(), p);
  -  return (unsigned)(p - orig_p);
  +  return encodeLEB128(Value, /* IsSigned */ false, p, PadTo);
   }
  
   /// Utility function to decode a ULEB128 value.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78796/new/

https://reviews.llvm.org/D78796

Files:
  llvm/include/llvm/Support/LEB128.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78796.259982.patch
Type: text/x-patch
Size: 10180 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200424/1f22f115/attachment.bin>


More information about the llvm-commits mailing list