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-2021 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 "data-streamer.h"
33 /* Adds a new block to output stream OBS. */
36 lto_append_block (struct lto_output_stream
*obs
)
38 struct lto_char_ptr_base
*new_block
;
40 gcc_assert (obs
->left_in_block
== 0);
42 if (obs
->first_block
== NULL
)
44 /* This is the first time the stream has been written
46 obs
->block_size
= 1024;
47 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
48 obs
->first_block
= new_block
;
52 struct lto_char_ptr_base
*tptr
;
53 /* Get a new block that is twice as big as the last block
54 and link it into the list. */
56 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
57 /* The first bytes of the block are reserved as a pointer to
58 the next block. Set the chain of the full block to the
59 pointer to the new block. */
60 tptr
= obs
->current_block
;
61 tptr
->ptr
= (char *) new_block
;
64 /* Set the place for the next char at the first position after the
65 chain to the next block. */
67 = ((char *) new_block
) + sizeof (struct lto_char_ptr_base
);
68 obs
->current_block
= new_block
;
69 /* Null out the newly allocated block's pointer to the next block. */
70 new_block
->ptr
= NULL
;
71 obs
->left_in_block
= obs
->block_size
- sizeof (struct lto_char_ptr_base
);
75 /* Return index used to reference STRING of LEN characters in the string table
76 in OB. The string might or might not include a trailing '\0'.
77 Then put the index onto the INDEX_STREAM.
78 When PERSISTENT is set, the string S is supposed to not change during
79 duration of the OB and thus OB can keep pointer into it. */
82 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
85 struct string_slot
**slot
;
86 struct string_slot s_slot
;
92 slot
= ob
->string_hash_table
->find_slot (&s_slot
, INSERT
);
95 struct lto_output_stream
*string_stream
= ob
->string_stream
;
96 unsigned int start
= string_stream
->total_size
;
97 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
103 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
104 memcpy (tmp
, s
, len
);
109 new_slot
->s
= string
;
111 new_slot
->slot_num
= start
;
113 streamer_write_uhwi_stream (string_stream
, len
);
114 streamer_write_data_stream (string_stream
, string
, len
);
119 struct string_slot
*old_slot
= *slot
;
120 return old_slot
->slot_num
+ 1;
125 /* Output STRING of LEN characters to the string table in OB. The
126 string might or might not include a trailing '\0'. Then put the
127 index onto the INDEX_STREAM.
128 When PERSISTENT is set, the string S is supposed to not change during
129 duration of the OB and thus OB can keep pointer into it. */
132 streamer_write_string_with_length (struct output_block
*ob
,
133 struct lto_output_stream
*index_stream
,
134 const char *s
, unsigned int len
,
138 streamer_write_uhwi_stream (index_stream
,
139 streamer_string_index (ob
, s
, len
, persistent
));
141 streamer_write_char_stream (index_stream
, 0);
145 /* Output the '\0' terminated STRING to the string
146 table in OB. Then put the index onto the INDEX_STREAM.
147 When PERSISTENT is set, the string S is supposed to not change during
148 duration of the OB and thus OB can keep pointer into it. */
151 streamer_write_string (struct output_block
*ob
,
152 struct lto_output_stream
*index_stream
,
153 const char *string
, bool persistent
)
156 streamer_write_string_with_length (ob
, index_stream
, string
,
160 streamer_write_char_stream (index_stream
, 0);
164 /* Output STRING of LEN characters to the string table in OB. Then
165 put the index into BP.
166 When PERSISTENT is set, the string S is supposed to not change during
167 duration of the OB and thus OB can keep pointer into it. */
170 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
171 const char *s
, unsigned int len
, bool persistent
)
175 index
= streamer_string_index (ob
, s
, len
, persistent
);
176 bp_pack_var_len_unsigned (bp
, index
);
180 /* Output the '\0' terminated STRING to the string
181 table in OB. Then put the index onto the bitpack BP.
182 When PERSISTENT is set, the string S is supposed to not change during
183 duration of the OB and thus OB can keep pointer into it. */
186 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
187 const char *s
, bool persistent
)
191 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
192 bp_pack_var_len_unsigned (bp
, index
);
197 /* Write a zero to the output stream. */
200 streamer_write_zero (struct output_block
*ob
)
202 streamer_write_char_stream (ob
->main_stream
, 0);
206 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
209 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
211 streamer_write_uhwi_stream (ob
->main_stream
, work
);
215 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
218 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
220 streamer_write_hwi_stream (ob
->main_stream
, work
);
223 /* Write a poly_uint64 value WORK to OB->main_stream. */
226 streamer_write_poly_uint64 (struct output_block
*ob
, poly_uint64 work
)
228 for (int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
229 streamer_write_uhwi_stream (ob
->main_stream
, work
.coeffs
[i
]);
232 /* Write a poly_int64 value WORK to OB->main_stream. */
235 streamer_write_poly_int64 (struct output_block
*ob
, poly_int64 work
)
237 for (int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
238 streamer_write_hwi_stream (ob
->main_stream
, work
.coeffs
[i
]);
241 /* Write a gcov counter value WORK to OB->main_stream. */
244 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
246 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
249 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
252 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
253 unsigned HOST_WIDE_INT work
)
255 if (obs
->left_in_block
== 0)
256 lto_append_block (obs
);
257 char *current_pointer
= obs
->current_pointer
;
258 unsigned int left_in_block
= obs
->left_in_block
;
259 unsigned int size
= 0;
262 unsigned int byte
= (work
& 0x7f);
265 /* More bytes to follow. */
268 *(current_pointer
++) = byte
;
272 while (work
!= 0 && left_in_block
> 0);
275 obs
->left_in_block
= 0;
276 lto_append_block (obs
);
277 current_pointer
= obs
->current_pointer
;
278 left_in_block
= obs
->left_in_block
;
281 unsigned int byte
= (work
& 0x7f);
284 /* More bytes to follow. */
287 *(current_pointer
++) = byte
;
293 obs
->current_pointer
= current_pointer
;
294 obs
->left_in_block
= left_in_block
;
295 obs
->total_size
+= size
;
299 /* Write a HOST_WIDE_INT value WORK to OBS. */
302 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
304 if (obs
->left_in_block
== 0)
305 lto_append_block (obs
);
306 char *current_pointer
= obs
->current_pointer
;
307 unsigned int left_in_block
= obs
->left_in_block
;
308 unsigned int size
= 0;
312 unsigned int byte
= (work
& 0x7f);
313 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
315 more
= !(work
== 0 || work
== -1);
318 /* More bits to follow. */
323 *(current_pointer
++) = byte
;
327 while (more
&& left_in_block
> 0);
330 obs
->left_in_block
= 0;
331 lto_append_block (obs
);
332 current_pointer
= obs
->current_pointer
;
333 left_in_block
= obs
->left_in_block
;
336 unsigned int byte
= (work
& 0x7f);
338 more
= !(work
== 0 || work
== -1);
345 *(current_pointer
++) = byte
;
351 obs
->current_pointer
= current_pointer
;
352 obs
->left_in_block
= left_in_block
;
353 obs
->total_size
+= size
;
356 /* Write a GCOV counter value WORK to OBS. */
359 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
361 gcc_assert ((HOST_WIDE_INT
) work
== work
);
362 streamer_write_hwi_stream (obs
, work
);
365 /* Write raw DATA of length LEN to the output block OB. */
368 streamer_write_data_stream (struct lto_output_stream
*obs
, const void *data
,
376 if (obs
->left_in_block
== 0)
377 lto_append_block (obs
);
379 /* Determine how many bytes to copy in this loop. */
380 if (len
<= obs
->left_in_block
)
383 copy
= obs
->left_in_block
;
385 /* Copy the data and do bookkeeping. */
386 memcpy (obs
->current_pointer
, data
, copy
);
387 obs
->current_pointer
+= copy
;
388 obs
->total_size
+= copy
;
389 obs
->left_in_block
-= copy
;
390 data
= (const char *) data
+ copy
;
395 /* Emit the physical representation of wide_int VAL to output block OB. */
398 streamer_write_wide_int (struct output_block
*ob
, const wide_int
&val
)
400 int len
= val
.get_len ();
402 streamer_write_uhwi (ob
, val
.get_precision ());
403 streamer_write_uhwi (ob
, len
);
404 for (int i
= 0; i
< len
; i
++)
405 streamer_write_hwi (ob
, val
.elt (i
));
408 /* Emit the physical representation of widest_int W to output block OB. */
411 streamer_write_widest_int (struct output_block
*ob
,
414 int len
= w
.get_len ();
416 streamer_write_uhwi (ob
, w
.get_precision ());
417 streamer_write_uhwi (ob
, len
);
418 for (int i
= 0; i
< len
; i
++)
419 streamer_write_hwi (ob
, w
.elt (i
));