1/* Data structures and functions for streaming trees.
2
3   Copyright (C) 2011-2015 Free Software Foundation, Inc.
4   Contributed by Diego Novillo <dnovillo@google.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3.  If not see
20<http://www.gnu.org/licenses/>.  */
21
22#ifndef GCC_TREE_STREAMER_H
23#define GCC_TREE_STREAMER_H
24
25#include "streamer-hooks.h"
26#include "lto-streamer.h"
27#include "data-streamer.h"
28#include "hash-map.h"
29
30/* Cache of pickled nodes.  Used to avoid writing the same node more
31   than once.  The first time a tree node is streamed out, it is
32   entered in this cache.  Subsequent references to the same node are
33   resolved by looking it up in this cache.
34
35   This is used in two ways:
36
37   - On the writing side, the first time T is added to STREAMER_CACHE,
38     a new reference index is created for T and T is emitted on the
39     stream.  If T needs to be emitted again to the stream, instead of
40     pickling it again, the reference index is emitted.
41
42   - On the reading side, the first time T is read from the stream, it
43     is reconstructed in memory and a new reference index created for
44     T.  The reconstructed T is inserted in some array so that when
45     the reference index for T is found in the input stream, it can be
46     used to look up into the array to get the reconstructed T.  */
47
48struct streamer_tree_cache_d
49{
50  /* The mapping between tree nodes and slots into the nodes array.  */
51  hash_map<tree, unsigned> *node_map;
52
53  /* The nodes pickled so far.  */
54  vec<tree> nodes;
55  /* The node hashes (if available).  */
56  vec<hashval_t> hashes;
57
58  /* Next index to assign.  */
59  unsigned next_idx;
60};
61
62/* Return true if tree node EXPR should be streamed as a builtin.  For
63   these nodes, we just emit the class and function code.  */
64static inline bool
65streamer_handle_as_builtin_p (tree expr)
66{
67  return (TREE_CODE (expr) == FUNCTION_DECL
68	  && DECL_IS_BUILTIN (expr)
69	  && (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
70	      || DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD));
71}
72
73/* In tree-streamer-in.c.  */
74tree streamer_read_string_cst (struct data_in *, struct lto_input_block *);
75tree streamer_read_chain (struct lto_input_block *, struct data_in *);
76tree streamer_alloc_tree (struct lto_input_block *, struct data_in *,
77		          enum LTO_tags);
78void streamer_read_tree_body (struct lto_input_block *, struct data_in *, tree);
79tree streamer_get_pickled_tree (struct lto_input_block *, struct data_in *);
80tree streamer_get_builtin_tree (struct lto_input_block *, struct data_in *);
81void streamer_read_tree_bitfields (struct lto_input_block *,
82				   struct data_in *, tree);
83
84/* In tree-streamer-out.c.  */
85void streamer_write_string_cst (struct output_block *,
86				struct lto_output_stream *, tree);
87void streamer_write_chain (struct output_block *, tree, bool);
88void streamer_write_tree_header (struct output_block *, tree);
89void streamer_write_tree_bitfields (struct output_block *, tree);
90void streamer_write_tree_body (struct output_block *, tree, bool);
91void streamer_write_integer_cst (struct output_block *, tree, bool);
92void streamer_write_builtin (struct output_block *, tree);
93
94/* In tree-streamer.c.  */
95extern unsigned char streamer_mode_table[1 << 8];
96void streamer_check_handled_ts_structures (void);
97bool streamer_tree_cache_insert (struct streamer_tree_cache_d *, tree,
98				 hashval_t, unsigned *);
99void streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *, tree,
100				       unsigned);
101void streamer_tree_cache_append (struct streamer_tree_cache_d *, tree,
102				 hashval_t);
103bool streamer_tree_cache_lookup (struct streamer_tree_cache_d *, tree,
104				 unsigned *);
105struct streamer_tree_cache_d *streamer_tree_cache_create (bool, bool, bool);
106void streamer_tree_cache_delete (struct streamer_tree_cache_d *);
107
108/* Return the tree node at slot IX in CACHE.  */
109
110static inline tree
111streamer_tree_cache_get_tree (struct streamer_tree_cache_d *cache, unsigned ix)
112{
113  return cache->nodes[ix];
114}
115
116/* Return the tree hash value at slot IX in CACHE.  */
117
118static inline hashval_t
119streamer_tree_cache_get_hash (struct streamer_tree_cache_d *cache, unsigned ix)
120{
121  return cache->hashes[ix];
122}
123
124static inline void
125bp_pack_machine_mode (struct bitpack_d *bp, machine_mode mode)
126{
127  streamer_mode_table[mode] = 1;
128  bp_pack_enum (bp, machine_mode, 1 << 8, mode);
129}
130
131static inline machine_mode
132bp_unpack_machine_mode (struct bitpack_d *bp)
133{
134  return (machine_mode)
135	   ((struct lto_input_block *)
136	    bp->stream)->mode_table[bp_unpack_enum (bp, machine_mode, 1 << 8)];
137}
138
139#endif  /* GCC_TREE_STREAMER_H  */
140