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
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
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/>. */
25 #include "coretypes.h"
30 #include "hard-reg-set.h"
32 #include "fold-const.h"
33 #include "internal-fn.h"
35 #include "data-streamer.h"
38 /* Adds a new block to output stream OBS. */
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
51 obs
->block_size
= 1024;
52 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
53 obs
->first_block
= new_block
;
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. */
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. */
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. */
87 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
90 struct string_slot
**slot
;
91 struct string_slot s_slot
;
97 slot
= ob
->string_hash_table
->find_slot (&s_slot
, INSERT
);
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
);
108 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
109 memcpy (tmp
, s
, len
);
114 new_slot
->s
= string
;
116 new_slot
->slot_num
= start
;
118 streamer_write_uhwi_stream (string_stream
, len
);
119 streamer_write_data_stream (string_stream
, string
, len
);
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. */
137 streamer_write_string_with_length (struct output_block
*ob
,
138 struct lto_output_stream
*index_stream
,
139 const char *s
, unsigned int len
,
143 streamer_write_uhwi_stream (index_stream
,
144 streamer_string_index (ob
, s
, len
, persistent
));
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. */
156 streamer_write_string (struct output_block
*ob
,
157 struct lto_output_stream
*index_stream
,
158 const char *string
, bool persistent
)
161 streamer_write_string_with_length (ob
, index_stream
, string
,
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. */
175 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
176 const char *s
, unsigned int len
, bool persistent
)
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. */
191 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
192 const char *s
, bool persistent
)
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. */
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. */
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. */
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. */
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. */
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);
252 /* More bytes to follow. */
255 *(current_pointer
++) = byte
;
259 while (work
!= 0 && left_in_block
> 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);
271 /* More bytes to follow. */
274 *(current_pointer
++) = byte
;
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. */
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;
299 unsigned int byte
= (work
& 0x7f);
300 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
302 more
= !(work
== 0 || work
== -1);
305 /* More bits to follow. */
310 *(current_pointer
++) = byte
;
314 while (more
&& left_in_block
> 0);
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);
325 more
= !(work
== 0 || work
== -1);
332 *(current_pointer
++) = byte
;
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. */
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. */
356 streamer_write_data_stream (struct lto_output_stream
*obs
, const void *data
,
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
)
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
;