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-2013 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"
28 #include "data-streamer.h"
30 /* Return index used to reference STRING of LEN characters in the string table
31 in OB. The string might or might not include a trailing '\0'.
32 Then put the index onto the INDEX_STREAM.
33 When PERSISTENT is set, the string S is supposed to not change during
34 duration of the OB and thus OB can keep pointer into it. */
37 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
40 struct string_slot
**slot
;
41 struct string_slot s_slot
;
47 slot
= ob
->string_hash_table
.find_slot (&s_slot
, INSERT
);
50 struct lto_output_stream
*string_stream
= ob
->string_stream
;
51 unsigned int start
= string_stream
->total_size
;
52 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
58 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
66 new_slot
->slot_num
= start
;
68 streamer_write_uhwi_stream (string_stream
, len
);
69 lto_output_data_stream (string_stream
, string
, len
);
74 struct string_slot
*old_slot
= *slot
;
75 return old_slot
->slot_num
+ 1;
80 /* Output STRING of LEN characters to the string table in OB. The
81 string might or might not include a trailing '\0'. Then put the
82 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_write_string_with_length (struct output_block
*ob
,
88 struct lto_output_stream
*index_stream
,
89 const char *s
, unsigned int len
,
93 streamer_write_uhwi_stream (index_stream
,
94 streamer_string_index (ob
, s
, len
, persistent
));
96 streamer_write_char_stream (index_stream
, 0);
100 /* Output the '\0' terminated STRING to the string
101 table in OB. Then put the index onto the INDEX_STREAM.
102 When PERSISTENT is set, the string S is supposed to not change during
103 duration of the OB and thus OB can keep pointer into it. */
106 streamer_write_string (struct output_block
*ob
,
107 struct lto_output_stream
*index_stream
,
108 const char *string
, bool persistent
)
111 streamer_write_string_with_length (ob
, index_stream
, string
,
115 streamer_write_char_stream (index_stream
, 0);
119 /* Output STRING of LEN characters to the string table in OB. Then
120 put the index into BP.
121 When PERSISTENT is set, the string S is supposed to not change during
122 duration of the OB and thus OB can keep pointer into it. */
125 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
126 const char *s
, unsigned int len
, bool persistent
)
130 index
= streamer_string_index (ob
, s
, len
, persistent
);
131 bp_pack_var_len_unsigned (bp
, index
);
135 /* Output the '\0' terminated STRING to the string
136 table in OB. Then put the index onto the bitpack BP.
137 When PERSISTENT is set, the string S is supposed to not change during
138 duration of the OB and thus OB can keep pointer into it. */
141 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
142 const char *s
, bool persistent
)
146 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
147 bp_pack_var_len_unsigned (bp
, index
);
152 /* Write a zero to the output stream. */
155 streamer_write_zero (struct output_block
*ob
)
157 streamer_write_char_stream (ob
->main_stream
, 0);
161 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
164 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
166 streamer_write_uhwi_stream (ob
->main_stream
, work
);
170 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
173 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
175 streamer_write_hwi_stream (ob
->main_stream
, work
);
178 /* Write a gcov counter value WORK to OB->main_stream. */
181 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
183 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
186 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
189 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
190 unsigned HOST_WIDE_INT work
)
192 if (obs
->left_in_block
== 0)
193 lto_append_block (obs
);
194 char *current_pointer
= obs
->current_pointer
;
195 unsigned int left_in_block
= obs
->left_in_block
;
196 unsigned int size
= 0;
199 unsigned int byte
= (work
& 0x7f);
202 /* More bytes to follow. */
205 *(current_pointer
++) = byte
;
209 while (work
!= 0 && left_in_block
> 0);
212 obs
->left_in_block
= 0;
213 lto_append_block (obs
);
214 current_pointer
= obs
->current_pointer
;
215 left_in_block
= obs
->left_in_block
;
218 unsigned int byte
= (work
& 0x7f);
221 /* More bytes to follow. */
224 *(current_pointer
++) = byte
;
230 obs
->current_pointer
= current_pointer
;
231 obs
->left_in_block
= left_in_block
;
232 obs
->total_size
+= size
;
236 /* Write a HOST_WIDE_INT value WORK to OBS. */
239 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
241 if (obs
->left_in_block
== 0)
242 lto_append_block (obs
);
243 char *current_pointer
= obs
->current_pointer
;
244 unsigned int left_in_block
= obs
->left_in_block
;
245 unsigned int size
= 0;
249 unsigned int byte
= (work
& 0x7f);
250 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
252 more
= !(work
== 0 || work
== -1);
255 /* More bits to follow. */
260 *(current_pointer
++) = byte
;
264 while (more
&& left_in_block
> 0);
267 obs
->left_in_block
= 0;
268 lto_append_block (obs
);
269 current_pointer
= obs
->current_pointer
;
270 left_in_block
= obs
->left_in_block
;
273 unsigned int byte
= (work
& 0x7f);
275 more
= !(work
== 0 || work
== -1);
282 *(current_pointer
++) = byte
;
288 obs
->current_pointer
= current_pointer
;
289 obs
->left_in_block
= left_in_block
;
290 obs
->total_size
+= size
;
293 /* Write a GCOV counter value WORK to OBS. */
296 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
298 gcc_assert (work
>= 0);
299 gcc_assert ((HOST_WIDE_INT
) work
== work
);
300 streamer_write_hwi_stream (obs
, work
);