gcc/c-family/
[official-gcc.git] / gcc / data-streamer-in.c
blob842424869412e9f7fe843192d8cd85d3c37a960f
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 "tree.h"
28 #include "gimple.h"
29 #include "data-streamer.h"
31 /* Read a string from the string table in DATA_IN using input block
32 IB. Write the length to RLEN. */
34 const char *
35 string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
37 struct lto_input_block str_tab;
38 unsigned int len;
39 const char *result;
41 if (!loc)
43 *rlen = 0;
44 return NULL;
47 /* Get the string stored at location LOC in DATA_IN->STRINGS. */
48 LTO_INIT_INPUT_BLOCK (str_tab, data_in->strings, loc - 1,
49 data_in->strings_len);
50 len = streamer_read_uhwi (&str_tab);
51 *rlen = len;
53 if (str_tab.p + len > data_in->strings_len)
54 internal_error ("bytecode stream: string too long for the string table");
56 result = (const char *)(data_in->strings + str_tab.p);
58 return result;
62 /* Read a string from the string table in DATA_IN using input block
63 IB. Write the length to RLEN. */
65 const char *
66 streamer_read_indexed_string (struct data_in *data_in,
67 struct lto_input_block *ib, unsigned int *rlen)
69 return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
73 /* Read a NULL terminated string from the string table in DATA_IN. */
75 const char *
76 streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
78 unsigned int len;
79 const char *ptr;
81 ptr = streamer_read_indexed_string (data_in, ib, &len);
82 if (!ptr)
83 return NULL;
84 if (ptr[len - 1] != '\0')
85 internal_error ("bytecode stream: found non-null terminated string");
87 return ptr;
91 /* Read a string from the string table in DATA_IN using the bitpack BP.
92 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)
98 return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
102 /* Read a NULL terminated string from the string table in DATA_IN. */
104 const char *
105 bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
107 unsigned int len;
108 const char *ptr;
110 ptr = bp_unpack_indexed_string (data_in, bp, &len);
111 if (!ptr)
112 return NULL;
113 if (ptr[len - 1] != '\0')
114 internal_error ("bytecode stream: found non-null terminated string");
116 return ptr;
120 /* Read an unsigned HOST_WIDE_INT number from IB. */
122 unsigned HOST_WIDE_INT
123 streamer_read_uhwi (struct lto_input_block *ib)
125 unsigned HOST_WIDE_INT result;
126 int shift;
127 unsigned HOST_WIDE_INT byte;
128 unsigned int p = ib->p;
129 unsigned int len = ib->len;
131 const char *data = ib->data;
132 result = data[p++];
133 if ((result & 0x80) != 0)
135 result &= 0x7f;
136 shift = 7;
139 byte = data[p++];
140 result |= (byte & 0x7f) << shift;
141 shift += 7;
143 while ((byte & 0x80) != 0);
146 /* We check for section overrun after the fact for performance reason. */
147 if (p > len)
148 lto_section_overrun (ib);
150 ib->p = p;
151 return result;
155 /* Read a HOST_WIDE_INT number from IB. */
157 HOST_WIDE_INT
158 streamer_read_hwi (struct lto_input_block *ib)
160 HOST_WIDE_INT result = 0;
161 int shift = 0;
162 unsigned HOST_WIDE_INT byte;
164 while (true)
166 byte = streamer_read_uchar (ib);
167 result |= (byte & 0x7f) << shift;
168 shift += 7;
169 if ((byte & 0x80) == 0)
171 if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
172 result |= - ((HOST_WIDE_INT)1 << shift);
174 return result;
179 /* Read gcov_type value from IB. */
181 gcov_type
182 streamer_read_gcov_count (struct lto_input_block *ib)
184 gcov_type ret = streamer_read_hwi (ib);
185 gcc_assert (ret >= 0);
186 return ret;