cp/
[official-gcc.git] / gcc / data-streamer-out.c
blob5d7a3b171102cf28e1eff05bd9ce5f9aa8ac61b6
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 "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
39 #include "gimple.h"
40 #include "plugin-api.h"
41 #include "ipa-ref.h"
42 #include "cgraph.h"
43 #include "data-streamer.h"
46 /* Adds a new block to output stream OBS. */
48 void
49 lto_append_block (struct lto_output_stream *obs)
51 struct lto_char_ptr_base *new_block;
53 gcc_assert (obs->left_in_block == 0);
55 if (obs->first_block == NULL)
57 /* This is the first time the stream has been written
58 into. */
59 obs->block_size = 1024;
60 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
61 obs->first_block = new_block;
63 else
65 struct lto_char_ptr_base *tptr;
66 /* Get a new block that is twice as big as the last block
67 and link it into the list. */
68 obs->block_size *= 2;
69 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
70 /* The first bytes of the block are reserved as a pointer to
71 the next block. Set the chain of the full block to the
72 pointer to the new block. */
73 tptr = obs->current_block;
74 tptr->ptr = (char *) new_block;
77 /* Set the place for the next char at the first position after the
78 chain to the next block. */
79 obs->current_pointer
80 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
81 obs->current_block = new_block;
82 /* Null out the newly allocated block's pointer to the next block. */
83 new_block->ptr = NULL;
84 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
88 /* Return index used to reference STRING of LEN characters in the string table
89 in OB. The string might or might not include a trailing '\0'.
90 Then put the index onto the INDEX_STREAM.
91 When PERSISTENT is set, the string S is supposed to not change during
92 duration of the OB and thus OB can keep pointer into it. */
94 static unsigned
95 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
96 bool persistent)
98 struct string_slot **slot;
99 struct string_slot s_slot;
101 s_slot.s = s;
102 s_slot.len = len;
103 s_slot.slot_num = 0;
105 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
106 if (*slot == NULL)
108 struct lto_output_stream *string_stream = ob->string_stream;
109 unsigned int start = string_stream->total_size;
110 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
111 const char *string;
113 if (!persistent)
115 char *tmp;
116 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
117 memcpy (tmp, s, len);
119 else
120 string = s;
122 new_slot->s = string;
123 new_slot->len = len;
124 new_slot->slot_num = start;
125 *slot = new_slot;
126 streamer_write_uhwi_stream (string_stream, len);
127 streamer_write_data_stream (string_stream, string, len);
128 return start + 1;
130 else
132 struct string_slot *old_slot = *slot;
133 return old_slot->slot_num + 1;
138 /* Output STRING of LEN characters to the string table in OB. The
139 string might or might not include a trailing '\0'. Then put the
140 index onto the INDEX_STREAM.
141 When PERSISTENT is set, the string S is supposed to not change during
142 duration of the OB and thus OB can keep pointer into it. */
144 void
145 streamer_write_string_with_length (struct output_block *ob,
146 struct lto_output_stream *index_stream,
147 const char *s, unsigned int len,
148 bool persistent)
150 if (s)
151 streamer_write_uhwi_stream (index_stream,
152 streamer_string_index (ob, s, len, persistent));
153 else
154 streamer_write_char_stream (index_stream, 0);
158 /* Output the '\0' terminated STRING to the string
159 table in OB. Then put the index onto the INDEX_STREAM.
160 When PERSISTENT is set, the string S is supposed to not change during
161 duration of the OB and thus OB can keep pointer into it. */
163 void
164 streamer_write_string (struct output_block *ob,
165 struct lto_output_stream *index_stream,
166 const char *string, bool persistent)
168 if (string)
169 streamer_write_string_with_length (ob, index_stream, string,
170 strlen (string) + 1,
171 persistent);
172 else
173 streamer_write_char_stream (index_stream, 0);
177 /* Output STRING of LEN characters to the string table in OB. Then
178 put the index into BP.
179 When PERSISTENT is set, the string S is supposed to not change during
180 duration of the OB and thus OB can keep pointer into it. */
182 void
183 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
184 const char *s, unsigned int len, bool persistent)
186 unsigned index = 0;
187 if (s)
188 index = streamer_string_index (ob, s, len, persistent);
189 bp_pack_var_len_unsigned (bp, index);
193 /* Output the '\0' terminated STRING to the string
194 table in OB. Then put the index onto the bitpack BP.
195 When PERSISTENT is set, the string S is supposed to not change during
196 duration of the OB and thus OB can keep pointer into it. */
198 void
199 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
200 const char *s, bool persistent)
202 unsigned index = 0;
203 if (s)
204 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
205 bp_pack_var_len_unsigned (bp, index);
210 /* Write a zero to the output stream. */
212 void
213 streamer_write_zero (struct output_block *ob)
215 streamer_write_char_stream (ob->main_stream, 0);
219 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
221 void
222 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
224 streamer_write_uhwi_stream (ob->main_stream, work);
228 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
230 void
231 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
233 streamer_write_hwi_stream (ob->main_stream, work);
236 /* Write a gcov counter value WORK to OB->main_stream. */
238 void
239 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
241 streamer_write_gcov_count_stream (ob->main_stream, work);
244 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
246 void
247 streamer_write_uhwi_stream (struct lto_output_stream *obs,
248 unsigned HOST_WIDE_INT work)
250 if (obs->left_in_block == 0)
251 lto_append_block (obs);
252 char *current_pointer = obs->current_pointer;
253 unsigned int left_in_block = obs->left_in_block;
254 unsigned int size = 0;
257 unsigned int byte = (work & 0x7f);
258 work >>= 7;
259 if (work != 0)
260 /* More bytes to follow. */
261 byte |= 0x80;
263 *(current_pointer++) = byte;
264 left_in_block--;
265 size++;
267 while (work != 0 && left_in_block > 0);
268 if (work != 0)
270 obs->left_in_block = 0;
271 lto_append_block (obs);
272 current_pointer = obs->current_pointer;
273 left_in_block = obs->left_in_block;
276 unsigned int byte = (work & 0x7f);
277 work >>= 7;
278 if (work != 0)
279 /* More bytes to follow. */
280 byte |= 0x80;
282 *(current_pointer++) = byte;
283 left_in_block--;
284 size++;
286 while (work != 0);
288 obs->current_pointer = current_pointer;
289 obs->left_in_block = left_in_block;
290 obs->total_size += size;
294 /* Write a HOST_WIDE_INT value WORK to OBS. */
296 void
297 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
299 if (obs->left_in_block == 0)
300 lto_append_block (obs);
301 char *current_pointer = obs->current_pointer;
302 unsigned int left_in_block = obs->left_in_block;
303 unsigned int size = 0;
304 bool more;
307 unsigned int byte = (work & 0x7f);
308 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
309 work >>= 6;
310 more = !(work == 0 || work == -1);
311 if (more)
313 /* More bits to follow. */
314 work >>= 1;
315 byte |= 0x80;
318 *(current_pointer++) = byte;
319 left_in_block--;
320 size++;
322 while (more && left_in_block > 0);
323 if (more)
325 obs->left_in_block = 0;
326 lto_append_block (obs);
327 current_pointer = obs->current_pointer;
328 left_in_block = obs->left_in_block;
331 unsigned int byte = (work & 0x7f);
332 work >>= 6;
333 more = !(work == 0 || work == -1);
334 if (more)
336 work >>= 1;
337 byte |= 0x80;
340 *(current_pointer++) = byte;
341 left_in_block--;
342 size++;
344 while (more);
346 obs->current_pointer = current_pointer;
347 obs->left_in_block = left_in_block;
348 obs->total_size += size;
351 /* Write a GCOV counter value WORK to OBS. */
353 void
354 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
356 gcc_assert (work >= 0);
357 gcc_assert ((HOST_WIDE_INT) work == work);
358 streamer_write_hwi_stream (obs, work);
361 /* Write raw DATA of length LEN to the output block OB. */
363 void
364 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
365 size_t len)
367 while (len)
369 size_t copy;
371 /* No space left. */
372 if (obs->left_in_block == 0)
373 lto_append_block (obs);
375 /* Determine how many bytes to copy in this loop. */
376 if (len <= obs->left_in_block)
377 copy = len;
378 else
379 copy = obs->left_in_block;
381 /* Copy the data and do bookkeeping. */
382 memcpy (obs->current_pointer, data, copy);
383 obs->current_pointer += copy;
384 obs->total_size += copy;
385 obs->left_in_block -= copy;
386 data = (const char *) data + copy;
387 len -= copy;