LCOV - code coverage report
Current view: top level - include/llvm/Support - LEB128.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 92 94 97.9 %
Date: 2017-09-14 15:23:50 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file declares some utility functions for encoding SLEB128 and
      11             : // ULEB128 values.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_SUPPORT_LEB128_H
      16             : #define LLVM_SUPPORT_LEB128_H
      17             : 
      18             : #include "llvm/Support/raw_ostream.h"
      19             : 
      20             : namespace llvm {
      21             : 
      22             : /// Utility function to encode a SLEB128 value to an output stream.
      23      355117 : inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
      24             :                           unsigned Padding = 0) {
      25             :   bool More;
      26      626375 :   do {
      27      626375 :     uint8_t Byte = Value & 0x7f;
      28             :     // NOTE: this assumes that this signed shift is an arithmetic right shift.
      29      626375 :     Value >>= 7;
      30     1252750 :     More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
      31      199966 :               ((Value == -1) && ((Byte & 0x40) != 0))));
      32      626375 :     if (More || Padding != 0)
      33      271268 :       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
      34     1252750 :     OS << char(Byte);
      35             :   } while (More);
      36             : 
      37             :   // Pad with 0x80 and emit a terminating byte at the end.
      38      355117 :   if (Padding != 0) {
      39          10 :     uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
      40          20 :     for (; Padding != 1; --Padding)
      41          10 :       OS << char(PadValue | 0x80);
      42          10 :     OS << char(PadValue);
      43             :   }
      44      355117 : }
      45             : 
      46             : /// Utility function to encode a SLEB128 value to a buffer. Returns
      47             : /// the length in bytes of the encoded value.
      48          18 : inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
      49          18 :   uint8_t *orig_p = p;
      50             :   bool More;
      51          26 :   do {
      52          26 :     uint8_t Byte = Value & 0x7f;
      53             :     // NOTE: this assumes that this signed shift is an arithmetic right shift.
      54          26 :     Value >>= 7;
      55          52 :     More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
      56          11 :               ((Value == -1) && ((Byte & 0x40) != 0))));
      57          26 :     if (More || Padding != 0)
      58          18 :       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
      59          26 :     *p++ = Byte;
      60             :   } while (More);
      61             : 
      62             :   // Pad with 0x80 and emit a terminating byte at the end.
      63          18 :   if (Padding != 0) {
      64          10 :     uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
      65          20 :     for (; Padding != 1; --Padding)
      66           5 :       *p++ = (PadValue | 0x80);
      67          10 :     *p++ = PadValue;
      68             :   }
      69          18 :   return (unsigned)(p - orig_p);
      70             : }
      71             : 
      72             : /// Utility function to encode a ULEB128 value to an output stream.
      73     1208562 : inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
      74             :                           unsigned Padding = 0) {
      75             :   do {
      76     1242984 :     uint8_t Byte = Value & 0x7f;
      77     1242984 :     Value >>= 7;
      78     1242984 :     if (Value != 0 || Padding != 0)
      79       35719 :       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
      80     2485968 :     OS << char(Byte);
      81     1242984 :   } while (Value != 0);
      82             : 
      83             :   // Pad with 0x80 and emit a null byte at the end.
      84     1208562 :   if (Padding != 0) {
      85        4427 :     for (; Padding != 1; --Padding)
      86        1565 :       OS << '\x80';
      87             :     OS << '\x00';
      88             :   }
      89     1208562 : }
      90             : 
      91             : /// Utility function to encode a ULEB128 value to a buffer. Returns
      92             : /// the length in bytes of the encoded value.
      93           0 : inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
      94             :                               unsigned Padding = 0) {
      95         312 :   uint8_t *orig_p = p;
      96             :   do {
      97       72893 :     uint8_t Byte = Value & 0x7f;
      98       72894 :     Value >>= 7;
      99       72881 :     if (Value != 0 || Padding != 0)
     100       21128 :       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
     101       72894 :     *p++ = Byte;
     102       72885 :   } while (Value != 0);
     103             : 
     104             :   // Pad with 0x80 and emit a null byte at the end.
     105           0 :   if (Padding != 0) {
     106           9 :     for (; Padding != 1; --Padding)
     107           3 :       *p++ = '\x80';
     108           6 :     *p++ = '\x00';
     109             :   }
     110       22092 :   return (unsigned)(p - orig_p);
     111             : }
     112             : 
     113             : /// Utility function to decode a ULEB128 value.
     114    25948749 : inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
     115             :                               const uint8_t *end = nullptr,
     116             :                               const char **error = nullptr) {
     117    25948859 :   const uint8_t *orig_p = p;
     118    25948859 :   uint64_t Value = 0;
     119    25948859 :   unsigned Shift = 0;
     120    25948749 :   if (error)
     121         496 :     *error = nullptr;
     122             :   do {
     123    33547121 :     if (end && p == end) {
     124           3 :       if (error)
     125           3 :         *error = "malformed uleb128, extends past end";
     126           3 :       if (n)
     127           3 :         *n = (unsigned)(p - orig_p);
     128             :       return 0;
     129             :     }
     130    33582231 :     uint64_t Slice = *p & 0x7f;
     131    33582231 :     if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
     132           2 :       if (error)
     133           2 :         *error = "uleb128 too big for uint64";
     134           2 :       if (n)
     135           2 :         *n = (unsigned)(p - orig_p);
     136             :       return 0;
     137             :     }
     138    33582229 :     Value += uint64_t(*p & 0x7f) << Shift;
     139    33582229 :     Shift += 7;
     140    33582229 :   } while (*p++ >= 128);
     141    25948744 :   if (n)
     142    25948744 :     *n = (unsigned)(p - orig_p);
     143             :   return Value;
     144             : }
     145             : 
     146             : /// Utility function to decode a SLEB128 value.
     147           8 : inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
     148             :                              const uint8_t *end = nullptr,
     149             :                              const char **error = nullptr) {
     150         164 :   const uint8_t *orig_p = p;
     151         164 :   int64_t Value = 0;
     152         164 :   unsigned Shift = 0;
     153             :   uint8_t Byte;
     154             :   do {
     155          11 :     if (end && p == end) {
     156           1 :       if (error)
     157           1 :         *error = "malformed sleb128, extends past end";
     158           1 :       if (n)
     159           1 :         *n = (unsigned)(p - orig_p);
     160             :       return 0;
     161             :     }
     162         372 :     Byte = *p++;
     163         372 :     Value |= (int64_t(Byte & 0x7f) << Shift);
     164         372 :     Shift += 7;
     165         366 :   } while (Byte >= 128);
     166             :   // Sign extend negative numbers.
     167         330 :   if (Byte & 0x40)
     168         165 :     Value |= (-1ULL) << Shift;
     169           7 :   if (n)
     170         336 :     *n = (unsigned)(p - orig_p);
     171             :   return Value;
     172             : }
     173             : 
     174             : /// Utility function to get the size of the ULEB128-encoded value.
     175             : extern unsigned getULEB128Size(uint64_t Value);
     176             : 
     177             : /// Utility function to get the size of the SLEB128-encoded value.
     178             : extern unsigned getSLEB128Size(int64_t Value);
     179             : 
     180             : } // namespace llvm
     181             : 
     182             : #endif // LLVM_SYSTEM_LEB128_H

Generated by: LCOV version 1.13