]>
Commit | Line | Data |
---|---|---|
f0efc7aa DN |
1 | /* Routines for restoring various data types from a file stream. This deals |
2 | with various data types like strings, integers, enums, etc. | |
3 | ||
a945c346 | 4 | Copyright (C) 2011-2024 Free Software Foundation, Inc. |
f0efc7aa DN |
5 | Contributed by Diego Novillo <dnovillo@google.com> |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it under | |
10 | the terms of the GNU General Public License as published by the Free | |
11 | Software Foundation; either version 3, or (at your option) any later | |
12 | version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along 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" | |
c7131fb2 | 26 | #include "backend.h" |
4d648807 | 27 | #include "tree.h" |
c7131fb2 | 28 | #include "gimple.h" |
c582198b | 29 | #include "cgraph.h" |
f0efc7aa | 30 | #include "data-streamer.h" |
029bfd4f AH |
31 | #include "value-range.h" |
32 | #include "streamer-hooks.h" | |
f0efc7aa DN |
33 | |
34 | /* Read a string from the string table in DATA_IN using input block | |
35 | IB. Write the length to RLEN. */ | |
36 | ||
4b5337e6 | 37 | static const char * |
99b1c316 | 38 | string_for_index (class data_in *data_in, unsigned int loc, unsigned int *rlen) |
f0efc7aa | 39 | { |
f0efc7aa DN |
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. */ | |
db847fa8 | 50 | lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL); |
412288f1 | 51 | len = streamer_read_uhwi (&str_tab); |
f0efc7aa DN |
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 | ||
66 | const char * | |
99b1c316 MS |
67 | streamer_read_indexed_string (class data_in *data_in, |
68 | class lto_input_block *ib, unsigned int *rlen) | |
f0efc7aa | 69 | { |
412288f1 | 70 | return string_for_index (data_in, streamer_read_uhwi (ib), rlen); |
f0efc7aa DN |
71 | } |
72 | ||
73 | ||
74 | /* Read a NULL terminated string from the string table in DATA_IN. */ | |
75 | ||
76 | const char * | |
99b1c316 | 77 | streamer_read_string (class data_in *data_in, class lto_input_block *ib) |
f0efc7aa DN |
78 | { |
79 | unsigned int len; | |
80 | const char *ptr; | |
81 | ||
412288f1 | 82 | ptr = streamer_read_indexed_string (data_in, ib, &len); |
f0efc7aa DN |
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 | ||
8135e1e6 RB |
92 | /* Read a string from the string table in DATA_IN using the bitpack BP. |
93 | Write the length to RLEN. */ | |
94 | ||
95 | const char * | |
99b1c316 | 96 | bp_unpack_indexed_string (class data_in *data_in, |
8135e1e6 RB |
97 | struct bitpack_d *bp, unsigned int *rlen) |
98 | { | |
99 | return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen); | |
100 | } | |
101 | ||
102 | ||
103 | /* Read a NULL terminated string from the string table in DATA_IN. */ | |
104 | ||
105 | const char * | |
99b1c316 | 106 | bp_unpack_string (class data_in *data_in, struct bitpack_d *bp) |
8135e1e6 RB |
107 | { |
108 | unsigned int len; | |
109 | const char *ptr; | |
110 | ||
111 | ptr = bp_unpack_indexed_string (data_in, bp, &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 | ||
412288f1 | 121 | /* Read an unsigned HOST_WIDE_INT number from IB. */ |
f0efc7aa DN |
122 | |
123 | unsigned HOST_WIDE_INT | |
99b1c316 | 124 | streamer_read_uhwi (class lto_input_block *ib) |
f0efc7aa | 125 | { |
d16e9a99 RB |
126 | unsigned HOST_WIDE_INT result; |
127 | int shift; | |
f0efc7aa | 128 | unsigned HOST_WIDE_INT byte; |
d16e9a99 RB |
129 | unsigned int p = ib->p; |
130 | unsigned int len = ib->len; | |
f0efc7aa | 131 | |
d16e9a99 RB |
132 | const char *data = ib->data; |
133 | result = data[p++]; | |
134 | if ((result & 0x80) != 0) | |
f0efc7aa | 135 | { |
d16e9a99 RB |
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); | |
f0efc7aa | 145 | } |
d16e9a99 RB |
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; | |
f0efc7aa DN |
153 | } |
154 | ||
155 | ||
412288f1 | 156 | /* Read a HOST_WIDE_INT number from IB. */ |
f0efc7aa DN |
157 | |
158 | HOST_WIDE_INT | |
99b1c316 | 159 | streamer_read_hwi (class lto_input_block *ib) |
f0efc7aa DN |
160 | { |
161 | HOST_WIDE_INT result = 0; | |
162 | int shift = 0; | |
163 | unsigned HOST_WIDE_INT byte; | |
164 | ||
165 | while (true) | |
166 | { | |
412288f1 | 167 | byte = streamer_read_uchar (ib); |
f0efc7aa DN |
168 | result |= (byte & 0x7f) << shift; |
169 | shift += 7; | |
170 | if ((byte & 0x80) == 0) | |
171 | { | |
172 | if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40)) | |
00a7ba58 | 173 | result |= - (HOST_WIDE_INT_1U << shift); |
f0efc7aa DN |
174 | |
175 | return result; | |
176 | } | |
177 | } | |
178 | } | |
89ab31c1 | 179 | |
86003645 RS |
180 | /* Read a poly_uint64 from IB. */ |
181 | ||
182 | poly_uint64 | |
183 | streamer_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 | ||
8d1cede1 JH |
191 | /* Read a poly_int64 from IB. */ |
192 | ||
193 | poly_int64 | |
194 | streamer_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 | ||
89ab31c1 JH |
202 | /* Read gcov_type value from IB. */ |
203 | ||
204 | gcov_type | |
99b1c316 | 205 | streamer_read_gcov_count (class lto_input_block *ib) |
89ab31c1 JH |
206 | { |
207 | gcov_type ret = streamer_read_hwi (ib); | |
89ab31c1 JH |
208 | return ret; |
209 | } | |
a73f34c2 | 210 | |
029bfd4f AH |
211 | /* Read REAL_VALUE_TYPE from IB. */ |
212 | ||
213 | void | |
214 | streamer_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 | ||
220 | void | |
221 | streamer_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> (vr)) | |
233 | { | |
234 | irange &r = as_a <irange> (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 | } | |
0c888665 AH |
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); | |
029bfd4f AH |
248 | return; |
249 | } | |
250 | if (is_a <frange> (vr)) | |
251 | { | |
252 | frange &r = as_a <frange> (vr); | |
e11685f7 AH |
253 | |
254 | // Stream in NAN bits. | |
029bfd4f AH |
255 | struct bitpack_d bp = streamer_read_bitpack (ib); |
256 | bool pos_nan = (bool) bp_unpack_value (&bp, 1); | |
257 | bool neg_nan = (bool) bp_unpack_value (&bp, 1); | |
258 | nan_state nan (pos_nan, neg_nan); | |
e11685f7 AH |
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, &lb); | |
266 | streamer_read_real_value (ib, &ub); | |
267 | r.set (type, lb, ub, nan); | |
268 | } | |
029bfd4f AH |
269 | return; |
270 | } | |
271 | gcc_unreachable (); | |
272 | } | |
273 | ||
a73f34c2 KV |
274 | /* Read the physical representation of a wide_int val from |
275 | input block IB. */ | |
276 | ||
277 | wide_int | |
99b1c316 | 278 | streamer_read_wide_int (class lto_input_block *ib) |
a73f34c2 | 279 | { |
0d00385e | 280 | HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf; |
a73f34c2 KV |
281 | int i; |
282 | int prec = streamer_read_uhwi (ib); | |
283 | int len = streamer_read_uhwi (ib); | |
0d00385e JJ |
284 | if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) |
285 | a = XALLOCAVEC (HOST_WIDE_INT, len); | |
a73f34c2 KV |
286 | for (i = 0; i < len; i++) |
287 | a[i] = streamer_read_hwi (ib); | |
288 | return wide_int::from_array (a, len, prec); | |
289 | } | |
290 | ||
291 | /* Read the physical representation of a widest_int val from | |
292 | input block IB. */ | |
293 | ||
294 | widest_int | |
99b1c316 | 295 | streamer_read_widest_int (class lto_input_block *ib) |
a73f34c2 | 296 | { |
0d00385e | 297 | HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf; |
a73f34c2 KV |
298 | int i; |
299 | int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib); | |
300 | int len = streamer_read_uhwi (ib); | |
0d00385e JJ |
301 | if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS)) |
302 | a = XALLOCAVEC (HOST_WIDE_INT, len); | |
a73f34c2 KV |
303 | for (i = 0; i < len; i++) |
304 | a[i] = streamer_read_hwi (ib); | |
305 | return widest_int::from_array (a, len); | |
306 | } | |
307 |