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 "fold-const.h"
33 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
41 #include "data-streamer.h"
44 /* Adds a new block to output stream OBS. */
47 lto_append_block (struct lto_output_stream
*obs
)
49 struct lto_char_ptr_base
*new_block
;
51 gcc_assert (obs
->left_in_block
== 0);
53 if (obs
->first_block
== NULL
)
55 /* This is the first time the stream has been written
57 obs
->block_size
= 1024;
58 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
59 obs
->first_block
= new_block
;
63 struct lto_char_ptr_base
*tptr
;
64 /* Get a new block that is twice as big as the last block
65 and link it into the list. */
67 new_block
= (struct lto_char_ptr_base
*) xmalloc (obs
->block_size
);
68 /* The first bytes of the block are reserved as a pointer to
69 the next block. Set the chain of the full block to the
70 pointer to the new block. */
71 tptr
= obs
->current_block
;
72 tptr
->ptr
= (char *) new_block
;
75 /* Set the place for the next char at the first position after the
76 chain to the next block. */
78 = ((char *) new_block
) + sizeof (struct lto_char_ptr_base
);
79 obs
->current_block
= new_block
;
80 /* Null out the newly allocated block's pointer to the next block. */
81 new_block
->ptr
= NULL
;
82 obs
->left_in_block
= obs
->block_size
- sizeof (struct lto_char_ptr_base
);
86 /* Return index used to reference STRING of LEN characters in the string table
87 in OB. The string might or might not include a trailing '\0'.
88 Then put the index onto the INDEX_STREAM.
89 When PERSISTENT is set, the string S is supposed to not change during
90 duration of the OB and thus OB can keep pointer into it. */
93 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
96 struct string_slot
**slot
;
97 struct string_slot s_slot
;
103 slot
= ob
->string_hash_table
->find_slot (&s_slot
, INSERT
);
106 struct lto_output_stream
*string_stream
= ob
->string_stream
;
107 unsigned int start
= string_stream
->total_size
;
108 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
114 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
115 memcpy (tmp
, s
, len
);
120 new_slot
->s
= string
;
122 new_slot
->slot_num
= start
;
124 streamer_write_uhwi_stream (string_stream
, len
);
125 streamer_write_data_stream (string_stream
, string
, len
);
130 struct string_slot
*old_slot
= *slot
;
131 return old_slot
->slot_num
+ 1;
136 /* Output STRING of LEN characters to the string table in OB. The
137 string might or might not include a trailing '\0'. Then put the
138 index onto the INDEX_STREAM.
139 When PERSISTENT is set, the string S is supposed to not change during
140 duration of the OB and thus OB can keep pointer into it. */
143 streamer_write_string_with_length (struct output_block
*ob
,
144 struct lto_output_stream
*index_stream
,
145 const char *s
, unsigned int len
,
149 streamer_write_uhwi_stream (index_stream
,
150 streamer_string_index (ob
, s
, len
, persistent
));
152 streamer_write_char_stream (index_stream
, 0);
156 /* Output the '\0' terminated STRING to the string
157 table in OB. Then put the index onto the INDEX_STREAM.
158 When PERSISTENT is set, the string S is supposed to not change during
159 duration of the OB and thus OB can keep pointer into it. */
162 streamer_write_string (struct output_block
*ob
,
163 struct lto_output_stream
*index_stream
,
164 const char *string
, bool persistent
)
167 streamer_write_string_with_length (ob
, index_stream
, string
,
171 streamer_write_char_stream (index_stream
, 0);
175 /* Output STRING of LEN characters to the string table in OB. Then
176 put the index into BP.
177 When PERSISTENT is set, the string S is supposed to not change during
178 duration of the OB and thus OB can keep pointer into it. */
181 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
182 const char *s
, unsigned int len
, bool persistent
)
186 index
= streamer_string_index (ob
, s
, len
, persistent
);
187 bp_pack_var_len_unsigned (bp
, index
);
191 /* Output the '\0' terminated STRING to the string
192 table in OB. Then put the index onto the bitpack BP.
193 When PERSISTENT is set, the string S is supposed to not change during
194 duration of the OB and thus OB can keep pointer into it. */
197 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
198 const char *s
, bool persistent
)
202 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
203 bp_pack_var_len_unsigned (bp
, index
);
208 /* Write a zero to the output stream. */
211 streamer_write_zero (struct output_block
*ob
)
213 streamer_write_char_stream (ob
->main_stream
, 0);
217 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
220 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
222 streamer_write_uhwi_stream (ob
->main_stream
, work
);
226 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
229 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
231 streamer_write_hwi_stream (ob
->main_stream
, work
);
234 /* Write a gcov counter value WORK to OB->main_stream. */
237 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
239 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
242 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
245 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
246 unsigned HOST_WIDE_INT work
)
248 if (obs
->left_in_block
== 0)
249 lto_append_block (obs
);
250 char *current_pointer
= obs
->current_pointer
;
251 unsigned int left_in_block
= obs
->left_in_block
;
252 unsigned int size
= 0;
255 unsigned int byte
= (work
& 0x7f);
258 /* More bytes to follow. */
261 *(current_pointer
++) = byte
;
265 while (work
!= 0 && left_in_block
> 0);
268 obs
->left_in_block
= 0;
269 lto_append_block (obs
);
270 current_pointer
= obs
->current_pointer
;
271 left_in_block
= obs
->left_in_block
;
274 unsigned int byte
= (work
& 0x7f);
277 /* More bytes to follow. */
280 *(current_pointer
++) = byte
;
286 obs
->current_pointer
= current_pointer
;
287 obs
->left_in_block
= left_in_block
;
288 obs
->total_size
+= size
;
292 /* Write a HOST_WIDE_INT value WORK to OBS. */
295 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
297 if (obs
->left_in_block
== 0)
298 lto_append_block (obs
);
299 char *current_pointer
= obs
->current_pointer
;
300 unsigned int left_in_block
= obs
->left_in_block
;
301 unsigned int size
= 0;
305 unsigned int byte
= (work
& 0x7f);
306 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
308 more
= !(work
== 0 || work
== -1);
311 /* More bits to follow. */
316 *(current_pointer
++) = byte
;
320 while (more
&& left_in_block
> 0);
323 obs
->left_in_block
= 0;
324 lto_append_block (obs
);
325 current_pointer
= obs
->current_pointer
;
326 left_in_block
= obs
->left_in_block
;
329 unsigned int byte
= (work
& 0x7f);
331 more
= !(work
== 0 || work
== -1);
338 *(current_pointer
++) = byte
;
344 obs
->current_pointer
= current_pointer
;
345 obs
->left_in_block
= left_in_block
;
346 obs
->total_size
+= size
;
349 /* Write a GCOV counter value WORK to OBS. */
352 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
354 gcc_assert (work
>= 0);
355 gcc_assert ((HOST_WIDE_INT
) work
== work
);
356 streamer_write_hwi_stream (obs
, work
);
359 /* Write raw DATA of length LEN to the output block OB. */
362 streamer_write_data_stream (struct lto_output_stream
*obs
, const void *data
,
370 if (obs
->left_in_block
== 0)
371 lto_append_block (obs
);
373 /* Determine how many bytes to copy in this loop. */
374 if (len
<= obs
->left_in_block
)
377 copy
= obs
->left_in_block
;
379 /* Copy the data and do bookkeeping. */
380 memcpy (obs
->current_pointer
, data
, copy
);
381 obs
->current_pointer
+= copy
;
382 obs
->total_size
+= copy
;
383 obs
->left_in_block
-= copy
;
384 data
= (const char *) data
+ copy
;