2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / data-streamer-in.c
blobc8f6dcbfff77cc8a1de2edd38c39b2ce57fa8aa1
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-2015 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 "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "options.h"
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "predict.h"
34 #include "tm.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "plugin-api.h"
45 #include "ipa-ref.h"
46 #include "cgraph.h"
47 #include "data-streamer.h"
49 /* Read a string from the string table in DATA_IN using input block
50 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)
55 unsigned int len;
56 const char *result;
58 if (!loc)
60 *rlen = 0;
61 return NULL;
64 /* Get the string stored at location LOC in DATA_IN->STRINGS. */
65 lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
66 len = streamer_read_uhwi (&str_tab);
67 *rlen = len;
69 if (str_tab.p + len > data_in->strings_len)
70 internal_error ("bytecode stream: string too long for the string table");
72 result = (const char *)(data_in->strings + str_tab.p);
74 return result;
78 /* Read a string from the string table in DATA_IN using input block
79 IB. Write the length to RLEN. */
81 const char *
82 streamer_read_indexed_string (struct data_in *data_in,
83 struct lto_input_block *ib, unsigned int *rlen)
85 return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
89 /* Read a NULL terminated string from the string table in DATA_IN. */
91 const char *
92 streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
94 unsigned int len;
95 const char *ptr;
97 ptr = streamer_read_indexed_string (data_in, ib, &len);
98 if (!ptr)
99 return NULL;
100 if (ptr[len - 1] != '\0')
101 internal_error ("bytecode stream: found non-null terminated string");
103 return ptr;
107 /* Read a string from the string table in DATA_IN using the bitpack BP.
108 Write the length to RLEN. */
110 const char *
111 bp_unpack_indexed_string (struct data_in *data_in,
112 struct bitpack_d *bp, unsigned int *rlen)
114 return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
118 /* Read a NULL terminated string from the string table in DATA_IN. */
120 const char *
121 bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
123 unsigned int len;
124 const char *ptr;
126 ptr = bp_unpack_indexed_string (data_in, bp, &len);
127 if (!ptr)
128 return NULL;
129 if (ptr[len - 1] != '\0')
130 internal_error ("bytecode stream: found non-null terminated string");
132 return ptr;
136 /* Read an unsigned HOST_WIDE_INT number from IB. */
138 unsigned HOST_WIDE_INT
139 streamer_read_uhwi (struct lto_input_block *ib)
141 unsigned HOST_WIDE_INT result;
142 int shift;
143 unsigned HOST_WIDE_INT byte;
144 unsigned int p = ib->p;
145 unsigned int len = ib->len;
147 const char *data = ib->data;
148 result = data[p++];
149 if ((result & 0x80) != 0)
151 result &= 0x7f;
152 shift = 7;
155 byte = data[p++];
156 result |= (byte & 0x7f) << shift;
157 shift += 7;
159 while ((byte & 0x80) != 0);
162 /* We check for section overrun after the fact for performance reason. */
163 if (p > len)
164 lto_section_overrun (ib);
166 ib->p = p;
167 return result;
171 /* Read a HOST_WIDE_INT number from IB. */
173 HOST_WIDE_INT
174 streamer_read_hwi (struct lto_input_block *ib)
176 HOST_WIDE_INT result = 0;
177 int shift = 0;
178 unsigned HOST_WIDE_INT byte;
180 while (true)
182 byte = streamer_read_uchar (ib);
183 result |= (byte & 0x7f) << shift;
184 shift += 7;
185 if ((byte & 0x80) == 0)
187 if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
188 result |= - (HOST_WIDE_INT_1U << shift);
190 return result;
195 /* Read gcov_type value from IB. */
197 gcov_type
198 streamer_read_gcov_count (struct lto_input_block *ib)
200 gcov_type ret = streamer_read_hwi (ib);
201 gcc_assert (ret >= 0);
202 return ret;