|
@@ -21,23 +21,25 @@ namespace llvm {
|
|
|
|
|
|
/// Utility function to encode a SLEB128 value to an output stream.
|
|
/// Utility function to encode a SLEB128 value to an output stream.
|
|
inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
|
|
inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
|
|
- unsigned Padding = 0) {
|
|
|
|
|
|
+ unsigned PadTo = 0) {
|
|
bool More;
|
|
bool More;
|
|
|
|
+ unsigned Count = 0;
|
|
do {
|
|
do {
|
|
uint8_t Byte = Value & 0x7f;
|
|
uint8_t Byte = Value & 0x7f;
|
|
// NOTE: this assumes that this signed shift is an arithmetic right shift.
|
|
// NOTE: this assumes that this signed shift is an arithmetic right shift.
|
|
Value >>= 7;
|
|
Value >>= 7;
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
|
- if (More || Padding != 0)
|
|
|
|
|
|
+ Count++;
|
|
|
|
+ if (More || Count < PadTo)
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
OS << char(Byte);
|
|
OS << char(Byte);
|
|
} while (More);
|
|
} while (More);
|
|
|
|
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
|
- if (Padding != 0) {
|
|
|
|
|
|
+ if (Count < PadTo) {
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
|
- for (; Padding != 1; --Padding)
|
|
|
|
|
|
+ for (; Count < PadTo - 1; ++Count)
|
|
OS << char(PadValue | 0x80);
|
|
OS << char(PadValue | 0x80);
|
|
OS << char(PadValue);
|
|
OS << char(PadValue);
|
|
}
|
|
}
|
|
@@ -45,8 +47,9 @@ inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
|
|
|
|
|
|
/// Utility function to encode a SLEB128 value to a buffer. Returns
|
|
/// Utility function to encode a SLEB128 value to a buffer. Returns
|
|
/// the length in bytes of the encoded value.
|
|
/// the length in bytes of the encoded value.
|
|
-inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
|
|
|
|
|
|
+inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
|
|
uint8_t *orig_p = p;
|
|
uint8_t *orig_p = p;
|
|
|
|
+ unsigned Count = 0;
|
|
bool More;
|
|
bool More;
|
|
do {
|
|
do {
|
|
uint8_t Byte = Value & 0x7f;
|
|
uint8_t Byte = Value & 0x7f;
|
|
@@ -54,15 +57,16 @@ inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
|
|
Value >>= 7;
|
|
Value >>= 7;
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
|
((Value == -1) && ((Byte & 0x40) != 0))));
|
|
- if (More || Padding != 0)
|
|
|
|
|
|
+ Count++;
|
|
|
|
+ if (More || Count < PadTo)
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
*p++ = Byte;
|
|
*p++ = Byte;
|
|
} while (More);
|
|
} while (More);
|
|
|
|
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
|
// Pad with 0x80 and emit a terminating byte at the end.
|
|
- if (Padding != 0) {
|
|
|
|
|
|
+ if (Count < PadTo) {
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
|
uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
|
|
- for (; Padding != 1; --Padding)
|
|
|
|
|
|
+ for (; Count < PadTo - 1; ++Count)
|
|
*p++ = (PadValue | 0x80);
|
|
*p++ = (PadValue | 0x80);
|
|
*p++ = PadValue;
|
|
*p++ = PadValue;
|
|
}
|
|
}
|
|
@@ -71,42 +75,48 @@ inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
|
|
|
|
|
|
/// Utility function to encode a ULEB128 value to an output stream.
|
|
/// Utility function to encode a ULEB128 value to an output stream.
|
|
inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
|
|
inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
|
|
- unsigned Padding = 0) {
|
|
|
|
|
|
+ unsigned PadTo = 0) {
|
|
|
|
+ unsigned Count = 0;
|
|
do {
|
|
do {
|
|
uint8_t Byte = Value & 0x7f;
|
|
uint8_t Byte = Value & 0x7f;
|
|
Value >>= 7;
|
|
Value >>= 7;
|
|
- if (Value != 0 || Padding != 0)
|
|
|
|
|
|
+ Count++;
|
|
|
|
+ if (Value != 0 || Count < PadTo)
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
OS << char(Byte);
|
|
OS << char(Byte);
|
|
} while (Value != 0);
|
|
} while (Value != 0);
|
|
|
|
|
|
// Pad with 0x80 and emit a null byte at the end.
|
|
// Pad with 0x80 and emit a null byte at the end.
|
|
- if (Padding != 0) {
|
|
|
|
- for (; Padding != 1; --Padding)
|
|
|
|
|
|
+ if (Count < PadTo) {
|
|
|
|
+ for (; Count < PadTo - 1; ++Count)
|
|
OS << '\x80';
|
|
OS << '\x80';
|
|
OS << '\x00';
|
|
OS << '\x00';
|
|
|
|
+ Count++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Utility function to encode a ULEB128 value to a buffer. Returns
|
|
/// Utility function to encode a ULEB128 value to a buffer. Returns
|
|
/// the length in bytes of the encoded value.
|
|
/// the length in bytes of the encoded value.
|
|
inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
|
|
inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
|
|
- unsigned Padding = 0) {
|
|
|
|
|
|
+ unsigned PadTo = 0) {
|
|
uint8_t *orig_p = p;
|
|
uint8_t *orig_p = p;
|
|
|
|
+ unsigned Count = 0;
|
|
do {
|
|
do {
|
|
uint8_t Byte = Value & 0x7f;
|
|
uint8_t Byte = Value & 0x7f;
|
|
Value >>= 7;
|
|
Value >>= 7;
|
|
- if (Value != 0 || Padding != 0)
|
|
|
|
|
|
+ Count++;
|
|
|
|
+ if (Value != 0 || Count < PadTo)
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
*p++ = Byte;
|
|
*p++ = Byte;
|
|
} while (Value != 0);
|
|
} while (Value != 0);
|
|
|
|
|
|
// Pad with 0x80 and emit a null byte at the end.
|
|
// Pad with 0x80 and emit a null byte at the end.
|
|
- if (Padding != 0) {
|
|
|
|
- for (; Padding != 1; --Padding)
|
|
|
|
|
|
+ if (Count < PadTo) {
|
|
|
|
+ for (; Count < PadTo - 1; ++Count)
|
|
*p++ = '\x80';
|
|
*p++ = '\x80';
|
|
*p++ = '\x00';
|
|
*p++ = '\x00';
|
|
}
|
|
}
|
|
|
|
+
|
|
return (unsigned)(p - orig_p);
|
|
return (unsigned)(p - orig_p);
|
|
}
|
|
}
|
|
|
|
|