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