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"
26 #include "data-streamer.h"
28 /* Return index used to reference STRING of LEN characters in the string table
29 in OB. The string might or might not include a trailing '\0'.
30 Then put the index onto the INDEX_STREAM.
31 When PERSISTENT is set, the string S is supposed to not change during
32 duration of the OB and thus OB can keep pointer into it. */
35 streamer_string_index (struct output_block
*ob
, const char *s
, unsigned int len
,
38 struct string_slot
**slot
;
39 struct string_slot s_slot
;
45 slot
= ob
->string_hash_table
.find_slot (&s_slot
, INSERT
);
48 struct lto_output_stream
*string_stream
= ob
->string_stream
;
49 unsigned int start
= string_stream
->total_size
;
50 struct string_slot
*new_slot
= XOBNEW (&ob
->obstack
, struct string_slot
);
56 string
= tmp
= XOBNEWVEC (&ob
->obstack
, char, len
);
64 new_slot
->slot_num
= start
;
66 streamer_write_uhwi_stream (string_stream
, len
);
67 lto_output_data_stream (string_stream
, string
, len
);
72 struct string_slot
*old_slot
= *slot
;
73 return old_slot
->slot_num
+ 1;
78 /* Output STRING of LEN characters to the string table in OB. The
79 string might or might not include a trailing '\0'. Then put the
80 index onto the INDEX_STREAM.
81 When PERSISTENT is set, the string S is supposed to not change during
82 duration of the OB and thus OB can keep pointer into it. */
85 streamer_write_string_with_length (struct output_block
*ob
,
86 struct lto_output_stream
*index_stream
,
87 const char *s
, unsigned int len
,
91 streamer_write_uhwi_stream (index_stream
,
92 streamer_string_index (ob
, s
, len
, persistent
));
94 streamer_write_char_stream (index_stream
, 0);
98 /* Output the '\0' terminated STRING to the string
99 table in OB. Then put the index onto the INDEX_STREAM.
100 When PERSISTENT is set, the string S is supposed to not change during
101 duration of the OB and thus OB can keep pointer into it. */
104 streamer_write_string (struct output_block
*ob
,
105 struct lto_output_stream
*index_stream
,
106 const char *string
, bool persistent
)
109 streamer_write_string_with_length (ob
, index_stream
, string
,
113 streamer_write_char_stream (index_stream
, 0);
117 /* Output STRING of LEN characters to the string table in OB. Then
118 put the index into BP.
119 When PERSISTENT is set, the string S is supposed to not change during
120 duration of the OB and thus OB can keep pointer into it. */
123 bp_pack_string_with_length (struct output_block
*ob
, struct bitpack_d
*bp
,
124 const char *s
, unsigned int len
, bool persistent
)
128 index
= streamer_string_index (ob
, s
, len
, persistent
);
129 bp_pack_var_len_unsigned (bp
, index
);
133 /* Output the '\0' terminated STRING to the string
134 table in OB. Then put the index onto the bitpack BP.
135 When PERSISTENT is set, the string S is supposed to not change during
136 duration of the OB and thus OB can keep pointer into it. */
139 bp_pack_string (struct output_block
*ob
, struct bitpack_d
*bp
,
140 const char *s
, bool persistent
)
144 index
= streamer_string_index (ob
, s
, strlen (s
) + 1, persistent
);
145 bp_pack_var_len_unsigned (bp
, index
);
150 /* Write a zero to the output stream. */
153 streamer_write_zero (struct output_block
*ob
)
155 streamer_write_char_stream (ob
->main_stream
, 0);
159 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
162 streamer_write_uhwi (struct output_block
*ob
, unsigned HOST_WIDE_INT work
)
164 streamer_write_uhwi_stream (ob
->main_stream
, work
);
168 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
171 streamer_write_hwi (struct output_block
*ob
, HOST_WIDE_INT work
)
173 streamer_write_hwi_stream (ob
->main_stream
, work
);
176 /* Write a gcov counter value WORK to OB->main_stream. */
179 streamer_write_gcov_count (struct output_block
*ob
, gcov_type work
)
181 streamer_write_gcov_count_stream (ob
->main_stream
, work
);
184 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
187 streamer_write_uhwi_stream (struct lto_output_stream
*obs
,
188 unsigned HOST_WIDE_INT work
)
192 unsigned int byte
= (work
& 0x7f);
195 /* More bytes to follow. */
198 streamer_write_char_stream (obs
, byte
);
204 /* Write a HOST_WIDE_INT value WORK to OBS. */
207 streamer_write_hwi_stream (struct lto_output_stream
*obs
, HOST_WIDE_INT work
)
213 byte
= (work
& 0x7f);
214 /* arithmetic shift */
216 more
= !((work
== 0 && (byte
& 0x40) == 0)
217 || (work
== -1 && (byte
& 0x40) != 0));
221 streamer_write_char_stream (obs
, byte
);
226 /* Write a GCOV counter value WORK to OBS. */
229 streamer_write_gcov_count_stream (struct lto_output_stream
*obs
, gcov_type work
)
231 gcc_assert (work
>= 0);
232 gcc_assert ((HOST_WIDE_INT
) work
== work
);
233 streamer_write_hwi_stream (obs
, work
);