cp/ChangeLog.pph
[official-gcc.git] / gcc / cp / pph-streamer.c
blob18a5e25e4c391bfffd881489c1a04c81f472b6bd
1 /* Routines 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)
10 any later version.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "langhooks.h"
26 #include "tree-iterator.h"
27 #include "tree-pretty-print.h"
28 #include "lto-streamer.h"
29 #include "pph-streamer.h"
30 #include "pph.h"
31 #include "tree-pass.h"
32 #include "version.h"
33 #include "cppbuiltin.h"
35 /* Return true if the given tree T is streamable. */
37 static bool
38 pph_is_streamable (tree t ATTRIBUTE_UNUSED)
40 /* We accept most trees. */
41 return TREE_CODE (t) != SSA_NAME
42 && (TREE_CODE (t) < OMP_PARALLEL
43 || TREE_CODE (t) > OMP_CRITICAL);
47 /* Return true if T can be emitted in the decls table as a reference.
48 This should only handle C++ specific declarations. All others are
49 handled by the LTO streamer directly. */
51 static bool
52 pph_indexable_with_decls_p (tree t)
54 return TREE_CODE (t) == TEMPLATE_DECL;
58 /* Generate a vector of common nodes that should always be streamed as
59 indexes into the streamer cache. These nodes are always built by
60 the front end, so there is no need to emit them. */
62 static VEC(tree,heap) *
63 pph_get_common_nodes (void)
65 unsigned i;
66 VEC(tree,heap) *common_nodes = NULL;
68 for (i = itk_char; i < itk_none; i++)
69 VEC_safe_push (tree, heap, common_nodes, integer_types[i]);
71 for (i = 0; i < TYPE_KIND_LAST; i++)
72 VEC_safe_push (tree, heap, common_nodes, sizetype_tab[i]);
74 /* global_trees[] can have NULL entries in it. Skip them. */
75 for (i = 0; i < TI_MAX; i++)
76 if (global_trees[i])
77 VEC_safe_push (tree, heap, common_nodes, global_trees[i]);
79 /* c_global_trees[] can have NULL entries in it. Skip them. */
80 for (i = 0; i < CTI_MAX; i++)
81 if (c_global_trees[i])
82 VEC_safe_push (tree, heap, common_nodes, c_global_trees[i]);
84 return common_nodes;
88 /* Initialize all the streamer hooks used for streaming ASTs. */
90 static void
91 pph_stream_hooks_init (void)
93 lto_streamer_hooks *h = streamer_hooks_init ();
94 h->name = "C++ AST";
95 h->get_common_nodes = pph_get_common_nodes;
96 h->is_streamable = pph_is_streamable;
97 h->write_tree = pph_stream_write_tree;
98 h->read_tree = pph_stream_read_tree;
99 h->pack_value_fields = pph_stream_pack_value_fields;
100 h->indexable_with_decls_p = pph_indexable_with_decls_p;
101 h->unpack_value_fields = pph_stream_unpack_value_fields;
102 h->alloc_tree = pph_stream_alloc_tree;
103 h->output_tree_header = pph_stream_output_tree_header;
104 h->has_unique_integer_csts_p = true;
108 /* Create a new PPH stream to be stored on the file called NAME.
109 MODE is passed to fopen directly. */
111 pph_stream *
112 pph_stream_open (const char *name, const char *mode)
114 pph_stream *stream;
115 FILE *f;
117 stream = NULL;
118 f = fopen (name, mode);
119 if (f)
121 pph_stream_hooks_init ();
122 stream = XCNEW (pph_stream);
123 stream->file = f;
124 stream->name = xstrdup (name);
125 stream->write_p = (strchr (mode, 'w') != NULL);
126 if (stream->write_p)
127 pph_stream_init_write (stream);
128 else
129 pph_stream_init_read (stream);
132 return stream;
136 /* Close PPH stream STREAM. Write all the ASTs to disk and deallocate
137 all memory used by it. */
139 void
140 pph_stream_close (pph_stream *stream)
142 if (stream->write_p)
143 pph_stream_flush_buffers (stream);
144 fclose (stream->file);
145 stream->file = NULL;
149 /* Data types supported by the PPH tracer. */
150 enum pph_trace_type
152 PPH_TRACE_TREE,
153 PPH_TRACE_REF,
154 PPH_TRACE_UINT,
155 PPH_TRACE_BYTES,
156 PPH_TRACE_STRING,
157 PPH_TRACE_CHAIN,
158 PPH_TRACE_BITPACK
162 /* Print tracing information for STREAM on pph_logfile. DATA is the
163 memory area to display, SIZE is the number of bytes to print, TYPE
164 is the kind of data to print. */
166 static void
167 pph_stream_trace (pph_stream *stream, const void *data, unsigned int nbytes,
168 enum pph_trace_type type)
170 const char *op = (stream->write_p) ? "<<" : ">>";
171 const char *type_s[] = { "tree", "ref", "uint", "bytes", "string", "chain",
172 "bitpack" };
174 if ((type == PPH_TRACE_TREE || type == PPH_TRACE_CHAIN)
175 && !data
176 && flag_pph_tracer <= 3)
177 return;
179 fprintf (pph_logfile, "*** %s: %s%s/%u, value=",
180 stream->name, op, type_s[type], (unsigned) nbytes);
182 switch (type)
184 case PPH_TRACE_TREE:
186 const_tree t = (const_tree) data;
187 if (t)
189 print_generic_expr (pph_logfile, CONST_CAST (union tree_node *, t),
191 fprintf (pph_logfile, ", code=%s", tree_code_name[TREE_CODE (t)]);
193 else
194 fprintf (pph_logfile, "NULL_TREE");
196 break;
198 case PPH_TRACE_REF:
200 const_tree t = (const_tree) data;
201 if (t)
203 print_generic_expr (pph_logfile, CONST_CAST (union tree_node *, t),
205 fprintf (pph_logfile, ", code=%s", tree_code_name[TREE_CODE (t)]);
207 else
208 fprintf (pph_logfile, "NULL_TREE");
210 break;
212 case PPH_TRACE_UINT:
214 unsigned int val = *((const unsigned int *) data);
215 fprintf (pph_logfile, "%u (0x%x)", val, val);
217 break;
219 case PPH_TRACE_BYTES:
221 size_t i;
222 const char *buffer = (const char *) data;
223 for (i = 0; i < MIN (nbytes, 100); i++)
225 if (ISPRINT (buffer[i]))
226 fprintf (pph_logfile, "%c", buffer[i]);
227 else
228 fprintf (pph_logfile, "[0x%02x]", (unsigned int) buffer[i]);
231 break;
233 case PPH_TRACE_STRING:
234 if (data)
235 fprintf (pph_logfile, "%.*s", (int) nbytes, (const char *) data);
236 else
237 fprintf (pph_logfile, "<nil>");
238 break;
240 case PPH_TRACE_CHAIN:
242 const_tree t = (const_tree) data;
243 print_generic_expr (pph_logfile, CONST_CAST (union tree_node *, t),
244 TDF_SLIM);
245 fprintf (pph_logfile, " (%d nodes in chain)", list_length (t));
247 break;
249 case PPH_TRACE_BITPACK:
251 const struct bitpack_d *bp = (const struct bitpack_d *) data;
252 fprintf (pph_logfile, "0x%lx", bp->word);
254 break;
256 default:
257 gcc_unreachable ();
260 fputc ('\n', pph_logfile);
264 /* Show tracing information for T on STREAM. */
266 void
267 pph_stream_trace_tree (pph_stream *stream, tree t, bool ref_p)
269 pph_stream_trace (stream, t, t ? tree_code_size (TREE_CODE (t)) : 0,
270 ref_p ? PPH_TRACE_REF : PPH_TRACE_TREE);
274 /* Show tracing information for VAL on STREAM. */
276 void
277 pph_stream_trace_uint (pph_stream *stream, unsigned int val)
279 pph_stream_trace (stream, &val, sizeof (val), PPH_TRACE_UINT);
283 /* Show tracing information for NBYTES bytes of memory area DATA on
284 STREAM. */
286 void
287 pph_stream_trace_bytes (pph_stream *stream, const void *data, size_t nbytes)
289 pph_stream_trace (stream, data, nbytes, PPH_TRACE_BYTES);
293 /* Show tracing information for S on STREAM. */
295 void
296 pph_stream_trace_string (pph_stream *stream, const char *s)
298 pph_stream_trace (stream, s, s ? strlen (s) : 0, PPH_TRACE_STRING);
302 /* Show tracing information for LEN bytes of S on STREAM. */
304 void
305 pph_stream_trace_string_with_length (pph_stream *stream, const char *s,
306 unsigned int len)
308 pph_stream_trace (stream, s, len, PPH_TRACE_STRING);
312 /* Show tracing information for a tree chain starting with T on STREAM. */
314 void
315 pph_stream_trace_chain (pph_stream *stream, tree t)
317 pph_stream_trace (stream, t, t ? tree_code_size (TREE_CODE (t)) : 0,
318 PPH_TRACE_CHAIN);
322 /* Show tracing information for a bitpack BP on STREAM. */
324 void
325 pph_stream_trace_bitpack (pph_stream *stream, struct bitpack_d *bp)
327 pph_stream_trace (stream, bp, sizeof (*bp), PPH_TRACE_BITPACK);