2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / data-streamer-out.c
blobfdc36628aa2a90517277425eefd592b2c5e8ca5b
1 /* Routines for saving various data types to 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 "alias.h"
27 #include "backend.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "hard-reg-set.h"
31 #include "options.h"
32 #include "fold-const.h"
33 #include "internal-fn.h"
34 #include "cgraph.h"
35 #include "data-streamer.h"
38 /* Adds a new block to output stream OBS. */
40 void
41 lto_append_block (struct lto_output_stream *obs)
43 struct lto_char_ptr_base *new_block;
45 gcc_assert (obs->left_in_block == 0);
47 if (obs->first_block == NULL)
49 /* This is the first time the stream has been written
50 into. */
51 obs->block_size = 1024;
52 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
53 obs->first_block = new_block;
55 else
57 struct lto_char_ptr_base *tptr;
58 /* Get a new block that is twice as big as the last block
59 and link it into the list. */
60 obs->block_size *= 2;
61 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
62 /* The first bytes of the block are reserved as a pointer to
63 the next block. Set the chain of the full block to the
64 pointer to the new block. */
65 tptr = obs->current_block;
66 tptr->ptr = (char *) new_block;
69 /* Set the place for the next char at the first position after the
70 chain to the next block. */
71 obs->current_pointer
72 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
73 obs->current_block = new_block;
74 /* Null out the newly allocated block's pointer to the next block. */
75 new_block->ptr = NULL;
76 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
80 /* Return index used to reference STRING of LEN characters in the string table
81 in OB. The string might or might not include a trailing '\0'.
82 Then put the index onto the INDEX_STREAM.
83 When PERSISTENT is set, the string S is supposed to not change during
84 duration of the OB and thus OB can keep pointer into it. */
86 static unsigned
87 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
88 bool persistent)
90 struct string_slot **slot;
91 struct string_slot s_slot;
93 s_slot.s = s;
94 s_slot.len = len;
95 s_slot.slot_num = 0;
97 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
98 if (*slot == NULL)
100 struct lto_output_stream *string_stream = ob->string_stream;
101 unsigned int start = string_stream->total_size;
102 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
103 const char *string;
105 if (!persistent)
107 char *tmp;
108 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
109 memcpy (tmp, s, len);
111 else
112 string = s;
114 new_slot->s = string;
115 new_slot->len = len;
116 new_slot->slot_num = start;
117 *slot = new_slot;
118 streamer_write_uhwi_stream (string_stream, len);
119 streamer_write_data_stream (string_stream, string, len);
120 return start + 1;
122 else
124 struct string_slot *old_slot = *slot;
125 return old_slot->slot_num + 1;
130 /* Output STRING of LEN characters to the string table in OB. The
131 string might or might not include a trailing '\0'. Then put the
132 index onto the INDEX_STREAM.
133 When PERSISTENT is set, the string S is supposed to not change during
134 duration of the OB and thus OB can keep pointer into it. */
136 void
137 streamer_write_string_with_length (struct output_block *ob,
138 struct lto_output_stream *index_stream,
139 const char *s, unsigned int len,
140 bool persistent)
142 if (s)
143 streamer_write_uhwi_stream (index_stream,
144 streamer_string_index (ob, s, len, persistent));
145 else
146 streamer_write_char_stream (index_stream, 0);
150 /* Output the '\0' terminated STRING to the string
151 table in OB. Then put the index onto the INDEX_STREAM.
152 When PERSISTENT is set, the string S is supposed to not change during
153 duration of the OB and thus OB can keep pointer into it. */
155 void
156 streamer_write_string (struct output_block *ob,
157 struct lto_output_stream *index_stream,
158 const char *string, bool persistent)
160 if (string)
161 streamer_write_string_with_length (ob, index_stream, string,
162 strlen (string) + 1,
163 persistent);
164 else
165 streamer_write_char_stream (index_stream, 0);
169 /* Output STRING of LEN characters to the string table in OB. Then
170 put the index into BP.
171 When PERSISTENT is set, the string S is supposed to not change during
172 duration of the OB and thus OB can keep pointer into it. */
174 void
175 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
176 const char *s, unsigned int len, bool persistent)
178 unsigned index = 0;
179 if (s)
180 index = streamer_string_index (ob, s, len, persistent);
181 bp_pack_var_len_unsigned (bp, index);
185 /* Output the '\0' terminated STRING to the string
186 table in OB. Then put the index onto the bitpack BP.
187 When PERSISTENT is set, the string S is supposed to not change during
188 duration of the OB and thus OB can keep pointer into it. */
190 void
191 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
192 const char *s, bool persistent)
194 unsigned index = 0;
195 if (s)
196 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
197 bp_pack_var_len_unsigned (bp, index);
202 /* Write a zero to the output stream. */
204 void
205 streamer_write_zero (struct output_block *ob)
207 streamer_write_char_stream (ob->main_stream, 0);
211 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
213 void
214 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
216 streamer_write_uhwi_stream (ob->main_stream, work);
220 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
222 void
223 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
225 streamer_write_hwi_stream (ob->main_stream, work);
228 /* Write a gcov counter value WORK to OB->main_stream. */
230 void
231 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
233 streamer_write_gcov_count_stream (ob->main_stream, work);
236 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
238 void
239 streamer_write_uhwi_stream (struct lto_output_stream *obs,
240 unsigned HOST_WIDE_INT work)
242 if (obs->left_in_block == 0)
243 lto_append_block (obs);
244 char *current_pointer = obs->current_pointer;
245 unsigned int left_in_block = obs->left_in_block;
246 unsigned int size = 0;
249 unsigned int byte = (work & 0x7f);
250 work >>= 7;
251 if (work != 0)
252 /* More bytes to follow. */
253 byte |= 0x80;
255 *(current_pointer++) = byte;
256 left_in_block--;
257 size++;
259 while (work != 0 && left_in_block > 0);
260 if (work != 0)
262 obs->left_in_block = 0;
263 lto_append_block (obs);
264 current_pointer = obs->current_pointer;
265 left_in_block = obs->left_in_block;
268 unsigned int byte = (work & 0x7f);
269 work >>= 7;
270 if (work != 0)
271 /* More bytes to follow. */
272 byte |= 0x80;
274 *(current_pointer++) = byte;
275 left_in_block--;
276 size++;
278 while (work != 0);
280 obs->current_pointer = current_pointer;
281 obs->left_in_block = left_in_block;
282 obs->total_size += size;
286 /* Write a HOST_WIDE_INT value WORK to OBS. */
288 void
289 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
291 if (obs->left_in_block == 0)
292 lto_append_block (obs);
293 char *current_pointer = obs->current_pointer;
294 unsigned int left_in_block = obs->left_in_block;
295 unsigned int size = 0;
296 bool more;
299 unsigned int byte = (work & 0x7f);
300 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
301 work >>= 6;
302 more = !(work == 0 || work == -1);
303 if (more)
305 /* More bits to follow. */
306 work >>= 1;
307 byte |= 0x80;
310 *(current_pointer++) = byte;
311 left_in_block--;
312 size++;
314 while (more && left_in_block > 0);
315 if (more)
317 obs->left_in_block = 0;
318 lto_append_block (obs);
319 current_pointer = obs->current_pointer;
320 left_in_block = obs->left_in_block;
323 unsigned int byte = (work & 0x7f);
324 work >>= 6;
325 more = !(work == 0 || work == -1);
326 if (more)
328 work >>= 1;
329 byte |= 0x80;
332 *(current_pointer++) = byte;
333 left_in_block--;
334 size++;
336 while (more);
338 obs->current_pointer = current_pointer;
339 obs->left_in_block = left_in_block;
340 obs->total_size += size;
343 /* Write a GCOV counter value WORK to OBS. */
345 void
346 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
348 gcc_assert (work >= 0);
349 gcc_assert ((HOST_WIDE_INT) work == work);
350 streamer_write_hwi_stream (obs, work);
353 /* Write raw DATA of length LEN to the output block OB. */
355 void
356 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
357 size_t len)
359 while (len)
361 size_t copy;
363 /* No space left. */
364 if (obs->left_in_block == 0)
365 lto_append_block (obs);
367 /* Determine how many bytes to copy in this loop. */
368 if (len <= obs->left_in_block)
369 copy = len;
370 else
371 copy = obs->left_in_block;
373 /* Copy the data and do bookkeeping. */
374 memcpy (obs->current_pointer, data, copy);
375 obs->current_pointer += copy;
376 obs->total_size += copy;
377 obs->left_in_block -= copy;
378 data = (const char *) data + copy;
379 len -= copy;