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