]>
Commit | Line | Data |
---|---|---|
2541503d | 1 | /* Routines for saving various data types to a file stream. This deals |
2 | with various data types like strings, integers, enums, etc. | |
3 | ||
71e45bc2 | 4 | Copyright 2011, 2012 Free Software Foundation, Inc. |
2541503d | 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 | |
7f385784 | 35 | streamer_string_index (struct output_block *ob, const char *s, unsigned int len, |
36 | bool persistent) | |
2541503d | 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; | |
7f385784 | 67 | streamer_write_uhwi_stream (string_stream, len); |
2541503d | 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 | |
7f385784 | 86 | streamer_write_string_with_length (struct output_block *ob, |
87 | struct lto_output_stream *index_stream, | |
88 | const char *s, unsigned int len, | |
89 | bool persistent) | |
2541503d | 90 | { |
91 | if (s) | |
7f385784 | 92 | streamer_write_uhwi_stream (index_stream, |
93 | streamer_string_index (ob, s, len, persistent)); | |
2541503d | 94 | else |
7f385784 | 95 | streamer_write_char_stream (index_stream, 0); |
2541503d | 96 | } |
97 | ||
98 | ||
99 | /* Output the '\0' terminated STRING to the string | |
100 | table in OB. Then put the index onto the INDEX_STREAM. | |
101 | When PERSISTENT is set, the string S is supposed to not change during | |
102 | duration of the OB and thus OB can keep pointer into it. */ | |
103 | ||
104 | void | |
7f385784 | 105 | streamer_write_string (struct output_block *ob, |
106 | struct lto_output_stream *index_stream, | |
107 | const char *string, bool persistent) | |
2541503d | 108 | { |
109 | if (string) | |
7f385784 | 110 | streamer_write_string_with_length (ob, index_stream, string, |
111 | strlen (string) + 1, | |
112 | persistent); | |
2541503d | 113 | else |
7f385784 | 114 | streamer_write_char_stream (index_stream, 0); |
2541503d | 115 | } |
116 | ||
117 | ||
07de37ab | 118 | /* Output STRING of LEN characters to the string table in OB. Then |
119 | put the index into BP. | |
120 | When PERSISTENT is set, the string S is supposed to not change during | |
121 | duration of the OB and thus OB can keep pointer into it. */ | |
122 | ||
123 | void | |
124 | bp_pack_string_with_length (struct output_block *ob, struct bitpack_d *bp, | |
125 | const char *s, unsigned int len, bool persistent) | |
126 | { | |
127 | unsigned index = 0; | |
128 | if (s) | |
129 | index = streamer_string_index (ob, s, len, persistent); | |
130 | bp_pack_var_len_unsigned (bp, index); | |
131 | } | |
132 | ||
133 | ||
134 | /* Output the '\0' terminated STRING to the string | |
135 | table in OB. Then put the index onto the bitpack BP. | |
136 | When PERSISTENT is set, the string S is supposed to not change during | |
137 | duration of the OB and thus OB can keep pointer into it. */ | |
138 | ||
139 | void | |
140 | bp_pack_string (struct output_block *ob, struct bitpack_d *bp, | |
141 | const char *s, bool persistent) | |
142 | { | |
143 | unsigned index = 0; | |
144 | if (s) | |
145 | index = streamer_string_index (ob, s, strlen (s) + 1, persistent); | |
146 | bp_pack_var_len_unsigned (bp, index); | |
147 | } | |
148 | ||
149 | ||
150 | ||
2541503d | 151 | /* Write a zero to the output stream. */ |
152 | ||
153 | void | |
7f385784 | 154 | streamer_write_zero (struct output_block *ob) |
2541503d | 155 | { |
7f385784 | 156 | streamer_write_char_stream (ob->main_stream, 0); |
2541503d | 157 | } |
158 | ||
159 | ||
7f385784 | 160 | /* Write an unsigned HOST_WIDE_INT value WORK to OB->main_stream. */ |
2541503d | 161 | |
162 | void | |
7f385784 | 163 | streamer_write_uhwi (struct output_block *ob, unsigned HOST_WIDE_INT work) |
2541503d | 164 | { |
7f385784 | 165 | streamer_write_uhwi_stream (ob->main_stream, work); |
2541503d | 166 | } |
167 | ||
168 | ||
7f385784 | 169 | /* Write a HOST_WIDE_INT value WORK to OB->main_stream. */ |
2541503d | 170 | |
171 | void | |
7f385784 | 172 | streamer_write_hwi (struct output_block *ob, HOST_WIDE_INT work) |
2541503d | 173 | { |
7f385784 | 174 | streamer_write_hwi_stream (ob->main_stream, work); |
2541503d | 175 | } |
176 | ||
177 | ||
7f385784 | 178 | /* Write an unsigned HOST_WIDE_INT value WORK to OBS. */ |
2541503d | 179 | |
180 | void | |
7f385784 | 181 | streamer_write_uhwi_stream (struct lto_output_stream *obs, |
182 | unsigned HOST_WIDE_INT work) | |
2541503d | 183 | { |
184 | do | |
185 | { | |
186 | unsigned int byte = (work & 0x7f); | |
187 | work >>= 7; | |
188 | if (work != 0) | |
189 | /* More bytes to follow. */ | |
190 | byte |= 0x80; | |
191 | ||
7f385784 | 192 | streamer_write_char_stream (obs, byte); |
2541503d | 193 | } |
194 | while (work != 0); | |
195 | } | |
196 | ||
197 | ||
7f385784 | 198 | /* Write a HOST_WIDE_INT value WORK to OBS. */ |
2541503d | 199 | |
200 | void | |
7f385784 | 201 | streamer_write_hwi_stream (struct lto_output_stream *obs, HOST_WIDE_INT work) |
2541503d | 202 | { |
203 | int more, byte; | |
204 | ||
205 | do | |
206 | { | |
207 | byte = (work & 0x7f); | |
208 | /* arithmetic shift */ | |
209 | work >>= 7; | |
210 | more = !((work == 0 && (byte & 0x40) == 0) | |
211 | || (work == -1 && (byte & 0x40) != 0)); | |
212 | if (more) | |
213 | byte |= 0x80; | |
214 | ||
7f385784 | 215 | streamer_write_char_stream (obs, byte); |
2541503d | 216 | } |
217 | while (more); | |
218 | } |