struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / cpp / gcc / data-streamer.h
blob1d1578262a367374625b876a613d0750d0c2dbe5
1 /* Generic streaming support for various data types.
3 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@google.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef GCC_DATA_STREAMER_H
23 #define GCC_DATA_STREAMER_H
25 #include "lto-streamer.h"
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;
32 typedef unsigned HOST_WIDE_INT bitpack_word_t;
34 struct bitpack_d
36 /* The position of the first unused or unconsumed bit in the word. */
37 unsigned pos;
39 /* The current word we are (un)packing. */
40 bitpack_word_t word;
42 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
43 void *stream;
46 /* In data-streamer.cc */
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 *);
52 /* In data-streamer-out.cc */
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);
56 // sdcpp void streamer_write_poly_uint64 (struct output_block *, poly_uint64);
57 // sdcpp void streamer_write_poly_int64 (struct output_block *, poly_int64);
58 void streamer_write_gcov_count (struct output_block *, gcov_type);
59 void streamer_write_string (struct output_block *, struct lto_output_stream *,
60 const char *, bool);
61 void streamer_write_string_with_length (struct output_block *,
62 struct lto_output_stream *,
63 const char *, unsigned int, bool);
64 void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
65 const char *, unsigned int, bool);
66 void bp_pack_string (struct output_block *, struct bitpack_d *,
67 const char *, bool);
68 void streamer_write_uhwi_stream (struct lto_output_stream *,
69 unsigned HOST_WIDE_INT);
70 void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
71 void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
72 void streamer_write_data_stream (struct lto_output_stream *, const void *,
73 size_t);
74 // sdcpp void streamer_write_wide_int (struct output_block *, const wide_int &);
75 // sdcpp void streamer_write_widest_int (struct output_block *, const widest_int &);
77 /* In data-streamer-in.cc */
78 const char *streamer_read_string (class data_in *, class lto_input_block *);
79 const char *streamer_read_indexed_string (class data_in *,
80 class lto_input_block *,
81 unsigned int *);
82 const char *bp_unpack_indexed_string (class data_in *, struct bitpack_d *,
83 unsigned int *);
84 const char *bp_unpack_string (class data_in *, struct bitpack_d *);
85 unsigned HOST_WIDE_INT streamer_read_uhwi (class lto_input_block *);
86 HOST_WIDE_INT streamer_read_hwi (class lto_input_block *);
87 // sdcpp poly_uint64 streamer_read_poly_uint64 (class lto_input_block *);
88 // sdcpp poly_int64 streamer_read_poly_int64 (class lto_input_block *);
89 gcov_type streamer_read_gcov_count (class lto_input_block *);
90 // sdcpp wide_int streamer_read_wide_int (class lto_input_block *);
91 // sdcpp widest_int streamer_read_widest_int (class lto_input_block *);
93 /* Returns a new bit-packing context for bit-packing into S. */
94 static inline struct bitpack_d
95 bitpack_create (struct lto_output_stream *s)
97 struct bitpack_d bp;
98 bp.pos = 0;
99 bp.word = 0;
100 bp.stream = (void *)s;
101 return bp;
104 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
105 static inline void
106 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
108 bitpack_word_t word = bp->word;
109 int pos = bp->pos;
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)));
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)
119 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
120 word);
121 word = val;
122 pos = nbits;
124 else
126 word |= val << pos;
127 pos += nbits;
129 bp->word = word;
130 bp->pos = pos;
133 #if 0 // sdcpp
134 /* Pack VAL into the bit-packing context BP, using NBITS for each
135 coefficient. */
136 static inline void
137 bp_pack_poly_value (struct bitpack_d *bp,
138 const poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> &val,
139 unsigned nbits)
141 for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
142 bp_pack_value (bp, val.coeffs[i], nbits);
144 #endif // sdcpp
146 /* Finishes bit-packing of BP. */
147 static inline void
148 streamer_write_bitpack (struct bitpack_d *bp)
150 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
151 bp->word);
152 bp->word = 0;
153 bp->pos = 0;
156 /* Returns a new bit-packing context for bit-unpacking from IB. */
157 static inline struct bitpack_d
158 streamer_read_bitpack (class lto_input_block *ib)
160 struct bitpack_d bp;
161 bp.word = streamer_read_uhwi (ib);
162 bp.pos = 0;
163 bp.stream = (void *)ib;
164 return bp;
167 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
168 static inline bitpack_word_t
169 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
171 bitpack_word_t mask, val;
172 int pos = bp->pos;
174 mask = (nbits == BITS_PER_BITPACK_WORD
175 ? (bitpack_word_t) -1
176 : ((bitpack_word_t) 1 << nbits) - 1);
178 /* If there are not continuous nbits in the current bitpack word
179 switch to the next one. */
180 if (pos + nbits > BITS_PER_BITPACK_WORD)
182 bp->word = val
183 = streamer_read_uhwi ((class lto_input_block *)bp->stream);
184 bp->pos = nbits;
185 return val & mask;
187 val = bp->word;
188 val >>= pos;
189 bp->pos = pos + nbits;
191 return val & mask;
194 #if 0 // sdcpp
195 /* Unpacks a polynomial value from the bit-packing context BP in which each
196 coefficient has NBITS bits. */
197 static inline poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t>
198 bp_unpack_poly_value (struct bitpack_d *bp, unsigned nbits)
200 poly_int_pod<NUM_POLY_INT_COEFFS, bitpack_word_t> x;
201 for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
202 x.coeffs[i] = bp_unpack_value (bp, nbits);
203 return x;
207 /* Write a character to the output block. */
209 static inline void
210 streamer_write_char_stream (struct lto_output_stream *obs, char c)
212 /* No space left. */
213 if (obs->left_in_block == 0)
214 lto_append_block (obs);
216 /* Write the actual character. */
217 char *current_pointer = obs->current_pointer;
218 *(current_pointer++) = c;
219 obs->current_pointer = current_pointer;
220 obs->total_size++;
221 obs->left_in_block--;
225 /* Read byte from the input block. */
227 static inline unsigned char
228 streamer_read_uchar (class lto_input_block *ib)
230 if (ib->p >= ib->len)
231 lto_section_overrun (ib);
232 return (ib->data[ib->p++]);
235 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
236 to be compile time constant.
237 Be host independent, limit range to 31bits. */
239 static inline void
240 streamer_write_hwi_in_range (struct lto_output_stream *obs,
241 HOST_WIDE_INT min,
242 HOST_WIDE_INT max,
243 HOST_WIDE_INT val)
245 HOST_WIDE_INT range = max - min;
247 gcc_checking_assert (val >= min && val <= max && range > 0
248 && range < 0x7fffffff);
250 val -= min;
251 streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
254 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
255 to be compile time constant. PURPOSE is used for error reporting. */
257 static inline HOST_WIDE_INT
258 streamer_read_hwi_in_range (class lto_input_block *ib,
259 const char *purpose,
260 HOST_WIDE_INT min,
261 HOST_WIDE_INT max)
263 HOST_WIDE_INT range = max - min;
264 unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
266 gcc_checking_assert (range > 0 && range < 0x7fffffff);
268 HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
269 if (val < min || val > max)
270 lto_value_range_error (purpose, val, min, max);
271 return val;
274 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
275 to be compile time constant.
276 Be host independent, limit range to 31bits. */
278 static inline void
279 bp_pack_int_in_range (struct bitpack_d *bp,
280 HOST_WIDE_INT min,
281 HOST_WIDE_INT max,
282 HOST_WIDE_INT val)
284 HOST_WIDE_INT range = max - min;
285 int nbits = floor_log2 (range) + 1;
287 gcc_checking_assert (val >= min && val <= max && range > 0
288 && range < 0x7fffffff);
290 val -= min;
291 bp_pack_value (bp, val, nbits);
294 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
295 to be compile time constant. PURPOSE is used for error reporting. */
297 static inline HOST_WIDE_INT
298 bp_unpack_int_in_range (struct bitpack_d *bp,
299 const char *purpose,
300 HOST_WIDE_INT min,
301 HOST_WIDE_INT max)
303 HOST_WIDE_INT range = max - min;
304 int nbits = floor_log2 (range) + 1;
305 HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
307 gcc_checking_assert (range > 0 && range < 0x7fffffff);
309 if (val < min || val > max)
310 lto_value_range_error (purpose, val, min, max);
311 return val;
314 /* Output VAL of type "enum enum_name" into OBS.
315 Assume range 0...ENUM_LAST - 1. */
316 #define streamer_write_enum(obs,enum_name,enum_last,val) \
317 streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
319 /* Input enum of type "enum enum_name" from IB.
320 Assume range 0...ENUM_LAST - 1. */
321 #define streamer_read_enum(ib,enum_name,enum_last) \
322 (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
323 (int)(enum_last) - 1)
325 /* Output VAL of type "enum enum_name" into BP.
326 Assume range 0...ENUM_LAST - 1. */
327 #define bp_pack_enum(bp,enum_name,enum_last,val) \
328 bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
330 /* Input enum of type "enum enum_name" from BP.
331 Assume range 0...ENUM_LAST - 1. */
332 #define bp_unpack_enum(bp,enum_name,enum_last) \
333 (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
334 (int)(enum_last) - 1)
336 /* Output the start of a record with TAG to output block OB. */
338 static inline void
339 streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
341 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
344 /* Return the next tag in the input block IB. */
346 static inline enum LTO_tags
347 streamer_read_record_start (class lto_input_block *ib)
349 return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
351 #endif // sdcpp
353 #endif /* GCC_DATA_STREAMER_H */