Make all gimple_omp_for_ accessors typesafe
[official-gcc.git] / gcc / data-streamer-out.c
blob324003ead816fd8aef0a696f0e1fa76aa72a7953
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-2014 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 "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "data-streamer.h"
45 /* Adds a new block to output stream OBS. */
47 void
48 lto_append_block (struct lto_output_stream *obs)
50 struct lto_char_ptr_base *new_block;
52 gcc_assert (obs->left_in_block == 0);
54 if (obs->first_block == NULL)
56 /* This is the first time the stream has been written
57 into. */
58 obs->block_size = 1024;
59 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
60 obs->first_block = new_block;
62 else
64 struct lto_char_ptr_base *tptr;
65 /* Get a new block that is twice as big as the last block
66 and link it into the list. */
67 obs->block_size *= 2;
68 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
69 /* The first bytes of the block are reserved as a pointer to
70 the next block. Set the chain of the full block to the
71 pointer to the new block. */
72 tptr = obs->current_block;
73 tptr->ptr = (char *) new_block;
76 /* Set the place for the next char at the first position after the
77 chain to the next block. */
78 obs->current_pointer
79 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
80 obs->current_block = new_block;
81 /* Null out the newly allocated block's pointer to the next block. */
82 new_block->ptr = NULL;
83 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
87 /* Return index used to reference STRING of LEN characters in the string table
88 in OB. The string might or might not include a trailing '\0'.
89 Then put the index onto the INDEX_STREAM.
90 When PERSISTENT is set, the string S is supposed to not change during
91 duration of the OB and thus OB can keep pointer into it. */
93 static unsigned
94 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
95 bool persistent)
97 struct string_slot **slot;
98 struct string_slot s_slot;
100 s_slot.s = s;
101 s_slot.len = len;
102 s_slot.slot_num = 0;
104 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
105 if (*slot == NULL)
107 struct lto_output_stream *string_stream = ob->string_stream;
108 unsigned int start = string_stream->total_size;
109 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
110 const char *string;
112 if (!persistent)
114 char *tmp;
115 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
116 memcpy (tmp, s, len);
118 else
119 string = s;
121 new_slot->s = string;
122 new_slot->len = len;
123 new_slot->slot_num = start;
124 *slot = new_slot;
125 streamer_write_uhwi_stream (string_stream, len);
126 streamer_write_data_stream (string_stream, string, len);
127 return start + 1;
129 else
131 struct string_slot *old_slot = *slot;
132 return old_slot->slot_num + 1;
137 /* Output STRING of LEN characters to the string table in OB. The
138 string might or might not include a trailing '\0'. Then put the
139 index onto the INDEX_STREAM.
140 When PERSISTENT is set, the string S is supposed to not change during
141 duration of the OB and thus OB can keep pointer into it. */
143 void
144 streamer_write_string_with_length (struct output_block *ob,
145 struct lto_output_stream *index_stream,
146 const char *s, unsigned int len,
147 bool persistent)
149 if (s)
150 streamer_write_uhwi_stream (index_stream,
151 streamer_string_index (ob, s, len, persistent));
152 else
153 streamer_write_char_stream (index_stream, 0);
157 /* Output the '\0' terminated STRING to the string
158 table in OB. Then put the index onto the INDEX_STREAM.
159 When PERSISTENT is set, the string S is supposed to not change during
160 duration of the OB and thus OB can keep pointer into it. */
162 void
163 streamer_write_string (struct output_block *ob,
164 struct lto_output_stream *index_stream,
165 const char *string, bool persistent)
167 if (string)
168 streamer_write_string_with_length (ob, index_stream, string,
169 strlen (string) + 1,
170 persistent);
171 else
172 streamer_write_char_stream (index_stream, 0);
176 /* Output STRING of LEN characters to the string table in OB. Then
177 put the index into BP.
178 When PERSISTENT is set, the string S is supposed to not change during
179 duration of the OB and thus OB can keep pointer into it. */
181 void
182 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
183 const char *s, unsigned int len, bool persistent)
185 unsigned index = 0;
186 if (s)
187 index = streamer_string_index (ob, s, len, persistent);
188 bp_pack_var_len_unsigned (bp, index);
192 /* Output the '\0' terminated STRING to the string
193 table in OB. Then put the index onto the bitpack BP.
194 When PERSISTENT is set, the string S is supposed to not change during
195 duration of the OB and thus OB can keep pointer into it. */
197 void
198 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
199 const char *s, bool persistent)
201 unsigned index = 0;
202 if (s)
203 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
204 bp_pack_var_len_unsigned (bp, index);
209 /* Write a zero to the output stream. */
211 void
212 streamer_write_zero (struct output_block *ob)
214 streamer_write_char_stream (ob->main_stream, 0);
218 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
220 void
221 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
223 streamer_write_uhwi_stream (ob->main_stream, work);
227 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
229 void
230 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
232 streamer_write_hwi_stream (ob->main_stream, work);
235 /* Write a gcov counter value WORK to OB->main_stream. */
237 void
238 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
240 streamer_write_gcov_count_stream (ob->main_stream, work);
243 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
245 void
246 streamer_write_uhwi_stream (struct lto_output_stream *obs,
247 unsigned HOST_WIDE_INT work)
249 if (obs->left_in_block == 0)
250 lto_append_block (obs);
251 char *current_pointer = obs->current_pointer;
252 unsigned int left_in_block = obs->left_in_block;
253 unsigned int size = 0;
256 unsigned int byte = (work & 0x7f);
257 work >>= 7;
258 if (work != 0)
259 /* More bytes to follow. */
260 byte |= 0x80;
262 *(current_pointer++) = byte;
263 left_in_block--;
264 size++;
266 while (work != 0 && left_in_block > 0);
267 if (work != 0)
269 obs->left_in_block = 0;
270 lto_append_block (obs);
271 current_pointer = obs->current_pointer;
272 left_in_block = obs->left_in_block;
275 unsigned int byte = (work & 0x7f);
276 work >>= 7;
277 if (work != 0)
278 /* More bytes to follow. */
279 byte |= 0x80;
281 *(current_pointer++) = byte;
282 left_in_block--;
283 size++;
285 while (work != 0);
287 obs->current_pointer = current_pointer;
288 obs->left_in_block = left_in_block;
289 obs->total_size += size;
293 /* Write a HOST_WIDE_INT value WORK to OBS. */
295 void
296 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
298 if (obs->left_in_block == 0)
299 lto_append_block (obs);
300 char *current_pointer = obs->current_pointer;
301 unsigned int left_in_block = obs->left_in_block;
302 unsigned int size = 0;
303 bool more;
306 unsigned int byte = (work & 0x7f);
307 /* If the lower 7-bits are sign-extended 0 or -1 we are finished. */
308 work >>= 6;
309 more = !(work == 0 || work == -1);
310 if (more)
312 /* More bits to follow. */
313 work >>= 1;
314 byte |= 0x80;
317 *(current_pointer++) = byte;
318 left_in_block--;
319 size++;
321 while (more && left_in_block > 0);
322 if (more)
324 obs->left_in_block = 0;
325 lto_append_block (obs);
326 current_pointer = obs->current_pointer;
327 left_in_block = obs->left_in_block;
330 unsigned int byte = (work & 0x7f);
331 work >>= 6;
332 more = !(work == 0 || work == -1);
333 if (more)
335 work >>= 1;
336 byte |= 0x80;
339 *(current_pointer++) = byte;
340 left_in_block--;
341 size++;
343 while (more);
345 obs->current_pointer = current_pointer;
346 obs->left_in_block = left_in_block;
347 obs->total_size += size;
350 /* Write a GCOV counter value WORK to OBS. */
352 void
353 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
355 gcc_assert (work >= 0);
356 gcc_assert ((HOST_WIDE_INT) work == work);
357 streamer_write_hwi_stream (obs, work);
360 /* Write raw DATA of length LEN to the output block OB. */
362 void
363 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
364 size_t len)
366 while (len)
368 size_t copy;
370 /* No space left. */
371 if (obs->left_in_block == 0)
372 lto_append_block (obs);
374 /* Determine how many bytes to copy in this loop. */
375 if (len <= obs->left_in_block)
376 copy = len;
377 else
378 copy = obs->left_in_block;
380 /* Copy the data and do bookkeeping. */
381 memcpy (obs->current_pointer, data, copy);
382 obs->current_pointer += copy;
383 obs->total_size += copy;
384 obs->left_in_block -= copy;
385 data = (const char *) data + copy;
386 len -= copy;