* gas/arm/thumb2_bcond.d: Allow for varying number of nops at the
[binutils/dougsmingw.git] / gold / int_encoding.cc
blobef58749ae859f8893fae3b9632283726d5cbea87
1 // varint.cc -- variable length and unaligned integer encoding support.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
5 // contents from other files in gold. Original code written by Ian
6 // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
8 // This file is part of gold.
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
25 #include "gold.h"
27 #include <vector>
29 #include "int_encoding.h"
31 namespace gold {
33 // Read an unsigned LEB128 number. Each byte contains 7 bits of
34 // information, plus one bit saying whether the number continues or
35 // not.
37 uint64_t
38 read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
40 uint64_t result = 0;
41 size_t num_read = 0;
42 unsigned int shift = 0;
43 unsigned char byte;
47 if (num_read >= 64 / 7)
49 gold_warning(_("Unusually large LEB128 decoded, "
50 "debug information may be corrupted"));
51 break;
53 byte = *buffer++;
54 num_read++;
55 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
56 shift += 7;
58 while (byte & 0x80);
60 *len = num_read;
62 return result;
65 // Read a signed LEB128 number. These are like regular LEB128
66 // numbers, except the last byte may have a sign bit set.
68 int64_t
69 read_signed_LEB_128(const unsigned char* buffer, size_t* len)
71 int64_t result = 0;
72 int shift = 0;
73 size_t num_read = 0;
74 unsigned char byte;
78 if (num_read >= 64 / 7)
80 gold_warning(_("Unusually large LEB128 decoded, "
81 "debug information may be corrupted"));
82 break;
84 byte = *buffer++;
85 num_read++;
86 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
87 shift += 7;
89 while (byte & 0x80);
91 if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
92 result |= -((static_cast<int64_t>(1)) << shift);
93 *len = num_read;
94 return result;
97 void
98 write_unsigned_LEB_128(std::vector<unsigned char>* buffer, uint64_t value)
102 unsigned char current_byte = value & 0x7f;
103 value >>= 7;
104 if (value != 0)
106 current_byte |= 0x80;
108 buffer->push_back(current_byte);
110 while (value != 0);
113 size_t
114 get_length_as_unsigned_LEB_128(uint64_t value)
116 size_t length = 0;
119 unsigned char current_byte = value & 0x7f;
120 value >>= 7;
121 if (value != 0)
123 current_byte |= 0x80;
125 length++;
127 while (value != 0);
128 return length;
131 } // End namespace gold.