Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / data-streamer-out.cc
blob07cc6bd201884b059a063d15d0d766cf667345e9
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-2024 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 "backend.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "data-streamer.h"
31 #include "value-range.h"
32 #include "streamer-hooks.h"
35 /* Adds a new block to output stream OBS. */
37 void
38 lto_append_block (struct lto_output_stream *obs)
40 struct lto_char_ptr_base *new_block;
42 gcc_assert (obs->left_in_block == 0);
44 if (obs->first_block == NULL)
46 /* This is the first time the stream has been written
47 into. */
48 obs->block_size = 1024;
49 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
50 obs->first_block = new_block;
52 else
54 struct lto_char_ptr_base *tptr;
55 /* Get a new block that is twice as big as the last block
56 and link it into the list. */
57 obs->block_size *= 2;
58 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
59 /* The first bytes of the block are reserved as a pointer to
60 the next block. Set the chain of the full block to the
61 pointer to the new block. */
62 tptr = obs->current_block;
63 tptr->ptr = (char *) new_block;
66 /* Set the place for the next char at the first position after the
67 chain to the next block. */
68 obs->current_pointer
69 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
70 obs->current_block = new_block;
71 /* Null out the newly allocated block's pointer to the next block. */
72 new_block->ptr = NULL;
73 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
77 /* Return index used to reference STRING of LEN characters in the string table
78 in OB. The string might or might not include a trailing '\0'.
79 Then put the index onto the INDEX_STREAM.
80 When PERSISTENT is set, the string S is supposed to not change during
81 duration of the OB and thus OB can keep pointer into it. */
83 static unsigned
84 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
85 bool persistent)
87 struct string_slot **slot;
88 struct string_slot s_slot;
90 s_slot.s = s;
91 s_slot.len = len;
92 s_slot.slot_num = 0;
94 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
95 if (*slot == NULL)
97 struct lto_output_stream *string_stream = ob->string_stream;
98 unsigned int start = string_stream->total_size;
99 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
100 const char *string;
102 if (!persistent)
104 char *tmp;
105 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
106 memcpy (tmp, s, len);
108 else
109 string = s;
111 new_slot->s = string;
112 new_slot->len = len;
113 new_slot->slot_num = start;
114 *slot = new_slot;
115 streamer_write_uhwi_stream (string_stream, len);
116 streamer_write_data_stream (string_stream, string, len);
117 return start + 1;
119 else
121 struct string_slot *old_slot = *slot;
122 return old_slot->slot_num + 1;
127 /* Output STRING of LEN characters to the string table in OB. The
128 string might or might not include a trailing '\0'. Then put the
129 index onto the INDEX_STREAM.
130 When PERSISTENT is set, the string S is supposed to not change during
131 duration of the OB and thus OB can keep pointer into it. */
133 void
134 streamer_write_string_with_length (struct output_block *ob,
135 struct lto_output_stream *index_stream,
136 const char *s, unsigned int len,
137 bool persistent)
139 if (s)
140 streamer_write_uhwi_stream (index_stream,
141 streamer_string_index (ob, s, len, persistent));
142 else
143 streamer_write_char_stream (index_stream, 0);
147 /* Output the '\0' terminated STRING to the string
148 table in OB. Then put the index onto the INDEX_STREAM.
149 When PERSISTENT is set, the string S is supposed to not change during
150 duration of the OB and thus OB can keep pointer into it. */
152 void
153 streamer_write_string (struct output_block *ob,
154 struct lto_output_stream *index_stream,
155 const char *string, bool persistent)
157 if (string)
158 streamer_write_string_with_length (ob, index_stream, string,
159 strlen (string) + 1,
160 persistent);
161 else
162 streamer_write_char_stream (index_stream, 0);
166 /* Output STRING of LEN characters to the string table in OB. Then
167 put the index into BP.
168 When PERSISTENT is set, the string S is supposed to not change during
169 duration of the OB and thus OB can keep pointer into it. */
171 void
172 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
173 const char *s, unsigned int len, bool persistent)
175 unsigned index = 0;
176 if (s)
177 index = streamer_string_index (ob, s, len, persistent);
178 bp_pack_var_len_unsigned (bp, index);
182 /* Output the '\0' terminated STRING to the string
183 table in OB. Then put the index onto the bitpack BP.
184 When PERSISTENT is set, the string S is supposed to not change during
185 duration of the OB and thus OB can keep pointer into it. */
187 void
188 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
189 const char *s, bool persistent)
191 unsigned index = 0;
192 if (s)
193 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
194 bp_pack_var_len_unsigned (bp, index);
199 /* Write a zero to the output stream. */
201 void
202 streamer_write_zero (struct output_block *ob)
204 streamer_write_char_stream (ob->main_stream, 0);
208 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
210 void
211 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
213 streamer_write_uhwi_stream (ob->main_stream, work);
217 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
219 void
220 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
222 streamer_write_hwi_stream (ob->main_stream, work);
225 /* Write a poly_uint64 value WORK to OB->main_stream. */
227 void
228 streamer_write_poly_uint64 (struct output_block *ob, poly_uint64 work)
230 for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
231 streamer_write_uhwi_stream (ob->main_stream, work.coeffs[i]);
234 /* Write a poly_int64 value WORK to OB->main_stream. */
236 void
237 streamer_write_poly_int64 (struct output_block *ob, poly_int64 work)
239 for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
240 streamer_write_hwi_stream (ob->main_stream, work.coeffs[i]);
243 /* Write a gcov counter value WORK to OB->main_stream. */
245 void
246 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
248 streamer_write_gcov_count_stream (ob->main_stream, work);
251 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
253 void
254 streamer_write_uhwi_stream (struct lto_output_stream *obs,
255 unsigned HOST_WIDE_INT work)
257 if (obs->left_in_block == 0)
258 lto_append_block (obs);
259 char *current_pointer = obs->current_pointer;
260 unsigned int left_in_block = obs->left_in_block;
261 unsigned int size = 0;
264 unsigned int byte = (work & 0x7f);
265 work >>= 7;
266 if (work != 0)
267 /* More bytes to follow. */
268 byte |= 0x80;
270 *(current_pointer++) = byte;
271 left_in_block--;
272 size++;
274 while (work != 0 && left_in_block > 0);
275 if (work != 0)
277 obs->left_in_block = 0;
278 lto_append_block (obs);
279 current_pointer = obs->current_pointer;
280 left_in_block = obs->left_in_block;
283 unsigned int byte = (work & 0x7f);
284 work >>= 7;
285 if (work != 0)
286 /* More bytes to follow. */
287 byte |= 0x80;
289 *(current_pointer++) = byte;
290 left_in_block--;
291 size++;
293 while (work != 0);
295 obs->current_pointer = current_pointer;
296 obs->left_in_block = left_in_block;
297 obs->total_size += size;
301 /* Write a HOST_WIDE_INT value WORK to OBS. */
303 void
304 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
306 if (obs->left_in_block == 0)
307 lto_append_block (obs);
308 char *current_pointer = obs->current_pointer;
309 unsigned int left_in_block = obs->left_in_block;
310 unsigned int size = 0;
311 bool more;
314 unsigned int byte = (work & 0x7f);
315 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
316 work >>= 6;
317 more = !(work == 0 || work == -1);
318 if (more)
320 /* More bits to follow. */
321 work >>= 1;
322 byte |= 0x80;
325 *(current_pointer++) = byte;
326 left_in_block--;
327 size++;
329 while (more && left_in_block > 0);
330 if (more)
332 obs->left_in_block = 0;
333 lto_append_block (obs);
334 current_pointer = obs->current_pointer;
335 left_in_block = obs->left_in_block;
338 unsigned int byte = (work & 0x7f);
339 work >>= 6;
340 more = !(work == 0 || work == -1);
341 if (more)
343 work >>= 1;
344 byte |= 0x80;
347 *(current_pointer++) = byte;
348 left_in_block--;
349 size++;
351 while (more);
353 obs->current_pointer = current_pointer;
354 obs->left_in_block = left_in_block;
355 obs->total_size += size;
358 /* Write a GCOV counter value WORK to OBS. */
360 void
361 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
363 gcc_assert ((HOST_WIDE_INT) work == work);
364 streamer_write_hwi_stream (obs, work);
367 /* Write raw DATA of length LEN to the output block OB. */
369 void
370 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
371 size_t len)
373 while (len)
375 size_t copy;
377 /* No space left. */
378 if (obs->left_in_block == 0)
379 lto_append_block (obs);
381 /* Determine how many bytes to copy in this loop. */
382 if (len <= obs->left_in_block)
383 copy = len;
384 else
385 copy = obs->left_in_block;
387 /* Copy the data and do bookkeeping. */
388 memcpy (obs->current_pointer, data, copy);
389 obs->current_pointer += copy;
390 obs->total_size += copy;
391 obs->left_in_block -= copy;
392 data = (const char *) data + copy;
393 len -= copy;
397 /* Write REAL_VALUE_TYPE into OB. */
399 void
400 streamer_write_real_value (struct output_block *ob, const REAL_VALUE_TYPE *r)
402 bitpack_d bp = bitpack_create (ob->main_stream);
403 bp_pack_real_value (&bp, r);
404 streamer_write_bitpack (&bp);
407 void
408 streamer_write_vrange (struct output_block *ob, const vrange &v)
410 gcc_checking_assert (!v.undefined_p ());
412 // Write the common fields to all vranges.
413 value_range_kind kind = v.m_kind;
414 streamer_write_enum (ob->main_stream, value_range_kind, VR_LAST, kind);
415 stream_write_tree (ob, v.type (), true);
417 if (is_a <irange> (v))
419 const irange &r = as_a <irange> (v);
420 streamer_write_uhwi (ob, r.num_pairs ());
421 for (unsigned i = 0; i < r.num_pairs (); ++i)
423 streamer_write_wide_int (ob, r.lower_bound (i));
424 streamer_write_wide_int (ob, r.upper_bound (i));
426 // TODO: We could avoid streaming out the value if the mask is -1.
427 irange_bitmask bm = r.get_bitmask ();
428 streamer_write_wide_int (ob, bm.value ());
429 streamer_write_wide_int (ob, bm.mask ());
430 return;
432 if (is_a <frange> (v))
434 const frange &r = as_a <frange> (v);
436 // Stream out NAN bits.
437 bitpack_d bp = bitpack_create (ob->main_stream);
438 nan_state nan = r.get_nan_state ();
439 bp_pack_value (&bp, nan.pos_p (), 1);
440 bp_pack_value (&bp, nan.neg_p (), 1);
441 streamer_write_bitpack (&bp);
443 // Stream out bounds.
444 if (kind != VR_NAN)
446 REAL_VALUE_TYPE lb = r.lower_bound ();
447 REAL_VALUE_TYPE ub = r.upper_bound ();
448 streamer_write_real_value (ob, &lb);
449 streamer_write_real_value (ob, &ub);
451 return;
453 gcc_unreachable ();
456 /* Emit the physical representation of wide_int VAL to output block OB. */
458 void
459 streamer_write_wide_int (struct output_block *ob, const wide_int &val)
461 int len = val.get_len ();
463 streamer_write_uhwi (ob, val.get_precision ());
464 streamer_write_uhwi (ob, len);
465 for (int i = 0; i < len; i++)
466 streamer_write_hwi (ob, val.elt (i));
469 /* Emit the physical representation of widest_int W to output block OB. */
471 void
472 streamer_write_widest_int (struct output_block *ob,
473 const widest_int &w)
475 int len = w.get_len ();
477 streamer_write_uhwi (ob, w.get_precision ());
478 streamer_write_uhwi (ob, len);
479 for (int i = 0; i < len; i++)
480 streamer_write_hwi (ob, w.elt (i));