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)
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/>. */
23 #include "coretypes.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"
31 #include "tree-pass.h"
33 #include "cppbuiltin.h"
35 /* Return true if the given tree T is streamable. */
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. */
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)
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
++)
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
]);
88 /* Initialize all the streamer hooks used for streaming ASTs. */
91 pph_stream_hooks_init (void)
93 lto_streamer_hooks
*h
= streamer_hooks_init ();
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. */
112 pph_stream_open (const char *name
, const char *mode
)
118 f
= fopen (name
, mode
);
121 pph_stream_hooks_init ();
122 stream
= XCNEW (pph_stream
);
124 stream
->name
= xstrdup (name
);
125 stream
->write_p
= (strchr (mode
, 'w') != NULL
);
127 pph_stream_init_write (stream
);
129 pph_stream_init_read (stream
);
136 /* Close PPH stream STREAM. Write all the ASTs to disk and deallocate
137 all memory used by it. */
140 pph_stream_close (pph_stream
*stream
)
143 pph_stream_flush_buffers (stream
);
144 fclose (stream
->file
);
149 /* Data types supported by the PPH tracer. */
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. */
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",
174 if ((type
== PPH_TRACE_TREE
|| type
== PPH_TRACE_CHAIN
)
176 && flag_pph_tracer
<= 3)
179 fprintf (pph_logfile
, "*** %s: %s%s/%u, value=",
180 stream
->name
, op
, type_s
[type
], (unsigned) nbytes
);
186 const_tree t
= (const_tree
) data
;
189 print_generic_expr (pph_logfile
, CONST_CAST (union tree_node
*, t
),
191 fprintf (pph_logfile
, ", code=%s", tree_code_name
[TREE_CODE (t
)]);
194 fprintf (pph_logfile
, "NULL_TREE");
200 const_tree t
= (const_tree
) data
;
203 print_generic_expr (pph_logfile
, CONST_CAST (union tree_node
*, t
),
205 fprintf (pph_logfile
, ", code=%s", tree_code_name
[TREE_CODE (t
)]);
208 fprintf (pph_logfile
, "NULL_TREE");
214 unsigned int val
= *((const unsigned int *) data
);
215 fprintf (pph_logfile
, "%u (0x%x)", val
, val
);
219 case PPH_TRACE_BYTES
:
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
]);
228 fprintf (pph_logfile
, "[0x%02x]", (unsigned int) buffer
[i
]);
233 case PPH_TRACE_STRING
:
235 fprintf (pph_logfile
, "%.*s", (int) nbytes
, (const char *) data
);
237 fprintf (pph_logfile
, "<nil>");
240 case PPH_TRACE_CHAIN
:
242 const_tree t
= (const_tree
) data
;
243 print_generic_expr (pph_logfile
, CONST_CAST (union tree_node
*, t
),
245 fprintf (pph_logfile
, " (%d nodes in chain)", list_length (t
));
249 case PPH_TRACE_BITPACK
:
251 const struct bitpack_d
*bp
= (const struct bitpack_d
*) data
;
252 fprintf (pph_logfile
, "0x%lx", bp
->word
);
260 fputc ('\n', pph_logfile
);
264 /* Show tracing information for T on STREAM. */
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. */
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
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. */
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. */
305 pph_stream_trace_string_with_length (pph_stream
*stream
, const char *s
,
308 pph_stream_trace (stream
, s
, len
, PPH_TRACE_STRING
);
312 /* Show tracing information for a tree chain starting with T on STREAM. */
315 pph_stream_trace_chain (pph_stream
*stream
, tree t
)
317 pph_stream_trace (stream
, t
, t
? tree_code_size (TREE_CODE (t
)) : 0,
322 /* Show tracing information for a bitpack BP on STREAM. */
325 pph_stream_trace_bitpack (pph_stream
*stream
, struct bitpack_d
*bp
)
327 pph_stream_trace (stream
, bp
, sizeof (*bp
), PPH_TRACE_BITPACK
);