* config/rx/rx.c (ADD_RX_BUILTIN0): New macro, used for builtins
[official-gcc.git] / gcc / data-streamer-out.c
blobfbbe0db27592ccc178f50d8bb8533a129faba338
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 "gimple.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. */
36 unsigned
37 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
38 bool persistent)
40 struct string_slot **slot;
41 struct string_slot s_slot;
43 s_slot.s = s;
44 s_slot.len = len;
45 s_slot.slot_num = 0;
47 slot = ob->string_hash_table.find_slot (&s_slot, INSERT);
48 if (*slot == NULL)
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);
53 const char *string;
55 if (!persistent)
57 char *tmp;
58 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
59 memcpy (tmp, s, len);
61 else
62 string = s;
64 new_slot->s = string;
65 new_slot->len = len;
66 new_slot->slot_num = start;
67 *slot = new_slot;
68 streamer_write_uhwi_stream (string_stream, len);
69 lto_output_data_stream (string_stream, string, len);
70 return start + 1;
72 else
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. */
86 void
87 streamer_write_string_with_length (struct output_block *ob,
88 struct lto_output_stream *index_stream,
89 const char *s, unsigned int len,
90 bool persistent)
92 if (s)
93 streamer_write_uhwi_stream (index_stream,
94 streamer_string_index (ob, s, len, persistent));
95 else
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. */
105 void
106 streamer_write_string (struct output_block *ob,
107 struct lto_output_stream *index_stream,
108 const char *string, bool persistent)
110 if (string)
111 streamer_write_string_with_length (ob, index_stream, string,
112 strlen (string) + 1,
113 persistent);
114 else
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. */
124 void
125 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
126 const char *s, unsigned int len, bool persistent)
128 unsigned index = 0;
129 if (s)
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. */
140 void
141 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
142 const char *s, bool persistent)
144 unsigned index = 0;
145 if (s)
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. */
154 void
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. */
163 void
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. */
172 void
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. */
180 void
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. */
188 void
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);
200 work >>= 7;
201 if (work != 0)
202 /* More bytes to follow. */
203 byte |= 0x80;
205 *(current_pointer++) = byte;
206 left_in_block--;
207 size++;
209 while (work != 0 && left_in_block > 0);
210 if (work != 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);
219 work >>= 7;
220 if (work != 0)
221 /* More bytes to follow. */
222 byte |= 0x80;
224 *(current_pointer++) = byte;
225 left_in_block--;
226 size++;
228 while (work != 0);
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. */
238 void
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;
246 bool more;
249 unsigned int byte = (work & 0x7f);
250 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
251 work >>= 6;
252 more = !(work == 0 || work == -1);
253 if (more)
255 /* More bits to follow. */
256 work >>= 1;
257 byte |= 0x80;
260 *(current_pointer++) = byte;
261 left_in_block--;
262 size++;
264 while (more && left_in_block > 0);
265 if (more)
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);
274 work >>= 6;
275 more = !(work == 0 || work == -1);
276 if (more)
278 work >>= 1;
279 byte |= 0x80;
282 *(current_pointer++) = byte;
283 left_in_block--;
284 size++;
286 while (more);
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. */
295 void
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);