]>
Commit | Line | Data |
---|---|---|
f0efc7aa DN |
1 | /* Generic streaming support for various data types. |
2 | ||
a5544970 | 3 | Copyright (C) 2011-2019 Free Software Foundation, Inc. |
f0efc7aa DN |
4 | Contributed by Diego Novillo <dnovillo@google.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #ifndef GCC_DATA_STREAMER_H | |
23 | #define GCC_DATA_STREAMER_H | |
24 | ||
f0efc7aa DN |
25 | #include "lto-streamer.h" |
26 | ||
27 | /* Data structures used to pack values and bitflags into a vector of | |
28 | words. Used to stream values of a fixed number of bits in a space | |
29 | efficient way. */ | |
30 | static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT; | |
31 | ||
32 | typedef unsigned HOST_WIDE_INT bitpack_word_t; | |
f0efc7aa DN |
33 | |
34 | struct bitpack_d | |
35 | { | |
36 | /* The position of the first unused or unconsumed bit in the word. */ | |
37 | unsigned pos; | |
38 | ||
39 | /* The current word we are (un)packing. */ | |
40 | bitpack_word_t word; | |
41 | ||
42 | /* The lto_output_stream or the lto_input_block we are streaming to/from. */ | |
43 | void *stream; | |
44 | }; | |
45 | ||
412288f1 DN |
46 | /* In data-streamer.c */ |
47 | void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT); | |
48 | void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT); | |
49 | unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *); | |
50 | HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *); | |
51 | ||
52 | /* In data-streamer-out.c */ | |
53 | void streamer_write_zero (struct output_block *); | |
54 | void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT); | |
55 | void streamer_write_hwi (struct output_block *, HOST_WIDE_INT); | |
89ab31c1 | 56 | void streamer_write_gcov_count (struct output_block *, gcov_type); |
412288f1 DN |
57 | void streamer_write_string (struct output_block *, struct lto_output_stream *, |
58 | const char *, bool); | |
412288f1 DN |
59 | void streamer_write_string_with_length (struct output_block *, |
60 | struct lto_output_stream *, | |
61 | const char *, unsigned int, bool); | |
8135e1e6 RB |
62 | void bp_pack_string_with_length (struct output_block *, struct bitpack_d *, |
63 | const char *, unsigned int, bool); | |
64 | void bp_pack_string (struct output_block *, struct bitpack_d *, | |
65 | const char *, bool); | |
412288f1 DN |
66 | void streamer_write_uhwi_stream (struct lto_output_stream *, |
67 | unsigned HOST_WIDE_INT); | |
68 | void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT); | |
89ab31c1 | 69 | void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type); |
bfa2ebe3 RB |
70 | void streamer_write_data_stream (struct lto_output_stream *, const void *, |
71 | size_t); | |
a73f34c2 KV |
72 | void streamer_write_wide_int (struct output_block *, const wide_int &); |
73 | void streamer_write_widest_int (struct output_block *, const widest_int &); | |
412288f1 DN |
74 | |
75 | /* In data-streamer-in.c */ | |
99b1c316 MS |
76 | const char *streamer_read_string (class data_in *, class lto_input_block *); |
77 | const char *streamer_read_indexed_string (class data_in *, | |
78 | class lto_input_block *, | |
412288f1 | 79 | unsigned int *); |
99b1c316 | 80 | const char *bp_unpack_indexed_string (class data_in *, struct bitpack_d *, |
8135e1e6 | 81 | unsigned int *); |
99b1c316 MS |
82 | const char *bp_unpack_string (class data_in *, struct bitpack_d *); |
83 | unsigned HOST_WIDE_INT streamer_read_uhwi (class lto_input_block *); | |
84 | HOST_WIDE_INT streamer_read_hwi (class lto_input_block *); | |
85 | gcov_type streamer_read_gcov_count (class lto_input_block *); | |
86 | wide_int streamer_read_wide_int (class lto_input_block *); | |
87 | widest_int streamer_read_widest_int (class lto_input_block *); | |
f0efc7aa | 88 | |
f0efc7aa DN |
89 | /* Returns a new bit-packing context for bit-packing into S. */ |
90 | static inline struct bitpack_d | |
91 | bitpack_create (struct lto_output_stream *s) | |
92 | { | |
93 | struct bitpack_d bp; | |
94 | bp.pos = 0; | |
95 | bp.word = 0; | |
96 | bp.stream = (void *)s; | |
97 | return bp; | |
98 | } | |
99 | ||
100 | /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */ | |
101 | static inline void | |
102 | bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits) | |
103 | { | |
104 | bitpack_word_t word = bp->word; | |
105 | int pos = bp->pos; | |
106 | ||
107 | /* Verify that VAL fits in the NBITS. */ | |
108 | gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD | |
109 | || !(val & ~(((bitpack_word_t)1<<nbits)-1))); | |
110 | ||
111 | /* If val does not fit into the current bitpack word switch to the | |
112 | next one. */ | |
113 | if (pos + nbits > BITS_PER_BITPACK_WORD) | |
114 | { | |
412288f1 DN |
115 | streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream, |
116 | word); | |
f0efc7aa DN |
117 | word = val; |
118 | pos = nbits; | |
119 | } | |
120 | else | |
121 | { | |
122 | word |= val << pos; | |
123 | pos += nbits; | |
124 | } | |
125 | bp->word = word; | |
126 | bp->pos = pos; | |
127 | } | |
128 | ||
7b777afa RS |
129 | /* Pack VAL into the bit-packing context BP, using NBITS for each |
130 | coefficient. */ | |
131 | static inline void | |
132 | bp_pack_poly_value (struct bitpack_d *bp, | |
133 | const poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> &val, | |
134 | unsigned nbits) | |
135 | { | |
136 | for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i) | |
137 | bp_pack_value (bp, val.coeffs[i], nbits); | |
138 | } | |
139 | ||
f0efc7aa DN |
140 | /* Finishes bit-packing of BP. */ |
141 | static inline void | |
412288f1 | 142 | streamer_write_bitpack (struct bitpack_d *bp) |
f0efc7aa | 143 | { |
412288f1 DN |
144 | streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream, |
145 | bp->word); | |
f0efc7aa DN |
146 | bp->word = 0; |
147 | bp->pos = 0; | |
148 | } | |
149 | ||
150 | /* Returns a new bit-packing context for bit-unpacking from IB. */ | |
151 | static inline struct bitpack_d | |
99b1c316 | 152 | streamer_read_bitpack (class lto_input_block *ib) |
f0efc7aa DN |
153 | { |
154 | struct bitpack_d bp; | |
412288f1 | 155 | bp.word = streamer_read_uhwi (ib); |
f0efc7aa DN |
156 | bp.pos = 0; |
157 | bp.stream = (void *)ib; | |
158 | return bp; | |
159 | } | |
160 | ||
161 | /* Unpacks NBITS bits from the bit-packing context BP and returns them. */ | |
162 | static inline bitpack_word_t | |
163 | bp_unpack_value (struct bitpack_d *bp, unsigned nbits) | |
164 | { | |
165 | bitpack_word_t mask, val; | |
166 | int pos = bp->pos; | |
167 | ||
168 | mask = (nbits == BITS_PER_BITPACK_WORD | |
169 | ? (bitpack_word_t) -1 | |
170 | : ((bitpack_word_t) 1 << nbits) - 1); | |
171 | ||
172 | /* If there are not continuous nbits in the current bitpack word | |
173 | switch to the next one. */ | |
174 | if (pos + nbits > BITS_PER_BITPACK_WORD) | |
175 | { | |
412288f1 | 176 | bp->word = val |
99b1c316 | 177 | = streamer_read_uhwi ((class lto_input_block *)bp->stream); |
f0efc7aa DN |
178 | bp->pos = nbits; |
179 | return val & mask; | |
180 | } | |
181 | val = bp->word; | |
182 | val >>= pos; | |
183 | bp->pos = pos + nbits; | |
184 | ||
185 | return val & mask; | |
186 | } | |
187 | ||
7b777afa RS |
188 | /* Unpacks a polynomial value from the bit-packing context BP in which each |
189 | coefficient has NBITS bits. */ | |
190 | static inline poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> | |
191 | bp_unpack_poly_value (struct bitpack_d *bp, unsigned nbits) | |
192 | { | |
193 | poly_int_pod<NUM_POLY_INT_COEFFS, bitpack_word_t> x; | |
194 | for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i) | |
195 | x.coeffs[i] = bp_unpack_value (bp, nbits); | |
196 | return x; | |
197 | } | |
198 | ||
f0efc7aa DN |
199 | |
200 | /* Write a character to the output block. */ | |
201 | ||
202 | static inline void | |
412288f1 | 203 | streamer_write_char_stream (struct lto_output_stream *obs, char c) |
f0efc7aa DN |
204 | { |
205 | /* No space left. */ | |
206 | if (obs->left_in_block == 0) | |
207 | lto_append_block (obs); | |
208 | ||
209 | /* Write the actual character. */ | |
a4fa02d1 RB |
210 | char *current_pointer = obs->current_pointer; |
211 | *(current_pointer++) = c; | |
212 | obs->current_pointer = current_pointer; | |
f0efc7aa DN |
213 | obs->total_size++; |
214 | obs->left_in_block--; | |
215 | } | |
216 | ||
217 | ||
218 | /* Read byte from the input block. */ | |
219 | ||
220 | static inline unsigned char | |
99b1c316 | 221 | streamer_read_uchar (class lto_input_block *ib) |
f0efc7aa DN |
222 | { |
223 | if (ib->p >= ib->len) | |
224 | lto_section_overrun (ib); | |
225 | return (ib->data[ib->p++]); | |
226 | } | |
227 | ||
228 | /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed | |
229 | to be compile time constant. | |
230 | Be host independent, limit range to 31bits. */ | |
231 | ||
232 | static inline void | |
412288f1 DN |
233 | streamer_write_hwi_in_range (struct lto_output_stream *obs, |
234 | HOST_WIDE_INT min, | |
235 | HOST_WIDE_INT max, | |
236 | HOST_WIDE_INT val) | |
f0efc7aa DN |
237 | { |
238 | HOST_WIDE_INT range = max - min; | |
239 | ||
240 | gcc_checking_assert (val >= min && val <= max && range > 0 | |
241 | && range < 0x7fffffff); | |
242 | ||
243 | val -= min; | |
15d16c8a | 244 | streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val); |
f0efc7aa DN |
245 | } |
246 | ||
247 | /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed | |
248 | to be compile time constant. PURPOSE is used for error reporting. */ | |
249 | ||
250 | static inline HOST_WIDE_INT | |
99b1c316 | 251 | streamer_read_hwi_in_range (class lto_input_block *ib, |
412288f1 DN |
252 | const char *purpose, |
253 | HOST_WIDE_INT min, | |
254 | HOST_WIDE_INT max) | |
f0efc7aa DN |
255 | { |
256 | HOST_WIDE_INT range = max - min; | |
15d16c8a | 257 | unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib); |
f0efc7aa DN |
258 | |
259 | gcc_checking_assert (range > 0 && range < 0x7fffffff); | |
260 | ||
15d16c8a | 261 | HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min); |
f0efc7aa DN |
262 | if (val < min || val > max) |
263 | lto_value_range_error (purpose, val, min, max); | |
264 | return val; | |
265 | } | |
266 | ||
267 | /* Output VAL into BP and verify it is in range MIN...MAX that is supposed | |
268 | to be compile time constant. | |
269 | Be host independent, limit range to 31bits. */ | |
270 | ||
271 | static inline void | |
272 | bp_pack_int_in_range (struct bitpack_d *bp, | |
273 | HOST_WIDE_INT min, | |
274 | HOST_WIDE_INT max, | |
275 | HOST_WIDE_INT val) | |
276 | { | |
277 | HOST_WIDE_INT range = max - min; | |
278 | int nbits = floor_log2 (range) + 1; | |
279 | ||
280 | gcc_checking_assert (val >= min && val <= max && range > 0 | |
281 | && range < 0x7fffffff); | |
282 | ||
283 | val -= min; | |
284 | bp_pack_value (bp, val, nbits); | |
285 | } | |
286 | ||
287 | /* Input VAL into BP and verify it is in range MIN...MAX that is supposed | |
288 | to be compile time constant. PURPOSE is used for error reporting. */ | |
289 | ||
290 | static inline HOST_WIDE_INT | |
291 | bp_unpack_int_in_range (struct bitpack_d *bp, | |
292 | const char *purpose, | |
293 | HOST_WIDE_INT min, | |
294 | HOST_WIDE_INT max) | |
295 | { | |
296 | HOST_WIDE_INT range = max - min; | |
297 | int nbits = floor_log2 (range) + 1; | |
298 | HOST_WIDE_INT val = bp_unpack_value (bp, nbits); | |
299 | ||
300 | gcc_checking_assert (range > 0 && range < 0x7fffffff); | |
301 | ||
302 | if (val < min || val > max) | |
303 | lto_value_range_error (purpose, val, min, max); | |
304 | return val; | |
305 | } | |
306 | ||
307 | /* Output VAL of type "enum enum_name" into OBS. | |
308 | Assume range 0...ENUM_LAST - 1. */ | |
412288f1 DN |
309 | #define streamer_write_enum(obs,enum_name,enum_last,val) \ |
310 | streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val)) | |
f0efc7aa DN |
311 | |
312 | /* Input enum of type "enum enum_name" from IB. | |
313 | Assume range 0...ENUM_LAST - 1. */ | |
412288f1 DN |
314 | #define streamer_read_enum(ib,enum_name,enum_last) \ |
315 | (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \ | |
316 | (int)(enum_last) - 1) | |
f0efc7aa DN |
317 | |
318 | /* Output VAL of type "enum enum_name" into BP. | |
319 | Assume range 0...ENUM_LAST - 1. */ | |
320 | #define bp_pack_enum(bp,enum_name,enum_last,val) \ | |
321 | bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val)) | |
322 | ||
323 | /* Input enum of type "enum enum_name" from BP. | |
324 | Assume range 0...ENUM_LAST - 1. */ | |
325 | #define bp_unpack_enum(bp,enum_name,enum_last) \ | |
326 | (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \ | |
327 | (int)(enum_last) - 1) | |
328 | ||
329 | /* Output the start of a record with TAG to output block OB. */ | |
330 | ||
331 | static inline void | |
412288f1 | 332 | streamer_write_record_start (struct output_block *ob, enum LTO_tags tag) |
f0efc7aa | 333 | { |
412288f1 | 334 | streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag); |
f0efc7aa DN |
335 | } |
336 | ||
337 | /* Return the next tag in the input block IB. */ | |
338 | ||
339 | static inline enum LTO_tags | |
99b1c316 | 340 | streamer_read_record_start (class lto_input_block *ib) |
f0efc7aa | 341 | { |
412288f1 | 342 | return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS); |
f0efc7aa DN |
343 | } |
344 | ||
f0efc7aa | 345 | #endif /* GCC_DATA_STREAMER_H */ |