1/* Routines for restoring various data types from a file stream. This deals
2 with various data types like strings, integers, enums, etc.
3
4 Copyright (C) 2011-2023 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "backend.h"
27#include "tree.h"
28#include "gimple.h"
29#include "cgraph.h"
30#include "data-streamer.h"
31#include "value-range.h"
32#include "streamer-hooks.h"
33
34/* Read a string from the string table in DATA_IN using input block
35 IB. Write the length to RLEN. */
36
37static const char *
38string_for_index (class data_in *data_in, unsigned int loc, unsigned int *rlen)
39{
40 unsigned int len;
41 const char *result;
42
43 if (!loc)
44 {
45 *rlen = 0;
46 return NULL;
47 }
48
49 /* Get the string stored at location LOC in DATA_IN->STRINGS. */
50 lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
51 len = streamer_read_uhwi (&str_tab);
52 *rlen = len;
53
54 if (str_tab.p + len > data_in->strings_len)
55 internal_error ("bytecode stream: string too long for the string table");
56
57 result = (const char *)(data_in->strings + str_tab.p);
58
59 return result;
60}
61
62
63/* Read a string from the string table in DATA_IN using input block
64 IB. Write the length to RLEN. */
65
66const char *
67streamer_read_indexed_string (class data_in *data_in,
68 class lto_input_block *ib, unsigned int *rlen)
69{
70 return string_for_index (data_in, loc: streamer_read_uhwi (ib), rlen);
71}
72
73
74/* Read a NULL terminated string from the string table in DATA_IN. */
75
76const char *
77streamer_read_string (class data_in *data_in, class lto_input_block *ib)
78{
79 unsigned int len;
80 const char *ptr;
81
82 ptr = streamer_read_indexed_string (data_in, ib, rlen: &len);
83 if (!ptr)
84 return NULL;
85 if (ptr[len - 1] != '\0')
86 internal_error ("bytecode stream: found non-null terminated string");
87
88 return ptr;
89}
90
91
92/* Read a string from the string table in DATA_IN using the bitpack BP.
93 Write the length to RLEN. */
94
95const char *
96bp_unpack_indexed_string (class data_in *data_in,
97 struct bitpack_d *bp, unsigned int *rlen)
98{
99 return string_for_index (data_in, loc: bp_unpack_var_len_unsigned (bp), rlen);
100}
101
102
103/* Read a NULL terminated string from the string table in DATA_IN. */
104
105const char *
106bp_unpack_string (class data_in *data_in, struct bitpack_d *bp)
107{
108 unsigned int len;
109 const char *ptr;
110
111 ptr = bp_unpack_indexed_string (data_in, bp, rlen: &len);
112 if (!ptr)
113 return NULL;
114 if (ptr[len - 1] != '\0')
115 internal_error ("bytecode stream: found non-null terminated string");
116
117 return ptr;
118}
119
120
121/* Read an unsigned HOST_WIDE_INT number from IB. */
122
123unsigned HOST_WIDE_INT
124streamer_read_uhwi (class lto_input_block *ib)
125{
126 unsigned HOST_WIDE_INT result;
127 int shift;
128 unsigned HOST_WIDE_INT byte;
129 unsigned int p = ib->p;
130 unsigned int len = ib->len;
131
132 const char *data = ib->data;
133 result = data[p++];
134 if ((result & 0x80) != 0)
135 {
136 result &= 0x7f;
137 shift = 7;
138 do
139 {
140 byte = data[p++];
141 result |= (byte & 0x7f) << shift;
142 shift += 7;
143 }
144 while ((byte & 0x80) != 0);
145 }
146
147 /* We check for section overrun after the fact for performance reason. */
148 if (p > len)
149 lto_section_overrun (ib);
150
151 ib->p = p;
152 return result;
153}
154
155
156/* Read a HOST_WIDE_INT number from IB. */
157
158HOST_WIDE_INT
159streamer_read_hwi (class lto_input_block *ib)
160{
161 HOST_WIDE_INT result = 0;
162 int shift = 0;
163 unsigned HOST_WIDE_INT byte;
164
165 while (true)
166 {
167 byte = streamer_read_uchar (ib);
168 result |= (byte & 0x7f) << shift;
169 shift += 7;
170 if ((byte & 0x80) == 0)
171 {
172 if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
173 result |= - (HOST_WIDE_INT_1U << shift);
174
175 return result;
176 }
177 }
178}
179
180/* Read a poly_uint64 from IB. */
181
182poly_uint64
183streamer_read_poly_uint64 (class lto_input_block *ib)
184{
185 poly_uint64 res;
186 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
187 res.coeffs[i] = streamer_read_uhwi (ib);
188 return res;
189}
190
191/* Read a poly_int64 from IB. */
192
193poly_int64
194streamer_read_poly_int64 (class lto_input_block *ib)
195{
196 poly_int64 res;
197 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
198 res.coeffs[i] = streamer_read_hwi (ib);
199 return res;
200}
201
202/* Read gcov_type value from IB. */
203
204gcov_type
205streamer_read_gcov_count (class lto_input_block *ib)
206{
207 gcov_type ret = streamer_read_hwi (ib);
208 return ret;
209}
210
211/* Read REAL_VALUE_TYPE from IB. */
212
213void
214streamer_read_real_value (class lto_input_block *ib, REAL_VALUE_TYPE *r)
215{
216 struct bitpack_d bp = streamer_read_bitpack (ib);
217 bp_unpack_real_value (&bp, r);
218}
219
220void
221streamer_read_value_range (class lto_input_block *ib, data_in *data_in,
222 Value_Range &vr)
223{
224 // Read the common fields to all vranges.
225 value_range_kind kind = streamer_read_enum (ib, value_range_kind, VR_LAST);
226 gcc_checking_assert (kind != VR_UNDEFINED);
227 tree type = stream_read_tree (ib, data_in);
228
229 // Initialize the Value_Range to the correct type.
230 vr.set_type (type);
231
232 if (is_a <irange> (v&: vr))
233 {
234 irange &r = as_a <irange> (v&: vr);
235 r.set_undefined ();
236 unsigned HOST_WIDE_INT num_pairs = streamer_read_uhwi (ib);
237 for (unsigned i = 0; i < num_pairs; ++i)
238 {
239 wide_int lb = streamer_read_wide_int (ib);
240 wide_int ub = streamer_read_wide_int (ib);
241 int_range<2> tmp (type, lb, ub);
242 r.union_ (tmp);
243 }
244 wide_int value = streamer_read_wide_int (ib);
245 wide_int mask = streamer_read_wide_int (ib);
246 irange_bitmask bm (value, mask);
247 r.update_bitmask (bm);
248 return;
249 }
250 if (is_a <frange> (v&: vr))
251 {
252 frange &r = as_a <frange> (v&: vr);
253
254 // Stream in NAN bits.
255 struct bitpack_d bp = streamer_read_bitpack (ib);
256 bool pos_nan = (bool) bp_unpack_value (bp: &bp, nbits: 1);
257 bool neg_nan = (bool) bp_unpack_value (bp: &bp, nbits: 1);
258 nan_state nan (pos_nan, neg_nan);
259
260 if (kind == VR_NAN)
261 r.set_nan (type, nan);
262 else
263 {
264 REAL_VALUE_TYPE lb, ub;
265 streamer_read_real_value (ib, r: &lb);
266 streamer_read_real_value (ib, r: &ub);
267 r.set (type, lb, ub, nan);
268 }
269 return;
270 }
271 gcc_unreachable ();
272}
273
274/* Read the physical representation of a wide_int val from
275 input block IB. */
276
277wide_int
278streamer_read_wide_int (class lto_input_block *ib)
279{
280 HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf;
281 int i;
282 int prec = streamer_read_uhwi (ib);
283 int len = streamer_read_uhwi (ib);
284 if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS))
285 a = XALLOCAVEC (HOST_WIDE_INT, len);
286 for (i = 0; i < len; i++)
287 a[i] = streamer_read_hwi (ib);
288 return wide_int::from_array (val: a, len, precision: prec);
289}
290
291/* Read the physical representation of a widest_int val from
292 input block IB. */
293
294widest_int
295streamer_read_widest_int (class lto_input_block *ib)
296{
297 HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf;
298 int i;
299 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
300 int len = streamer_read_uhwi (ib);
301 if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS))
302 a = XALLOCAVEC (HOST_WIDE_INT, len);
303 for (i = 0; i < len; i++)
304 a[i] = streamer_read_hwi (ib);
305 return widest_int::from_array (val: a, len);
306}
307
308

source code of gcc/data-streamer-in.cc