2013-10-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / data-streamer-out.c
blob2e55e3df759ab6d997716ede04f326d5e9aecd57
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
12 version.
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
17 for more details.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "data-streamer.h"
29 /* Return index used to reference STRING of LEN characters in the string table
30 in OB. The string might or might not include a trailing '\0'.
31 Then put the index onto the INDEX_STREAM.
32 When PERSISTENT is set, the string S is supposed to not change during
33 duration of the OB and thus OB can keep pointer into it. */
35 unsigned
36 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
37 bool persistent)
39 struct string_slot **slot;
40 struct string_slot s_slot;
42 s_slot.s = s;
43 s_slot.len = len;
44 s_slot.slot_num = 0;
46 slot = ob->string_hash_table.find_slot (&s_slot, INSERT);
47 if (*slot == NULL)
49 struct lto_output_stream *string_stream = ob->string_stream;
50 unsigned int start = string_stream->total_size;
51 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
52 const char *string;
54 if (!persistent)
56 char *tmp;
57 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
58 memcpy (tmp, s, len);
60 else
61 string = s;
63 new_slot->s = string;
64 new_slot->len = len;
65 new_slot->slot_num = start;
66 *slot = new_slot;
67 streamer_write_uhwi_stream (string_stream, len);
68 lto_output_data_stream (string_stream, string, len);
69 return start + 1;
71 else
73 struct string_slot *old_slot = *slot;
74 return old_slot->slot_num + 1;
79 /* Output STRING of LEN characters to the string table in OB. The
80 string might or might not include a trailing '\0'. Then put the
81 index onto the INDEX_STREAM.
82 When PERSISTENT is set, the string S is supposed to not change during
83 duration of the OB and thus OB can keep pointer into it. */
85 void
86 streamer_write_string_with_length (struct output_block *ob,
87 struct lto_output_stream *index_stream,
88 const char *s, unsigned int len,
89 bool persistent)
91 if (s)
92 streamer_write_uhwi_stream (index_stream,
93 streamer_string_index (ob, s, len, persistent));
94 else
95 streamer_write_char_stream (index_stream, 0);
99 /* Output the '\0' terminated STRING to the string
100 table in OB. 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. */
104 void
105 streamer_write_string (struct output_block *ob,
106 struct lto_output_stream *index_stream,
107 const char *string, bool persistent)
109 if (string)
110 streamer_write_string_with_length (ob, index_stream, string,
111 strlen (string) + 1,
112 persistent);
113 else
114 streamer_write_char_stream (index_stream, 0);
118 /* Output STRING of LEN characters to the string table in OB. Then
119 put the index into BP.
120 When PERSISTENT is set, the string S is supposed to not change during
121 duration of the OB and thus OB can keep pointer into it. */
123 void
124 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
125 const char *s, unsigned int len, bool persistent)
127 unsigned index = 0;
128 if (s)
129 index = streamer_string_index (ob, s, len, persistent);
130 bp_pack_var_len_unsigned (bp, index);
134 /* Output the '\0' terminated STRING to the string
135 table in OB. Then put the index onto the bitpack BP.
136 When PERSISTENT is set, the string S is supposed to not change during
137 duration of the OB and thus OB can keep pointer into it. */
139 void
140 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
141 const char *s, bool persistent)
143 unsigned index = 0;
144 if (s)
145 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
146 bp_pack_var_len_unsigned (bp, index);
151 /* Write a zero to the output stream. */
153 void
154 streamer_write_zero (struct output_block *ob)
156 streamer_write_char_stream (ob->main_stream, 0);
160 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
162 void
163 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
165 streamer_write_uhwi_stream (ob->main_stream, work);
169 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
171 void
172 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
174 streamer_write_hwi_stream (ob->main_stream, work);
177 /* Write a gcov counter value WORK to OB->main_stream. */
179 void
180 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
182 streamer_write_gcov_count_stream (ob->main_stream, work);
185 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
187 void
188 streamer_write_uhwi_stream (struct lto_output_stream *obs,
189 unsigned HOST_WIDE_INT work)
191 if (obs->left_in_block == 0)
192 lto_append_block (obs);
193 char *current_pointer = obs->current_pointer;
194 unsigned int left_in_block = obs->left_in_block;
195 unsigned int size = 0;
198 unsigned int byte = (work & 0x7f);
199 work >>= 7;
200 if (work != 0)
201 /* More bytes to follow. */
202 byte |= 0x80;
204 *(current_pointer++) = byte;
205 left_in_block--;
206 size++;
208 while (work != 0 && left_in_block > 0);
209 if (work != 0)
211 obs->left_in_block = 0;
212 lto_append_block (obs);
213 current_pointer = obs->current_pointer;
214 left_in_block = obs->left_in_block;
217 unsigned int byte = (work & 0x7f);
218 work >>= 7;
219 if (work != 0)
220 /* More bytes to follow. */
221 byte |= 0x80;
223 *(current_pointer++) = byte;
224 left_in_block--;
225 size++;
227 while (work != 0);
229 obs->current_pointer = current_pointer;
230 obs->left_in_block = left_in_block;
231 obs->total_size += size;
235 /* Write a HOST_WIDE_INT value WORK to OBS. */
237 void
238 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
240 if (obs->left_in_block == 0)
241 lto_append_block (obs);
242 char *current_pointer = obs->current_pointer;
243 unsigned int left_in_block = obs->left_in_block;
244 unsigned int size = 0;
245 bool more;
248 unsigned int byte = (work & 0x7f);
249 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
250 work >>= 6;
251 more = !(work == 0 || work == -1);
252 if (more)
254 /* More bits to follow. */
255 work >>= 1;
256 byte |= 0x80;
259 *(current_pointer++) = byte;
260 left_in_block--;
261 size++;
263 while (more && left_in_block > 0);
264 if (more)
266 obs->left_in_block = 0;
267 lto_append_block (obs);
268 current_pointer = obs->current_pointer;
269 left_in_block = obs->left_in_block;
272 unsigned int byte = (work & 0x7f);
273 work >>= 6;
274 more = !(work == 0 || work == -1);
275 if (more)
277 work >>= 1;
278 byte |= 0x80;
281 *(current_pointer++) = byte;
282 left_in_block--;
283 size++;
285 while (more);
287 obs->current_pointer = current_pointer;
288 obs->left_in_block = left_in_block;
289 obs->total_size += size;
292 /* Write a GCOV counter value WORK to OBS. */
294 void
295 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
297 gcc_assert (work >= 0);
298 gcc_assert ((HOST_WIDE_INT) work == work);
299 streamer_write_hwi_stream (obs, work);