1/* Routines for restoring various data types from 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
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along 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 "diagnostic.h"
27#include "hash-set.h"
28#include "machmode.h"
29#include "vec.h"
30#include "double-int.h"
31#include "input.h"
32#include "alias.h"
33#include "symtab.h"
34#include "options.h"
35#include "wide-int.h"
36#include "inchash.h"
37#include "tree.h"
38#include "fold-const.h"
39#include "predict.h"
40#include "vec.h"
41#include "tm.h"
42#include "hard-reg-set.h"
43#include "input.h"
44#include "function.h"
45#include "basic-block.h"
46#include "tree-ssa-alias.h"
47#include "internal-fn.h"
48#include "gimple-expr.h"
49#include "is-a.h"
50#include "gimple.h"
51#include "hash-map.h"
52#include "plugin-api.h"
53#include "ipa-ref.h"
54#include "cgraph.h"
55#include "data-streamer.h"
56
57/* Read a string from the string table in DATA_IN using input block
58   IB.  Write the length to RLEN.  */
59
60static const char *
61string_for_index (struct data_in *data_in, unsigned int loc, unsigned int *rlen)
62{
63  unsigned int len;
64  const char *result;
65
66  if (!loc)
67    {
68      *rlen = 0;
69      return NULL;
70    }
71
72  /* Get the string stored at location LOC in DATA_IN->STRINGS.  */
73  lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
74  len = streamer_read_uhwi (&str_tab);
75  *rlen = len;
76
77  if (str_tab.p + len > data_in->strings_len)
78    internal_error ("bytecode stream: string too long for the string table");
79
80  result = (const char *)(data_in->strings + str_tab.p);
81
82  return result;
83}
84
85
86/* Read a string from the string table in DATA_IN using input block
87   IB.  Write the length to RLEN.  */
88
89const char *
90streamer_read_indexed_string (struct data_in *data_in,
91			      struct lto_input_block *ib, unsigned int *rlen)
92{
93  return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
94}
95
96
97/* Read a NULL terminated string from the string table in DATA_IN.  */
98
99const char *
100streamer_read_string (struct data_in *data_in, struct lto_input_block *ib)
101{
102  unsigned int len;
103  const char *ptr;
104
105  ptr = streamer_read_indexed_string (data_in, ib, &len);
106  if (!ptr)
107    return NULL;
108  if (ptr[len - 1] != '\0')
109    internal_error ("bytecode stream: found non-null terminated string");
110
111  return ptr;
112}
113
114
115/* Read a string from the string table in DATA_IN using the bitpack BP.
116   Write the length to RLEN.  */
117
118const char *
119bp_unpack_indexed_string (struct data_in *data_in,
120			  struct bitpack_d *bp, unsigned int *rlen)
121{
122  return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
123}
124
125
126/* Read a NULL terminated string from the string table in DATA_IN.  */
127
128const char *
129bp_unpack_string (struct data_in *data_in, struct bitpack_d *bp)
130{
131  unsigned int len;
132  const char *ptr;
133
134  ptr = bp_unpack_indexed_string (data_in, bp, &len);
135  if (!ptr)
136    return NULL;
137  if (ptr[len - 1] != '\0')
138    internal_error ("bytecode stream: found non-null terminated string");
139
140  return ptr;
141}
142
143
144/* Read an unsigned HOST_WIDE_INT number from IB.  */
145
146unsigned HOST_WIDE_INT
147streamer_read_uhwi (struct lto_input_block *ib)
148{
149  unsigned HOST_WIDE_INT result;
150  int shift;
151  unsigned HOST_WIDE_INT byte;
152  unsigned int p = ib->p;
153  unsigned int len = ib->len;
154
155  const char *data = ib->data;
156  result = data[p++];
157  if ((result & 0x80) != 0)
158    {
159      result &= 0x7f;
160      shift = 7;
161      do
162	{
163	  byte = data[p++];
164	  result |= (byte & 0x7f) << shift;
165	  shift += 7;
166	}
167      while ((byte & 0x80) != 0);
168    }
169
170  /* We check for section overrun after the fact for performance reason.  */
171  if (p > len)
172    lto_section_overrun (ib);
173
174  ib->p = p;
175  return result;
176}
177
178
179/* Read a HOST_WIDE_INT number from IB.  */
180
181HOST_WIDE_INT
182streamer_read_hwi (struct lto_input_block *ib)
183{
184  HOST_WIDE_INT result = 0;
185  int shift = 0;
186  unsigned HOST_WIDE_INT byte;
187
188  while (true)
189    {
190      byte = streamer_read_uchar (ib);
191      result |= (byte & 0x7f) << shift;
192      shift += 7;
193      if ((byte & 0x80) == 0)
194	{
195	  if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
196	    result |= - (HOST_WIDE_INT_1U << shift);
197
198	  return result;
199	}
200    }
201}
202
203/* Read gcov_type value from IB.  */
204
205gcov_type
206streamer_read_gcov_count (struct lto_input_block *ib)
207{
208  gcov_type ret = streamer_read_hwi (ib);
209  gcc_assert (ret >= 0);
210  return ret;
211}
212