1 /* Support routines and data structures for streaming PPH data.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@google.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_CP_PPH_STREAMER_H
22 #define GCC_CP_PPH_STREAMER_H
24 #include "lto-streamer.h"
28 static const unsigned char PPH_RECORD_START
= 0xff;
29 static const unsigned char PPH_RECORD_END
= 0xfe;
31 /* Number of sections in a PPH file. FIXME, currently only one section
32 is supported. To add more, it will also be necessary to handle
33 section names in pph_get_section_data and pph_free_section_data. */
34 #define PPH_NUM_SECTIONS 1
36 /* String to identify PPH files. Keep it to 7 characters, so it takes
37 exactly 8 bytes in the file. */
38 static const char pph_id_str
[] = "PPH0x42";
40 /* Structure of the header of a PPH file. */
41 typedef struct pph_file_header
{
42 /* Identification string. */
43 char id_str
[sizeof(pph_id_str
)];
45 /* Version information. */
50 /* Size of the string table in bytes. */
51 unsigned int strtab_size
;
55 /* A PPH stream contains all the data and attributes needed to
56 write symbols, declarations and other parsing products to disk. */
57 typedef struct pph_stream
{
58 /* Path name of the PPH file. */
61 /* FILE object associated with it. */
64 /* LTO output block to hold pickled ASTs and references. This is
65 NULL when the file is opened for reading. */
66 struct output_block
*ob
;
67 struct lto_out_decl_state
*out_state
;
68 struct lto_output_stream
*decl_state_stream
;
70 /* LTO input block to read ASTs and references from. This is NULL
71 when the file is opened for writing. */
72 struct lto_input_block
*ib
;
74 /* String tables and other descriptors used by the LTO reading
75 routines. NULL when the file is opened for reading. */
76 struct data_in
*data_in
;
78 /* Array of sections in the PPH file. */
79 struct lto_file_decl_data
**pph_sections
;
81 /* Buffer holding the file contents. FIXME pph, we are bringing
82 the whole file in memory at once. This seems wasteful. */
86 /* Nonzero if the stream was opened for writing. */
87 unsigned int write_p
: 1;
90 /* Filter values for pph_output_chain_filtered. */
91 enum chain_filter
{ NONE
, NO_BUILTINS
};
93 /* In pph-streamer.c. */
94 pph_stream
*pph_stream_open (const char *, const char *);
95 void pph_stream_close (pph_stream
*);
96 void pph_stream_trace_tree (pph_stream
*, tree
, bool ref_p
);
97 void pph_stream_trace_uint (pph_stream
*, unsigned int);
98 void pph_stream_trace_bytes (pph_stream
*, const void *, size_t);
99 void pph_stream_trace_string (pph_stream
*, const char *);
100 void pph_stream_trace_string_with_length (pph_stream
*, const char *, unsigned);
101 void pph_stream_trace_chain (pph_stream
*, tree
);
102 void pph_stream_trace_bitpack (pph_stream
*, struct bitpack_d
*);
104 /* In pph-streamer-out.c. */
105 void pph_stream_flush_buffers (pph_stream
*);
106 void pph_stream_init_write (pph_stream
*);
107 void pph_stream_write_tree (struct output_block
*, tree
, bool ref_p
);
108 void pph_stream_pack_value_fields (struct bitpack_d
*, tree
);
109 void pph_stream_output_tree_header (struct output_block
*, tree
);
110 void pph_output_chain_filtered (pph_stream
*, tree
, bool, enum chain_filter
);
112 /* In name-lookup.c. */
113 struct binding_table_s
;
114 void pph_stream_write_binding_table (pph_stream
*, struct binding_table_s
*,
116 struct binding_table_s
*pph_stream_read_binding_table (pph_stream
*);
118 /* In pph-streamer-in.c. */
119 void pph_stream_init_read (pph_stream
*);
120 void pph_stream_read_tree (struct lto_input_block
*, struct data_in
*, tree
);
121 void pph_stream_unpack_value_fields (struct bitpack_d
*, tree
);
122 tree
pph_stream_alloc_tree (enum tree_code
, struct lto_input_block
*,
124 /* In pph.c. FIXME pph move these to pph-streamer.c. */
125 struct cp_token_cache
;
126 void pth_save_token_cache (struct cp_token_cache
*, pph_stream
*);
127 struct cp_token_cache
*pth_load_token_cache (pph_stream
*);
129 /* Inline functions. */
131 /* Output AST T to STREAM. If REF_P is true, output all the leaves of T
132 as references. This function is the primary interface. */
134 pph_output_tree (pph_stream
*stream
, tree t
, bool ref_p
)
136 if (flag_pph_tracer
>= 1)
137 pph_stream_trace_tree (stream
, t
, ref_p
);
138 lto_output_tree (stream
->ob
, t
, ref_p
);
141 /* Output AST T to STREAM. If REF_P is true, output all the leaves of T
142 as references. This function is an list auxillary routine. */
144 pph_output_tree_lst (pph_stream
*stream
, tree t
, bool ref_p
)
146 if (flag_pph_tracer
>= 2)
147 pph_stream_trace_tree (stream
, t
, ref_p
);
148 lto_output_tree (stream
->ob
, t
, ref_p
);
151 /* Output AST T to STREAM. If REF_P is true, output all the leaves of T
152 as references. This function is an internal auxillary routine. */
154 pph_output_tree_aux (pph_stream
*stream
, tree t
, bool ref_p
)
156 if (flag_pph_tracer
>= 3)
157 pph_stream_trace_tree (stream
, t
, ref_p
);
158 lto_output_tree (stream
->ob
, t
, ref_p
);
161 /* Output AST T to STREAM. If REF_P is true, output a reference to T. */
163 pph_output_tree_or_ref (pph_stream
*stream
, tree t
, bool ref_p
)
165 if (flag_pph_tracer
>= 2)
166 pph_stream_trace_tree (stream
, t
, ref_p
);
167 lto_output_tree_or_ref (stream
->ob
, t
, ref_p
);
170 /* Write an unsigned int VALUE to STREAM. */
172 pph_output_uint (pph_stream
*stream
, unsigned int value
)
174 if (flag_pph_tracer
>= 4)
175 pph_stream_trace_uint (stream
, value
);
176 lto_output_sleb128_stream (stream
->ob
->main_stream
, value
);
179 /* Write an unsigned char VALUE to STREAM. */
181 pph_output_uchar (pph_stream
*stream
, unsigned char value
)
183 if (flag_pph_tracer
>= 4)
184 pph_stream_trace_uint (stream
, value
);
185 lto_output_1_stream (stream
->ob
->main_stream
, value
);
188 /* Write N bytes from P to STREAM. */
190 pph_output_bytes (pph_stream
*stream
, const void *p
, size_t n
)
192 if (flag_pph_tracer
>= 4)
193 pph_stream_trace_bytes (stream
, p
, n
);
194 lto_output_data_stream (stream
->ob
->main_stream
, p
, n
);
197 /* Write string STR to STREAM. */
199 pph_output_string (pph_stream
*stream
, const char *str
)
201 if (flag_pph_tracer
>= 4)
202 pph_stream_trace_string (stream
, str
);
203 lto_output_string (stream
->ob
, stream
->ob
->main_stream
, str
);
206 /* Write string STR of length LEN to STREAM. */
208 pph_output_string_with_length (pph_stream
*stream
, const char *str
,
213 if (flag_pph_tracer
>= 4)
214 pph_stream_trace_string_with_length (stream
, str
, len
);
215 lto_output_string_with_length (stream
->ob
, stream
->ob
->main_stream
,
220 /* lto_output_string_with_length does not handle NULL strings,
221 but lto_output_string does. */
222 if (flag_pph_tracer
>= 4)
223 pph_stream_trace_string (stream
, str
);
224 pph_output_string (stream
, NULL
);
228 /* Write a chain of ASTs to STREAM starting with FIRST. REF_P is true
229 if the nodes should be emitted as references. */
231 pph_output_chain (pph_stream
*stream
, tree first
, bool ref_p
)
233 if (flag_pph_tracer
>= 2)
234 pph_stream_trace_chain (stream
, first
);
235 lto_output_chain (stream
->ob
, first
, ref_p
);
238 /* Write a bitpack BP to STREAM. */
240 pph_output_bitpack (pph_stream
*stream
, struct bitpack_d
*bp
)
242 gcc_assert (stream
->ob
->main_stream
== bp
->stream
);
243 if (flag_pph_tracer
>= 4)
244 pph_stream_trace_bitpack (stream
, bp
);
245 lto_output_bitpack (bp
);
248 /* Read an unsigned HOST_WIDE_INT integer from STREAM. */
249 static inline unsigned int
250 pph_input_uint (pph_stream
*stream
)
252 HOST_WIDE_INT
unsigned n
= lto_input_uleb128 (stream
->ib
);
253 gcc_assert (n
== (unsigned) n
);
254 if (flag_pph_tracer
>= 4)
255 pph_stream_trace_uint (stream
, n
);
259 /* Read an unsigned char VALUE to STREAM. */
260 static inline unsigned char
261 pph_input_uchar (pph_stream
*stream
)
263 unsigned char n
= lto_input_1_unsigned (stream
->ib
);
264 if (flag_pph_tracer
>= 4)
265 pph_stream_trace_uint (stream
, n
);
269 /* Read N bytes from STREAM into P. The caller is responsible for
270 allocating a sufficiently large buffer. */
272 pph_input_bytes (pph_stream
*stream
, void *p
, size_t n
)
274 lto_input_data_block (stream
->ib
, p
, n
);
275 if (flag_pph_tracer
>= 4)
276 pph_stream_trace_bytes (stream
, p
, n
);
279 /* Read and return a string of up to MAX characters from STREAM.
280 The caller is responsible for freeing the memory allocated
283 static inline const char *
284 pph_input_string (pph_stream
*stream
)
286 const char *s
= lto_input_string (stream
->data_in
, stream
->ib
);
287 if (flag_pph_tracer
>= 4)
288 pph_stream_trace_string (stream
, s
);
292 /* Load an AST from STREAM. Return the corresponding tree. */
294 pph_input_tree (pph_stream
*stream
)
296 tree t
= lto_input_tree (stream
->ib
, stream
->data_in
);
297 if (flag_pph_tracer
>= 4)
298 pph_stream_trace_tree (stream
, t
, false); /* FIXME pph: always false? */
302 /* Read a chain of ASTs from STREAM. */
304 pph_input_chain (pph_stream
*stream
)
306 tree t
= lto_input_chain (stream
->ib
, stream
->data_in
);
307 if (flag_pph_tracer
>= 2)
308 pph_stream_trace_chain (stream
, t
);
312 /* Read a bitpack from STREAM. */
313 static inline struct bitpack_d
314 pph_input_bitpack (pph_stream
*stream
)
316 struct bitpack_d bp
= lto_input_bitpack (stream
->ib
);
317 if (flag_pph_tracer
>= 4)
318 pph_stream_trace_bitpack (stream
, &bp
);
322 #endif /* GCC_CP_PPH_STREAMER_H */