data-streamer.h 11 KB

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