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