GCC Middle and Back End API Reference
data-streamer.h
Go to the documentation of this file.
1 /* Generic streaming support for various data types.
2 
3  Copyright (C) 2011-2013 Free Software Foundation, Inc.
4  Contributed by Diego Novillo <dnovillo@google.com>
5 
6 This file is part of GCC.
7 
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.
12 
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.
17 
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/>. */
21 
22 #ifndef GCC_DATA_STREAMER_H
23 #define GCC_DATA_STREAMER_H
24 
25 #include "vec.h"
26 #include "lto-streamer.h"
27 
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. */
32 
33 typedef unsigned HOST_WIDE_INT bitpack_word_t;
34 
35 struct bitpack_d
36 {
37  /* The position of the first unused or unconsumed bit in the word. */
38  unsigned pos;
39 
40  /* The current word we are (un)packing. */
42 
43  /* The lto_output_stream or the lto_input_block we are streaming to/from. */
44  void *stream;
45 };
46 
47 /* In data-streamer.c */
48 void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
52 
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);
59  const char *, bool);
60 unsigned streamer_string_index (struct output_block *, const char *,
61  unsigned int, bool);
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 *,
68  const char *, bool);
70  unsigned HOST_WIDE_INT);
73 
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 *,
79  unsigned int *);
80 const char *bp_unpack_indexed_string (struct data_in *, struct bitpack_d *,
81  unsigned int *);
82 const char *bp_unpack_string (struct data_in *, struct bitpack_d *);
86 
87 /* Returns a new bit-packing context for bit-packing into S. */
88 static inline struct bitpack_d
90 {
91  struct bitpack_d bp;
92  bp.pos = 0;
93  bp.word = 0;
94  bp.stream = (void *)s;
95  return bp;
96 }
97 
98 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
99 static inline void
100 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
101 {
102  bitpack_word_t word = bp->word;
103  int pos = bp->pos;
104 
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)));
108 
109  /* If val does not fit into the current bitpack word switch to the
110  next one. */
111  if (pos + nbits > BITS_PER_BITPACK_WORD)
112  {
114  word);
115  word = val;
116  pos = nbits;
117  }
118  else
119  {
120  word |= val << pos;
121  pos += nbits;
122  }
123  bp->word = word;
124  bp->pos = pos;
125 }
126 
127 /* Finishes bit-packing of BP. */
128 static inline void
130 {
132  bp->word);
133  bp->word = 0;
134  bp->pos = 0;
135 }
136 
137 /* Returns a new bit-packing context for bit-unpacking from IB. */
138 static inline struct bitpack_d
140 {
141  struct bitpack_d bp;
142  bp.word = streamer_read_uhwi (ib);
143  bp.pos = 0;
144  bp.stream = (void *)ib;
145  return bp;
146 }
147 
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)
151 {
152  bitpack_word_t mask, val;
153  int pos = bp->pos;
154 
155  mask = (nbits == BITS_PER_BITPACK_WORD
156  ? (bitpack_word_t) -1
157  : ((bitpack_word_t) 1 << nbits) - 1);
158 
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)
162  {
163  bp->word = val
164  = streamer_read_uhwi ((struct lto_input_block *)bp->stream);
165  bp->pos = nbits;
166  return val & mask;
167  }
168  val = bp->word;
169  val >>= pos;
170  bp->pos = pos + nbits;
171 
172  return val & mask;
173 }
174 
175 
176 /* Write a character to the output block. */
177 
178 static inline void
180 {
181  /* No space left. */
182  if (obs->left_in_block == 0)
183  lto_append_block (obs);
184 
185  /* Write the actual character. */
186  char *current_pointer = obs->current_pointer;
187  *(current_pointer++) = c;
188  obs->current_pointer = current_pointer;
189  obs->total_size++;
190  obs->left_in_block--;
191 }
192 
193 
194 /* Read byte from the input block. */
195 
196 static inline unsigned char
198 {
199  if (ib->p >= ib->len)
200  lto_section_overrun (ib);
201  return (ib->data[ib->p++]);
202 }
203 
204 /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
205  to be compile time constant.
206  Be host independent, limit range to 31bits. */
207 
208 static inline void
210  HOST_WIDE_INT min,
211  HOST_WIDE_INT max,
212  HOST_WIDE_INT val)
213 {
214  HOST_WIDE_INT range = max - min;
215 
216  gcc_checking_assert (val >= min && val <= max && range > 0
217  && range < 0x7fffffff);
218 
219  val -= min;
220  streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
221 }
222 
223 /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
224  to be compile time constant. PURPOSE is used for error reporting. */
225 
226 static inline HOST_WIDE_INT
228  const char *purpose,
229  HOST_WIDE_INT min,
230  HOST_WIDE_INT max)
231 {
232  HOST_WIDE_INT range = max - min;
233  unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
234 
235  gcc_checking_assert (range > 0 && range < 0x7fffffff);
236 
237  HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
238  if (val < min || val > max)
239  lto_value_range_error (purpose, val, min, max);
240  return val;
241 }
242 
243 /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
244  to be compile time constant.
245  Be host independent, limit range to 31bits. */
246 
247 static inline void
249  HOST_WIDE_INT min,
250  HOST_WIDE_INT max,
251  HOST_WIDE_INT val)
252 {
253  HOST_WIDE_INT range = max - min;
254  int nbits = floor_log2 (range) + 1;
255 
256  gcc_checking_assert (val >= min && val <= max && range > 0
257  && range < 0x7fffffff);
258 
259  val -= min;
260  bp_pack_value (bp, val, nbits);
261 }
262 
263 /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
264  to be compile time constant. PURPOSE is used for error reporting. */
265 
266 static inline HOST_WIDE_INT
268  const char *purpose,
269  HOST_WIDE_INT min,
270  HOST_WIDE_INT max)
271 {
272  HOST_WIDE_INT range = max - min;
273  int nbits = floor_log2 (range) + 1;
274  HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
275 
276  gcc_checking_assert (range > 0 && range < 0x7fffffff);
277 
278  if (val < min || val > max)
279  lto_value_range_error (purpose, val, min, max);
280  return val;
281 }
282 
283 /* Output VAL of type "enum enum_name" into OBS.
284  Assume range 0...ENUM_LAST - 1. */
285 #define streamer_write_enum(obs,enum_name,enum_last,val) \
286  streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
287 
288 /* Input enum of type "enum enum_name" from IB.
289  Assume range 0...ENUM_LAST - 1. */
290 #define streamer_read_enum(ib,enum_name,enum_last) \
291  (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
292  (int)(enum_last) - 1)
293 
294 /* Output VAL of type "enum enum_name" into BP.
295  Assume range 0...ENUM_LAST - 1. */
296 #define bp_pack_enum(bp,enum_name,enum_last,val) \
297  bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
298 
299 /* Input enum of type "enum enum_name" from BP.
300  Assume range 0...ENUM_LAST - 1. */
301 #define bp_unpack_enum(bp,enum_name,enum_last) \
302  (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
303  (int)(enum_last) - 1)
304 
305 /* Output the start of a record with TAG to output block OB. */
306 
307 static inline void
309 {
310  streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
311 }
312 
313 /* Return the next tag in the input block IB. */
314 
315 static inline enum LTO_tags
317 {
318  return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
319 }
320 
321 #endif /* GCC_DATA_STREAMER_H */