1/* Generic streaming support for various data types.
2
3 Copyright (C) 2011-2017 Free Software Foundation, Inc.
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
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;
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
46/* In data-streamer.c */
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
52/* In data-streamer-out.c */
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);
56void streamer_write_gcov_count (struct output_block *, gcov_type);
57void streamer_write_string (struct output_block *, struct lto_output_stream *,
58 const char *, bool);
59void streamer_write_string_with_length (struct output_block *,
60 struct lto_output_stream *,
61 const char *, unsigned int, bool);
62void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
63 const char *, unsigned int, bool);
64void bp_pack_string (struct output_block *, struct bitpack_d *,
65 const char *, bool);
66void streamer_write_uhwi_stream (struct lto_output_stream *,
67 unsigned HOST_WIDE_INT);
68void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
69void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
70void streamer_write_data_stream (struct lto_output_stream *, const void *,
71 size_t);
72void streamer_write_wide_int (struct output_block *, const wide_int &);
73void streamer_write_widest_int (struct output_block *, const widest_int &);
74
75/* In data-streamer-in.c */
76const char *streamer_read_string (struct data_in *, struct lto_input_block *);
77const char *streamer_read_indexed_string (struct data_in *,
78 struct lto_input_block *,
79 unsigned int *);
80const char *bp_unpack_indexed_string (struct data_in *, struct bitpack_d *,
81 unsigned int *);
82const char *bp_unpack_string (struct data_in *, struct bitpack_d *);
83unsigned HOST_WIDE_INT streamer_read_uhwi (struct lto_input_block *);
84HOST_WIDE_INT streamer_read_hwi (struct lto_input_block *);
85gcov_type streamer_read_gcov_count (struct lto_input_block *);
86wide_int streamer_read_wide_int (struct lto_input_block *);
87widest_int streamer_read_widest_int (struct lto_input_block *);
88
89/* Returns a new bit-packing context for bit-packing into S. */
90static inline struct bitpack_d
91bitpack_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. */
101static inline void
102bp_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 {
115 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
116 word);
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
129/* Finishes bit-packing of BP. */
130static inline void
131streamer_write_bitpack (struct bitpack_d *bp)
132{
133 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
134 bp->word);
135 bp->word = 0;
136 bp->pos = 0;
137}
138
139/* Returns a new bit-packing context for bit-unpacking from IB. */
140static inline struct bitpack_d
141streamer_read_bitpack (struct lto_input_block *ib)
142{
143 struct bitpack_d bp;
144 bp.word = streamer_read_uhwi (ib);
145 bp.pos = 0;
146 bp.stream = (void *)ib;
147 return bp;
148}
149
150/* Unpacks NBITS bits from the bit-packing context BP and returns them. */
151static inline bitpack_word_t
152bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
153{
154 bitpack_word_t mask, val;
155 int pos = bp->pos;
156
157 mask = (nbits == BITS_PER_BITPACK_WORD
158 ? (bitpack_word_t) -1
159 : ((bitpack_word_t) 1 << nbits) - 1);
160
161 /* If there are not continuous nbits in the current bitpack word
162 switch to the next one. */
163 if (pos + nbits > BITS_PER_BITPACK_WORD)
164 {
165 bp->word = val
166 = streamer_read_uhwi ((struct lto_input_block *)bp->stream);
167 bp->pos = nbits;
168 return val & mask;
169 }
170 val = bp->word;
171 val >>= pos;
172 bp->pos = pos + nbits;
173
174 return val & mask;
175}
176
177
178/* Write a character to the output block. */
179
180static inline void
181streamer_write_char_stream (struct lto_output_stream *obs, char c)
182{
183 /* No space left. */
184 if (obs->left_in_block == 0)
185 lto_append_block (obs);
186
187 /* Write the actual character. */
188 char *current_pointer = obs->current_pointer;
189 *(current_pointer++) = c;
190 obs->current_pointer = current_pointer;
191 obs->total_size++;
192 obs->left_in_block--;
193}
194
195
196/* Read byte from the input block. */
197
198static inline unsigned char
199streamer_read_uchar (struct lto_input_block *ib)
200{
201 if (ib->p >= ib->len)
202 lto_section_overrun (ib);
203 return (ib->data[ib->p++]);
204}
205
206/* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
207 to be compile time constant.
208 Be host independent, limit range to 31bits. */
209
210static inline void
211streamer_write_hwi_in_range (struct lto_output_stream *obs,
212 HOST_WIDE_INT min,
213 HOST_WIDE_INT max,
214 HOST_WIDE_INT val)
215{
216 HOST_WIDE_INT range = max - min;
217
218 gcc_checking_assert (val >= min && val <= max && range > 0
219 && range < 0x7fffffff);
220
221 val -= min;
222 streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
223}
224
225/* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
226 to be compile time constant. PURPOSE is used for error reporting. */
227
228static inline HOST_WIDE_INT
229streamer_read_hwi_in_range (struct lto_input_block *ib,
230 const char *purpose,
231 HOST_WIDE_INT min,
232 HOST_WIDE_INT max)
233{
234 HOST_WIDE_INT range = max - min;
235 unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
236
237 gcc_checking_assert (range > 0 && range < 0x7fffffff);
238
239 HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
240 if (val < min || val > max)
241 lto_value_range_error (purpose, val, min, max);
242 return val;
243}
244
245/* Output VAL into BP and verify it is in range MIN...MAX that is supposed
246 to be compile time constant.
247 Be host independent, limit range to 31bits. */
248
249static inline void
250bp_pack_int_in_range (struct bitpack_d *bp,
251 HOST_WIDE_INT min,
252 HOST_WIDE_INT max,
253 HOST_WIDE_INT val)
254{
255 HOST_WIDE_INT range = max - min;
256 int nbits = floor_log2 (range) + 1;
257
258 gcc_checking_assert (val >= min && val <= max && range > 0
259 && range < 0x7fffffff);
260
261 val -= min;
262 bp_pack_value (bp, val, nbits);
263}
264
265/* Input VAL into BP and verify it is in range MIN...MAX that is supposed
266 to be compile time constant. PURPOSE is used for error reporting. */
267
268static inline HOST_WIDE_INT
269bp_unpack_int_in_range (struct bitpack_d *bp,
270 const char *purpose,
271 HOST_WIDE_INT min,
272 HOST_WIDE_INT max)
273{
274 HOST_WIDE_INT range = max - min;
275 int nbits = floor_log2 (range) + 1;
276 HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
277
278 gcc_checking_assert (range > 0 && range < 0x7fffffff);
279
280 if (val < min || val > max)
281 lto_value_range_error (purpose, val, min, max);
282 return val;
283}
284
285/* Output VAL of type "enum enum_name" into OBS.
286 Assume range 0...ENUM_LAST - 1. */
287#define streamer_write_enum(obs,enum_name,enum_last,val) \
288 streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
289
290/* Input enum of type "enum enum_name" from IB.
291 Assume range 0...ENUM_LAST - 1. */
292#define streamer_read_enum(ib,enum_name,enum_last) \
293 (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
294 (int)(enum_last) - 1)
295
296/* Output VAL of type "enum enum_name" into BP.
297 Assume range 0...ENUM_LAST - 1. */
298#define bp_pack_enum(bp,enum_name,enum_last,val) \
299 bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
300
301/* Input enum of type "enum enum_name" from BP.
302 Assume range 0...ENUM_LAST - 1. */
303#define bp_unpack_enum(bp,enum_name,enum_last) \
304 (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
305 (int)(enum_last) - 1)
306
307/* Output the start of a record with TAG to output block OB. */
308
309static inline void
310streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
311{
312 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
313}
314
315/* Return the next tag in the input block IB. */
316
317static inline enum LTO_tags
318streamer_read_record_start (struct lto_input_block *ib)
319{
320 return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
321}
322
323#endif /* GCC_DATA_STREAMER_H */
324