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"
29 #include "double-int.h"
37 #include "fold-const.h"
40 #include "hard-reg-set.h"
43 #include "basic-block.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "gimple-expr.h"
50 #include "plugin-api.h"
53 #include "data-streamer.h"
56 /* Adds a new block to output stream OBS. */
59 lto_append_block (struct lto_output_stream
*obs
)
61 struct lto_char_ptr_base
*new_block
;
63 gcc_assert (obs
->left_in_block
== 0);
65 if (obs
->first_block
== NULL
)
67 /* This is the first time the stream has been written
69 obs
->block_size
= 1024;
70 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
71 obs
->first_block
= new_block
;
75 struct lto_char_ptr_base
*tptr
;
76 /* Get a new block that is twice as big as the last block
77 and link it into the list. */
79 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
80 /* The first bytes of the block are reserved as a pointer to
81 the next block. Set the chain of the full block to the
82 pointer to the new block. */
83 tptr
= obs
->current_block
;
84 tptr
->ptr
= (char *) new_block
;
87 /* Set the place for the next char at the first position after the
88 chain to the next block. */
90 = ((char *) new_block
) + sizeof (struct lto_char_ptr_base
);
91 obs
->current_block
= new_block
;
92 /* Null out the newly allocated block's pointer to the next block. */
93 new_block
->ptr
= NULL
;
94 obs
->left_in_block
= obs
->block_size
- sizeof (struct lto_char_ptr_base
);
98 /* Return index used to reference STRING of LEN characters in the string table
99 in OB. The string might or might not include a trailing '\0'.
100 Then put the index onto the INDEX_STREAM.
101 When PERSISTENT is set, the string S is supposed to not change during
102 duration of the OB and thus OB can keep pointer into it. */
105 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
108 struct string_slot
**slot
;
109 struct string_slot s_slot
;
115 slot
= ob
->string_hash_table
->find_slot (&s_slot
, INSERT
);
118 struct lto_output_stream
*string_stream
= ob
->string_stream
;
119 unsigned int start
= string_stream
->total_size
;
120 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
126 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
127 memcpy (tmp
, s
, len
);
132 new_slot
->s
= string
;
134 new_slot
->slot_num
= start
;
136 streamer_write_uhwi_stream (string_stream
, len
);
137 streamer_write_data_stream (string_stream
, string
, len
);
142 struct string_slot
*old_slot
= *slot
;
143 return old_slot
->slot_num
+ 1;
148 /* Output STRING of LEN characters to the string table in OB. The
149 string might or might not include a trailing '\0'. Then put the
150 index onto the INDEX_STREAM.
151 When PERSISTENT is set, the string S is supposed to not change during
152 duration of the OB and thus OB can keep pointer into it. */
155 streamer_write_string_with_length (struct output_block
*ob
,
156 struct lto_output_stream
*index_stream
,
157 const char *s
, unsigned int len
,
161 streamer_write_uhwi_stream (index_stream
,
162 streamer_string_index (ob
, s
, len
, persistent
));
164 streamer_write_char_stream (index_stream
, 0);
168 /* Output the '\0' terminated STRING to the string
169 table in OB. Then put the index onto the INDEX_STREAM.
170 When PERSISTENT is set, the string S is supposed to not change during
171 duration of the OB and thus OB can keep pointer into it. */
174 streamer_write_string (struct output_block
*ob
,
175 struct lto_output_stream
*index_stream
,
176 const char *string
, bool persistent
)
179 streamer_write_string_with_length (ob
, index_stream
, string
,
183 streamer_write_char_stream (index_stream
, 0);
187 /* Output STRING of LEN characters to the string table in OB. Then
188 put the index into BP.
189 When PERSISTENT is set, the string S is supposed to not change during
190 duration of the OB and thus OB can keep pointer into it. */
193 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
194 const char *s
, unsigned int len
, bool persistent
)
198 index
= streamer_string_index (ob
, s
, len
, persistent
);
199 bp_pack_var_len_unsigned (bp
, index
);
203 /* Output the '\0' terminated STRING to the string
204 table in OB. Then put the index onto the bitpack BP.
205 When PERSISTENT is set, the string S is supposed to not change during
206 duration of the OB and thus OB can keep pointer into it. */
209 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
210 const char *s
, bool persistent
)
214 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
215 bp_pack_var_len_unsigned (bp
, index
);
220 /* Write a zero to the output stream. */
223 streamer_write_zero (struct output_block
*ob
)
225 streamer_write_char_stream (ob
->main_stream
, 0);
229 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
232 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
234 streamer_write_uhwi_stream (ob
->main_stream
, work
);
238 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
241 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
243 streamer_write_hwi_stream (ob
->main_stream
, work
);
246 /* Write a gcov counter value WORK to OB->main_stream. */
249 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
251 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
254 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
257 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
258 unsigned HOST_WIDE_INT work
)
260 if (obs
->left_in_block
== 0)
261 lto_append_block (obs
);
262 char *current_pointer
= obs
->current_pointer
;
263 unsigned int left_in_block
= obs
->left_in_block
;
264 unsigned int size
= 0;
267 unsigned int byte
= (work
& 0x7f);
270 /* More bytes to follow. */
273 *(current_pointer
++) = byte
;
277 while (work
!= 0 && left_in_block
> 0);
280 obs
->left_in_block
= 0;
281 lto_append_block (obs
);
282 current_pointer
= obs
->current_pointer
;
283 left_in_block
= obs
->left_in_block
;
286 unsigned int byte
= (work
& 0x7f);
289 /* More bytes to follow. */
292 *(current_pointer
++) = byte
;
298 obs
->current_pointer
= current_pointer
;
299 obs
->left_in_block
= left_in_block
;
300 obs
->total_size
+= size
;
304 /* Write a HOST_WIDE_INT value WORK to OBS. */
307 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
309 if (obs
->left_in_block
== 0)
310 lto_append_block (obs
);
311 char *current_pointer
= obs
->current_pointer
;
312 unsigned int left_in_block
= obs
->left_in_block
;
313 unsigned int size
= 0;
317 unsigned int byte
= (work
& 0x7f);
318 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
320 more
= !(work
== 0 || work
== -1);
323 /* More bits to follow. */
328 *(current_pointer
++) = byte
;
332 while (more
&& left_in_block
> 0);
335 obs
->left_in_block
= 0;
336 lto_append_block (obs
);
337 current_pointer
= obs
->current_pointer
;
338 left_in_block
= obs
->left_in_block
;
341 unsigned int byte
= (work
& 0x7f);
343 more
= !(work
== 0 || work
== -1);
350 *(current_pointer
++) = byte
;
356 obs
->current_pointer
= current_pointer
;
357 obs
->left_in_block
= left_in_block
;
358 obs
->total_size
+= size
;
361 /* Write a GCOV counter value WORK to OBS. */
364 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
366 gcc_assert (work
>= 0);
367 gcc_assert ((HOST_WIDE_INT
) work
== work
);
368 streamer_write_hwi_stream (obs
, work
);
371 /* Write raw DATA of length LEN to the output block OB. */
374 streamer_write_data_stream (struct lto_output_stream
*obs
, const void *data
,
382 if (obs
->left_in_block
== 0)
383 lto_append_block (obs
);
385 /* Determine how many bytes to copy in this loop. */
386 if (len
<= obs
->left_in_block
)
389 copy
= obs
->left_in_block
;
391 /* Copy the data and do bookkeeping. */
392 memcpy (obs
->current_pointer
, data
, copy
);
393 obs
->current_pointer
+= copy
;
394 obs
->total_size
+= copy
;
395 obs
->left_in_block
-= copy
;
396 data
= (const char *) data
+ copy
;