rebase
[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 2011 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 "data-streamer.h"
27
28 /* Return index used to reference STRING of LEN characters in the string table
29 in OB. The string might or might not include a trailing '\0'.
30 Then put the index onto the INDEX_STREAM.
31 When PERSISTENT is set, the string S is supposed to not change during
32 duration of the OB and thus OB can keep pointer into it. */
33
34 unsigned
35 lto_string_index (struct output_block *ob, const char *s, unsigned int len,
36 bool persistent)
37 {
38 struct string_slot **slot;
39 struct string_slot s_slot;
40
41 s_slot.s = s;
42 s_slot.len = len;
43 s_slot.slot_num = 0;
44
45 slot = (struct string_slot **) htab_find_slot (ob->string_hash_table,
46 &s_slot, INSERT);
47 if (*slot == NULL)
48 {
49 struct lto_output_stream *string_stream = ob->string_stream;
50 unsigned int start = string_stream->total_size;
51 struct string_slot *new_slot = XOBNEW (&ob->obstack, struct string_slot);
52 const char *string;
53
54 if (!persistent)
55 {
56 char *tmp;
57 string = tmp = XOBNEWVEC (&ob->obstack, char, len);
58 memcpy (tmp, s, len);
59 }
60 else
61 string = s;
62
63 new_slot->s = string;
64 new_slot->len = len;
65 new_slot->slot_num = start;
66 *slot = new_slot;
67 lto_output_uleb128_stream (string_stream, len);
68 lto_output_data_stream (string_stream, string, len);
69 return start + 1;
70 }
71 else
72 {
73 struct string_slot *old_slot = *slot;
74 return old_slot->slot_num + 1;
75 }
76 }
77
78
79 /* Output STRING of LEN characters to the string table in OB. The
80 string might or might not include a trailing '\0'. Then put the
81 index onto the INDEX_STREAM.
82 When PERSISTENT is set, the string S is supposed to not change during
83 duration of the OB and thus OB can keep pointer into it. */
84
85 void
86 lto_output_string_with_length (struct output_block *ob,
87 struct lto_output_stream *index_stream,
88 const char *s, unsigned int len, bool persistent)
89 {
90 if (s)
91 lto_output_uleb128_stream (index_stream,
92 lto_string_index (ob, s, len, persistent));
93 else
94 lto_output_1_stream (index_stream, 0);
95 }
96
97
98 /* Output the '\0' terminated STRING to the string
99 table in OB. Then put the index onto the INDEX_STREAM.
100 When PERSISTENT is set, the string S is supposed to not change during
101 duration of the OB and thus OB can keep pointer into it. */
102
103 void
104 lto_output_string (struct output_block *ob,
105 struct lto_output_stream *index_stream,
106 const char *string, bool persistent)
107 {
108 if (string)
109 lto_output_string_with_length (ob, index_stream, string,
110 strlen (string) + 1,
111 persistent);
112 else
113 lto_output_1_stream (index_stream, 0);
114 }
115
116
117 /* Write a zero to the output stream. */
118
119 void
120 output_zero (struct output_block *ob)
121 {
122 lto_output_1_stream (ob->main_stream, 0);
123 }
124
125
126 /* Output an unsigned LEB128 quantity to OB->main_stream. */
127
128 void
129 output_uleb128 (struct output_block *ob, unsigned HOST_WIDE_INT work)
130 {
131 lto_output_uleb128_stream (ob->main_stream, work);
132 }
133
134
135 /* Output a signed LEB128 quantity to OB->main_stream. */
136
137 void
138 output_sleb128 (struct output_block *ob, HOST_WIDE_INT work)
139 {
140 lto_output_sleb128_stream (ob->main_stream, work);
141 }
142
143
144 /* Output an unsigned LEB128 quantity to OBS. */
145
146 void
147 lto_output_uleb128_stream (struct lto_output_stream *obs,
148 unsigned HOST_WIDE_INT work)
149 {
150 do
151 {
152 unsigned int byte = (work & 0x7f);
153 work >>= 7;
154 if (work != 0)
155 /* More bytes to follow. */
156 byte |= 0x80;
157
158 lto_output_1_stream (obs, byte);
159 }
160 while (work != 0);
161 }
162
163
164 /* Identical to output_uleb128_stream above except using unsigned
165 HOST_WIDEST_INT type. For efficiency on host where unsigned HOST_WIDEST_INT
166 is not native, we only use this if we know that HOST_WIDE_INT is not wide
167 enough. */
168
169 void
170 lto_output_widest_uint_uleb128_stream (struct lto_output_stream *obs,
171 unsigned HOST_WIDEST_INT work)
172 {
173 do
174 {
175 unsigned int byte = (work & 0x7f);
176 work >>= 7;
177 if (work != 0)
178 /* More bytes to follow. */
179 byte |= 0x80;
180
181 lto_output_1_stream (obs, byte);
182 }
183 while (work != 0);
184 }
185
186
187 /* Output a signed LEB128 quantity. */
188
189 void
190 lto_output_sleb128_stream (struct lto_output_stream *obs, HOST_WIDE_INT work)
191 {
192 int more, byte;
193
194 do
195 {
196 byte = (work & 0x7f);
197 /* arithmetic shift */
198 work >>= 7;
199 more = !((work == 0 && (byte & 0x40) == 0)
200 || (work == -1 && (byte & 0x40) != 0));
201 if (more)
202 byte |= 0x80;
203
204 lto_output_1_stream (obs, byte);
205 }
206 while (more);
207 }