data-streamer.h (streamer_string_index, [...]): Remove.
[gcc.git] / gcc / data-streamer-out.c
1 /* Routines for saving various data types to a file stream. This deals
2 with various data types like strings, integers, enums, etc.
3
4 Copyright (C) 2011-2014 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 "tree.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-expr.h"
31 #include "is-a.h"
32 #include "gimple.h"
33 #include "data-streamer.h"
34
35
36 /* Adds a new block to output stream OBS. */
37
38 void
39 lto_append_block (struct lto_output_stream *obs)
40 {
41 struct lto_char_ptr_base *new_block;
42
43 gcc_assert (obs->left_in_block == 0);
44
45 if (obs->first_block == NULL)
46 {
47 /* This is the first time the stream has been written
48 into. */
49 obs->block_size = 1024;
50 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
51 obs->first_block = new_block;
52 }
53 else
54 {
55 struct lto_char_ptr_base *tptr;
56 /* Get a new block that is twice as big as the last block
57 and link it into the list. */
58 obs->block_size *= 2;
59 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
60 /* The first bytes of the block are reserved as a pointer to
61 the next block. Set the chain of the full block to the
62 pointer to the new block. */
63 tptr = obs->current_block;
64 tptr->ptr = (char *) new_block;
65 }
66
67 /* Set the place for the next char at the first position after the
68 chain to the next block. */
69 obs->current_pointer
70 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
71 obs->current_block = new_block;
72 /* Null out the newly allocated block's pointer to the next block. */
73 new_block->ptr = NULL;
74 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
75 }
76
77
78 /* Return index used to reference STRING of LEN characters in the string table
79 in OB. The string might or might not include a trailing '\0'.
80 Then put the index onto the INDEX_STREAM.
81 When PERSISTENT is set, the string S is supposed to not change during
82 duration of the OB and thus OB can keep pointer into it. */
83
84 static unsigned
85 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
86 bool persistent)
87 {
88 struct string_slot **slot;
89 struct string_slot s_slot;
90
91 s_slot.s = s;
92 s_slot.len = len;
93 s_slot.slot_num = 0;
94
95 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
96 if (*slot == NULL)
97 {
98 struct lto_output_stream *string_stream = ob->string_stream;
99 unsigned int start = string_stream->total_size;
100 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
101 const char *string;
102
103 if (!persistent)
104 {
105 char *tmp;
106 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
107 memcpy (tmp, s, len);
108 }
109 else
110 string = s;
111
112 new_slot->s = string;
113 new_slot->len = len;
114 new_slot->slot_num = start;
115 *slot = new_slot;
116 streamer_write_uhwi_stream (string_stream, len);
117 streamer_write_data_stream (string_stream, string, len);
118 return start + 1;
119 }
120 else
121 {
122 struct string_slot *old_slot = *slot;
123 return old_slot->slot_num + 1;
124 }
125 }
126
127
128 /* Output STRING of LEN characters to the string table in OB. The
129 string might or might not include a trailing '\0'. Then put the
130 index onto the INDEX_STREAM.
131 When PERSISTENT is set, the string S is supposed to not change during
132 duration of the OB and thus OB can keep pointer into it. */
133
134 void
135 streamer_write_string_with_length (struct output_block *ob,
136 struct lto_output_stream *index_stream,
137 const char *s, unsigned int len,
138 bool persistent)
139 {
140 if (s)
141 streamer_write_uhwi_stream (index_stream,
142 streamer_string_index (ob, s, len, persistent));
143 else
144 streamer_write_char_stream (index_stream, 0);
145 }
146
147
148 /* Output the '\0' terminated STRING to the string
149 table in OB. Then put the index onto the INDEX_STREAM.
150 When PERSISTENT is set, the string S is supposed to not change during
151 duration of the OB and thus OB can keep pointer into it. */
152
153 void
154 streamer_write_string (struct output_block *ob,
155 struct lto_output_stream *index_stream,
156 const char *string, bool persistent)
157 {
158 if (string)
159 streamer_write_string_with_length (ob, index_stream, string,
160 strlen (string) + 1,
161 persistent);
162 else
163 streamer_write_char_stream (index_stream, 0);
164 }
165
166
167 /* Output STRING of LEN characters to the string table in OB. Then
168 put the index into BP.
169 When PERSISTENT is set, the string S is supposed to not change during
170 duration of the OB and thus OB can keep pointer into it. */
171
172 void
173 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
174 const char *s, unsigned int len, bool persistent)
175 {
176 unsigned index = 0;
177 if (s)
178 index = streamer_string_index (ob, s, len, persistent);
179 bp_pack_var_len_unsigned (bp, index);
180 }
181
182
183 /* Output the '\0' terminated STRING to the string
184 table in OB. Then put the index onto the bitpack BP.
185 When PERSISTENT is set, the string S is supposed to not change during
186 duration of the OB and thus OB can keep pointer into it. */
187
188 void
189 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
190 const char *s, bool persistent)
191 {
192 unsigned index = 0;
193 if (s)
194 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
195 bp_pack_var_len_unsigned (bp, index);
196 }
197
198
199
200 /* Write a zero to the output stream. */
201
202 void
203 streamer_write_zero (struct output_block *ob)
204 {
205 streamer_write_char_stream (ob->main_stream, 0);
206 }
207
208
209 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
210
211 void
212 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
213 {
214 streamer_write_uhwi_stream (ob->main_stream, work);
215 }
216
217
218 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
219
220 void
221 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
222 {
223 streamer_write_hwi_stream (ob->main_stream, work);
224 }
225
226 /* Write a gcov counter value WORK to OB->main_stream. */
227
228 void
229 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
230 {
231 streamer_write_gcov_count_stream (ob->main_stream, work);
232 }
233
234 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
235
236 void
237 streamer_write_uhwi_stream (struct lto_output_stream *obs,
238 unsigned HOST_WIDE_INT work)
239 {
240 if (obs->left_in_block == 0)
241 lto_append_block (obs);
242 char *current_pointer = obs->current_pointer;
243 unsigned int left_in_block = obs->left_in_block;
244 unsigned int size = 0;
245 do
246 {
247 unsigned int byte = (work & 0x7f);
248 work >>= 7;
249 if (work != 0)
250 /* More bytes to follow. */
251 byte |= 0x80;
252
253 *(current_pointer++) = byte;
254 left_in_block--;
255 size++;
256 }
257 while (work != 0 && left_in_block > 0);
258 if (work != 0)
259 {
260 obs->left_in_block = 0;
261 lto_append_block (obs);
262 current_pointer = obs->current_pointer;
263 left_in_block = obs->left_in_block;
264 do
265 {
266 unsigned int byte = (work & 0x7f);
267 work >>= 7;
268 if (work != 0)
269 /* More bytes to follow. */
270 byte |= 0x80;
271
272 *(current_pointer++) = byte;
273 left_in_block--;
274 size++;
275 }
276 while (work != 0);
277 }
278 obs->current_pointer = current_pointer;
279 obs->left_in_block = left_in_block;
280 obs->total_size += size;
281 }
282
283
284 /* Write a HOST_WIDE_INT value WORK to OBS. */
285
286 void
287 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
288 {
289 if (obs->left_in_block == 0)
290 lto_append_block (obs);
291 char *current_pointer = obs->current_pointer;
292 unsigned int left_in_block = obs->left_in_block;
293 unsigned int size = 0;
294 bool more;
295 do
296 {
297 unsigned int byte = (work & 0x7f);
298 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
299 work >>= 6;
300 more = !(work == 0 || work == -1);
301 if (more)
302 {
303 /* More bits to follow. */
304 work >>= 1;
305 byte |= 0x80;
306 }
307
308 *(current_pointer++) = byte;
309 left_in_block--;
310 size++;
311 }
312 while (more && left_in_block > 0);
313 if (more)
314 {
315 obs->left_in_block = 0;
316 lto_append_block (obs);
317 current_pointer = obs->current_pointer;
318 left_in_block = obs->left_in_block;
319 do
320 {
321 unsigned int byte = (work & 0x7f);
322 work >>= 6;
323 more = !(work == 0 || work == -1);
324 if (more)
325 {
326 work >>= 1;
327 byte |= 0x80;
328 }
329
330 *(current_pointer++) = byte;
331 left_in_block--;
332 size++;
333 }
334 while (more);
335 }
336 obs->current_pointer = current_pointer;
337 obs->left_in_block = left_in_block;
338 obs->total_size += size;
339 }
340
341 /* Write a GCOV counter value WORK to OBS. */
342
343 void
344 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
345 {
346 gcc_assert (work >= 0);
347 gcc_assert ((HOST_WIDE_INT) work == work);
348 streamer_write_hwi_stream (obs, work);
349 }
350
351 /* Write raw DATA of length LEN to the output block OB. */
352
353 void
354 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
355 size_t len)
356 {
357 while (len)
358 {
359 size_t copy;
360
361 /* No space left. */
362 if (obs->left_in_block == 0)
363 lto_append_block (obs);
364
365 /* Determine how many bytes to copy in this loop. */
366 if (len <= obs->left_in_block)
367 copy = len;
368 else
369 copy = obs->left_in_block;
370
371 /* Copy the data and do bookkeeping. */
372 memcpy (obs->current_pointer, data, copy);
373 obs->current_pointer += copy;
374 obs->total_size += copy;
375 obs->left_in_block -= copy;
376 data = (const char *) data + copy;
377 len -= copy;
378 }
379 }
380