coretypes.h: Include machmode.h...
[gcc.git] / gcc / data-streamer-out.c
1 /* Routines for saving various data types to a file stream. This deals
2 with various data types like strings, integers, enums, etc.
3
4 Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "options.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "predict.h"
36 #include "tm.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "hash-map.h"
47 #include "plugin-api.h"
48 #include "ipa-ref.h"
49 #include "cgraph.h"
50 #include "data-streamer.h"
51
52
53 /* Adds a new block to output stream OBS. */
54
55 void
56 lto_append_block (struct lto_output_stream *obs)
57 {
58 struct lto_char_ptr_base *new_block;
59
60 gcc_assert (obs->left_in_block == 0);
61
62 if (obs->first_block == NULL)
63 {
64 /* This is the first time the stream has been written
65 into. */
66 obs->block_size = 1024;
67 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
68 obs->first_block = new_block;
69 }
70 else
71 {
72 struct lto_char_ptr_base *tptr;
73 /* Get a new block that is twice as big as the last block
74 and link it into the list. */
75 obs->block_size *= 2;
76 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
77 /* The first bytes of the block are reserved as a pointer to
78 the next block. Set the chain of the full block to the
79 pointer to the new block. */
80 tptr = obs->current_block;
81 tptr->ptr = (char *) new_block;
82 }
83
84 /* Set the place for the next char at the first position after the
85 chain to the next block. */
86 obs->current_pointer
87 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
88 obs->current_block = new_block;
89 /* Null out the newly allocated block's pointer to the next block. */
90 new_block->ptr = NULL;
91 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
92 }
93
94
95 /* Return index used to reference STRING of LEN characters in the string table
96 in OB. The string might or might not include a trailing '\0'.
97 Then put the index onto the INDEX_STREAM.
98 When PERSISTENT is set, the string S is supposed to not change during
99 duration of the OB and thus OB can keep pointer into it. */
100
101 static unsigned
102 streamer_string_index (struct output_block *ob, const char *s, unsigned int len,
103 bool persistent)
104 {
105 struct string_slot **slot;
106 struct string_slot s_slot;
107
108 s_slot.s = s;
109 s_slot.len = len;
110 s_slot.slot_num = 0;
111
112 slot = ob->string_hash_table->find_slot (&s_slot, INSERT);
113 if (*slot == NULL)
114 {
115 struct lto_output_stream *string_stream = ob->string_stream;
116 unsigned int start = string_stream->total_size;
117 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
118 const char *string;
119
120 if (!persistent)
121 {
122 char *tmp;
123 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
124 memcpy (tmp, s, len);
125 }
126 else
127 string = s;
128
129 new_slot->s = string;
130 new_slot->len = len;
131 new_slot->slot_num = start;
132 *slot = new_slot;
133 streamer_write_uhwi_stream (string_stream, len);
134 streamer_write_data_stream (string_stream, string, len);
135 return start + 1;
136 }
137 else
138 {
139 struct string_slot *old_slot = *slot;
140 return old_slot->slot_num + 1;
141 }
142 }
143
144
145 /* Output STRING of LEN characters to the string table in OB. The
146 string might or might not include a trailing '\0'. Then put the
147 index onto the INDEX_STREAM.
148 When PERSISTENT is set, the string S is supposed to not change during
149 duration of the OB and thus OB can keep pointer into it. */
150
151 void
152 streamer_write_string_with_length (struct output_block *ob,
153 struct lto_output_stream *index_stream,
154 const char *s, unsigned int len,
155 bool persistent)
156 {
157 if (s)
158 streamer_write_uhwi_stream (index_stream,
159 streamer_string_index (ob, s, len, persistent));
160 else
161 streamer_write_char_stream (index_stream, 0);
162 }
163
164
165 /* Output the '\0' terminated STRING to the string
166 table in OB. Then put the index onto the INDEX_STREAM.
167 When PERSISTENT is set, the string S is supposed to not change during
168 duration of the OB and thus OB can keep pointer into it. */
169
170 void
171 streamer_write_string (struct output_block *ob,
172 struct lto_output_stream *index_stream,
173 const char *string, bool persistent)
174 {
175 if (string)
176 streamer_write_string_with_length (ob, index_stream, string,
177 strlen (string) + 1,
178 persistent);
179 else
180 streamer_write_char_stream (index_stream, 0);
181 }
182
183
184 /* Output STRING of LEN characters to the string table in OB. Then
185 put the index into BP.
186 When PERSISTENT is set, the string S is supposed to not change during
187 duration of the OB and thus OB can keep pointer into it. */
188
189 void
190 bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp,
191 const char *s, unsigned int len, bool persistent)
192 {
193 unsigned index = 0;
194 if (s)
195 index = streamer_string_index (ob, s, len, persistent);
196 bp_pack_var_len_unsigned (bp, index);
197 }
198
199
200 /* Output the '\0' terminated STRING to the string
201 table in OB. Then put the index onto the bitpack BP.
202 When PERSISTENT is set, the string S is supposed to not change during
203 duration of the OB and thus OB can keep pointer into it. */
204
205 void
206 bp_pack_string (struct output_block *ob, struct bitpack_d *bp,
207 const char *s, bool persistent)
208 {
209 unsigned index = 0;
210 if (s)
211 index = streamer_string_index (ob, s, strlen (s) + 1, persistent);
212 bp_pack_var_len_unsigned (bp, index);
213 }
214
215
216
217 /* Write a zero to the output stream. */
218
219 void
220 streamer_write_zero (struct output_block *ob)
221 {
222 streamer_write_char_stream (ob->main_stream, 0);
223 }
224
225
226 /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */
227
228 void
229 streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work)
230 {
231 streamer_write_uhwi_stream (ob->main_stream, work);
232 }
233
234
235 /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */
236
237 void
238 streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work)
239 {
240 streamer_write_hwi_stream (ob->main_stream, work);
241 }
242
243 /* Write a gcov counter value WORK to OB->main_stream. */
244
245 void
246 streamer_write_gcov_count (struct output_block *ob, gcov_type work)
247 {
248 streamer_write_gcov_count_stream (ob->main_stream, work);
249 }
250
251 /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */
252
253 void
254 streamer_write_uhwi_stream (struct lto_output_stream *obs,
255 unsigned HOST_WIDE_INT work)
256 {
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;
262 do
263 {
264 unsigned int byte = (work & 0x7f);
265 work >>= 7;
266 if (work != 0)
267 /* More bytes to follow. */
268 byte |= 0x80;
269
270 *(current_pointer++) = byte;
271 left_in_block--;
272 size++;
273 }
274 while (work != 0 && left_in_block > 0);
275 if (work != 0)
276 {
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;
281 do
282 {
283 unsigned int byte = (work & 0x7f);
284 work >>= 7;
285 if (work != 0)
286 /* More bytes to follow. */
287 byte |= 0x80;
288
289 *(current_pointer++) = byte;
290 left_in_block--;
291 size++;
292 }
293 while (work != 0);
294 }
295 obs->current_pointer = current_pointer;
296 obs->left_in_block = left_in_block;
297 obs->total_size += size;
298 }
299
300
301 /* Write a HOST_WIDE_INT value WORK to OBS. */
302
303 void
304 streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
305 {
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;
312 do
313 {
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)
319 {
320 /* More bits to follow. */
321 work >>= 1;
322 byte |= 0x80;
323 }
324
325 *(current_pointer++) = byte;
326 left_in_block--;
327 size++;
328 }
329 while (more && left_in_block > 0);
330 if (more)
331 {
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;
336 do
337 {
338 unsigned int byte = (work & 0x7f);
339 work >>= 6;
340 more = !(work == 0 || work == -1);
341 if (more)
342 {
343 work >>= 1;
344 byte |= 0x80;
345 }
346
347 *(current_pointer++) = byte;
348 left_in_block--;
349 size++;
350 }
351 while (more);
352 }
353 obs->current_pointer = current_pointer;
354 obs->left_in_block = left_in_block;
355 obs->total_size += size;
356 }
357
358 /* Write a GCOV counter value WORK to OBS. */
359
360 void
361 streamer_write_gcov_count_stream (struct lto_output_stream *obs, gcov_type work)
362 {
363 gcc_assert (work >= 0);
364 gcc_assert ((HOST_WIDE_INT) work == work);
365 streamer_write_hwi_stream (obs, work);
366 }
367
368 /* Write raw DATA of length LEN to the output block OB. */
369
370 void
371 streamer_write_data_stream (struct lto_output_stream *obs, const void *data,
372 size_t len)
373 {
374 while (len)
375 {
376 size_t copy;
377
378 /* No space left. */
379 if (obs->left_in_block == 0)
380 lto_append_block (obs);
381
382 /* Determine how many bytes to copy in this loop. */
383 if (len <= obs->left_in_block)
384 copy = len;
385 else
386 copy = obs->left_in_block;
387
388 /* Copy the data and do bookkeeping. */
389 memcpy (obs->current_pointer, data, copy);
390 obs->current_pointer += copy;
391 obs->total_size += copy;
392 obs->left_in_block -= copy;
393 data = (const char *) data + copy;
394 len -= copy;
395 }
396 }
397