data-streamer-in.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Routines for restoring various data types from a file stream. This deals
  2. with various data types like strings, integers, enums, etc.
  3. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  4. Contributed by Diego Novillo <dnovillo@google.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "diagnostic.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "options.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "fold-const.h"
  33. #include "predict.h"
  34. #include "vec.h"
  35. #include "tm.h"
  36. #include "hard-reg-set.h"
  37. #include "input.h"
  38. #include "function.h"
  39. #include "basic-block.h"
  40. #include "tree-ssa-alias.h"
  41. #include "internal-fn.h"
  42. #include "gimple-expr.h"
  43. #include "is-a.h"
  44. #include "gimple.h"
  45. #include "hash-map.h"
  46. #include "plugin-api.h"
  47. #include "ipa-ref.h"
  48. #include "cgraph.h"
  49. #include "data-streamer.h"
  50. /* Read a string from the string table in DATA_IN using input block
  51. IB. Write the length to RLEN. */
  52. static const char *
  53. string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
  54. {
  55. unsigned int len;
  56. const char *result;
  57. if (!loc)
  58. {
  59. *rlen = 0;
  60. return NULL;
  61. }
  62. /* Get the string stored at location LOC in DATA_IN->STRINGS. */
  63. lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
  64. len = streamer_read_uhwi (&str_tab);
  65. *rlen = len;
  66. if (str_tab.p + len > data_in->strings_len)
  67. internal_error ("bytecode stream: string too long for the string table");
  68. result = (const char *)(data_in->strings + str_tab.p);
  69. return result;
  70. }
  71. /* Read a string from the string table in DATA_IN using input block
  72. IB. Write the length to RLEN. */
  73. const char *
  74. streamer_read_indexed_string (struct data_in *data_in,
  75. struct lto_input_block *ib, unsigned int *rlen)
  76. {
  77. return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
  78. }
  79. /* Read a NULL terminated string from the string table in DATA_IN. */
  80. const char *
  81. streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
  82. {
  83. unsigned int len;
  84. const char *ptr;
  85. ptr = streamer_read_indexed_string (data_in, ib, &len);
  86. if (!ptr)
  87. return NULL;
  88. if (ptr[len - 1] != '\0')
  89. internal_error ("bytecode stream: found non-null terminated string");
  90. return ptr;
  91. }
  92. /* Read a string from the string table in DATA_IN using the bitpack BP.
  93. Write the length to RLEN. */
  94. const char *
  95. bp_unpack_indexed_string (struct data_in *data_in,
  96. struct bitpack_d *bp, unsigned int *rlen)
  97. {
  98. return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
  99. }
  100. /* Read a NULL terminated string from the string table in DATA_IN. */
  101. const char *
  102. bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
  103. {
  104. unsigned int len;
  105. const char *ptr;
  106. ptr = bp_unpack_indexed_string (data_in, bp, &len);
  107. if (!ptr)
  108. return NULL;
  109. if (ptr[len - 1] != '\0')
  110. internal_error ("bytecode stream: found non-null terminated string");
  111. return ptr;
  112. }
  113. /* Read an unsigned HOST_WIDE_INT number from IB. */
  114. unsigned HOST_WIDE_INT
  115. streamer_read_uhwi (struct lto_input_block *ib)
  116. {
  117. unsigned HOST_WIDE_INT result;
  118. int shift;
  119. unsigned HOST_WIDE_INT byte;
  120. unsigned int p = ib->p;
  121. unsigned int len = ib->len;
  122. const char *data = ib->data;
  123. result = data[p++];
  124. if ((result & 0x80) != 0)
  125. {
  126. result &= 0x7f;
  127. shift = 7;
  128. do
  129. {
  130. byte = data[p++];
  131. result |= (byte & 0x7f) << shift;
  132. shift += 7;
  133. }
  134. while ((byte & 0x80) != 0);
  135. }
  136. /* We check for section overrun after the fact for performance reason. */
  137. if (p > len)
  138. lto_section_overrun (ib);
  139. ib->p = p;
  140. return result;
  141. }
  142. /* Read a HOST_WIDE_INT number from IB. */
  143. HOST_WIDE_INT
  144. streamer_read_hwi (struct lto_input_block *ib)
  145. {
  146. HOST_WIDE_INT result = 0;
  147. int shift = 0;
  148. unsigned HOST_WIDE_INT byte;
  149. while (true)
  150. {
  151. byte = streamer_read_uchar (ib);
  152. result |= (byte & 0x7f) << shift;
  153. shift += 7;
  154. if ((byte & 0x80) == 0)
  155. {
  156. if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
  157. result |= - (HOST_WIDE_INT_1U << shift);
  158. return result;
  159. }
  160. }
  161. }
  162. /* Read gcov_type value from IB. */
  163. gcov_type
  164. streamer_read_gcov_count (struct lto_input_block *ib)
  165. {
  166. gcov_type ret = streamer_read_hwi (ib);
  167. gcc_assert (ret >= 0);
  168. return ret;
  169. }