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