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