IA MCU psABI support: changes to libraries
[gcc.git] / gcc / data-streamer-in.c
1 /* Routines for restoring various data types from a file stream. This deals
2 with various data types like strings, integers, enums, etc.
3
4 Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "diagnostic.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "predict.h"
33 #include "tm.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "cgraph.h"
42 #include "data-streamer.h"
43
44 /* Read a string from the string table in DATA_IN using input block
45 IB. Write the length to RLEN. */
46
47 static const char *
48 string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
49 {
50 unsigned int len;
51 const char *result;
52
53 if (!loc)
54 {
55 *rlen = 0;
56 return NULL;
57 }
58
59 /* Get the string stored at location LOC in DATA_IN->STRINGS. */
60 lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
61 len = streamer_read_uhwi (&str_tab);
62 *rlen = len;
63
64 if (str_tab.p + len > data_in->strings_len)
65 internal_error ("bytecode stream: string too long for the string table");
66
67 result = (const char *)(data_in->strings + str_tab.p);
68
69 return result;
70 }
71
72
73 /* Read a string from the string table in DATA_IN using input block
74 IB. Write the length to RLEN. */
75
76 const char *
77 streamer_read_indexed_string (struct data_in *data_in,
78 struct lto_input_block *ib, unsigned int *rlen)
79 {
80 return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
81 }
82
83
84 /* Read a NULL terminated string from the string table in DATA_IN. */
85
86 const char *
87 streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
88 {
89 unsigned int len;
90 const char *ptr;
91
92 ptr = streamer_read_indexed_string (data_in, ib, &len);
93 if (!ptr)
94 return NULL;
95 if (ptr[len - 1] != '\0')
96 internal_error ("bytecode stream: found non-null terminated string");
97
98 return ptr;
99 }
100
101
102 /* Read a string from the string table in DATA_IN using the bitpack BP.
103 Write the length to RLEN. */
104
105 const char *
106 bp_unpack_indexed_string (struct data_in *data_in,
107 struct bitpack_d *bp, unsigned int *rlen)
108 {
109 return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
110 }
111
112
113 /* Read a NULL terminated string from the string table in DATA_IN. */
114
115 const char *
116 bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
117 {
118 unsigned int len;
119 const char *ptr;
120
121 ptr = bp_unpack_indexed_string (data_in, bp, &len);
122 if (!ptr)
123 return NULL;
124 if (ptr[len - 1] != '\0')
125 internal_error ("bytecode stream: found non-null terminated string");
126
127 return ptr;
128 }
129
130
131 /* Read an unsigned HOST_WIDE_INT number from IB. */
132
133 unsigned HOST_WIDE_INT
134 streamer_read_uhwi (struct lto_input_block *ib)
135 {
136 unsigned HOST_WIDE_INT result;
137 int shift;
138 unsigned HOST_WIDE_INT byte;
139 unsigned int p = ib->p;
140 unsigned int len = ib->len;
141
142 const char *data = ib->data;
143 result = data[p++];
144 if ((result & 0x80) != 0)
145 {
146 result &= 0x7f;
147 shift = 7;
148 do
149 {
150 byte = data[p++];
151 result |= (byte & 0x7f) << shift;
152 shift += 7;
153 }
154 while ((byte & 0x80) != 0);
155 }
156
157 /* We check for section overrun after the fact for performance reason. */
158 if (p > len)
159 lto_section_overrun (ib);
160
161 ib->p = p;
162 return result;
163 }
164
165
166 /* Read a HOST_WIDE_INT number from IB. */
167
168 HOST_WIDE_INT
169 streamer_read_hwi (struct lto_input_block *ib)
170 {
171 HOST_WIDE_INT result = 0;
172 int shift = 0;
173 unsigned HOST_WIDE_INT byte;
174
175 while (true)
176 {
177 byte = streamer_read_uchar (ib);
178 result |= (byte & 0x7f) << shift;
179 shift += 7;
180 if ((byte & 0x80) == 0)
181 {
182 if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
183 result |= - (HOST_WIDE_INT_1U << shift);
184
185 return result;
186 }
187 }
188 }
189
190 /* Read gcov_type value from IB. */
191
192 gcov_type
193 streamer_read_gcov_count (struct lto_input_block *ib)
194 {
195 gcov_type ret = streamer_read_hwi (ib);
196 gcc_assert (ret >= 0);
197 return ret;
198 }