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-2014 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"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-expr.h"
33 #include "data-streamer.h"
35 /* Return index used to reference STRING of LEN characters in the string table
36 in OB. The string might or might not include a trailing '\0'.
37 Then put the index onto the INDEX_STREAM.
38 When PERSISTENT is set, the string S is supposed to not change during
39 duration of the OB and thus OB can keep pointer into it. */
42 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
45 struct string_slot
**slot
;
46 struct string_slot s_slot
;
52 slot
= ob
->string_hash_table
->find_slot (&s_slot
, INSERT
);
55 struct lto_output_stream
*string_stream
= ob
->string_stream
;
56 unsigned int start
= string_stream
->total_size
;
57 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
63 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
71 new_slot
->slot_num
= start
;
73 streamer_write_uhwi_stream (string_stream
, len
);
74 lto_output_data_stream (string_stream
, string
, len
);
79 struct string_slot
*old_slot
= *slot
;
80 return old_slot
->slot_num
+ 1;
85 /* Output STRING of LEN characters to the string table in OB. The
86 string might or might not include a trailing '\0'. Then put the
87 index onto the INDEX_STREAM.
88 When PERSISTENT is set, the string S is supposed to not change during
89 duration of the OB and thus OB can keep pointer into it. */
92 streamer_write_string_with_length (struct output_block
*ob
,
93 struct lto_output_stream
*index_stream
,
94 const char *s
, unsigned int len
,
98 streamer_write_uhwi_stream (index_stream
,
99 streamer_string_index (ob
, s
, len
, persistent
));
101 streamer_write_char_stream (index_stream
, 0);
105 /* Output the '\0' terminated STRING to the string
106 table in OB. Then put the index onto the INDEX_STREAM.
107 When PERSISTENT is set, the string S is supposed to not change during
108 duration of the OB and thus OB can keep pointer into it. */
111 streamer_write_string (struct output_block
*ob
,
112 struct lto_output_stream
*index_stream
,
113 const char *string
, bool persistent
)
116 streamer_write_string_with_length (ob
, index_stream
, string
,
120 streamer_write_char_stream (index_stream
, 0);
124 /* Output STRING of LEN characters to the string table in OB. Then
125 put the index into BP.
126 When PERSISTENT is set, the string S is supposed to not change during
127 duration of the OB and thus OB can keep pointer into it. */
130 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
131 const char *s
, unsigned int len
, bool persistent
)
135 index
= streamer_string_index (ob
, s
, len
, persistent
);
136 bp_pack_var_len_unsigned (bp
, index
);
140 /* Output the '\0' terminated STRING to the string
141 table in OB. Then put the index onto the bitpack BP.
142 When PERSISTENT is set, the string S is supposed to not change during
143 duration of the OB and thus OB can keep pointer into it. */
146 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
147 const char *s
, bool persistent
)
151 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
152 bp_pack_var_len_unsigned (bp
, index
);
157 /* Write a zero to the output stream. */
160 streamer_write_zero (struct output_block
*ob
)
162 streamer_write_char_stream (ob
->main_stream
, 0);
166 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
169 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
171 streamer_write_uhwi_stream (ob
->main_stream
, work
);
175 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
178 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
180 streamer_write_hwi_stream (ob
->main_stream
, work
);
183 /* Write a gcov counter value WORK to OB->main_stream. */
186 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
188 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
191 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
194 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
195 unsigned HOST_WIDE_INT work
)
197 if (obs
->left_in_block
== 0)
198 lto_append_block (obs
);
199 char *current_pointer
= obs
->current_pointer
;
200 unsigned int left_in_block
= obs
->left_in_block
;
201 unsigned int size
= 0;
204 unsigned int byte
= (work
& 0x7f);
207 /* More bytes to follow. */
210 *(current_pointer
++) = byte
;
214 while (work
!= 0 && left_in_block
> 0);
217 obs
->left_in_block
= 0;
218 lto_append_block (obs
);
219 current_pointer
= obs
->current_pointer
;
220 left_in_block
= obs
->left_in_block
;
223 unsigned int byte
= (work
& 0x7f);
226 /* More bytes to follow. */
229 *(current_pointer
++) = byte
;
235 obs
->current_pointer
= current_pointer
;
236 obs
->left_in_block
= left_in_block
;
237 obs
->total_size
+= size
;
241 /* Write a HOST_WIDE_INT value WORK to OBS. */
244 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
246 if (obs
->left_in_block
== 0)
247 lto_append_block (obs
);
248 char *current_pointer
= obs
->current_pointer
;
249 unsigned int left_in_block
= obs
->left_in_block
;
250 unsigned int size
= 0;
254 unsigned int byte
= (work
& 0x7f);
255 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
257 more
= !(work
== 0 || work
== -1);
260 /* More bits to follow. */
265 *(current_pointer
++) = byte
;
269 while (more
&& left_in_block
> 0);
272 obs
->left_in_block
= 0;
273 lto_append_block (obs
);
274 current_pointer
= obs
->current_pointer
;
275 left_in_block
= obs
->left_in_block
;
278 unsigned int byte
= (work
& 0x7f);
280 more
= !(work
== 0 || work
== -1);
287 *(current_pointer
++) = byte
;
293 obs
->current_pointer
= current_pointer
;
294 obs
->left_in_block
= left_in_block
;
295 obs
->total_size
+= size
;
298 /* Write a GCOV counter value WORK to OBS. */
301 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
303 gcc_assert (work
>= 0);
304 gcc_assert ((HOST_WIDE_INT
) work
== work
);
305 streamer_write_hwi_stream (obs
, work
);