2012-08-09 01:56:06 +02:00
|
|
|
//===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-09 01:56:06 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares some utility functions for encoding SLEB128 and
|
|
|
|
// ULEB128 values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_SUPPORT_LEB128_H
|
|
|
|
#define LLVM_SUPPORT_LEB128_H
|
2012-08-09 01:56:06 +02:00
|
|
|
|
2012-09-15 20:41:37 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-08-09 01:56:06 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
[DebugInfo] Align comments in debug_loc section
Summary:
This commit updates the BufferByteStreamer, used by DebugLocStream
to buffer bytes/comments to put in the debug_loc section, to
make sure that the Buffer and Comments vectors are synced.
Previously, when an SLEB128 or ULEB128 was emitted together with
a comment, the vectors could be out-of-sync if the LEB encoding
added several entries to the Buffer vectors, while we only added
a single entry to the Comments vector.
The goal with this is to get the comments in the debug_loc
section in the .s file correctly aligned.
Example (using ARM as target):
Instead of
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @ DW_OP_piece
.byte 147 @ 8
.byte 8 @ sub-register DW_OP_regx
.byte 144 @ 257
.byte 129 @ DW_OP_piece
.byte 2 @ 8
.byte 147 @
.byte 8 @
we now get
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
.byte 144 @ sub-register DW_OP_regx
.byte 129 @ 257
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
Reviewers: JDevlieghere, rnk, aprantl
Reviewed By: aprantl
Subscribers: davide, Ka-Ka, uabelho, aemerson, javed.absar, kristof.beyls, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D41763
llvm-svn: 321907
2018-01-05 23:20:30 +01:00
|
|
|
/// 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) {
|
2012-08-09 01:56:06 +02:00
|
|
|
bool More;
|
2017-09-15 22:34:47 +02:00
|
|
|
unsigned Count = 0;
|
2012-08-09 01:56:06 +02:00
|
|
|
do {
|
|
|
|
uint8_t Byte = Value & 0x7f;
|
|
|
|
// NOTE: this assumes that this signed shift is an arithmetic right shift.
|
|
|
|
Value >>= 7;
|
|
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
2017-09-15 22:34:47 +02:00
|
|
|
Count++;
|
|
|
|
if (More || Count < PadTo)
|
2013-07-22 20:26:18 +02:00
|
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
2012-08-09 01:56:06 +02:00
|
|
|
OS << char(Byte);
|
|
|
|
} while (More);
|
2017-02-10 01:02:58 +01:00
|
|
|
|
|
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
2017-09-15 22:34:47 +02:00
|
|
|
if (Count < PadTo) {
|
2017-02-10 01:02:58 +01:00
|
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
2017-09-15 22:34:47 +02:00
|
|
|
for (; Count < PadTo - 1; ++Count)
|
2017-02-10 01:02:58 +01:00
|
|
|
OS << char(PadValue | 0x80);
|
|
|
|
OS << char(PadValue);
|
[DebugInfo] Align comments in debug_loc section
Summary:
This commit updates the BufferByteStreamer, used by DebugLocStream
to buffer bytes/comments to put in the debug_loc section, to
make sure that the Buffer and Comments vectors are synced.
Previously, when an SLEB128 or ULEB128 was emitted together with
a comment, the vectors could be out-of-sync if the LEB encoding
added several entries to the Buffer vectors, while we only added
a single entry to the Comments vector.
The goal with this is to get the comments in the debug_loc
section in the .s file correctly aligned.
Example (using ARM as target):
Instead of
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @ DW_OP_piece
.byte 147 @ 8
.byte 8 @ sub-register DW_OP_regx
.byte 144 @ 257
.byte 129 @ DW_OP_piece
.byte 2 @ 8
.byte 147 @
.byte 8 @
we now get
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
.byte 144 @ sub-register DW_OP_regx
.byte 129 @ 257
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
Reviewers: JDevlieghere, rnk, aprantl
Reviewed By: aprantl
Subscribers: davide, Ka-Ka, uabelho, aemerson, javed.absar, kristof.beyls, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D41763
llvm-svn: 321907
2018-01-05 23:20:30 +01:00
|
|
|
Count++;
|
2017-02-10 01:02:58 +01:00
|
|
|
}
|
[DebugInfo] Align comments in debug_loc section
Summary:
This commit updates the BufferByteStreamer, used by DebugLocStream
to buffer bytes/comments to put in the debug_loc section, to
make sure that the Buffer and Comments vectors are synced.
Previously, when an SLEB128 or ULEB128 was emitted together with
a comment, the vectors could be out-of-sync if the LEB encoding
added several entries to the Buffer vectors, while we only added
a single entry to the Comments vector.
The goal with this is to get the comments in the debug_loc
section in the .s file correctly aligned.
Example (using ARM as target):
Instead of
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @ DW_OP_piece
.byte 147 @ 8
.byte 8 @ sub-register DW_OP_regx
.byte 144 @ 257
.byte 129 @ DW_OP_piece
.byte 2 @ 8
.byte 147 @
.byte 8 @
we now get
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
.byte 144 @ sub-register DW_OP_regx
.byte 129 @ 257
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
Reviewers: JDevlieghere, rnk, aprantl
Reviewed By: aprantl
Subscribers: davide, Ka-Ka, uabelho, aemerson, javed.absar, kristof.beyls, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D41763
llvm-svn: 321907
2018-01-05 23:20:30 +01:00
|
|
|
return Count;
|
2017-02-10 01:02:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Utility function to encode a SLEB128 value to a buffer. Returns
|
|
|
|
/// the length in bytes of the encoded value.
|
2017-09-15 22:34:47 +02:00
|
|
|
inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
|
2017-02-10 01:02:58 +01:00
|
|
|
uint8_t *orig_p = p;
|
2017-09-15 22:34:47 +02:00
|
|
|
unsigned Count = 0;
|
2017-02-10 01:02:58 +01:00
|
|
|
bool More;
|
|
|
|
do {
|
|
|
|
uint8_t Byte = Value & 0x7f;
|
|
|
|
// NOTE: this assumes that this signed shift is an arithmetic right shift.
|
|
|
|
Value >>= 7;
|
|
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
2017-09-15 22:34:47 +02:00
|
|
|
Count++;
|
|
|
|
if (More || Count < PadTo)
|
2017-02-10 01:02:58 +01:00
|
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
|
|
*p++ = Byte;
|
|
|
|
} while (More);
|
|
|
|
|
|
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
2017-09-15 22:34:47 +02:00
|
|
|
if (Count < PadTo) {
|
2017-02-10 01:02:58 +01:00
|
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
2017-09-15 22:34:47 +02:00
|
|
|
for (; Count < PadTo - 1; ++Count)
|
2017-02-10 01:02:58 +01:00
|
|
|
*p++ = (PadValue | 0x80);
|
|
|
|
*p++ = PadValue;
|
|
|
|
}
|
|
|
|
return (unsigned)(p - orig_p);
|
2012-08-09 01:56:06 +02:00
|
|
|
}
|
|
|
|
|
[DebugInfo] Align comments in debug_loc section
Summary:
This commit updates the BufferByteStreamer, used by DebugLocStream
to buffer bytes/comments to put in the debug_loc section, to
make sure that the Buffer and Comments vectors are synced.
Previously, when an SLEB128 or ULEB128 was emitted together with
a comment, the vectors could be out-of-sync if the LEB encoding
added several entries to the Buffer vectors, while we only added
a single entry to the Comments vector.
The goal with this is to get the comments in the debug_loc
section in the .s file correctly aligned.
Example (using ARM as target):
Instead of
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @ DW_OP_piece
.byte 147 @ 8
.byte 8 @ sub-register DW_OP_regx
.byte 144 @ 257
.byte 129 @ DW_OP_piece
.byte 2 @ 8
.byte 147 @
.byte 8 @
we now get
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
.byte 144 @ sub-register DW_OP_regx
.byte 129 @ 257
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
Reviewers: JDevlieghere, rnk, aprantl
Reviewed By: aprantl
Subscribers: davide, Ka-Ka, uabelho, aemerson, javed.absar, kristof.beyls, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D41763
llvm-svn: 321907
2018-01-05 23:20:30 +01:00
|
|
|
/// 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) {
|
2017-09-15 22:34:47 +02:00
|
|
|
unsigned Count = 0;
|
2012-08-09 01:56:06 +02:00
|
|
|
do {
|
|
|
|
uint8_t Byte = Value & 0x7f;
|
|
|
|
Value >>= 7;
|
2017-09-15 22:34:47 +02:00
|
|
|
Count++;
|
|
|
|
if (Value != 0 || Count < PadTo)
|
2013-07-22 20:26:18 +02:00
|
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
2012-08-09 01:56:06 +02:00
|
|
|
OS << char(Byte);
|
|
|
|
} while (Value != 0);
|
|
|
|
|
|
|
|
// Pad with 0x80 and emit a null byte at the end.
|
2017-09-15 22:34:47 +02:00
|
|
|
if (Count < PadTo) {
|
|
|
|
for (; Count < PadTo - 1; ++Count)
|
2012-08-09 01:56:06 +02:00
|
|
|
OS << '\x80';
|
|
|
|
OS << '\x00';
|
2017-09-15 22:34:47 +02:00
|
|
|
Count++;
|
2012-08-09 01:56:06 +02:00
|
|
|
}
|
[DebugInfo] Align comments in debug_loc section
Summary:
This commit updates the BufferByteStreamer, used by DebugLocStream
to buffer bytes/comments to put in the debug_loc section, to
make sure that the Buffer and Comments vectors are synced.
Previously, when an SLEB128 or ULEB128 was emitted together with
a comment, the vectors could be out-of-sync if the LEB encoding
added several entries to the Buffer vectors, while we only added
a single entry to the Comments vector.
The goal with this is to get the comments in the debug_loc
section in the .s file correctly aligned.
Example (using ARM as target):
Instead of
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @ DW_OP_piece
.byte 147 @ 8
.byte 8 @ sub-register DW_OP_regx
.byte 144 @ 257
.byte 129 @ DW_OP_piece
.byte 2 @ 8
.byte 147 @
.byte 8 @
we now get
.byte 144 @ sub-register DW_OP_regx
.byte 128 @ 256
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
.byte 144 @ sub-register DW_OP_regx
.byte 129 @ 257
.byte 2 @
.byte 147 @ DW_OP_piece
.byte 8 @ 8
Reviewers: JDevlieghere, rnk, aprantl
Reviewed By: aprantl
Subscribers: davide, Ka-Ka, uabelho, aemerson, javed.absar, kristof.beyls, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D41763
llvm-svn: 321907
2018-01-05 23:20:30 +01:00
|
|
|
return Count;
|
2012-08-09 01:56:06 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 21:06:05 +02:00
|
|
|
/// Utility function to encode a ULEB128 value to a buffer. Returns
|
|
|
|
/// the length in bytes of the encoded value.
|
2013-07-31 00:35:06 +02:00
|
|
|
inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
|
2017-09-15 22:34:47 +02:00
|
|
|
unsigned PadTo = 0) {
|
2012-08-14 21:06:05 +02:00
|
|
|
uint8_t *orig_p = p;
|
2017-09-15 22:34:47 +02:00
|
|
|
unsigned Count = 0;
|
2012-08-14 21:06:05 +02:00
|
|
|
do {
|
|
|
|
uint8_t Byte = Value & 0x7f;
|
|
|
|
Value >>= 7;
|
2017-09-15 22:34:47 +02:00
|
|
|
Count++;
|
|
|
|
if (Value != 0 || Count < PadTo)
|
2013-07-22 20:26:18 +02:00
|
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
2012-08-14 21:06:05 +02:00
|
|
|
*p++ = Byte;
|
|
|
|
} while (Value != 0);
|
|
|
|
|
|
|
|
// Pad with 0x80 and emit a null byte at the end.
|
2017-09-15 22:34:47 +02:00
|
|
|
if (Count < PadTo) {
|
|
|
|
for (; Count < PadTo - 1; ++Count)
|
2012-08-14 21:06:05 +02:00
|
|
|
*p++ = '\x80';
|
|
|
|
*p++ = '\x00';
|
|
|
|
}
|
2017-09-15 22:34:47 +02:00
|
|
|
|
2012-08-14 21:06:05 +02:00
|
|
|
return (unsigned)(p - orig_p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Utility function to decode a ULEB128 value.
|
2017-03-20 20:46:55 +01:00
|
|
|
inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
|
|
|
|
const uint8_t *end = nullptr,
|
|
|
|
const char **error = nullptr) {
|
2012-08-14 21:06:05 +02:00
|
|
|
const uint8_t *orig_p = p;
|
|
|
|
uint64_t Value = 0;
|
|
|
|
unsigned Shift = 0;
|
2017-04-27 04:09:42 +02:00
|
|
|
if (error)
|
2017-03-20 20:46:55 +01:00
|
|
|
*error = nullptr;
|
2012-08-14 21:06:05 +02:00
|
|
|
do {
|
2020-04-02 14:48:12 +02:00
|
|
|
if (p == end) {
|
2017-04-27 04:09:42 +02:00
|
|
|
if (error)
|
2017-03-20 20:46:55 +01:00
|
|
|
*error = "malformed uleb128, extends past end";
|
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint64_t Slice = *p & 0x7f;
|
2021-01-27 08:36:01 +01:00
|
|
|
if ((Shift >= 64 && Slice != 0) || Slice << Shift >> Shift != Slice) {
|
2017-04-27 04:09:42 +02:00
|
|
|
if (error)
|
2017-03-20 20:46:55 +01:00
|
|
|
*error = "uleb128 too big for uint64";
|
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-27 08:36:01 +01:00
|
|
|
Value += Slice << Shift;
|
2012-08-14 21:06:05 +02:00
|
|
|
Shift += 7;
|
|
|
|
} while (*p++ >= 128);
|
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2014-09-15 23:51:49 +02:00
|
|
|
/// Utility function to decode a SLEB128 value.
|
2017-03-20 20:46:55 +01:00
|
|
|
inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
|
|
|
|
const uint8_t *end = nullptr,
|
|
|
|
const char **error = nullptr) {
|
2014-09-15 23:51:49 +02:00
|
|
|
const uint8_t *orig_p = p;
|
|
|
|
int64_t Value = 0;
|
|
|
|
unsigned Shift = 0;
|
|
|
|
uint8_t Byte;
|
2019-06-25 01:45:18 +02:00
|
|
|
if (error)
|
|
|
|
*error = nullptr;
|
2014-09-15 23:51:49 +02:00
|
|
|
do {
|
2020-04-02 14:48:12 +02:00
|
|
|
if (p == end) {
|
2017-04-27 04:09:42 +02:00
|
|
|
if (error)
|
2017-03-20 20:46:55 +01:00
|
|
|
*error = "malformed sleb128, extends past end";
|
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-27 08:36:01 +01:00
|
|
|
Byte = *p;
|
2021-02-03 21:51:11 +01:00
|
|
|
uint64_t Slice = Byte & 0x7f;
|
2021-01-27 08:36:01 +01:00
|
|
|
if ((Shift >= 64 && Slice != (Value < 0 ? 0x7f : 0x00)) ||
|
|
|
|
(Shift == 63 && Slice != 0 && Slice != 0x7f)) {
|
|
|
|
if (error)
|
|
|
|
*error = "sleb128 too big for int64";
|
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Value |= Slice << Shift;
|
2014-09-15 23:51:49 +02:00
|
|
|
Shift += 7;
|
2021-01-27 08:36:01 +01:00
|
|
|
++p;
|
2014-09-15 23:51:49 +02:00
|
|
|
} while (Byte >= 128);
|
2019-06-25 01:45:18 +02:00
|
|
|
// Sign extend negative numbers if needed.
|
|
|
|
if (Shift < 64 && (Byte & 0x40))
|
2014-09-17 20:23:07 +02:00
|
|
|
Value |= (-1ULL) << Shift;
|
2014-09-15 23:51:49 +02:00
|
|
|
if (n)
|
|
|
|
*n = (unsigned)(p - orig_p);
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:00:39 +01:00
|
|
|
/// Utility function to get the size of the ULEB128-encoded value.
|
2014-02-22 16:39:39 +01:00
|
|
|
extern unsigned getULEB128Size(uint64_t Value);
|
2014-02-22 15:00:39 +01:00
|
|
|
|
|
|
|
/// Utility function to get the size of the SLEB128-encoded value.
|
2014-02-22 16:39:39 +01:00
|
|
|
extern unsigned getSLEB128Size(int64_t Value);
|
2014-02-22 15:00:39 +01:00
|
|
|
|
2017-04-27 04:09:42 +02:00
|
|
|
} // namespace llvm
|
2012-08-09 01:56:06 +02:00
|
|
|
|
2021-02-06 06:02:06 +01:00
|
|
|
#endif // LLVM_SUPPORT_LEB128_H
|