1*e4b17023SJohn Marino /* Routines for restoring various data types from a file stream. This deals
2*e4b17023SJohn Marino with various data types like strings, integers, enums, etc.
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino Copyright 2011 Free Software Foundation, Inc.
5*e4b17023SJohn Marino Contributed by Diego Novillo <dnovillo@google.com>
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino This file is part of GCC.
8*e4b17023SJohn Marino
9*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
10*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
11*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
12*e4b17023SJohn Marino version.
13*e4b17023SJohn Marino
14*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
16*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17*e4b17023SJohn Marino for more details.
18*e4b17023SJohn Marino
19*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
20*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
21*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
22*e4b17023SJohn Marino
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #include "system.h"
25*e4b17023SJohn Marino #include "coretypes.h"
26*e4b17023SJohn Marino #include "diagnostic.h"
27*e4b17023SJohn Marino #include "data-streamer.h"
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino /* Read a string from the string table in DATA_IN using input block
30*e4b17023SJohn Marino IB. Write the length to RLEN. */
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino const char *
string_for_index(struct data_in * data_in,unsigned int loc,unsigned int * rlen)33*e4b17023SJohn Marino string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
34*e4b17023SJohn Marino {
35*e4b17023SJohn Marino struct lto_input_block str_tab;
36*e4b17023SJohn Marino unsigned int len;
37*e4b17023SJohn Marino const char *result;
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino if (!loc)
40*e4b17023SJohn Marino {
41*e4b17023SJohn Marino *rlen = 0;
42*e4b17023SJohn Marino return NULL;
43*e4b17023SJohn Marino }
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino /* Get the string stored at location LOC in DATA_IN->STRINGS. */
46*e4b17023SJohn Marino LTO_INIT_INPUT_BLOCK (str_tab, data_in->strings, loc - 1,
47*e4b17023SJohn Marino data_in->strings_len);
48*e4b17023SJohn Marino len = streamer_read_uhwi (&str_tab);
49*e4b17023SJohn Marino *rlen = len;
50*e4b17023SJohn Marino
51*e4b17023SJohn Marino if (str_tab.p + len > data_in->strings_len)
52*e4b17023SJohn Marino internal_error ("bytecode stream: string too long for the string table");
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino result = (const char *)(data_in->strings + str_tab.p);
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino return result;
57*e4b17023SJohn Marino }
58*e4b17023SJohn Marino
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino /* Read a string from the string table in DATA_IN using input block
61*e4b17023SJohn Marino IB. Write the length to RLEN. */
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino const char *
streamer_read_indexed_string(struct data_in * data_in,struct lto_input_block * ib,unsigned int * rlen)64*e4b17023SJohn Marino streamer_read_indexed_string (struct data_in *data_in,
65*e4b17023SJohn Marino struct lto_input_block *ib, unsigned int *rlen)
66*e4b17023SJohn Marino {
67*e4b17023SJohn Marino return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
68*e4b17023SJohn Marino }
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino /* Read a NULL terminated string from the string table in DATA_IN. */
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino const char *
streamer_read_string(struct data_in * data_in,struct lto_input_block * ib)74*e4b17023SJohn Marino streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
75*e4b17023SJohn Marino {
76*e4b17023SJohn Marino unsigned int len;
77*e4b17023SJohn Marino const char *ptr;
78*e4b17023SJohn Marino
79*e4b17023SJohn Marino ptr = streamer_read_indexed_string (data_in, ib, &len);
80*e4b17023SJohn Marino if (!ptr)
81*e4b17023SJohn Marino return NULL;
82*e4b17023SJohn Marino if (ptr[len - 1] != '\0')
83*e4b17023SJohn Marino internal_error ("bytecode stream: found non-null terminated string");
84*e4b17023SJohn Marino
85*e4b17023SJohn Marino return ptr;
86*e4b17023SJohn Marino }
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino
89*e4b17023SJohn Marino /* Read an unsigned HOST_WIDE_INT number from IB. */
90*e4b17023SJohn Marino
91*e4b17023SJohn Marino unsigned HOST_WIDE_INT
streamer_read_uhwi(struct lto_input_block * ib)92*e4b17023SJohn Marino streamer_read_uhwi (struct lto_input_block *ib)
93*e4b17023SJohn Marino {
94*e4b17023SJohn Marino unsigned HOST_WIDE_INT result = 0;
95*e4b17023SJohn Marino int shift = 0;
96*e4b17023SJohn Marino unsigned HOST_WIDE_INT byte;
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino while (true)
99*e4b17023SJohn Marino {
100*e4b17023SJohn Marino byte = streamer_read_uchar (ib);
101*e4b17023SJohn Marino result |= (byte & 0x7f) << shift;
102*e4b17023SJohn Marino shift += 7;
103*e4b17023SJohn Marino if ((byte & 0x80) == 0)
104*e4b17023SJohn Marino return result;
105*e4b17023SJohn Marino }
106*e4b17023SJohn Marino }
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino
109*e4b17023SJohn Marino /* Read a HOST_WIDE_INT number from IB. */
110*e4b17023SJohn Marino
111*e4b17023SJohn Marino HOST_WIDE_INT
streamer_read_hwi(struct lto_input_block * ib)112*e4b17023SJohn Marino streamer_read_hwi (struct lto_input_block *ib)
113*e4b17023SJohn Marino {
114*e4b17023SJohn Marino HOST_WIDE_INT result = 0;
115*e4b17023SJohn Marino int shift = 0;
116*e4b17023SJohn Marino unsigned HOST_WIDE_INT byte;
117*e4b17023SJohn Marino
118*e4b17023SJohn Marino while (true)
119*e4b17023SJohn Marino {
120*e4b17023SJohn Marino byte = streamer_read_uchar (ib);
121*e4b17023SJohn Marino result |= (byte & 0x7f) << shift;
122*e4b17023SJohn Marino shift += 7;
123*e4b17023SJohn Marino if ((byte & 0x80) == 0)
124*e4b17023SJohn Marino {
125*e4b17023SJohn Marino if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
126*e4b17023SJohn Marino result |= - ((HOST_WIDE_INT)1 << shift);
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino return result;
129*e4b17023SJohn Marino }
130*e4b17023SJohn Marino }
131*e4b17023SJohn Marino }
132