annotate gcc/data-streamer.h @ 16:04ced10e8804

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