data-streamer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Generic streaming support for various data types.
  2. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. Contributed by Diego Novillo <dnovillo@google.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_DATA_STREAMER_H
  17. #define GCC_DATA_STREAMER_H
  18. #include "vec.h"
  19. #include "lto-streamer.h"
  20. /* Data structures used to pack values and bitflags into a vector of
  21. words. Used to stream values of a fixed number of bits in a space
  22. efficient way. */
  23. static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
  24. typedef unsigned HOST_WIDE_INT bitpack_word_t;
  25. struct bitpack_d
  26. {
  27. /* The position of the first unused or unconsumed bit in the word. */
  28. unsigned pos;
  29. /* The current word we are (un)packing. */
  30. bitpack_word_t word;
  31. /* The lto_output_stream or the lto_input_block we are streaming to/from. */
  32. void *stream;
  33. };
  34. /* In data-streamer.c */
  35. void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
  36. void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT);
  37. unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
  38. HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
  39. /* In data-streamer-out.c */
  40. void streamer_write_zero (struct output_block *);
  41. void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT);
  42. void streamer_write_hwi (struct output_block *, HOST_WIDE_INT);
  43. void streamer_write_gcov_count (struct output_block *, gcov_type);
  44. void streamer_write_string (struct output_block *, struct lto_output_stream *,
  45. const char *, bool);
  46. void streamer_write_string_with_length (struct output_block *,
  47. struct lto_output_stream *,
  48. const char *, unsigned int, bool);
  49. void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
  50. const char *, unsigned int, bool);
  51. void bp_pack_string (struct output_block *, struct bitpack_d *,
  52. const char *, bool);
  53. void streamer_write_uhwi_stream (struct lto_output_stream *,
  54. unsigned HOST_WIDE_INT);
  55. void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
  56. void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
  57. void streamer_write_data_stream (struct lto_output_stream *, const void *,
  58. size_t);
  59. /* In data-streamer-in.c */
  60. const char *streamer_read_string (struct data_in *, struct lto_input_block *);
  61. const char *streamer_read_indexed_string (struct data_in *,
  62. struct lto_input_block *,
  63. unsigned int *);
  64. const char *bp_unpack_indexed_string (struct data_in *, struct bitpack_d *,
  65. unsigned int *);
  66. const char *bp_unpack_string (struct data_in *, struct bitpack_d *);
  67. unsigned HOST_WIDE_INT streamer_read_uhwi (struct lto_input_block *);
  68. HOST_WIDE_INT streamer_read_hwi (struct lto_input_block *);
  69. gcov_type streamer_read_gcov_count (struct lto_input_block *);
  70. /* Returns a new bit-packing context for bit-packing into S. */
  71. static inline struct bitpack_d
  72. bitpack_create (struct lto_output_stream *s)
  73. {
  74. struct bitpack_d bp;
  75. bp.pos = 0;
  76. bp.word = 0;
  77. bp.stream = (void *)s;
  78. return bp;
  79. }
  80. /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
  81. static inline void
  82. bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
  83. {
  84. bitpack_word_t word = bp->word;
  85. int pos = bp->pos;
  86. /* Verify that VAL fits in the NBITS. */
  87. gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
  88. || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
  89. /* If val does not fit into the current bitpack word switch to the
  90. next one. */
  91. if (pos + nbits > BITS_PER_BITPACK_WORD)
  92. {
  93. streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
  94. word);
  95. word = val;
  96. pos = nbits;
  97. }
  98. else
  99. {
  100. word |= val << pos;
  101. pos += nbits;
  102. }
  103. bp->word = word;
  104. bp->pos = pos;
  105. }
  106. /* Finishes bit-packing of BP. */
  107. static inline void
  108. streamer_write_bitpack (struct bitpack_d *bp)
  109. {
  110. streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
  111. bp->word);
  112. bp->word = 0;
  113. bp->pos = 0;
  114. }
  115. /* Returns a new bit-packing context for bit-unpacking from IB. */
  116. static inline struct bitpack_d
  117. streamer_read_bitpack (struct lto_input_block *ib)
  118. {
  119. struct bitpack_d bp;
  120. bp.word = streamer_read_uhwi (ib);
  121. bp.pos = 0;
  122. bp.stream = (void *)ib;
  123. return bp;
  124. }
  125. /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
  126. static inline bitpack_word_t
  127. bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
  128. {
  129. bitpack_word_t mask, val;
  130. int pos = bp->pos;
  131. mask = (nbits == BITS_PER_BITPACK_WORD
  132. ? (bitpack_word_t) -1
  133. : ((bitpack_word_t) 1 << nbits) - 1);
  134. /* If there are not continuous nbits in the current bitpack word
  135. switch to the next one. */
  136. if (pos + nbits > BITS_PER_BITPACK_WORD)
  137. {
  138. bp->word = val
  139. = streamer_read_uhwi ((struct lto_input_block *)bp->stream);
  140. bp->pos = nbits;
  141. return val & mask;
  142. }
  143. val = bp->word;
  144. val >>= pos;
  145. bp->pos = pos + nbits;
  146. return val & mask;
  147. }
  148. /* Write a character to the output block. */
  149. static inline void
  150. streamer_write_char_stream (struct lto_output_stream *obs, char c)
  151. {
  152. /* No space left. */
  153. if (obs->left_in_block == 0)
  154. lto_append_block (obs);
  155. /* Write the actual character. */
  156. char *current_pointer = obs->current_pointer;
  157. *(current_pointer++) = c;
  158. obs->current_pointer = current_pointer;
  159. obs->total_size++;
  160. obs->left_in_block--;
  161. }
  162. /* Read byte from the input block. */
  163. static inline unsigned char
  164. streamer_read_uchar (struct lto_input_block *ib)
  165. {
  166. if (ib->p >= ib->len)
  167. lto_section_overrun (ib);
  168. return (ib->data[ib->p++]);
  169. }
  170. /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
  171. to be compile time constant.
  172. Be host independent, limit range to 31bits. */
  173. static inline void
  174. streamer_write_hwi_in_range (struct lto_output_stream *obs,
  175. HOST_WIDE_INT min,
  176. HOST_WIDE_INT max,
  177. HOST_WIDE_INT val)
  178. {
  179. HOST_WIDE_INT range = max - min;
  180. gcc_checking_assert (val >= min && val <= max && range > 0
  181. && range < 0x7fffffff);
  182. val -= min;
  183. streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
  184. }
  185. /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
  186. to be compile time constant. PURPOSE is used for error reporting. */
  187. static inline HOST_WIDE_INT
  188. streamer_read_hwi_in_range (struct lto_input_block *ib,
  189. const char *purpose,
  190. HOST_WIDE_INT min,
  191. HOST_WIDE_INT max)
  192. {
  193. HOST_WIDE_INT range = max - min;
  194. unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
  195. gcc_checking_assert (range > 0 && range < 0x7fffffff);
  196. HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
  197. if (val < min || val > max)
  198. lto_value_range_error (purpose, val, min, max);
  199. return val;
  200. }
  201. /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
  202. to be compile time constant.
  203. Be host independent, limit range to 31bits. */
  204. static inline void
  205. bp_pack_int_in_range (struct bitpack_d *bp,
  206. HOST_WIDE_INT min,
  207. HOST_WIDE_INT max,
  208. HOST_WIDE_INT val)
  209. {
  210. HOST_WIDE_INT range = max - min;
  211. int nbits = floor_log2 (range) + 1;
  212. gcc_checking_assert (val >= min && val <= max && range > 0
  213. && range < 0x7fffffff);
  214. val -= min;
  215. bp_pack_value (bp, val, nbits);
  216. }
  217. /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
  218. to be compile time constant. PURPOSE is used for error reporting. */
  219. static inline HOST_WIDE_INT
  220. bp_unpack_int_in_range (struct bitpack_d *bp,
  221. const char *purpose,
  222. HOST_WIDE_INT min,
  223. HOST_WIDE_INT max)
  224. {
  225. HOST_WIDE_INT range = max - min;
  226. int nbits = floor_log2 (range) + 1;
  227. HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
  228. gcc_checking_assert (range > 0 && range < 0x7fffffff);
  229. if (val < min || val > max)
  230. lto_value_range_error (purpose, val, min, max);
  231. return val;
  232. }
  233. /* Output VAL of type "enum enum_name" into OBS.
  234. Assume range 0...ENUM_LAST - 1. */
  235. #define streamer_write_enum(obs,enum_name,enum_last,val) \
  236. streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
  237. /* Input enum of type "enum enum_name" from IB.
  238. Assume range 0...ENUM_LAST - 1. */
  239. #define streamer_read_enum(ib,enum_name,enum_last) \
  240. (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
  241. (int)(enum_last) - 1)
  242. /* Output VAL of type "enum enum_name" into BP.
  243. Assume range 0...ENUM_LAST - 1. */
  244. #define bp_pack_enum(bp,enum_name,enum_last,val) \
  245. bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
  246. /* Input enum of type "enum enum_name" from BP.
  247. Assume range 0...ENUM_LAST - 1. */
  248. #define bp_unpack_enum(bp,enum_name,enum_last) \
  249. (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
  250. (int)(enum_last) - 1)
  251. /* Output the start of a record with TAG to output block OB. */
  252. static inline void
  253. streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
  254. {
  255. streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
  256. }
  257. /* Return the next tag in the input block IB. */
  258. static inline enum LTO_tags
  259. streamer_read_record_start (struct lto_input_block *ib)
  260. {
  261. return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
  262. }
  263. #endif /* GCC_DATA_STREAMER_H */