Merge in trunk.
[platform/upstream/linaro-gcc.git] / gcc / tree-streamer-in.c
1 /* Routines for reading trees from a file stream.
2
3    Copyright (C) 2011-2013 Free Software Foundation, Inc.
4    Contributed by Diego Novillo <dnovillo@google.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "tree-streamer.h"
29 #include "data-streamer.h"
30 #include "streamer-hooks.h"
31 #include "lto-streamer.h"
32
33 /* Read a STRING_CST from the string table in DATA_IN using input
34    block IB.  */
35
36 tree
37 streamer_read_string_cst (struct data_in *data_in, struct lto_input_block *ib)
38 {
39   unsigned int len;
40   const char * ptr;
41
42   ptr = streamer_read_indexed_string (data_in, ib, &len);
43   if (!ptr)
44     return NULL;
45   return build_string (len, ptr);
46 }
47
48
49 /* Read an IDENTIFIER from the string table in DATA_IN using input
50    block IB.  */
51
52 static tree
53 input_identifier (struct data_in *data_in, struct lto_input_block *ib)
54 {
55   unsigned int len;
56   const char *ptr;
57
58   ptr = streamer_read_indexed_string (data_in, ib, &len);
59   if (!ptr)
60     return NULL;
61   return get_identifier_with_length (ptr, len);
62 }
63
64
65 /* Read a chain of tree nodes from input block IB. DATA_IN contains
66    tables and descriptors for the file being read.  */
67
68 tree
69 streamer_read_chain (struct lto_input_block *ib, struct data_in *data_in)
70 {
71   tree first, prev, curr;
72
73   /* The chain is written as NULL terminated list of trees.  */
74   first = prev = NULL_TREE;
75   do
76     {
77       curr = stream_read_tree (ib, data_in);
78       if (prev)
79         TREE_CHAIN (prev) = curr;
80       else
81         first = curr;
82
83       prev = curr;
84     }
85   while (curr);
86
87   return first;
88 }
89
90
91 /* Unpack all the non-pointer fields of the TS_BASE structure of
92    expression EXPR from bitpack BP.  */
93
94 static void
95 unpack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
96 {
97   /* Note that the code for EXPR has already been unpacked to create EXPR in
98      streamer_alloc_tree.  */
99   if (!TYPE_P (expr))
100     {
101       TREE_SIDE_EFFECTS (expr) = (unsigned) bp_unpack_value (bp, 1);
102       TREE_CONSTANT (expr) = (unsigned) bp_unpack_value (bp, 1);
103       TREE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
104
105       /* TREE_PUBLIC is used on types to indicate that the type
106          has a TYPE_CACHED_VALUES vector.  This is not streamed out,
107          so we skip it here.  */
108       TREE_PUBLIC (expr) = (unsigned) bp_unpack_value (bp, 1);
109     }
110   else
111     bp_unpack_value (bp, 4);
112   TREE_ADDRESSABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
113   TREE_THIS_VOLATILE (expr) = (unsigned) bp_unpack_value (bp, 1);
114   if (DECL_P (expr))
115     DECL_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
116   else if (TYPE_P (expr))
117     TYPE_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
118   else
119     bp_unpack_value (bp, 1);
120   TREE_ASM_WRITTEN (expr) = (unsigned) bp_unpack_value (bp, 1);
121   if (TYPE_P (expr))
122     TYPE_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
123   else
124     TREE_NO_WARNING (expr) = (unsigned) bp_unpack_value (bp, 1);
125   TREE_NOTHROW (expr) = (unsigned) bp_unpack_value (bp, 1);
126   TREE_STATIC (expr) = (unsigned) bp_unpack_value (bp, 1);
127   if (TREE_CODE (expr) != TREE_BINFO)
128     TREE_PRIVATE (expr) = (unsigned) bp_unpack_value (bp, 1);
129   TREE_PROTECTED (expr) = (unsigned) bp_unpack_value (bp, 1);
130   TREE_DEPRECATED (expr) = (unsigned) bp_unpack_value (bp, 1);
131   if (TYPE_P (expr))
132     {
133       TYPE_SATURATING (expr) = (unsigned) bp_unpack_value (bp, 1);
134       TYPE_ADDR_SPACE (expr) = (unsigned) bp_unpack_value (bp, 8);
135     }
136   else if (TREE_CODE (expr) == SSA_NAME)
137     SSA_NAME_IS_DEFAULT_DEF (expr) = (unsigned) bp_unpack_value (bp, 1);
138   else
139     bp_unpack_value (bp, 1);
140 }
141
142
143 /* Unpack all the non-pointer fields of the TS_INT_CST structure of
144    expression EXPR from bitpack BP.  */
145
146 static void
147 unpack_ts_int_cst_value_fields (struct bitpack_d *bp, tree expr)
148 {
149   int i;
150   for (i = 0; i < TREE_INT_CST_NUNITS (expr); i++)
151     TREE_INT_CST_ELT (expr, i) = bp_unpack_var_len_int (bp);
152 }
153
154
155 /* Unpack all the non-pointer fields of the TS_REAL_CST structure of
156    expression EXPR from bitpack BP.  */
157
158 static void
159 unpack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
160 {
161   unsigned i;
162   REAL_VALUE_TYPE r;
163   REAL_VALUE_TYPE *rp;
164
165   r.cl = (unsigned) bp_unpack_value (bp, 2);
166   r.decimal = (unsigned) bp_unpack_value (bp, 1);
167   r.sign = (unsigned) bp_unpack_value (bp, 1);
168   r.signalling = (unsigned) bp_unpack_value (bp, 1);
169   r.canonical = (unsigned) bp_unpack_value (bp, 1);
170   r.uexp = (unsigned) bp_unpack_value (bp, EXP_BITS);
171   for (i = 0; i < SIGSZ; i++)
172     r.sig[i] = (unsigned long) bp_unpack_value (bp, HOST_BITS_PER_LONG);
173
174   rp = ggc_alloc_real_value ();
175   memcpy (rp, &r, sizeof (REAL_VALUE_TYPE));
176   TREE_REAL_CST_PTR (expr) = rp;
177 }
178
179
180 /* Unpack all the non-pointer fields of the TS_FIXED_CST structure of
181    expression EXPR from bitpack BP.  */
182
183 static void
184 unpack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
185 {
186   FIXED_VALUE_TYPE *fp = ggc_alloc_fixed_value ();
187   fp->mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
188   fp->data.low = bp_unpack_var_len_int (bp);
189   fp->data.high = bp_unpack_var_len_int (bp);
190   TREE_FIXED_CST_PTR (expr) = fp;
191 }
192
193 /* Unpack all the non-pointer fields of the TS_DECL_COMMON structure
194    of expression EXPR from bitpack BP.  */
195
196 static void
197 unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
198 {
199   DECL_MODE (expr) = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
200   DECL_NONLOCAL (expr) = (unsigned) bp_unpack_value (bp, 1);
201   DECL_VIRTUAL_P (expr) = (unsigned) bp_unpack_value (bp, 1);
202   DECL_IGNORED_P (expr) = (unsigned) bp_unpack_value (bp, 1);
203   DECL_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
204   DECL_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
205   DECL_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
206   DECL_PRESERVE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
207   DECL_EXTERNAL (expr) = (unsigned) bp_unpack_value (bp, 1);
208   DECL_GIMPLE_REG_P (expr) = (unsigned) bp_unpack_value (bp, 1);
209   DECL_ALIGN (expr) = (unsigned) bp_unpack_var_len_unsigned (bp);
210
211   if (TREE_CODE (expr) == LABEL_DECL)
212     {
213       EH_LANDING_PAD_NR (expr) = (int) bp_unpack_var_len_unsigned (bp);
214
215       /* Always assume an initial value of -1 for LABEL_DECL_UID to
216          force gimple_set_bb to recreate label_to_block_map.  */
217       LABEL_DECL_UID (expr) = -1;
218     }
219
220   if (TREE_CODE (expr) == FIELD_DECL)
221     {
222       DECL_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
223       DECL_NONADDRESSABLE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
224       expr->decl_common.off_align = bp_unpack_value (bp, 8);
225     }
226
227   if (TREE_CODE (expr) == VAR_DECL)
228     {
229       DECL_HAS_DEBUG_EXPR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
230       DECL_NONLOCAL_FRAME (expr) = (unsigned) bp_unpack_value (bp, 1);
231     }
232
233   if (TREE_CODE (expr) == RESULT_DECL
234       || TREE_CODE (expr) == PARM_DECL
235       || TREE_CODE (expr) == VAR_DECL)
236     {
237       DECL_BY_REFERENCE (expr) = (unsigned) bp_unpack_value (bp, 1);
238       if (TREE_CODE (expr) == VAR_DECL
239           || TREE_CODE (expr) == PARM_DECL)
240         DECL_HAS_VALUE_EXPR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
241     }
242 }
243
244
245 /* Unpack all the non-pointer fields of the TS_DECL_WRTL structure
246    of expression EXPR from bitpack BP.  */
247
248 static void
249 unpack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
250 {
251   DECL_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
252 }
253
254
255 /* Unpack all the non-pointer fields of the TS_DECL_WITH_VIS structure
256    of expression EXPR from bitpack BP.  */
257
258 static void
259 unpack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
260 {
261   DECL_COMMON (expr) = (unsigned) bp_unpack_value (bp, 1);
262   DECL_DLLIMPORT_P (expr) = (unsigned) bp_unpack_value (bp, 1);
263   DECL_WEAK (expr) = (unsigned) bp_unpack_value (bp, 1);
264   DECL_SEEN_IN_BIND_EXPR_P (expr) = (unsigned) bp_unpack_value (bp,  1);
265   DECL_COMDAT (expr) = (unsigned) bp_unpack_value (bp,  1);
266   DECL_VISIBILITY (expr) = (enum symbol_visibility) bp_unpack_value (bp,  2);
267   DECL_VISIBILITY_SPECIFIED (expr) = (unsigned) bp_unpack_value (bp,  1);
268
269   if (TREE_CODE (expr) == VAR_DECL)
270     {
271       DECL_HARD_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
272       DECL_IN_CONSTANT_POOL (expr) = (unsigned) bp_unpack_value (bp, 1);
273       DECL_TLS_MODEL (expr) = (enum tls_model) bp_unpack_value (bp,  3);
274     }
275
276   if (TREE_CODE (expr) == FUNCTION_DECL)
277     {
278       DECL_FINAL_P (expr) = (unsigned) bp_unpack_value (bp, 1);
279       DECL_CXX_CONSTRUCTOR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
280       DECL_CXX_DESTRUCTOR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
281     }
282   if (VAR_OR_FUNCTION_DECL_P (expr))
283     {
284       priority_type p;
285       p = (priority_type) bp_unpack_var_len_unsigned (bp);
286       SET_DECL_INIT_PRIORITY (expr, p);
287     }
288 }
289
290
291 /* Unpack all the non-pointer fields of the TS_FUNCTION_DECL structure
292    of expression EXPR from bitpack BP.  */
293
294 static void
295 unpack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
296 {
297   DECL_BUILT_IN_CLASS (expr) = bp_unpack_enum (bp, built_in_class,
298                                                BUILT_IN_LAST);
299   DECL_STATIC_CONSTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
300   DECL_STATIC_DESTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
301   DECL_UNINLINABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
302   DECL_POSSIBLY_INLINED (expr) = (unsigned) bp_unpack_value (bp, 1);
303   DECL_IS_NOVOPS (expr) = (unsigned) bp_unpack_value (bp, 1);
304   DECL_IS_RETURNS_TWICE (expr) = (unsigned) bp_unpack_value (bp, 1);
305   DECL_IS_MALLOC (expr) = (unsigned) bp_unpack_value (bp, 1);
306   DECL_IS_OPERATOR_NEW (expr) = (unsigned) bp_unpack_value (bp, 1);
307   DECL_DECLARED_INLINE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
308   DECL_STATIC_CHAIN (expr) = (unsigned) bp_unpack_value (bp, 1);
309   DECL_NO_INLINE_WARNING_P (expr) = (unsigned) bp_unpack_value (bp, 1);
310   DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr)
311                         = (unsigned) bp_unpack_value (bp, 1);
312   DECL_NO_LIMIT_STACK (expr) = (unsigned) bp_unpack_value (bp, 1);
313   DECL_DISREGARD_INLINE_LIMITS (expr) = (unsigned) bp_unpack_value (bp, 1);
314   DECL_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
315   DECL_LOOPING_CONST_OR_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
316   if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
317     {
318       DECL_FUNCTION_CODE (expr) = (enum built_in_function) bp_unpack_value (bp,
319                                                                             11);
320       if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
321           && DECL_FUNCTION_CODE (expr) >= END_BUILTINS)
322         fatal_error ("machine independent builtin code out of range");
323       else if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD)
324         {
325           tree result = targetm.builtin_decl (DECL_FUNCTION_CODE (expr), true);
326           if (!result || result == error_mark_node)
327             fatal_error ("target specific builtin not available");
328         }
329     }
330   if (DECL_STATIC_DESTRUCTOR (expr))
331     {
332       priority_type p;
333       p = (priority_type) bp_unpack_var_len_unsigned (bp);
334       SET_DECL_FINI_PRIORITY (expr, p);
335     }
336 }
337
338
339 /* Unpack all the non-pointer fields of the TS_TYPE_COMMON structure
340    of expression EXPR from bitpack BP.  */
341
342 static void
343 unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
344 {
345   enum machine_mode mode;
346
347   mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
348   SET_TYPE_MODE (expr, mode);
349   TYPE_STRING_FLAG (expr) = (unsigned) bp_unpack_value (bp, 1);
350   TYPE_NO_FORCE_BLK (expr) = (unsigned) bp_unpack_value (bp, 1);
351   TYPE_NEEDS_CONSTRUCTING (expr) = (unsigned) bp_unpack_value (bp, 1);
352   if (RECORD_OR_UNION_TYPE_P (expr))
353     {
354       TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1);
355       TYPE_FINAL_P (expr) = (unsigned) bp_unpack_value (bp, 1);
356     }
357   else if (TREE_CODE (expr) == ARRAY_TYPE)
358     TYPE_NONALIASED_COMPONENT (expr) = (unsigned) bp_unpack_value (bp, 1);
359   TYPE_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
360   TYPE_RESTRICT (expr) = (unsigned) bp_unpack_value (bp, 1);
361   TYPE_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
362   TYPE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
363   TYPE_PRECISION (expr) = bp_unpack_var_len_unsigned (bp);
364   TYPE_ALIGN (expr) = bp_unpack_var_len_unsigned (bp);
365   TYPE_ALIAS_SET (expr) = bp_unpack_var_len_int (bp);
366 }
367
368
369 /* Unpack all the non-pointer fields of the TS_BLOCK structure
370    of expression EXPR from bitpack BP.  */
371
372 static void
373 unpack_ts_block_value_fields (struct data_in *data_in,
374                               struct bitpack_d *bp, tree expr)
375 {
376   BLOCK_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
377   /* BLOCK_NUMBER is recomputed.  */
378   BLOCK_SOURCE_LOCATION (expr) = stream_input_location (bp, data_in);
379 }
380
381 /* Unpack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL
382    structure of expression EXPR from bitpack BP.  */
383
384 static void
385 unpack_ts_translation_unit_decl_value_fields (struct data_in *data_in,
386                                               struct bitpack_d *bp, tree expr)
387 {
388   TRANSLATION_UNIT_LANGUAGE (expr) = xstrdup (bp_unpack_string (data_in, bp));
389   vec_safe_push (all_translation_units, expr);
390 }
391
392 /* Unpack a TS_TARGET_OPTION tree from BP into EXPR.  */
393
394 static void
395 unpack_ts_target_option (struct bitpack_d *bp, tree expr)
396 {
397   unsigned i, len;
398   struct cl_target_option *t = TREE_TARGET_OPTION (expr);
399
400   len = sizeof (struct cl_target_option);
401   for (i = 0; i < len; i++)
402     ((unsigned char *)t)[i] = bp_unpack_value (bp, 8);
403   if (bp_unpack_value (bp, 32) != 0x12345678)
404     fatal_error ("cl_target_option size mismatch in LTO reader and writer");
405 }
406
407 /* Unpack a TS_OPTIMIZATION tree from BP into EXPR.  */
408
409 static void
410 unpack_ts_optimization (struct bitpack_d *bp, tree expr)
411 {
412   unsigned i, len;
413   struct cl_optimization *t = TREE_OPTIMIZATION (expr);
414
415   len = sizeof (struct cl_optimization);
416   for (i = 0; i < len; i++)
417     ((unsigned char *)t)[i] = bp_unpack_value (bp, 8);
418   if (bp_unpack_value (bp, 32) != 0x12345678)
419     fatal_error ("cl_optimization size mismatch in LTO reader and writer");
420 }
421
422
423 /* Unpack all the non-pointer fields in EXPR into a bit pack.  */
424
425 static void
426 unpack_value_fields (struct data_in *data_in, struct bitpack_d *bp, tree expr)
427 {
428   enum tree_code code;
429
430   code = TREE_CODE (expr);
431
432   /* Note that all these functions are highly sensitive to changes in
433      the types and sizes of each of the fields being packed.  */
434   unpack_ts_base_value_fields (bp, expr);
435
436   if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
437     unpack_ts_int_cst_value_fields (bp, expr);
438
439   if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
440     unpack_ts_real_cst_value_fields (bp, expr);
441
442   if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
443     unpack_ts_fixed_cst_value_fields (bp, expr);
444
445   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
446     DECL_SOURCE_LOCATION (expr) = stream_input_location (bp, data_in);
447
448   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
449     unpack_ts_decl_common_value_fields (bp, expr);
450
451   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
452     unpack_ts_decl_wrtl_value_fields (bp, expr);
453
454   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
455     unpack_ts_decl_with_vis_value_fields (bp, expr);
456
457   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
458     unpack_ts_function_decl_value_fields (bp, expr);
459
460   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
461     unpack_ts_type_common_value_fields (bp, expr);
462
463   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
464     SET_EXPR_LOCATION (expr, stream_input_location (bp, data_in));
465
466   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
467     unpack_ts_block_value_fields (data_in, bp, expr);
468
469   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
470     unpack_ts_translation_unit_decl_value_fields (data_in, bp, expr);
471
472   if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
473     unpack_ts_target_option (bp, expr);
474
475   if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
476     unpack_ts_optimization (bp, expr);
477
478   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
479     {
480       unsigned HOST_WIDE_INT length = bp_unpack_var_len_unsigned (bp);
481       if (length > 0)
482         vec_safe_grow (BINFO_BASE_ACCESSES (expr), length);
483     }
484
485   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
486     {
487       unsigned HOST_WIDE_INT length = bp_unpack_var_len_unsigned (bp);
488       if (length > 0)
489         vec_safe_grow (CONSTRUCTOR_ELTS (expr), length);
490     }
491 }
492
493
494 /* Read all the language-independent bitfield values for EXPR from IB.
495    Return the partially unpacked bitpack so the caller can unpack any other
496    bitfield values that the writer may have written.  */
497
498 struct bitpack_d
499 streamer_read_tree_bitfields (struct lto_input_block *ib,
500                               struct data_in *data_in, tree expr)
501 {
502   enum tree_code code;
503   struct bitpack_d bp;
504
505   /* Read the bitpack of non-pointer values from IB.  */
506   bp = streamer_read_bitpack (ib);
507
508   /* The first word in BP contains the code of the tree that we
509      are about to read.  */
510   code = (enum tree_code) bp_unpack_value (&bp, 16);
511   lto_tag_check (lto_tree_code_to_tag (code),
512                  lto_tree_code_to_tag (TREE_CODE (expr)));
513
514   /* Unpack all the value fields from BP.  */
515   unpack_value_fields (data_in, &bp, expr);
516
517   return bp;
518 }
519
520
521 /* Materialize a new tree from input block IB using descriptors in
522    DATA_IN.  The code for the new tree should match TAG.  Store in
523    *IX_P the index into the reader cache where the new tree is stored.  */
524
525 tree
526 streamer_alloc_tree (struct lto_input_block *ib, struct data_in *data_in,
527                      enum LTO_tags tag)
528 {
529   enum tree_code code;
530   tree result;
531 #ifdef LTO_STREAMER_DEBUG
532   HOST_WIDEST_INT orig_address_in_writer;
533 #endif
534
535   result = NULL_TREE;
536
537 #ifdef LTO_STREAMER_DEBUG
538   /* Read the word representing the memory address for the tree
539      as it was written by the writer.  This is useful when
540      debugging differences between the writer and reader.  */
541   orig_address_in_writer = streamer_read_hwi (ib);
542   gcc_assert ((intptr_t) orig_address_in_writer == orig_address_in_writer);
543 #endif
544
545   code = lto_tag_to_tree_code (tag);
546
547   /* We should never see an SSA_NAME tree.  Only the version numbers of
548      SSA names are ever written out.  See input_ssa_names.  */
549   gcc_assert (code != SSA_NAME);
550
551   /* Instantiate a new tree using the header data.  */
552   if (CODE_CONTAINS_STRUCT (code, TS_STRING))
553     result = streamer_read_string_cst (data_in, ib);
554   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
555     result = input_identifier (data_in, ib);
556   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
557     {
558       HOST_WIDE_INT len = streamer_read_hwi (ib);
559       result = make_tree_vec (len);
560     }
561   else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
562     {
563       HOST_WIDE_INT len = streamer_read_hwi (ib);
564       result = make_vector (len);
565     }
566   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
567     {
568       unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
569       result = make_tree_binfo (len);
570     }
571   else if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
572     {
573       unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
574       result = make_int_cst (len);
575       TREE_INT_CST_NUNITS (result) = len;
576     }
577   else if (code == CALL_EXPR)
578     {
579       unsigned HOST_WIDE_INT nargs = streamer_read_uhwi (ib);
580       return build_vl_exp (CALL_EXPR, nargs + 3);
581     }
582   else
583     {
584       /* For all other nodes, materialize the tree with a raw
585          make_node call.  */
586       result = make_node (code);
587     }
588
589 #ifdef LTO_STREAMER_DEBUG
590   /* Store the original address of the tree as seen by the writer
591      in RESULT's aux field.  This is useful when debugging streaming
592      problems.  This way, a debugging session can be started on
593      both writer and reader with a breakpoint using this address
594      value in both.  */
595   lto_orig_address_map (result, (intptr_t) orig_address_in_writer);
596 #endif
597
598   return result;
599 }
600
601
602 /* Read all pointer fields in the TS_COMMON structure of EXPR from input
603    block IB.  DATA_IN contains tables and descriptors for the
604    file being read.  */
605
606
607 static void
608 lto_input_ts_common_tree_pointers (struct lto_input_block *ib,
609                                    struct data_in *data_in, tree expr)
610 {
611   if (TREE_CODE (expr) != IDENTIFIER_NODE)
612     TREE_TYPE (expr) = stream_read_tree (ib, data_in);
613 }
614
615
616 /* Read all pointer fields in the TS_VECTOR structure of EXPR from input
617    block IB.  DATA_IN contains tables and descriptors for the
618    file being read.  */
619
620 static void
621 lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
622                                    struct data_in *data_in, tree expr)
623 {
624   unsigned i;
625   for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
626     VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
627 }
628
629
630 /* Read all pointer fields in the TS_COMPLEX structure of EXPR from input
631    block IB.  DATA_IN contains tables and descriptors for the
632    file being read.  */
633
634 static void
635 lto_input_ts_complex_tree_pointers (struct lto_input_block *ib,
636                                     struct data_in *data_in, tree expr)
637 {
638   TREE_REALPART (expr) = stream_read_tree (ib, data_in);
639   TREE_IMAGPART (expr) = stream_read_tree (ib, data_in);
640 }
641
642
643 /* Read all pointer fields in the TS_DECL_MINIMAL structure of EXPR
644    from input block IB.  DATA_IN contains tables and descriptors for the
645    file being read.  */
646
647 static void
648 lto_input_ts_decl_minimal_tree_pointers (struct lto_input_block *ib,
649                                          struct data_in *data_in, tree expr)
650 {
651   DECL_NAME (expr) = stream_read_tree (ib, data_in);
652   DECL_CONTEXT (expr) = stream_read_tree (ib, data_in);
653 }
654
655
656 /* Read all pointer fields in the TS_DECL_COMMON structure of EXPR from
657    input block IB.  DATA_IN contains tables and descriptors for the
658    file being read.  */
659
660 static void
661 lto_input_ts_decl_common_tree_pointers (struct lto_input_block *ib,
662                                         struct data_in *data_in, tree expr)
663 {
664   DECL_SIZE (expr) = stream_read_tree (ib, data_in);
665   DECL_SIZE_UNIT (expr) = stream_read_tree (ib, data_in);
666   DECL_ATTRIBUTES (expr) = stream_read_tree (ib, data_in);
667
668   /* Do not stream DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
669      for early inlining so drop it on the floor instead of ICEing in
670      dwarf2out.c.  */
671
672   if ((TREE_CODE (expr) == VAR_DECL
673        || TREE_CODE (expr) == PARM_DECL)
674       && DECL_HAS_VALUE_EXPR_P (expr))
675     SET_DECL_VALUE_EXPR (expr, stream_read_tree (ib, data_in));
676
677   if (TREE_CODE (expr) == VAR_DECL)
678     {
679       tree dexpr = stream_read_tree (ib, data_in);
680       if (dexpr)
681         SET_DECL_DEBUG_EXPR (expr, dexpr);
682     }
683 }
684
685
686 /* Read all pointer fields in the TS_DECL_NON_COMMON structure of
687    EXPR from input block IB.  DATA_IN contains tables and descriptors for the
688    file being read.  */
689
690 static void
691 lto_input_ts_decl_non_common_tree_pointers (struct lto_input_block *ib,
692                                             struct data_in *data_in, tree expr)
693 {
694   if (TREE_CODE (expr) == TYPE_DECL)
695     DECL_ORIGINAL_TYPE (expr) = stream_read_tree (ib, data_in);
696   DECL_VINDEX (expr) = stream_read_tree (ib, data_in);
697 }
698
699
700 /* Read all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
701    from input block IB.  DATA_IN contains tables and descriptors for the
702    file being read.  */
703
704 static void
705 lto_input_ts_decl_with_vis_tree_pointers (struct lto_input_block *ib,
706                                           struct data_in *data_in, tree expr)
707 {
708   tree id;
709
710   id = stream_read_tree (ib, data_in);
711   if (id)
712     {
713       gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
714       SET_DECL_ASSEMBLER_NAME (expr, id);
715     }
716
717   DECL_SECTION_NAME (expr) = stream_read_tree (ib, data_in);
718   DECL_COMDAT_GROUP (expr) = stream_read_tree (ib, data_in);
719 }
720
721
722 /* Read all pointer fields in the TS_FIELD_DECL structure of EXPR from
723    input block IB.  DATA_IN contains tables and descriptors for the
724    file being read.  */
725
726 static void
727 lto_input_ts_field_decl_tree_pointers (struct lto_input_block *ib,
728                                        struct data_in *data_in, tree expr)
729 {
730   DECL_FIELD_OFFSET (expr) = stream_read_tree (ib, data_in);
731   DECL_BIT_FIELD_TYPE (expr) = stream_read_tree (ib, data_in);
732   DECL_BIT_FIELD_REPRESENTATIVE (expr) = stream_read_tree (ib, data_in);
733   DECL_FIELD_BIT_OFFSET (expr) = stream_read_tree (ib, data_in);
734   DECL_FCONTEXT (expr) = stream_read_tree (ib, data_in);
735 }
736
737
738 /* Read all pointer fields in the TS_FUNCTION_DECL structure of EXPR
739    from input block IB.  DATA_IN contains tables and descriptors for the
740    file being read.  */
741
742 static void
743 lto_input_ts_function_decl_tree_pointers (struct lto_input_block *ib,
744                                           struct data_in *data_in, tree expr)
745 {
746   /* DECL_STRUCT_FUNCTION is handled by lto_input_function.  FIXME lto,
747      maybe it should be handled here?  */
748   DECL_FUNCTION_PERSONALITY (expr) = stream_read_tree (ib, data_in);
749   DECL_FUNCTION_SPECIFIC_TARGET (expr) = stream_read_tree (ib, data_in);
750   DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr) = stream_read_tree (ib, data_in);
751
752   /* If the file contains a function with an EH personality set,
753      then it was compiled with -fexceptions.  In that case, initialize
754      the backend EH machinery.  */
755   if (DECL_FUNCTION_PERSONALITY (expr))
756     lto_init_eh ();
757 }
758
759
760 /* Read all pointer fields in the TS_TYPE_COMMON structure of EXPR from
761    input block IB.  DATA_IN contains tables and descriptors for the file
762    being read.  */
763
764 static void
765 lto_input_ts_type_common_tree_pointers (struct lto_input_block *ib,
766                                         struct data_in *data_in, tree expr)
767 {
768   TYPE_SIZE (expr) = stream_read_tree (ib, data_in);
769   TYPE_SIZE_UNIT (expr) = stream_read_tree (ib, data_in);
770   TYPE_ATTRIBUTES (expr) = stream_read_tree (ib, data_in);
771   TYPE_NAME (expr) = stream_read_tree (ib, data_in);
772   /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
773      reconstructed during fixup.  */
774   /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
775      during fixup.  */
776   TYPE_MAIN_VARIANT (expr) = stream_read_tree (ib, data_in);
777   TYPE_CONTEXT (expr) = stream_read_tree (ib, data_in);
778   /* TYPE_CANONICAL gets re-computed during type merging.  */
779   TYPE_CANONICAL (expr) = NULL_TREE;
780   TYPE_STUB_DECL (expr) = stream_read_tree (ib, data_in);
781 }
782
783 /* Read all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
784    from input block IB.  DATA_IN contains tables and descriptors for the
785    file being read.  */
786
787 static void
788 lto_input_ts_type_non_common_tree_pointers (struct lto_input_block *ib,
789                                             struct data_in *data_in,
790                                             tree expr)
791 {
792   if (TREE_CODE (expr) == ENUMERAL_TYPE)
793     TYPE_VALUES (expr) = stream_read_tree (ib, data_in);
794   else if (TREE_CODE (expr) == ARRAY_TYPE)
795     TYPE_DOMAIN (expr) = stream_read_tree (ib, data_in);
796   else if (RECORD_OR_UNION_TYPE_P (expr))
797     TYPE_FIELDS (expr) = streamer_read_chain (ib, data_in);
798   else if (TREE_CODE (expr) == FUNCTION_TYPE
799            || TREE_CODE (expr) == METHOD_TYPE)
800     TYPE_ARG_TYPES (expr) = stream_read_tree (ib, data_in);
801
802   if (!POINTER_TYPE_P (expr))
803     TYPE_MINVAL (expr) = stream_read_tree (ib, data_in);
804   TYPE_MAXVAL (expr) = stream_read_tree (ib, data_in);
805   if (RECORD_OR_UNION_TYPE_P (expr))
806     TYPE_BINFO (expr) = stream_read_tree (ib, data_in);
807 }
808
809
810 /* Read all pointer fields in the TS_LIST structure of EXPR from input
811    block IB.  DATA_IN contains tables and descriptors for the
812    file being read.  */
813
814 static void
815 lto_input_ts_list_tree_pointers (struct lto_input_block *ib,
816                                  struct data_in *data_in, tree expr)
817 {
818   TREE_PURPOSE (expr) = stream_read_tree (ib, data_in);
819   TREE_VALUE (expr) = stream_read_tree (ib, data_in);
820   TREE_CHAIN (expr) = stream_read_tree (ib, data_in);
821 }
822
823
824 /* Read all pointer fields in the TS_VEC structure of EXPR from input
825    block IB.  DATA_IN contains tables and descriptors for the
826    file being read.  */
827
828 static void
829 lto_input_ts_vec_tree_pointers (struct lto_input_block *ib,
830                                 struct data_in *data_in, tree expr)
831 {
832   int i;
833
834   /* Note that TREE_VEC_LENGTH was read by streamer_alloc_tree to
835      instantiate EXPR.  */
836   for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
837     TREE_VEC_ELT (expr, i) = stream_read_tree (ib, data_in);
838 }
839
840
841 /* Read all pointer fields in the TS_EXP structure of EXPR from input
842    block IB.  DATA_IN contains tables and descriptors for the
843    file being read.  */
844
845
846 static void
847 lto_input_ts_exp_tree_pointers (struct lto_input_block *ib,
848                                 struct data_in *data_in, tree expr)
849 {
850   int i;
851
852   for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
853     TREE_OPERAND (expr, i) = stream_read_tree (ib, data_in);
854
855   TREE_SET_BLOCK (expr, stream_read_tree (ib, data_in));
856 }
857
858
859 /* Read all pointer fields in the TS_BLOCK structure of EXPR from input
860    block IB.  DATA_IN contains tables and descriptors for the
861    file being read.  */
862
863 static void
864 lto_input_ts_block_tree_pointers (struct lto_input_block *ib,
865                                   struct data_in *data_in, tree expr)
866 {
867   BLOCK_VARS (expr) = streamer_read_chain (ib, data_in);
868
869   BLOCK_SUPERCONTEXT (expr) = stream_read_tree (ib, data_in);
870
871   /* Stream BLOCK_ABSTRACT_ORIGIN and BLOCK_SOURCE_LOCATION for
872      the limited cases we can handle - those that represent inlined
873      function scopes.  For the rest them on the floor instead of ICEing in
874      dwarf2out.c.  */
875   BLOCK_ABSTRACT_ORIGIN (expr) = stream_read_tree (ib, data_in);
876   /* Do not stream BLOCK_NONLOCALIZED_VARS.  We cannot handle debug information
877      for early inlined BLOCKs so drop it on the floor instead of ICEing in
878      dwarf2out.c.  */
879
880   /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
881      streaming time.  */
882
883   /* We re-compute BLOCK_SUBBLOCKS of our parent here instead
884      of streaming it.  For non-BLOCK BLOCK_SUPERCONTEXTs we still
885      stream the child relationship explicitly.  */
886   if (BLOCK_SUPERCONTEXT (expr)
887       && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == BLOCK)
888     {
889       BLOCK_CHAIN (expr) = BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr));
890       BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr)) = expr;
891     }
892
893   /* The global block is rooted at the TU decl.  Hook it here to
894      avoid the need to stream in this block during WPA time.  */
895   else if (BLOCK_SUPERCONTEXT (expr)
896            && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == TRANSLATION_UNIT_DECL)
897     DECL_INITIAL (BLOCK_SUPERCONTEXT (expr)) = expr;
898
899   /* The function-level block is connected at the time we read in
900      function bodies for the same reason.  */
901 }
902
903
904 /* Read all pointer fields in the TS_BINFO structure of EXPR from input
905    block IB.  DATA_IN contains tables and descriptors for the
906    file being read.  */
907
908 static void
909 lto_input_ts_binfo_tree_pointers (struct lto_input_block *ib,
910                                   struct data_in *data_in, tree expr)
911 {
912   unsigned i;
913   tree t;
914
915   /* Note that the number of slots in EXPR was read in
916      streamer_alloc_tree when instantiating EXPR.  However, the
917      vector is empty so we cannot rely on vec::length to know how many
918      elements to read.  So, this list is emitted as a 0-terminated
919      list on the writer side.  */
920   do
921     {
922       t = stream_read_tree (ib, data_in);
923       if (t)
924         BINFO_BASE_BINFOS (expr)->quick_push (t);
925     }
926   while (t);
927
928   BINFO_OFFSET (expr) = stream_read_tree (ib, data_in);
929   BINFO_VTABLE (expr) = stream_read_tree (ib, data_in);
930   BINFO_VPTR_FIELD (expr) = stream_read_tree (ib, data_in);
931
932   /* The vector of BINFO_BASE_ACCESSES is pre-allocated during
933      unpacking the bitfield section.  */
934   for (i = 0; i < vec_safe_length (BINFO_BASE_ACCESSES (expr)); i++)
935     {
936       tree a = stream_read_tree (ib, data_in);
937       (*BINFO_BASE_ACCESSES (expr))[i] = a;
938     }
939   /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
940      and BINFO_VPTR_INDEX; these are used by C++ FE only.  */
941 }
942
943
944 /* Read all pointer fields in the TS_CONSTRUCTOR structure of EXPR from
945    input block IB.  DATA_IN contains tables and descriptors for the
946    file being read.  */
947
948 static void
949 lto_input_ts_constructor_tree_pointers (struct lto_input_block *ib,
950                                         struct data_in *data_in, tree expr)
951 {
952   unsigned i;
953
954   for (i = 0; i < CONSTRUCTOR_NELTS (expr); i++)
955     {
956       constructor_elt e;
957       e.index = stream_read_tree (ib, data_in);
958       e.value = stream_read_tree (ib, data_in);
959       (*CONSTRUCTOR_ELTS (expr))[i] = e;
960     }
961 }
962
963
964 /* Read all pointer fields in EXPR from input block IB.  DATA_IN
965    contains tables and descriptors for the file being read.  */
966
967 void
968 streamer_read_tree_body (struct lto_input_block *ib, struct data_in *data_in,
969                          tree expr)
970 {
971   enum tree_code code;
972
973   code = TREE_CODE (expr);
974
975   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
976     lto_input_ts_common_tree_pointers (ib, data_in, expr);
977
978   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
979     lto_input_ts_vector_tree_pointers (ib, data_in, expr);
980
981   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
982     lto_input_ts_complex_tree_pointers (ib, data_in, expr);
983
984   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
985     lto_input_ts_decl_minimal_tree_pointers (ib, data_in, expr);
986
987   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
988     lto_input_ts_decl_common_tree_pointers (ib, data_in, expr);
989
990   if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
991     lto_input_ts_decl_non_common_tree_pointers (ib, data_in, expr);
992
993   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
994     lto_input_ts_decl_with_vis_tree_pointers (ib, data_in, expr);
995
996   if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
997     lto_input_ts_field_decl_tree_pointers (ib, data_in, expr);
998
999   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1000     lto_input_ts_function_decl_tree_pointers (ib, data_in, expr);
1001
1002   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1003     lto_input_ts_type_common_tree_pointers (ib, data_in, expr);
1004
1005   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1006     lto_input_ts_type_non_common_tree_pointers (ib, data_in, expr);
1007
1008   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1009     lto_input_ts_list_tree_pointers (ib, data_in, expr);
1010
1011   if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1012     lto_input_ts_vec_tree_pointers (ib, data_in, expr);
1013
1014   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1015     lto_input_ts_exp_tree_pointers (ib, data_in, expr);
1016
1017   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1018     lto_input_ts_block_tree_pointers (ib, data_in, expr);
1019
1020   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1021     lto_input_ts_binfo_tree_pointers (ib, data_in, expr);
1022
1023   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1024     lto_input_ts_constructor_tree_pointers (ib, data_in, expr);
1025 }
1026
1027
1028 /* Read an index IX from input block IB and return the tree node at
1029    DATA_IN->FILE_DATA->GLOBALS_INDEX[IX].  */
1030
1031 tree
1032 streamer_get_pickled_tree (struct lto_input_block *ib, struct data_in *data_in)
1033 {
1034   unsigned HOST_WIDE_INT ix;
1035   tree result;
1036   enum LTO_tags expected_tag;
1037
1038   ix = streamer_read_uhwi (ib);
1039   expected_tag = streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
1040
1041   result = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
1042   gcc_assert (result
1043               && TREE_CODE (result) == lto_tag_to_tree_code (expected_tag));
1044
1045   return result;
1046 }
1047
1048
1049 /* Read a code and class from input block IB and return the
1050    corresponding builtin.  DATA_IN is as in stream_read_tree.  */
1051
1052 tree
1053 streamer_get_builtin_tree (struct lto_input_block *ib, struct data_in *data_in)
1054 {
1055   enum built_in_class fclass;
1056   enum built_in_function fcode;
1057   const char *asmname;
1058   tree result;
1059
1060   fclass = streamer_read_enum (ib, built_in_class, BUILT_IN_LAST);
1061   gcc_assert (fclass == BUILT_IN_NORMAL || fclass == BUILT_IN_MD);
1062
1063   fcode = (enum built_in_function) streamer_read_uhwi (ib);
1064
1065   if (fclass == BUILT_IN_NORMAL)
1066     {
1067       if (fcode >= END_BUILTINS)
1068         fatal_error ("machine independent builtin code out of range");
1069       result = builtin_decl_explicit (fcode);
1070       gcc_assert (result);
1071     }
1072   else if (fclass == BUILT_IN_MD)
1073     {
1074       result = targetm.builtin_decl (fcode, true);
1075       if (!result || result == error_mark_node)
1076         fatal_error ("target specific builtin not available");
1077     }
1078   else
1079     gcc_unreachable ();
1080
1081   asmname = streamer_read_string (data_in, ib);
1082   if (asmname)
1083     set_builtin_user_assembler_name (result, asmname);
1084
1085   streamer_tree_cache_append (data_in->reader_cache, result, 0);
1086
1087   return result;
1088 }