gcc/testsuite/
[official-gcc.git] / gcc / data-streamer.h
blob705713cd1bd7589d2889ce8834580718aa4b7572
1 /* Generic streaming support for various data types.
3 Copyright 2011 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;
34 DEF_VEC_I(bitpack_word_t);
35 DEF_VEC_ALLOC_I(bitpack_word_t, heap);
37 struct bitpack_d
39 /* The position of the first unused or unconsumed bit in the word. */
40 unsigned pos;
42 /* The current word we are (un)packing. */
43 bitpack_word_t word;
45 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
46 void *stream;
50 /* String hashing. */
51 struct string_slot
53 const char *s;
54 int len;
55 unsigned int slot_num;
58 /* In data-streamer.c */
59 void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
60 void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT);
61 unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
62 HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
64 /* In data-streamer-out.c */
65 void streamer_write_zero (struct output_block *);
66 void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT);
67 void streamer_write_hwi (struct output_block *, HOST_WIDE_INT);
68 void streamer_write_string (struct output_block *, struct lto_output_stream *,
69 const char *, bool);
70 unsigned streamer_string_index (struct output_block *, const char *,
71 unsigned int, bool);
72 void streamer_write_string_with_length (struct output_block *,
73 struct lto_output_stream *,
74 const char *, unsigned int, bool);
75 void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
76 const char *, unsigned int, bool);
77 void bp_pack_string (struct output_block *, struct bitpack_d *,
78 const char *, bool);
79 void streamer_write_uhwi_stream (struct lto_output_stream *,
80 unsigned HOST_WIDE_INT);
81 void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
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 *);
95 /* Returns a hash code for P. Adapted from libiberty's htab_hash_string
96 to support strings that may not end in '\0'. */
98 static inline hashval_t
99 hash_string_slot_node (const void *p)
101 const struct string_slot *ds = (const struct string_slot *) p;
102 hashval_t r = ds->len;
103 int i;
105 for (i = 0; i < ds->len; i++)
106 r = r * 67 + (unsigned)ds->s[i] - 113;
107 return r;
110 /* Returns nonzero if P1 and P2 are equal. */
112 static inline int
113 eq_string_slot_node (const void *p1, const void *p2)
115 const struct string_slot *ds1 = (const struct string_slot *) p1;
116 const struct string_slot *ds2 = (const struct string_slot *) p2;
118 if (ds1->len == ds2->len)
119 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
121 return 0;
124 /* Returns a new bit-packing context for bit-packing into S. */
125 static inline struct bitpack_d
126 bitpack_create (struct lto_output_stream *s)
128 struct bitpack_d bp;
129 bp.pos = 0;
130 bp.word = 0;
131 bp.stream = (void *)s;
132 return bp;
135 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
136 static inline void
137 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
139 bitpack_word_t word = bp->word;
140 int pos = bp->pos;
142 /* Verify that VAL fits in the NBITS. */
143 gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
144 || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
146 /* If val does not fit into the current bitpack word switch to the
147 next one. */
148 if (pos + nbits > BITS_PER_BITPACK_WORD)
150 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
151 word);
152 word = val;
153 pos = nbits;
155 else
157 word |= val << pos;
158 pos += nbits;
160 bp->word = word;
161 bp->pos = pos;
164 /* Finishes bit-packing of BP. */
165 static inline void
166 streamer_write_bitpack (struct bitpack_d *bp)
168 streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
169 bp->word);
170 bp->word = 0;
171 bp->pos = 0;
174 /* Returns a new bit-packing context for bit-unpacking from IB. */
175 static inline struct bitpack_d
176 streamer_read_bitpack (struct lto_input_block *ib)
178 struct bitpack_d bp;
179 bp.word = streamer_read_uhwi (ib);
180 bp.pos = 0;
181 bp.stream = (void *)ib;
182 return bp;
185 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
186 static inline bitpack_word_t
187 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
189 bitpack_word_t mask, val;
190 int pos = bp->pos;
192 mask = (nbits == BITS_PER_BITPACK_WORD
193 ? (bitpack_word_t) -1
194 : ((bitpack_word_t) 1 << nbits) - 1);
196 /* If there are not continuous nbits in the current bitpack word
197 switch to the next one. */
198 if (pos + nbits > BITS_PER_BITPACK_WORD)
200 bp->word = val
201 = streamer_read_uhwi ((struct lto_input_block *)bp->stream);
202 bp->pos = nbits;
203 return val & mask;
205 val = bp->word;
206 val >>= pos;
207 bp->pos = pos + nbits;
209 return val & mask;
213 /* Write a character to the output block. */
215 static inline void
216 streamer_write_char_stream (struct lto_output_stream *obs, char c)
218 /* No space left. */
219 if (obs->left_in_block == 0)
220 lto_append_block (obs);
222 /* Write the actual character. */
223 *obs->current_pointer = c;
224 obs->current_pointer++;
225 obs->total_size++;
226 obs->left_in_block--;
230 /* Read byte from the input block. */
232 static inline unsigned char
233 streamer_read_uchar (struct lto_input_block *ib)
235 if (ib->p >= ib->len)
236 lto_section_overrun (ib);
237 return (ib->data[ib->p++]);
240 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
241 to be compile time constant.
242 Be host independent, limit range to 31bits. */
244 static inline void
245 streamer_write_hwi_in_range (struct lto_output_stream *obs,
246 HOST_WIDE_INT min,
247 HOST_WIDE_INT max,
248 HOST_WIDE_INT val)
250 HOST_WIDE_INT range = max - min;
252 gcc_checking_assert (val >= min && val <= max && range > 0
253 && range < 0x7fffffff);
255 val -= min;
256 streamer_write_char_stream (obs, val & 255);
257 if (range >= 0xff)
258 streamer_write_char_stream (obs, (val >> 8) & 255);
259 if (range >= 0xffff)
260 streamer_write_char_stream (obs, (val >> 16) & 255);
261 if (range >= 0xffffff)
262 streamer_write_char_stream (obs, (val >> 24) & 255);
265 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
266 to be compile time constant. PURPOSE is used for error reporting. */
268 static inline HOST_WIDE_INT
269 streamer_read_hwi_in_range (struct lto_input_block *ib,
270 const char *purpose,
271 HOST_WIDE_INT min,
272 HOST_WIDE_INT max)
274 HOST_WIDE_INT range = max - min;
275 HOST_WIDE_INT val = streamer_read_uchar (ib);
277 gcc_checking_assert (range > 0 && range < 0x7fffffff);
279 if (range >= 0xff)
280 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 8;
281 if (range >= 0xffff)
282 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 16;
283 if (range >= 0xffffff)
284 val |= ((HOST_WIDE_INT)streamer_read_uchar (ib)) << 24;
285 val += min;
286 if (val < min || val > max)
287 lto_value_range_error (purpose, val, min, max);
288 return val;
291 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
292 to be compile time constant.
293 Be host independent, limit range to 31bits. */
295 static inline void
296 bp_pack_int_in_range (struct bitpack_d *bp,
297 HOST_WIDE_INT min,
298 HOST_WIDE_INT max,
299 HOST_WIDE_INT val)
301 HOST_WIDE_INT range = max - min;
302 int nbits = floor_log2 (range) + 1;
304 gcc_checking_assert (val >= min && val <= max && range > 0
305 && range < 0x7fffffff);
307 val -= min;
308 bp_pack_value (bp, val, nbits);
311 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
312 to be compile time constant. PURPOSE is used for error reporting. */
314 static inline HOST_WIDE_INT
315 bp_unpack_int_in_range (struct bitpack_d *bp,
316 const char *purpose,
317 HOST_WIDE_INT min,
318 HOST_WIDE_INT max)
320 HOST_WIDE_INT range = max - min;
321 int nbits = floor_log2 (range) + 1;
322 HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
324 gcc_checking_assert (range > 0 && range < 0x7fffffff);
326 if (val < min || val > max)
327 lto_value_range_error (purpose, val, min, max);
328 return val;
331 /* Output VAL of type "enum enum_name" into OBS.
332 Assume range 0...ENUM_LAST - 1. */
333 #define streamer_write_enum(obs,enum_name,enum_last,val) \
334 streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
336 /* Input enum of type "enum enum_name" from IB.
337 Assume range 0...ENUM_LAST - 1. */
338 #define streamer_read_enum(ib,enum_name,enum_last) \
339 (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
340 (int)(enum_last) - 1)
342 /* Output VAL of type "enum enum_name" into BP.
343 Assume range 0...ENUM_LAST - 1. */
344 #define bp_pack_enum(bp,enum_name,enum_last,val) \
345 bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
347 /* Input enum of type "enum enum_name" from BP.
348 Assume range 0...ENUM_LAST - 1. */
349 #define bp_unpack_enum(bp,enum_name,enum_last) \
350 (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
351 (int)(enum_last) - 1)
353 /* Output the start of a record with TAG to output block OB. */
355 static inline void
356 streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
358 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
361 /* Return the next tag in the input block IB. */
363 static inline enum LTO_tags
364 streamer_read_record_start (struct lto_input_block *ib)
366 return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
369 #endif /* GCC_DATA_STREAMER_H */