xref: /dflybsd-src/contrib/gcc-8.0/gcc/data-streamer-in.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Routines for restoring various data types from 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 /* Read a string from the string table in DATA_IN using input block
33*38fd1498Szrj    IB.  Write the length to RLEN.  */
34*38fd1498Szrj 
35*38fd1498Szrj static const char *
string_for_index(struct data_in * data_in,unsigned int loc,unsigned int * rlen)36*38fd1498Szrj string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
37*38fd1498Szrj {
38*38fd1498Szrj   unsigned int len;
39*38fd1498Szrj   const char *result;
40*38fd1498Szrj 
41*38fd1498Szrj   if (!loc)
42*38fd1498Szrj     {
43*38fd1498Szrj       *rlen = 0;
44*38fd1498Szrj       return NULL;
45*38fd1498Szrj     }
46*38fd1498Szrj 
47*38fd1498Szrj   /* Get the string stored at location LOC in DATA_IN->STRINGS.  */
48*38fd1498Szrj   lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
49*38fd1498Szrj   len = streamer_read_uhwi (&str_tab);
50*38fd1498Szrj   *rlen = len;
51*38fd1498Szrj 
52*38fd1498Szrj   if (str_tab.p + len > data_in->strings_len)
53*38fd1498Szrj     internal_error ("bytecode stream: string too long for the string table");
54*38fd1498Szrj 
55*38fd1498Szrj   result = (const char *)(data_in->strings + str_tab.p);
56*38fd1498Szrj 
57*38fd1498Szrj   return result;
58*38fd1498Szrj }
59*38fd1498Szrj 
60*38fd1498Szrj 
61*38fd1498Szrj /* Read a string from the string table in DATA_IN using input block
62*38fd1498Szrj    IB.  Write the length to RLEN.  */
63*38fd1498Szrj 
64*38fd1498Szrj const char *
streamer_read_indexed_string(struct data_in * data_in,struct lto_input_block * ib,unsigned int * rlen)65*38fd1498Szrj streamer_read_indexed_string (struct data_in *data_in,
66*38fd1498Szrj 			      struct lto_input_block *ib, unsigned int *rlen)
67*38fd1498Szrj {
68*38fd1498Szrj   return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
69*38fd1498Szrj }
70*38fd1498Szrj 
71*38fd1498Szrj 
72*38fd1498Szrj /* Read a NULL terminated string from the string table in DATA_IN.  */
73*38fd1498Szrj 
74*38fd1498Szrj const char *
streamer_read_string(struct data_in * data_in,struct lto_input_block * ib)75*38fd1498Szrj streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
76*38fd1498Szrj {
77*38fd1498Szrj   unsigned int len;
78*38fd1498Szrj   const char *ptr;
79*38fd1498Szrj 
80*38fd1498Szrj   ptr = streamer_read_indexed_string (data_in, ib, &len);
81*38fd1498Szrj   if (!ptr)
82*38fd1498Szrj     return NULL;
83*38fd1498Szrj   if (ptr[len - 1] != '\0')
84*38fd1498Szrj     internal_error ("bytecode stream: found non-null terminated string");
85*38fd1498Szrj 
86*38fd1498Szrj   return ptr;
87*38fd1498Szrj }
88*38fd1498Szrj 
89*38fd1498Szrj 
90*38fd1498Szrj /* Read a string from the string table in DATA_IN using the bitpack BP.
91*38fd1498Szrj    Write the length to RLEN.  */
92*38fd1498Szrj 
93*38fd1498Szrj const char *
bp_unpack_indexed_string(struct data_in * data_in,struct bitpack_d * bp,unsigned int * rlen)94*38fd1498Szrj bp_unpack_indexed_string (struct data_in *data_in,
95*38fd1498Szrj 			  struct bitpack_d *bp, unsigned int *rlen)
96*38fd1498Szrj {
97*38fd1498Szrj   return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
98*38fd1498Szrj }
99*38fd1498Szrj 
100*38fd1498Szrj 
101*38fd1498Szrj /* Read a NULL terminated string from the string table in DATA_IN.  */
102*38fd1498Szrj 
103*38fd1498Szrj const char *
bp_unpack_string(struct data_in * data_in,struct bitpack_d * bp)104*38fd1498Szrj bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
105*38fd1498Szrj {
106*38fd1498Szrj   unsigned int len;
107*38fd1498Szrj   const char *ptr;
108*38fd1498Szrj 
109*38fd1498Szrj   ptr = bp_unpack_indexed_string (data_in, bp, &len);
110*38fd1498Szrj   if (!ptr)
111*38fd1498Szrj     return NULL;
112*38fd1498Szrj   if (ptr[len - 1] != '\0')
113*38fd1498Szrj     internal_error ("bytecode stream: found non-null terminated string");
114*38fd1498Szrj 
115*38fd1498Szrj   return ptr;
116*38fd1498Szrj }
117*38fd1498Szrj 
118*38fd1498Szrj 
119*38fd1498Szrj /* Read an unsigned HOST_WIDE_INT number from IB.  */
120*38fd1498Szrj 
121*38fd1498Szrj unsigned HOST_WIDE_INT
streamer_read_uhwi(struct lto_input_block * ib)122*38fd1498Szrj streamer_read_uhwi (struct lto_input_block *ib)
123*38fd1498Szrj {
124*38fd1498Szrj   unsigned HOST_WIDE_INT result;
125*38fd1498Szrj   int shift;
126*38fd1498Szrj   unsigned HOST_WIDE_INT byte;
127*38fd1498Szrj   unsigned int p = ib->p;
128*38fd1498Szrj   unsigned int len = ib->len;
129*38fd1498Szrj 
130*38fd1498Szrj   const char *data = ib->data;
131*38fd1498Szrj   result = data[p++];
132*38fd1498Szrj   if ((result & 0x80) != 0)
133*38fd1498Szrj     {
134*38fd1498Szrj       result &= 0x7f;
135*38fd1498Szrj       shift = 7;
136*38fd1498Szrj       do
137*38fd1498Szrj 	{
138*38fd1498Szrj 	  byte = data[p++];
139*38fd1498Szrj 	  result |= (byte & 0x7f) << shift;
140*38fd1498Szrj 	  shift += 7;
141*38fd1498Szrj 	}
142*38fd1498Szrj       while ((byte & 0x80) != 0);
143*38fd1498Szrj     }
144*38fd1498Szrj 
145*38fd1498Szrj   /* We check for section overrun after the fact for performance reason.  */
146*38fd1498Szrj   if (p > len)
147*38fd1498Szrj     lto_section_overrun (ib);
148*38fd1498Szrj 
149*38fd1498Szrj   ib->p = p;
150*38fd1498Szrj   return result;
151*38fd1498Szrj }
152*38fd1498Szrj 
153*38fd1498Szrj 
154*38fd1498Szrj /* Read a HOST_WIDE_INT number from IB.  */
155*38fd1498Szrj 
156*38fd1498Szrj HOST_WIDE_INT
streamer_read_hwi(struct lto_input_block * ib)157*38fd1498Szrj streamer_read_hwi (struct lto_input_block *ib)
158*38fd1498Szrj {
159*38fd1498Szrj   HOST_WIDE_INT result = 0;
160*38fd1498Szrj   int shift = 0;
161*38fd1498Szrj   unsigned HOST_WIDE_INT byte;
162*38fd1498Szrj 
163*38fd1498Szrj   while (true)
164*38fd1498Szrj     {
165*38fd1498Szrj       byte = streamer_read_uchar (ib);
166*38fd1498Szrj       result |= (byte & 0x7f) << shift;
167*38fd1498Szrj       shift += 7;
168*38fd1498Szrj       if ((byte & 0x80) == 0)
169*38fd1498Szrj 	{
170*38fd1498Szrj 	  if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
171*38fd1498Szrj 	    result |= - (HOST_WIDE_INT_1U << shift);
172*38fd1498Szrj 
173*38fd1498Szrj 	  return result;
174*38fd1498Szrj 	}
175*38fd1498Szrj     }
176*38fd1498Szrj }
177*38fd1498Szrj 
178*38fd1498Szrj /* Read gcov_type value from IB.  */
179*38fd1498Szrj 
180*38fd1498Szrj gcov_type
streamer_read_gcov_count(struct lto_input_block * ib)181*38fd1498Szrj streamer_read_gcov_count (struct lto_input_block *ib)
182*38fd1498Szrj {
183*38fd1498Szrj   gcov_type ret = streamer_read_hwi (ib);
184*38fd1498Szrj   return ret;
185*38fd1498Szrj }
186*38fd1498Szrj 
187*38fd1498Szrj /* Read the physical representation of a wide_int val from
188*38fd1498Szrj    input block IB.  */
189*38fd1498Szrj 
190*38fd1498Szrj wide_int
streamer_read_wide_int(struct lto_input_block * ib)191*38fd1498Szrj streamer_read_wide_int (struct lto_input_block *ib)
192*38fd1498Szrj {
193*38fd1498Szrj   HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
194*38fd1498Szrj   int i;
195*38fd1498Szrj   int prec = streamer_read_uhwi (ib);
196*38fd1498Szrj   int len = streamer_read_uhwi (ib);
197*38fd1498Szrj   for (i = 0; i < len; i++)
198*38fd1498Szrj     a[i] = streamer_read_hwi (ib);
199*38fd1498Szrj   return wide_int::from_array (a, len, prec);
200*38fd1498Szrj }
201*38fd1498Szrj 
202*38fd1498Szrj /* Read the physical representation of a widest_int val from
203*38fd1498Szrj    input block IB.  */
204*38fd1498Szrj 
205*38fd1498Szrj widest_int
streamer_read_widest_int(struct lto_input_block * ib)206*38fd1498Szrj streamer_read_widest_int (struct lto_input_block *ib)
207*38fd1498Szrj {
208*38fd1498Szrj   HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
209*38fd1498Szrj   int i;
210*38fd1498Szrj   int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
211*38fd1498Szrj   int len = streamer_read_uhwi (ib);
212*38fd1498Szrj   for (i = 0; i < len; i++)
213*38fd1498Szrj     a[i] = streamer_read_hwi (ib);
214*38fd1498Szrj   return widest_int::from_array (a, len);
215*38fd1498Szrj }
216*38fd1498Szrj 
217