cgraph.c (cgraph_turn_edge_to_speculative): Fix debug output.
[official-gcc.git] / gcc / data-streamer-in.c
blob93fe2ff4c3b6219049b0581209d569f2ca5ff641
1 /* Routines for restoring various data types from a file stream. This deals
2 with various data types like strings, integers, enums, etc.
4 Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "diagnostic.h"
27 #include "data-streamer.h"
29 /* Read a string from the string table in DATA_IN using input block
30 IB. Write the length to RLEN. */
32 const char *
33 string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
35 struct lto_input_block str_tab;
36 unsigned int len;
37 const char *result;
39 if (!loc)
41 *rlen = 0;
42 return NULL;
45 /* Get the string stored at location LOC in DATA_IN->STRINGS. */
46 LTO_INIT_INPUT_BLOCK (str_tab, data_in->strings, loc - 1,
47 data_in->strings_len);
48 len = streamer_read_uhwi (&str_tab);
49 *rlen = len;
51 if (str_tab.p + len > data_in->strings_len)
52 internal_error ("bytecode stream: string too long for the string table");
54 result = (const char *)(data_in->strings + str_tab.p);
56 return result;
60 /* Read a string from the string table in DATA_IN using input block
61 IB. Write the length to RLEN. */
63 const char *
64 streamer_read_indexed_string (struct data_in *data_in,
65 struct lto_input_block *ib, unsigned int *rlen)
67 return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
71 /* Read a NULL terminated string from the string table in DATA_IN. */
73 const char *
74 streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
76 unsigned int len;
77 const char *ptr;
79 ptr = streamer_read_indexed_string (data_in, ib, &len);
80 if (!ptr)
81 return NULL;
82 if (ptr[len - 1] != '\0')
83 internal_error ("bytecode stream: found non-null terminated string");
85 return ptr;
89 /* Read a string from the string table in DATA_IN using the bitpack BP.
90 Write the length to RLEN. */
92 const char *
93 bp_unpack_indexed_string (struct data_in *data_in,
94 struct bitpack_d *bp, unsigned int *rlen)
96 return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
100 /* Read a NULL terminated string from the string table in DATA_IN. */
102 const char *
103 bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
105 unsigned int len;
106 const char *ptr;
108 ptr = bp_unpack_indexed_string (data_in, bp, &len);
109 if (!ptr)
110 return NULL;
111 if (ptr[len - 1] != '\0')
112 internal_error ("bytecode stream: found non-null terminated string");
114 return ptr;
118 /* Read an unsigned HOST_WIDE_INT number from IB. */
120 unsigned HOST_WIDE_INT
121 streamer_read_uhwi (struct lto_input_block *ib)
123 unsigned HOST_WIDE_INT result;
124 int shift;
125 unsigned HOST_WIDE_INT byte;
126 unsigned int p = ib->p;
127 unsigned int len = ib->len;
129 const char *data = ib->data;
130 result = data[p++];
131 if ((result & 0x80) != 0)
133 result &= 0x7f;
134 shift = 7;
137 byte = data[p++];
138 result |= (byte & 0x7f) << shift;
139 shift += 7;
141 while ((byte & 0x80) != 0);
144 /* We check for section overrun after the fact for performance reason. */
145 if (p > len)
146 lto_section_overrun (ib);
148 ib->p = p;
149 return result;
153 /* Read a HOST_WIDE_INT number from IB. */
155 HOST_WIDE_INT
156 streamer_read_hwi (struct lto_input_block *ib)
158 HOST_WIDE_INT result = 0;
159 int shift = 0;
160 unsigned HOST_WIDE_INT byte;
162 while (true)
164 byte = streamer_read_uchar (ib);
165 result |= (byte & 0x7f) << shift;
166 shift += 7;
167 if ((byte & 0x80) == 0)
169 if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
170 result |= - ((HOST_WIDE_INT)1 << shift);
172 return result;
177 /* Read gcov_type value from IB. */
179 gcov_type
180 streamer_read_gcov_count (struct lto_input_block *ib)
182 gcov_type ret = streamer_read_hwi (ib);
183 gcc_assert (ret >= 0);
184 return ret;