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
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
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
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
31 static unsigned const BITS_PER_BITPACK_WORD
= HOST_BITS_PER_WIDE_INT
;
33 typedef unsigned HOST_WIDE_INT bitpack_word_t
;
37 /* The position of the first unused or unconsumed bit in the word. */
40 /* The current word we are (un)packing. */
43 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
47 /* In data-streamer.c */
48 void bp_pack_var_len_unsigned (struct bitpack_d
*, unsigned HOST_WIDE_INT
);
49 void bp_pack_var_len_int (struct bitpack_d
*, HOST_WIDE_INT
);
50 unsigned HOST_WIDE_INT
bp_unpack_var_len_unsigned (struct bitpack_d
*);
51 HOST_WIDE_INT
bp_unpack_var_len_int (struct bitpack_d
*);
53 /* In data-streamer-out.c */
54 void streamer_write_zero (struct output_block
*);
55 void streamer_write_uhwi (struct output_block
*, unsigned HOST_WIDE_INT
);
56 void streamer_write_hwi (struct output_block
*, HOST_WIDE_INT
);
57 void streamer_write_gcov_count (struct output_block
*, gcov_type
);
58 void streamer_write_string (struct output_block
*, struct lto_output_stream
*,
60 unsigned streamer_string_index (struct output_block
*, const char *,
62 void streamer_write_string_with_length (struct output_block
*,
63 struct lto_output_stream
*,
64 const char *, unsigned int, bool);
65 void bp_pack_string_with_length (struct output_block
*, struct bitpack_d
*,
66 const char *, unsigned int, bool);
67 void bp_pack_string (struct output_block
*, struct bitpack_d
*,
69 void streamer_write_uhwi_stream (struct lto_output_stream
*,
70 unsigned HOST_WIDE_INT
);
71 void streamer_write_hwi_stream (struct lto_output_stream
*, HOST_WIDE_INT
);
72 void streamer_write_gcov_count_stream (struct lto_output_stream
*, gcov_type
);
74 /* In data-streamer-in.c */
75 const char *string_for_index (struct data_in
*, unsigned int, unsigned int *);
76 const char *streamer_read_string (struct data_in
*, struct lto_input_block
*);
77 const char *streamer_read_indexed_string (struct data_in
*,
78 struct lto_input_block
*,
80 const char *bp_unpack_indexed_string (struct data_in
*, struct bitpack_d
*,
82 const char *bp_unpack_string (struct data_in
*, struct bitpack_d
*);
83 unsigned HOST_WIDE_INT
streamer_read_uhwi (struct lto_input_block
*);
84 HOST_WIDE_INT
streamer_read_hwi (struct lto_input_block
*);
85 gcov_type
streamer_read_gcov_count (struct lto_input_block
*);
87 /* Returns a new bit-packing context for bit-packing into S. */
88 static inline struct bitpack_d
89 bitpack_create (struct lto_output_stream
*s
)
94 bp
.stream
= (void *)s
;
98 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
100 bp_pack_value (struct bitpack_d
*bp
, bitpack_word_t val
, unsigned nbits
)
102 bitpack_word_t word
= bp
->word
;
105 /* Verify that VAL fits in the NBITS. */
106 gcc_checking_assert (nbits
== BITS_PER_BITPACK_WORD
107 || !(val
& ~(((bitpack_word_t
)1<<nbits
)-1)));
109 /* If val does not fit into the current bitpack word switch to the
111 if (pos
+ nbits
> BITS_PER_BITPACK_WORD
)
113 streamer_write_uhwi_stream ((struct lto_output_stream
*) bp
->stream
,
127 /* Finishes bit-packing of BP. */
129 streamer_write_bitpack (struct bitpack_d
*bp
)
131 streamer_write_uhwi_stream ((struct lto_output_stream
*) bp
->stream
,
137 /* Returns a new bit-packing context for bit-unpacking from IB. */
138 static inline struct bitpack_d
139 streamer_read_bitpack (struct lto_input_block
*ib
)
142 bp
.word
= streamer_read_uhwi (ib
);
144 bp
.stream
= (void *)ib
;
148 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
149 static inline bitpack_word_t
150 bp_unpack_value (struct bitpack_d
*bp
, unsigned nbits
)
152 bitpack_word_t mask
, val
;
155 mask
= (nbits
== BITS_PER_BITPACK_WORD
156 ? (bitpack_word_t
) -1
157 : ((bitpack_word_t
) 1 << nbits
) - 1);
159 /* If there are not continuous nbits in the current bitpack word
160 switch to the next one. */
161 if (pos
+ nbits
> BITS_PER_BITPACK_WORD
)
164 = streamer_read_uhwi ((struct lto_input_block
*)bp
->stream
);
170 bp
->pos
= pos
+ nbits
;
176 /* Write a character to the output block. */
179 streamer_write_char_stream (struct lto_output_stream
*obs
, char c
)
182 if (obs
->left_in_block
== 0)
183 lto_append_block (obs
);
185 /* Write the actual character. */
186 *obs
->current_pointer
= c
;
187 obs
->current_pointer
++;
189 obs
->left_in_block
--;
193 /* Read byte from the input block. */
195 static inline unsigned char
196 streamer_read_uchar (struct lto_input_block
*ib
)
198 if (ib
->p
>= ib
->len
)
199 lto_section_overrun (ib
);
200 return (ib
->data
[ib
->p
++]);
203 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
204 to be compile time constant.
205 Be host independent, limit range to 31bits. */
208 streamer_write_hwi_in_range (struct lto_output_stream
*obs
,
213 HOST_WIDE_INT range
= max
- min
;
215 gcc_checking_assert (val
>= min
&& val
<= max
&& range
> 0
216 && range
< 0x7fffffff);
219 streamer_write_char_stream (obs
, val
& 255);
221 streamer_write_char_stream (obs
, (val
>> 8) & 255);
223 streamer_write_char_stream (obs
, (val
>> 16) & 255);
224 if (range
>= 0xffffff)
225 streamer_write_char_stream (obs
, (val
>> 24) & 255);
228 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
229 to be compile time constant. PURPOSE is used for error reporting. */
231 static inline HOST_WIDE_INT
232 streamer_read_hwi_in_range (struct lto_input_block
*ib
,
237 HOST_WIDE_INT range
= max
- min
;
238 HOST_WIDE_INT val
= streamer_read_uchar (ib
);
240 gcc_checking_assert (range
> 0 && range
< 0x7fffffff);
243 val
|= ((HOST_WIDE_INT
)streamer_read_uchar (ib
)) << 8;
245 val
|= ((HOST_WIDE_INT
)streamer_read_uchar (ib
)) << 16;
246 if (range
>= 0xffffff)
247 val
|= ((HOST_WIDE_INT
)streamer_read_uchar (ib
)) << 24;
249 if (val
< min
|| val
> max
)
250 lto_value_range_error (purpose
, val
, min
, max
);
254 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
255 to be compile time constant.
256 Be host independent, limit range to 31bits. */
259 bp_pack_int_in_range (struct bitpack_d
*bp
,
264 HOST_WIDE_INT range
= max
- min
;
265 int nbits
= floor_log2 (range
) + 1;
267 gcc_checking_assert (val
>= min
&& val
<= max
&& range
> 0
268 && range
< 0x7fffffff);
271 bp_pack_value (bp
, val
, nbits
);
274 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
275 to be compile time constant. PURPOSE is used for error reporting. */
277 static inline HOST_WIDE_INT
278 bp_unpack_int_in_range (struct bitpack_d
*bp
,
283 HOST_WIDE_INT range
= max
- min
;
284 int nbits
= floor_log2 (range
) + 1;
285 HOST_WIDE_INT val
= bp_unpack_value (bp
, nbits
);
287 gcc_checking_assert (range
> 0 && range
< 0x7fffffff);
289 if (val
< min
|| val
> max
)
290 lto_value_range_error (purpose
, val
, min
, max
);
294 /* Output VAL of type "enum enum_name" into OBS.
295 Assume range 0...ENUM_LAST - 1. */
296 #define streamer_write_enum(obs,enum_name,enum_last,val) \
297 streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
299 /* Input enum of type "enum enum_name" from IB.
300 Assume range 0...ENUM_LAST - 1. */
301 #define streamer_read_enum(ib,enum_name,enum_last) \
302 (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
303 (int)(enum_last) - 1)
305 /* Output VAL of type "enum enum_name" into BP.
306 Assume range 0...ENUM_LAST - 1. */
307 #define bp_pack_enum(bp,enum_name,enum_last,val) \
308 bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
310 /* Input enum of type "enum enum_name" from BP.
311 Assume range 0...ENUM_LAST - 1. */
312 #define bp_unpack_enum(bp,enum_name,enum_last) \
313 (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
314 (int)(enum_last) - 1)
316 /* Output the start of a record with TAG to output block OB. */
319 streamer_write_record_start (struct output_block
*ob
, enum LTO_tags tag
)
321 streamer_write_enum (ob
->main_stream
, LTO_tags
, LTO_NUM_TAGS
, tag
);
324 /* Return the next tag in the input block IB. */
326 static inline enum LTO_tags
327 streamer_read_record_start (struct lto_input_block
*ib
)
329 return streamer_read_enum (ib
, LTO_tags
, LTO_NUM_TAGS
);
332 #endif /* GCC_DATA_STREAMER_H */