Replace enum gfc_try with bool type.
[official-gcc.git] / gcc / data-streamer.h
blobe2ab7adfad65b8550781f2ab91bacd5350e7ee3b
1 /* Generic streaming support for various data types.
3 Copyright (C) 2011-2013 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 "vec.h"
26 #include "lto-streamer.h"
28 /* Data structures used to pack values and bitflags into a vector of
29 words. Used to stream values of a fixed number of bits in a space
30 efficient way. */
31 static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
33 typedef unsigned HOST_WIDE_INT bitpack_word_t;
35 struct bitpack_d
37 /* The position of the first unused or unconsumed bit in the word. */
38 unsigned pos;
40 /* The current word we are (un)packing. */
41 bitpack_word_t word;
43 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
44 void *stream;
48 /* String hashing. */
49 struct string_slot
51 const char *s;
52 int len;
53 unsigned int slot_num;
56 /* In data-streamer.c */
57 void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
58 void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT);
59 unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
60 HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
62 /* In data-streamer-out.c */
63 void streamer_write_zero (struct output_block *);
64 void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT);
65 void streamer_write_hwi (struct output_block *, HOST_WIDE_INT);
66 void streamer_write_gcov_count (struct output_block *, gcov_type);
67 void streamer_write_string (struct output_block *, struct lto_output_stream *,
68 const char *, bool);
69 unsigned streamer_string_index (struct output_block *, const char *,
70 unsigned int, bool);
71 void streamer_write_string_with_length (struct output_block *,
72 struct lto_output_stream *,
73 const char *, unsigned int, bool);
74 void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
75 const char *, unsigned int, bool);
76 void bp_pack_string (struct output_block *, struct bitpack_d *,
77 const char *, bool);
78 void streamer_write_uhwi_stream (struct lto_output_stream *,
79 unsigned HOST_WIDE_INT);
80 void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
81 void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
83 /* In data-streamer-in.c */
84 const char *string_for_index (struct data_in *, unsigned int, unsigned int *);
85 const char *streamer_read_string (struct data_in *, struct lto_input_block *);
86 const char *streamer_read_indexed_string (struct data_in *,
87 struct lto_input_block *,
88 unsigned int *);
89 const char *bp_unpack_indexed_string (struct data_in *, struct bitpack_d *,
90 unsigned int *);
91 const char *bp_unpack_string (struct data_in *, struct bitpack_d *);
92 unsigned HOST_WIDE_INT streamer_read_uhwi (struct lto_input_block *);
93 HOST_WIDE_INT streamer_read_hwi (struct lto_input_block *);
94 gcov_type streamer_read_gcov_count (struct lto_input_block *);
96 /* Returns a hash code for P. Adapted from libiberty's htab_hash_string
97 to support strings that may not end in '\0'. */
99 static inline hashval_t
100 hash_string_slot_node (const void *p)
102 const struct string_slot *ds = (const struct string_slot *) p;
103 hashval_t r = ds->len;
104 int i;
106 for (i = 0; i < ds->len; i++)
107 r = r * 67 + (unsigned)ds->s[i] - 113;
108 return r;
111 /* Returns nonzero if P1 and P2 are equal. */
113 static inline int
114 eq_string_slot_node (const void *p1, const void *p2)
116 const struct string_slot *ds1 = (const struct string_slot *) p1;
117 const struct string_slot *ds2 = (const struct string_slot *) p2;
119 if (ds1->len == ds2->len)
120 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
122 return 0;
125 /* Returns a new bit-packing context for bit-packing into S. */
126 static inline struct bitpack_d
127 bitpack_create (struct lto_output_stream *s)
129 struct bitpack_d bp;
130 bp.pos = 0;
131 bp.word = 0;
132 bp.stream = (void *)s;
133 return bp;
136 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
137 static inline void
138 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
140 bitpack_word_t word = bp->word;
141 int pos = bp->pos;
143 /* Verify that VAL fits in the NBITS. */
144 gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
145 || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
147 /* If val does not fit into the current bitpack word switch to the
148 next one. */
149 if (pos + nbits > BITS_PER_BITPACK_WORD)
151 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
152 word);
153 word = val;
154 pos = nbits;
156 else
158 word |= val << pos;
159 pos += nbits;
161 bp->word = word;
162 bp->pos = pos;
165 /* Finishes bit-packing of BP. */
166 static inline void
167 streamer_write_bitpack (struct bitpack_d *bp)
169 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
170 bp->word);
171 bp->word = 0;
172 bp->pos = 0;
175 /* Returns a new bit-packing context for bit-unpacking from IB. */
176 static inline struct bitpack_d
177 streamer_read_bitpack (struct lto_input_block *ib)
179 struct bitpack_d bp;
180 bp.word = streamer_read_uhwi (ib);
181 bp.pos = 0;
182 bp.stream = (void *)ib;
183 return bp;
186 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
187 static inline bitpack_word_t
188 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
190 bitpack_word_t mask, val;
191 int pos = bp->pos;
193 mask = (nbits == BITS_PER_BITPACK_WORD
194 ? (bitpack_word_t) -1
195 : ((bitpack_word_t) 1 << nbits) - 1);
197 /* If there are not continuous nbits in the current bitpack word
198 switch to the next one. */
199 if (pos + nbits > BITS_PER_BITPACK_WORD)
201 bp->word = val
202 = streamer_read_uhwi ((struct lto_input_block *)bp->stream);
203 bp->pos = nbits;
204 return val & mask;
206 val = bp->word;
207 val >>= pos;
208 bp->pos = pos + nbits;
210 return val & mask;
214 /* Write a character to the output block. */
216 static inline void
217 streamer_write_char_stream (struct lto_output_stream *obs, char c)
219 /* No space left. */
220 if (obs->left_in_block == 0)
221 lto_append_block (obs);
223 /* Write the actual character. */
224 *obs->current_pointer = c;
225 obs->current_pointer++;
226 obs->total_size++;
227 obs->left_in_block--;
231 /* Read byte from the input block. */
233 static inline unsigned char
234 streamer_read_uchar (struct lto_input_block *ib)
236 if (ib->p >= ib->len)
237 lto_section_overrun (ib);
238 return (ib->data[ib->p++]);
241 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
242 to be compile time constant.
243 Be host independent, limit range to 31bits. */
245 static inline void
246 streamer_write_hwi_in_range (struct lto_output_stream *obs,
247 HOST_WIDE_INT min,
248 HOST_WIDE_INT max,
249 HOST_WIDE_INT val)
251 HOST_WIDE_INT range = max - min;
253 gcc_checking_assert (val >= min && val <= max && range > 0
254 && range < 0x7fffffff);
256 val -= min;
257 streamer_write_char_stream (obs, val & 255);
258 if (range >= 0xff)
259 streamer_write_char_stream (obs, (val >> 8) & 255);
260 if (range >= 0xffff)
261 streamer_write_char_stream (obs, (val >> 16) & 255);
262 if (range >= 0xffffff)
263 streamer_write_char_stream (obs, (val >> 24) & 255);
266 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
267 to be compile time constant. PURPOSE is used for error reporting. */
269 static inline HOST_WIDE_INT
270 streamer_read_hwi_in_range (struct lto_input_block *ib,
271 const char *purpose,
272 HOST_WIDE_INT min,
273 HOST_WIDE_INT max)
275 HOST_WIDE_INT range = max - min;
276 HOST_WIDE_INT val = streamer_read_uchar (ib);
278 gcc_checking_assert (range > 0 && range < 0x7fffffff);
280 if (range >= 0xff)
281 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 8;
282 if (range >= 0xffff)
283 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 16;
284 if (range >= 0xffffff)
285 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 24;
286 val += min;
287 if (val < min || val > max)
288 lto_value_range_error (purpose, val, min, max);
289 return val;
292 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
293 to be compile time constant.
294 Be host independent, limit range to 31bits. */
296 static inline void
297 bp_pack_int_in_range (struct bitpack_d *bp,
298 HOST_WIDE_INT min,
299 HOST_WIDE_INT max,
300 HOST_WIDE_INT val)
302 HOST_WIDE_INT range = max - min;
303 int nbits = floor_log2 (range) + 1;
305 gcc_checking_assert (val >= min && val <= max && range > 0
306 && range < 0x7fffffff);
308 val -= min;
309 bp_pack_value (bp, val, nbits);
312 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
313 to be compile time constant. PURPOSE is used for error reporting. */
315 static inline HOST_WIDE_INT
316 bp_unpack_int_in_range (struct bitpack_d *bp,
317 const char *purpose,
318 HOST_WIDE_INT min,
319 HOST_WIDE_INT max)
321 HOST_WIDE_INT range = max - min;
322 int nbits = floor_log2 (range) + 1;
323 HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
325 gcc_checking_assert (range > 0 && range < 0x7fffffff);
327 if (val < min || val > max)
328 lto_value_range_error (purpose, val, min, max);
329 return val;
332 /* Output VAL of type "enum enum_name" into OBS.
333 Assume range 0...ENUM_LAST - 1. */
334 #define streamer_write_enum(obs,enum_name,enum_last,val) \
335 streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
337 /* Input enum of type "enum enum_name" from IB.
338 Assume range 0...ENUM_LAST - 1. */
339 #define streamer_read_enum(ib,enum_name,enum_last) \
340 (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
341 (int)(enum_last) - 1)
343 /* Output VAL of type "enum enum_name" into BP.
344 Assume range 0...ENUM_LAST - 1. */
345 #define bp_pack_enum(bp,enum_name,enum_last,val) \
346 bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
348 /* Input enum of type "enum enum_name" from BP.
349 Assume range 0...ENUM_LAST - 1. */
350 #define bp_unpack_enum(bp,enum_name,enum_last) \
351 (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
352 (int)(enum_last) - 1)
354 /* Output the start of a record with TAG to output block OB. */
356 static inline void
357 streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
359 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
362 /* Return the next tag in the input block IB. */
364 static inline enum LTO_tags
365 streamer_read_record_start (struct lto_input_block *ib)
367 return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
370 #endif /* GCC_DATA_STREAMER_H */