PR debug/66668
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2016 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
94
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
99
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
103
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
107
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110
111 /* Define this macro to be a nonzero value if the directory specifications
112     which are output in the debug info should end with a separator.  */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115    from generating indirect strings in DWARF2 debug information, for instance
116    if your target is stuck with an old version of GDB that is unable to
117    process them properly or uses VMS Debug.  */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
123
124 /* ??? Poison these here until it can be done generically.  They've been
125    totally replaced in this file; make sure it stays that way.  */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
131
132 /* The size of the target's pointer type.  */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
136
137 /* Array of RTXes referenced by the debugging information, which therefore
138    must be kept around forever.  */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140
141 /* A pointer to the base of a list of incomplete types which might be
142    completed at some later time.  incomplete_types_list needs to be a
143    vec<tree, va_gc> *because we want to tell the garbage collector about
144    it.  */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
146
147 /* A pointer to the base of a table of references to declaration
148    scopes.  This table is a display which tracks the nesting
149    of declaration scopes at the current scope and containing
150    scopes.  This table is used to find the proper place to
151    define type declaration DIE's.  */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153
154 /* Pointers to various DWARF2 sections.  */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
172
173 /* Maximum size (in bytes) of an artificially generated label.  */
174 #define MAX_ARTIFICIAL_LABEL_BYTES      30
175
176 /* According to the (draft) DWARF 3 specification, the initial length
177    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
178    bytes are 0xffffffff, followed by the length stored in the next 8
179    bytes.
180
181    However, the SGI/MIPS ABI uses an initial length which is equal to
182    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
183
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
187
188 /* Round SIZE up to the nearest BOUNDARY.  */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191
192 /* CIE identifier.  */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
199
200
201 /* A vector for a table that contains frame description
202    information for each routine.  */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
205
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207
208 struct GTY((for_user)) indirect_string_node {
209   const char *str;
210   unsigned int refcount;
211   enum dwarf_form form;
212   char *label;
213   unsigned int index;
214 };
215
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
217 {
218   typedef const char *compare_type;
219
220   static hashval_t hash (indirect_string_node *);
221   static bool equal (indirect_string_node *, const char *);
222 };
223
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
225
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227    main object file, rather than the dwo, similar to the force_direct
228    parameter elsewhere but with additional complications:
229
230    1) The string is needed in both the main object file and the dwo.
231    That is, the comp_dir and dwo_name will appear in both places.
232
233    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234    DW_FORM_GNU_str_index.
235
236    3) GCC chooses the form to use late, depending on the size and
237    reference count.
238
239    Rather than forcing the all debug string handling functions and
240    callers to deal with these complications, simply use a separate,
241    special-cased string table for any attribute that should go in the
242    main object file.  This limits the complexity to just the places
243    that need it.  */
244
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
246
247 static GTY(()) int dw2_string_counter;
248
249 /* True if the compilation unit places functions in more than one section.  */
250 static GTY(()) bool have_multiple_function_sections = false;
251
252 /* Whether the default text and cold text sections have been used at all.  */
253
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
256
257 /* The default cold text section.  */
258 static GTY(()) section *cold_text_section;
259
260 /* The DIE for C++14 'auto' in a function return type.  */
261 static GTY(()) dw_die_ref auto_die;
262
263 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
264 static GTY(()) dw_die_ref decltype_auto_die;
265
266 /* Forward declarations for functions defined in this file.  */
267
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
271
272 /* Personality decl of current unit.  Used only when assembler does not support
273    personality CFI.  */
274 static GTY(()) rtx current_unit_personality;
275
276 /* Data and reference forms for relocatable data.  */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
279
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION     ".debug_frame"
282 #endif
283
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL        "LFB"
286 #endif
287
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL          "LFE"
290 #endif
291
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL      "LPE"
294 #endif
295
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL    "LEB"
298 #endif
299
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL       "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
304 #define CIE_END_LABEL           "LECIE"
305 #define FDE_LABEL               "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
307 #define FDE_END_LABEL           "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL   "LELT"
310 #define LN_PROLOG_AS_LABEL      "LASLTP"
311 #define LN_PROLOG_END_LABEL     "LELTP"
312 #define DIE_LABEL_PREFIX        "DW"
313 \f
314 /* Match the base name of a file to the base name of a compilation unit. */
315
316 static int
317 matches_main_base (const char *path)
318 {
319   /* Cache the last query. */
320   static const char *last_path = NULL;
321   static int last_match = 0;
322   if (path != last_path)
323     {
324       const char *base;
325       int length = base_of_path (path, &base);
326       last_path = path;
327       last_match = (length == main_input_baselength
328                     && memcmp (base, main_input_basename, length) == 0);
329     }
330   return last_match;
331 }
332
333 #ifdef DEBUG_DEBUG_STRUCT
334
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337                    enum debug_struct_file criterion, int generic,
338                    int matches, int result)
339 {
340   /* Find the type name. */
341   tree type_decl = TYPE_STUB_DECL (type);
342   tree t = type_decl;
343   const char *name = 0;
344   if (TREE_CODE (t) == TYPE_DECL)
345     t = DECL_NAME (t);
346   if (t)
347     name = IDENTIFIER_POINTER (t);
348
349   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
350            criterion,
351            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352            matches ? "bas" : "hdr",
353            generic ? "gen" : "ord",
354            usage == DINFO_USAGE_DFN ? ";" :
355              usage == DINFO_USAGE_DIR_USE ? "." : "*",
356            result,
357            (void*) type_decl, name);
358   return result;
359 }
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361   dump_struct_debug (type, usage, criterion, generic, matches, result)
362
363 #else
364
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366   (result)
367
368 #endif
369
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371    of the number.  Some constants have a large uniform precision, so
372    we get the precision needed for the actual value of the number.  */
373
374 static unsigned int
375 get_full_len (const wide_int &op)
376 {
377   int prec = wi::min_precision (op, UNSIGNED);
378   return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379           / HOST_BITS_PER_WIDE_INT);
380 }
381
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
384 {
385   enum debug_struct_file criterion;
386   tree type_decl;
387   bool generic = lang_hooks.types.generic_p (type);
388
389   if (generic)
390     criterion = debug_struct_generic[usage];
391   else
392     criterion = debug_struct_ordinary[usage];
393
394   if (criterion == DINFO_STRUCT_FILE_NONE)
395     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396   if (criterion == DINFO_STRUCT_FILE_ANY)
397     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
398
399   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
400
401   if (type_decl != NULL)
402     {
403      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
405
406       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
408     }
409
410   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 }
412 \f
413 /* Return a pointer to a copy of the section string name S with all
414    attributes stripped off, and an asterisk prepended (for assemble_name).  */
415
416 static inline char *
417 stripattributes (const char *s)
418 {
419   char *stripped = XNEWVEC (char, strlen (s) + 2);
420   char *p = stripped;
421
422   *p++ = '*';
423
424   while (*s && *s != ',')
425     *p++ = *s++;
426
427   *p = '\0';
428   return stripped;
429 }
430
431 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
432    switch to the data section instead, and write out a synthetic start label
433    for collect2 the first time around.  */
434
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 {
438   if (eh_frame_section == 0)
439     {
440       int flags;
441
442       if (EH_TABLES_CAN_BE_READ_ONLY)
443         {
444           int fde_encoding;
445           int per_encoding;
446           int lsda_encoding;
447
448           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449                                                        /*global=*/0);
450           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451                                                        /*global=*/1);
452           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453                                                         /*global=*/0);
454           flags = ((! flag_pic
455                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
457                         && (per_encoding & 0x70) != DW_EH_PE_absptr
458                         && (per_encoding & 0x70) != DW_EH_PE_aligned
459                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461                    ? 0 : SECTION_WRITE);
462         }
463       else
464         flags = SECTION_WRITE;
465
466 #ifdef EH_FRAME_SECTION_NAME
467       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469       eh_frame_section = ((flags == SECTION_WRITE)
470                           ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
472     }
473
474   switch_to_section (eh_frame_section);
475
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477   /* We have no special eh_frame section.  Emit special labels to guide
478      collect2.  */
479   if (!back)
480     {
481       tree label = get_file_function_name ("F");
482       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483       targetm.asm_out.globalize_label (asm_out_file,
484                                         IDENTIFIER_POINTER (label));
485       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486     }
487 #endif
488 }
489
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491    FOR_EH.  */
492
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
495 {
496   if (for_eh)
497     switch_to_eh_frame_section (back);
498   else
499     {
500       if (!debug_frame_section)
501         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502                                            SECTION_DEBUG, NULL);
503       switch_to_section (debug_frame_section);
504     }
505 }
506
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
508
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 {
512   switch (cfi)
513     {
514     case DW_CFA_nop:
515     case DW_CFA_GNU_window_save:
516     case DW_CFA_remember_state:
517     case DW_CFA_restore_state:
518       return dw_cfi_oprnd_unused;
519
520     case DW_CFA_set_loc:
521     case DW_CFA_advance_loc1:
522     case DW_CFA_advance_loc2:
523     case DW_CFA_advance_loc4:
524     case DW_CFA_MIPS_advance_loc8:
525       return dw_cfi_oprnd_addr;
526
527     case DW_CFA_offset:
528     case DW_CFA_offset_extended:
529     case DW_CFA_def_cfa:
530     case DW_CFA_offset_extended_sf:
531     case DW_CFA_def_cfa_sf:
532     case DW_CFA_restore:
533     case DW_CFA_restore_extended:
534     case DW_CFA_undefined:
535     case DW_CFA_same_value:
536     case DW_CFA_def_cfa_register:
537     case DW_CFA_register:
538     case DW_CFA_expression:
539       return dw_cfi_oprnd_reg_num;
540
541     case DW_CFA_def_cfa_offset:
542     case DW_CFA_GNU_args_size:
543     case DW_CFA_def_cfa_offset_sf:
544       return dw_cfi_oprnd_offset;
545
546     case DW_CFA_def_cfa_expression:
547       return dw_cfi_oprnd_loc;
548
549     default:
550       gcc_unreachable ();
551     }
552 }
553
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
555
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 {
559   switch (cfi)
560     {
561     case DW_CFA_def_cfa:
562     case DW_CFA_def_cfa_sf:
563     case DW_CFA_offset:
564     case DW_CFA_offset_extended_sf:
565     case DW_CFA_offset_extended:
566       return dw_cfi_oprnd_offset;
567
568     case DW_CFA_register:
569       return dw_cfi_oprnd_reg_num;
570
571     case DW_CFA_expression:
572       return dw_cfi_oprnd_loc;
573
574     default:
575       return dw_cfi_oprnd_unused;
576     }
577 }
578
579 /* Output one FDE.  */
580
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583             char *section_start_label, int fde_encoding, char *augmentation,
584             bool any_lsda_needed, int lsda_encoding)
585 {
586   const char *begin, *end;
587   static unsigned int j;
588   char l1[20], l2[20];
589
590   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591                                      /* empty */ 0);
592   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593                                   for_eh + j);
594   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596   if (!XCOFF_DEBUGGING_INFO || for_eh)
597     {
598       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599         dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600                              " indicating 64-bit DWARF extension");
601       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602                             "FDE Length");
603     }
604   ASM_OUTPUT_LABEL (asm_out_file, l1);
605
606   if (for_eh)
607     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608   else
609     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610                            debug_frame_section, "FDE CIE offset");
611
612   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614
615   if (for_eh)
616     {
617       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620                                        "FDE initial location");
621       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622                             end, begin, "FDE address range");
623     }
624   else
625     {
626       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628     }
629
630   if (augmentation[0])
631     {
632       if (any_lsda_needed)
633         {
634           int size = size_of_encoded_value (lsda_encoding);
635
636           if (lsda_encoding == DW_EH_PE_aligned)
637             {
638               int offset = (  4         /* Length */
639                             + 4         /* CIE offset */
640                             + 2 * size_of_encoded_value (fde_encoding)
641                             + 1         /* Augmentation size */ );
642               int pad = -offset & (PTR_SIZE - 1);
643
644               size += pad;
645               gcc_assert (size_of_uleb128 (size) == 1);
646             }
647
648           dw2_asm_output_data_uleb128 (size, "Augmentation size");
649
650           if (fde->uses_eh_lsda)
651             {
652               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653                                            fde->funcdef_number);
654               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655                                                gen_rtx_SYMBOL_REF (Pmode, l1),
656                                                false,
657                                                "Language Specific Data Area");
658             }
659           else
660             {
661               if (lsda_encoding == DW_EH_PE_aligned)
662                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664                                    "Language Specific Data Area (none)");
665             }
666         }
667       else
668         dw2_asm_output_data_uleb128 (0, "Augmentation size");
669     }
670
671   /* Loop through the Call Frame Instructions associated with this FDE.  */
672   fde->dw_fde_current_label = begin;
673   {
674     size_t from, until, i;
675
676     from = 0;
677     until = vec_safe_length (fde->dw_fde_cfi);
678
679     if (fde->dw_fde_second_begin == NULL)
680       ;
681     else if (!second)
682       until = fde->dw_fde_switch_cfi_index;
683     else
684       from = fde->dw_fde_switch_cfi_index;
685
686     for (i = from; i < until; i++)
687       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688   }
689
690   /* If we are to emit a ref/link from function bodies to their frame tables,
691      do it now.  This is typically performed to make sure that tables
692      associated with functions are dragged with them and not discarded in
693      garbage collecting links. We need to do this on a per function basis to
694      cope with -ffunction-sections.  */
695
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697   /* Switch to the function section, emit the ref to the tables, and
698      switch *back* into the table section.  */
699   switch_to_section (function_section (fde->decl));
700   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701   switch_to_frame_table_section (for_eh, true);
702 #endif
703
704   /* Pad the FDE out to an address sized boundary.  */
705   ASM_OUTPUT_ALIGN (asm_out_file,
706                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707   ASM_OUTPUT_LABEL (asm_out_file, l2);
708
709   j += 2;
710 }
711
712 /* Return true if frame description entry FDE is needed for EH.  */
713
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
716 {
717   if (flag_asynchronous_unwind_tables)
718     return true;
719
720   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721     return true;
722
723   if (fde->uses_eh_lsda)
724     return true;
725
726   /* If exceptions are enabled, we have collected nothrow info.  */
727   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728     return false;
729
730   return true;
731 }
732
733 /* Output the call frame information used to record information
734    that relates to calculating the frame pointer, and records the
735    location of saved registers.  */
736
737 static void
738 output_call_frame_info (int for_eh)
739 {
740   unsigned int i;
741   dw_fde_ref fde;
742   dw_cfi_ref cfi;
743   char l1[20], l2[20], section_start_label[20];
744   bool any_lsda_needed = false;
745   char augmentation[6];
746   int augmentation_size;
747   int fde_encoding = DW_EH_PE_absptr;
748   int per_encoding = DW_EH_PE_absptr;
749   int lsda_encoding = DW_EH_PE_absptr;
750   int return_reg;
751   rtx personality = NULL;
752   int dw_cie_version;
753
754   /* Don't emit a CIE if there won't be any FDEs.  */
755   if (!fde_vec)
756     return;
757
758   /* Nothing to do if the assembler's doing it all.  */
759   if (dwarf2out_do_cfi_asm ())
760     return;
761
762   /* If we don't have any functions we'll want to unwind out of, don't emit
763      any EH unwind information.  If we make FDEs linkonce, we may have to
764      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
765      want to avoid having an FDE kept around when the function it refers to
766      is discarded.  Example where this matters: a primary function template
767      in C++ requires EH information, an explicit specialization doesn't.  */
768   if (for_eh)
769     {
770       bool any_eh_needed = false;
771
772       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773         {
774           if (fde->uses_eh_lsda)
775             any_eh_needed = any_lsda_needed = true;
776           else if (fde_needed_for_eh_p (fde))
777             any_eh_needed = true;
778           else if (TARGET_USES_WEAK_UNWIND_INFO)
779             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
780         }
781
782       if (!any_eh_needed)
783         return;
784     }
785
786   /* We're going to be generating comments, so turn on app.  */
787   if (flag_debug_asm)
788     app_enable ();
789
790   /* Switch to the proper frame section, first time.  */
791   switch_to_frame_table_section (for_eh, false);
792
793   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795
796   /* Output the CIE.  */
797   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799   if (!XCOFF_DEBUGGING_INFO || for_eh)
800     {
801       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802         dw2_asm_output_data (4, 0xffffffff,
803           "Initial length escape value indicating 64-bit DWARF extension");
804       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805                             "Length of Common Information Entry");
806     }
807   ASM_OUTPUT_LABEL (asm_out_file, l1);
808
809   /* Now that the CIE pointer is PC-relative for EH,
810      use 0 to identify the CIE.  */
811   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812                        (for_eh ? 0 : DWARF_CIE_ID),
813                        "CIE Identifier Tag");
814
815   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816      use CIE version 1, unless that would produce incorrect results
817      due to overflowing the return register column.  */
818   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819   dw_cie_version = 1;
820   if (return_reg >= 256 || dwarf_version > 2)
821     dw_cie_version = 3;
822   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823
824   augmentation[0] = 0;
825   augmentation_size = 0;
826
827   personality = current_unit_personality;
828   if (for_eh)
829     {
830       char *p;
831
832       /* Augmentation:
833          z      Indicates that a uleb128 is present to size the
834                 augmentation section.
835          L      Indicates the encoding (and thus presence) of
836                 an LSDA pointer in the FDE augmentation.
837          R      Indicates a non-default pointer encoding for
838                 FDE code pointers.
839          P      Indicates the presence of an encoding + language
840                 personality routine in the CIE augmentation.  */
841
842       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845
846       p = augmentation + 1;
847       if (personality)
848         {
849           *p++ = 'P';
850           augmentation_size += 1 + size_of_encoded_value (per_encoding);
851           assemble_external_libcall (personality);
852         }
853       if (any_lsda_needed)
854         {
855           *p++ = 'L';
856           augmentation_size += 1;
857         }
858       if (fde_encoding != DW_EH_PE_absptr)
859         {
860           *p++ = 'R';
861           augmentation_size += 1;
862         }
863       if (p > augmentation + 1)
864         {
865           augmentation[0] = 'z';
866           *p = '\0';
867         }
868
869       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
870       if (personality && per_encoding == DW_EH_PE_aligned)
871         {
872           int offset = (  4             /* Length */
873                         + 4             /* CIE Id */
874                         + 1             /* CIE version */
875                         + strlen (augmentation) + 1     /* Augmentation */
876                         + size_of_uleb128 (1)           /* Code alignment */
877                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878                         + 1             /* RA column */
879                         + 1             /* Augmentation size */
880                         + 1             /* Personality encoding */ );
881           int pad = -offset & (PTR_SIZE - 1);
882
883           augmentation_size += pad;
884
885           /* Augmentations should be small, so there's scarce need to
886              iterate for a solution.  Die if we exceed one uleb128 byte.  */
887           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888         }
889     }
890
891   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892   if (dw_cie_version >= 4)
893     {
894       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895       dw2_asm_output_data (1, 0, "CIE Segment Size");
896     }
897   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899                                "CIE Data Alignment Factor");
900
901   if (dw_cie_version == 1)
902     dw2_asm_output_data (1, return_reg, "CIE RA Column");
903   else
904     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905
906   if (augmentation[0])
907     {
908       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909       if (personality)
910         {
911           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912                                eh_data_format_name (per_encoding));
913           dw2_asm_output_encoded_addr_rtx (per_encoding,
914                                            personality,
915                                            true, NULL);
916         }
917
918       if (any_lsda_needed)
919         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920                              eh_data_format_name (lsda_encoding));
921
922       if (fde_encoding != DW_EH_PE_absptr)
923         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924                              eh_data_format_name (fde_encoding));
925     }
926
927   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928     output_cfi (cfi, NULL, for_eh);
929
930   /* Pad the CIE out to an address sized boundary.  */
931   ASM_OUTPUT_ALIGN (asm_out_file,
932                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933   ASM_OUTPUT_LABEL (asm_out_file, l2);
934
935   /* Loop through all of the FDE's.  */
936   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937     {
938       unsigned int k;
939
940       /* Don't emit EH unwind info for leaf functions that don't need it.  */
941       if (for_eh && !fde_needed_for_eh_p (fde))
942         continue;
943
944       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946                     augmentation, any_lsda_needed, lsda_encoding);
947     }
948
949   if (for_eh && targetm.terminate_dw2_eh_frame_info)
950     dw2_asm_output_data (4, 0, "End of Table");
951
952   /* Turn off app to make assembly quicker.  */
953   if (flag_debug_asm)
954     app_disable ();
955 }
956
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
958
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
961 {
962   int enc;
963   rtx ref;
964   rtx personality = get_personality_function (current_function_decl);
965
966   fprintf (asm_out_file, "\t.cfi_startproc\n");
967
968   if (personality)
969     {
970       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971       ref = personality;
972
973       /* ??? The GAS support isn't entirely consistent.  We have to
974          handle indirect support ourselves, but PC-relative is done
975          in the assembler.  Further, the assembler can't handle any
976          of the weirder relocation types.  */
977       if (enc & DW_EH_PE_indirect)
978         ref = dw2_force_const_mem (ref, true);
979
980       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981       output_addr_const (asm_out_file, ref);
982       fputc ('\n', asm_out_file);
983     }
984
985   if (crtl->uses_eh_lsda)
986     {
987       char lab[20];
988
989       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991                                    current_function_funcdef_no);
992       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994
995       if (enc & DW_EH_PE_indirect)
996         ref = dw2_force_const_mem (ref, true);
997
998       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999       output_addr_const (asm_out_file, ref);
1000       fputc ('\n', asm_out_file);
1001     }
1002 }
1003
1004 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1005    this allocation may be done before pass_final.  */
1006
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1009 {
1010   dw_fde_ref fde;
1011
1012   fde = ggc_cleared_alloc<dw_fde_node> ();
1013   fde->decl = current_function_decl;
1014   fde->funcdef_number = current_function_funcdef_no;
1015   fde->fde_index = vec_safe_length (fde_vec);
1016   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018   fde->nothrow = crtl->nothrow;
1019   fde->drap_reg = INVALID_REGNUM;
1020   fde->vdrap_reg = INVALID_REGNUM;
1021
1022   /* Record the FDE associated with this function.  */
1023   cfun->fde = fde;
1024   vec_safe_push (fde_vec, fde);
1025
1026   return fde;
1027 }
1028
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030    the prologue.  */
1031
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034                           const char *file ATTRIBUTE_UNUSED)
1035 {
1036   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037   char * dup_label;
1038   dw_fde_ref fde;
1039   section *fnsec;
1040   bool do_frame;
1041
1042   current_function_func_begin_label = NULL;
1043
1044   do_frame = dwarf2out_do_frame ();
1045
1046   /* ??? current_function_func_begin_label is also used by except.c for
1047      call-site information.  We must emit this label if it might be used.  */
1048   if (!do_frame
1049       && (!flag_exceptions
1050           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051     return;
1052
1053   fnsec = function_section (current_function_decl);
1054   switch_to_section (fnsec);
1055   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056                                current_function_funcdef_no);
1057   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058                           current_function_funcdef_no);
1059   dup_label = xstrdup (label);
1060   current_function_func_begin_label = dup_label;
1061
1062   /* We can elide the fde allocation if we're not emitting debug info.  */
1063   if (!do_frame)
1064     return;
1065
1066   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1069      do so now.  */
1070   fde = cfun->fde;
1071   if (fde == NULL)
1072     fde = dwarf2out_alloc_current_fde ();
1073
1074   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1075   fde->dw_fde_begin = dup_label;
1076   fde->dw_fde_current_label = dup_label;
1077   fde->in_std_section = (fnsec == text_section
1078                          || (cold_text_section && fnsec == cold_text_section));
1079
1080   /* We only want to output line number information for the genuine dwarf2
1081      prologue case, not the eh frame case.  */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083   if (file)
1084     dwarf2out_source_line (line, file, 0, true);
1085 #endif
1086
1087   if (dwarf2out_do_cfi_asm ())
1088     dwarf2out_do_cfi_startproc (false);
1089   else
1090     {
1091       rtx personality = get_personality_function (current_function_decl);
1092       if (!current_unit_personality)
1093         current_unit_personality = personality;
1094
1095       /* We cannot keep a current personality per function as without CFI
1096          asm, at the point where we emit the CFI data, there is no current
1097          function anymore.  */
1098       if (personality && current_unit_personality != personality)
1099         sorry ("multiple EH personalities are supported only with assemblers "
1100                "supporting .cfi_personality directive");
1101     }
1102 }
1103
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105    for a function prologue.  This gets called *after* the prologue code has
1106    been generated.  */
1107
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110                         const char *file ATTRIBUTE_UNUSED)
1111 {
1112   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113
1114   /* Output a label to mark the endpoint of the code generated for this
1115      function.  */
1116   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117                                current_function_funcdef_no);
1118   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119                           current_function_funcdef_no);
1120   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 }
1122
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124    for a function epilogue.  This gets called *before* the prologue code has
1125    been generated.  */
1126
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129                           const char *file ATTRIBUTE_UNUSED)
1130 {
1131   dw_fde_ref fde = cfun->fde;
1132   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1133
1134   if (fde->dw_fde_vms_begin_epilogue)
1135     return;
1136
1137   /* Output a label to mark the endpoint of the code generated for this
1138      function.  */
1139   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140                                current_function_funcdef_no);
1141   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142                           current_function_funcdef_no);
1143   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 }
1145
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147    for a function definition.  This gets called *after* the epilogue code has
1148    been generated.  */
1149
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152                         const char *file ATTRIBUTE_UNUSED)
1153 {
1154   dw_fde_ref fde;
1155   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1156
1157   last_var_location_insn = NULL;
1158   cached_next_real_insn = NULL;
1159
1160   if (dwarf2out_do_cfi_asm ())
1161     fprintf (asm_out_file, "\t.cfi_endproc\n");
1162
1163   /* Output a label to mark the endpoint of the code generated for this
1164      function.  */
1165   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166                                current_function_funcdef_no);
1167   ASM_OUTPUT_LABEL (asm_out_file, label);
1168   fde = cfun->fde;
1169   gcc_assert (fde != NULL);
1170   if (fde->dw_fde_second_begin == NULL)
1171     fde->dw_fde_end = xstrdup (label);
1172 }
1173
1174 void
1175 dwarf2out_frame_finish (void)
1176 {
1177   /* Output call frame information.  */
1178   if (targetm.debug_unwind_info () == UI_DWARF2)
1179     output_call_frame_info (0);
1180
1181   /* Output another copy for the unwinder.  */
1182   if ((flag_unwind_tables || flag_exceptions)
1183       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184     output_call_frame_info (1);
1185 }
1186
1187 /* Note that the current function section is being used for code.  */
1188
1189 static void
1190 dwarf2out_note_section_used (void)
1191 {
1192   section *sec = current_function_section ();
1193   if (sec == text_section)
1194     text_section_used = true;
1195   else if (sec == cold_text_section)
1196     cold_text_section_used = true;
1197 }
1198
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1201
1202 void
1203 dwarf2out_switch_text_section (void)
1204 {
1205   section *sect;
1206   dw_fde_ref fde = cfun->fde;
1207
1208   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1209
1210   if (!in_cold_section_p)
1211     {
1212       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1215     }
1216   else
1217     {
1218       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1221     }
1222   have_multiple_function_sections = true;
1223
1224   /* There is no need to mark used sections when not debugging.  */
1225   if (cold_text_section != NULL)
1226     dwarf2out_note_section_used ();
1227
1228   if (dwarf2out_do_cfi_asm ())
1229     fprintf (asm_out_file, "\t.cfi_endproc\n");
1230
1231   /* Now do the real section switch.  */
1232   sect = current_function_section ();
1233   switch_to_section (sect);
1234
1235   fde->second_in_std_section
1236     = (sect == text_section
1237        || (cold_text_section && sect == cold_text_section));
1238
1239   if (dwarf2out_do_cfi_asm ())
1240     dwarf2out_do_cfi_startproc (true);
1241
1242   var_location_switch_text_section ();
1243
1244   if (cold_text_section != NULL)
1245     set_cur_line_info_table (sect);
1246 }
1247 \f
1248 /* And now, the subset of the debugging information support code necessary
1249    for emitting location expressions.  */
1250
1251 /* Data about a single source file.  */
1252 struct GTY((for_user)) dwarf_file_data {
1253   const char * filename;
1254   int emitted_number;
1255 };
1256
1257 /* Describe an entry into the .debug_addr section.  */
1258
1259 enum ate_kind {
1260   ate_kind_rtx,
1261   ate_kind_rtx_dtprel,
1262   ate_kind_label
1263 };
1264
1265 struct GTY((for_user)) addr_table_entry {
1266   enum ate_kind kind;
1267   unsigned int refcount;
1268   unsigned int index;
1269   union addr_table_entry_struct_union
1270     {
1271       rtx GTY ((tag ("0"))) rtl;
1272       char * GTY ((tag ("1"))) label;
1273     }
1274   GTY ((desc ("%1.kind"))) addr;
1275 };
1276
1277 /* Location lists are ranges + location descriptions for that range,
1278    so you can track variables that are in different places over
1279    their entire life.  */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281   dw_loc_list_ref dw_loc_next;
1282   const char *begin; /* Label and addr_entry for start of range */
1283   addr_table_entry *begin_entry;
1284   const char *end;  /* Label for end of range */
1285   char *ll_symbol; /* Label for beginning of location list.
1286                       Only on head of list */
1287   const char *section; /* Section this loclist is relative to */
1288   dw_loc_descr_ref expr;
1289   hashval_t hash;
1290   /* True if all addresses in this and subsequent lists are known to be
1291      resolved.  */
1292   bool resolved_addr;
1293   /* True if this list has been replaced by dw_loc_next.  */
1294   bool replaced;
1295   bool emitted;
1296   /* True if the range should be emitted even if begin and end
1297      are the same.  */
1298   bool force;
1299 } dw_loc_list_node;
1300
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1303
1304 /* Convert a DWARF stack opcode into its string name.  */
1305
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1308 {
1309   const char *name = get_DW_OP_name (op);
1310
1311   if (name != NULL)
1312     return name;
1313
1314   return "OP_<unknown>";
1315 }
1316
1317 /* Return a pointer to a newly allocated location description.  Location
1318    descriptions are simple expression terms that can be strung
1319    together to form more complicated location (address) descriptions.  */
1320
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323                unsigned HOST_WIDE_INT oprnd2)
1324 {
1325   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1326
1327   descr->dw_loc_opc = op;
1328 #if ENABLE_CHECKING
1329   descr->dw_loc_frame_offset = -1;
1330 #endif
1331   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1332   descr->dw_loc_oprnd1.val_entry = NULL;
1333   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1334   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1335   descr->dw_loc_oprnd2.val_entry = NULL;
1336   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337
1338   return descr;
1339 }
1340
1341 /* Return a pointer to a newly allocated location description for
1342    REG and OFFSET.  */
1343
1344 static inline dw_loc_descr_ref
1345 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1346 {
1347   if (reg <= 31)
1348     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1349                           offset, 0);
1350   else
1351     return new_loc_descr (DW_OP_bregx, reg, offset);
1352 }
1353
1354 /* Add a location description term to a location description expression.  */
1355
1356 static inline void
1357 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 {
1359   dw_loc_descr_ref *d;
1360
1361   /* Find the end of the chain.  */
1362   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1363     ;
1364
1365   *d = descr;
1366 }
1367
1368 /* Compare two location operands for exact equality.  */
1369
1370 static bool
1371 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 {
1373   if (a->val_class != b->val_class)
1374     return false;
1375   switch (a->val_class)
1376     {
1377     case dw_val_class_none:
1378       return true;
1379     case dw_val_class_addr:
1380       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381
1382     case dw_val_class_offset:
1383     case dw_val_class_unsigned_const:
1384     case dw_val_class_const:
1385     case dw_val_class_range_list:
1386     case dw_val_class_lineptr:
1387     case dw_val_class_macptr:
1388       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1389       return a->v.val_unsigned == b->v.val_unsigned;
1390
1391     case dw_val_class_loc:
1392       return a->v.val_loc == b->v.val_loc;
1393     case dw_val_class_loc_list:
1394       return a->v.val_loc_list == b->v.val_loc_list;
1395     case dw_val_class_die_ref:
1396       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1397     case dw_val_class_fde_ref:
1398       return a->v.val_fde_index == b->v.val_fde_index;
1399     case dw_val_class_lbl_id:
1400     case dw_val_class_high_pc:
1401       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402     case dw_val_class_str:
1403       return a->v.val_str == b->v.val_str;
1404     case dw_val_class_flag:
1405       return a->v.val_flag == b->v.val_flag;
1406     case dw_val_class_file:
1407       return a->v.val_file == b->v.val_file;
1408     case dw_val_class_decl_ref:
1409       return a->v.val_decl_ref == b->v.val_decl_ref;
1410     
1411     case dw_val_class_const_double:
1412       return (a->v.val_double.high == b->v.val_double.high
1413               && a->v.val_double.low == b->v.val_double.low);
1414
1415     case dw_val_class_wide_int:
1416       return *a->v.val_wide == *b->v.val_wide;
1417
1418     case dw_val_class_vec:
1419       {
1420         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1421         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422
1423         return (a_len == b_len
1424                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1425       }
1426
1427     case dw_val_class_data8:
1428       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429
1430     case dw_val_class_vms_delta:
1431       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1432               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433
1434     case dw_val_class_discr_value:
1435       return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1436               && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1437     case dw_val_class_discr_list:
1438       /* It makes no sense comparing two discriminant value lists.  */
1439       return false;
1440     }
1441   gcc_unreachable ();
1442 }
1443
1444 /* Compare two location atoms for exact equality.  */
1445
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 {
1449   if (a->dw_loc_opc != b->dw_loc_opc)
1450     return false;
1451
1452   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453      address size, but since we always allocate cleared storage it
1454      should be zero for other types of locations.  */
1455   if (a->dtprel != b->dtprel)
1456     return false;
1457
1458   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1460 }
1461
1462 /* Compare two complete location expressions for exact equality.  */
1463
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 {
1467   while (1)
1468     {
1469       if (a == b)
1470         return true;
1471       if (a == NULL || b == NULL)
1472         return false;
1473       if (!loc_descr_equal_p_1 (a, b))
1474         return false;
1475
1476       a = a->dw_loc_next;
1477       b = b->dw_loc_next;
1478     }
1479 }
1480
1481
1482 /* Add a constant OFFSET to a location expression.  */
1483
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 {
1487   dw_loc_descr_ref loc;
1488   HOST_WIDE_INT *p;
1489
1490   gcc_assert (*list_head != NULL);
1491
1492   if (!offset)
1493     return;
1494
1495   /* Find the end of the chain.  */
1496   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1497     ;
1498
1499   p = NULL;
1500   if (loc->dw_loc_opc == DW_OP_fbreg
1501       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502     p = &loc->dw_loc_oprnd1.v.val_int;
1503   else if (loc->dw_loc_opc == DW_OP_bregx)
1504     p = &loc->dw_loc_oprnd2.v.val_int;
1505
1506   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507      offset.  Don't optimize if an signed integer overflow would happen.  */
1508   if (p != NULL
1509       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511     *p += offset;
1512
1513   else if (offset > 0)
1514     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515
1516   else
1517     {
1518       loc->dw_loc_next = int_loc_descriptor (-offset);
1519       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1520     }
1521 }
1522
1523 /* Add a constant OFFSET to a location list.  */
1524
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 {
1528   dw_loc_list_ref d;
1529   for (d = list_head; d != NULL; d = d->dw_loc_next)
1530     loc_descr_plus_const (&d->expr, offset);
1531 }
1532
1533 #define DWARF_REF_SIZE  \
1534   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1537
1538 /* Return the size of a location descriptor.  */
1539
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1542 {
1543   unsigned long size = 1;
1544
1545   switch (loc->dw_loc_opc)
1546     {
1547     case DW_OP_addr:
1548       size += DWARF2_ADDR_SIZE;
1549       break;
1550     case DW_OP_GNU_addr_index:
1551     case DW_OP_GNU_const_index:
1552       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554       break;
1555     case DW_OP_const1u:
1556     case DW_OP_const1s:
1557       size += 1;
1558       break;
1559     case DW_OP_const2u:
1560     case DW_OP_const2s:
1561       size += 2;
1562       break;
1563     case DW_OP_const4u:
1564     case DW_OP_const4s:
1565       size += 4;
1566       break;
1567     case DW_OP_const8u:
1568     case DW_OP_const8s:
1569       size += 8;
1570       break;
1571     case DW_OP_constu:
1572       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573       break;
1574     case DW_OP_consts:
1575       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576       break;
1577     case DW_OP_pick:
1578       size += 1;
1579       break;
1580     case DW_OP_plus_uconst:
1581       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582       break;
1583     case DW_OP_skip:
1584     case DW_OP_bra:
1585       size += 2;
1586       break;
1587     case DW_OP_breg0:
1588     case DW_OP_breg1:
1589     case DW_OP_breg2:
1590     case DW_OP_breg3:
1591     case DW_OP_breg4:
1592     case DW_OP_breg5:
1593     case DW_OP_breg6:
1594     case DW_OP_breg7:
1595     case DW_OP_breg8:
1596     case DW_OP_breg9:
1597     case DW_OP_breg10:
1598     case DW_OP_breg11:
1599     case DW_OP_breg12:
1600     case DW_OP_breg13:
1601     case DW_OP_breg14:
1602     case DW_OP_breg15:
1603     case DW_OP_breg16:
1604     case DW_OP_breg17:
1605     case DW_OP_breg18:
1606     case DW_OP_breg19:
1607     case DW_OP_breg20:
1608     case DW_OP_breg21:
1609     case DW_OP_breg22:
1610     case DW_OP_breg23:
1611     case DW_OP_breg24:
1612     case DW_OP_breg25:
1613     case DW_OP_breg26:
1614     case DW_OP_breg27:
1615     case DW_OP_breg28:
1616     case DW_OP_breg29:
1617     case DW_OP_breg30:
1618     case DW_OP_breg31:
1619       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620       break;
1621     case DW_OP_regx:
1622       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623       break;
1624     case DW_OP_fbreg:
1625       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626       break;
1627     case DW_OP_bregx:
1628       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630       break;
1631     case DW_OP_piece:
1632       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633       break;
1634     case DW_OP_bit_piece:
1635       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637       break;
1638     case DW_OP_deref_size:
1639     case DW_OP_xderef_size:
1640       size += 1;
1641       break;
1642     case DW_OP_call2:
1643       size += 2;
1644       break;
1645     case DW_OP_call4:
1646       size += 4;
1647       break;
1648     case DW_OP_call_ref:
1649       size += DWARF_REF_SIZE;
1650       break;
1651     case DW_OP_implicit_value:
1652       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653               + loc->dw_loc_oprnd1.v.val_unsigned;
1654       break;
1655     case DW_OP_GNU_implicit_pointer:
1656       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657       break;
1658     case DW_OP_GNU_entry_value:
1659       {
1660         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661         size += size_of_uleb128 (op_size) + op_size;
1662         break;
1663       }
1664     case DW_OP_GNU_const_type:
1665       {
1666         unsigned long o
1667           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668         size += size_of_uleb128 (o) + 1;
1669         switch (loc->dw_loc_oprnd2.val_class)
1670           {
1671           case dw_val_class_vec:
1672             size += loc->dw_loc_oprnd2.v.val_vec.length
1673                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674             break;
1675           case dw_val_class_const:
1676             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677             break;
1678           case dw_val_class_const_double:
1679             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680             break;
1681           case dw_val_class_wide_int:
1682             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684             break;
1685           default:
1686             gcc_unreachable ();
1687           }
1688         break;
1689       }
1690     case DW_OP_GNU_regval_type:
1691       {
1692         unsigned long o
1693           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695                 + size_of_uleb128 (o);
1696       }
1697       break;
1698     case DW_OP_GNU_deref_type:
1699       {
1700         unsigned long o
1701           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702         size += 1 + size_of_uleb128 (o);
1703       }
1704       break;
1705     case DW_OP_GNU_convert:
1706     case DW_OP_GNU_reinterpret:
1707       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709       else
1710         {
1711           unsigned long o
1712             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713           size += size_of_uleb128 (o);
1714         }
1715       break;
1716     case DW_OP_GNU_parameter_ref:
1717       size += 4;
1718       break;
1719     default:
1720       break;
1721     }
1722
1723   return size;
1724 }
1725
1726 /* Return the size of a series of location descriptors.  */
1727
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1730 {
1731   dw_loc_descr_ref l;
1732   unsigned long size;
1733
1734   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735      field, to avoid writing to a PCH file.  */
1736   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737     {
1738       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739         break;
1740       size += size_of_loc_descr (l);
1741     }
1742   if (! l)
1743     return size;
1744
1745   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1746     {
1747       l->dw_loc_addr = size;
1748       size += size_of_loc_descr (l);
1749     }
1750
1751   return size;
1752 }
1753
1754 /* Return the size of the value in a DW_AT_discr_value attribute.  */
1755
1756 static int
1757 size_of_discr_value (dw_discr_value *discr_value)
1758 {
1759   if (discr_value->pos)
1760     return size_of_uleb128 (discr_value->v.uval);
1761   else
1762     return size_of_sleb128 (discr_value->v.sval);
1763 }
1764
1765 /* Return the size of the value in a DW_discr_list attribute.  */
1766
1767 static int
1768 size_of_discr_list (dw_discr_list_ref discr_list)
1769 {
1770   int size = 0;
1771
1772   for (dw_discr_list_ref list = discr_list;
1773        list != NULL;
1774        list = list->dw_discr_next)
1775     {
1776       /* One byte for the discriminant value descriptor, and then one or two
1777          LEB128 numbers, depending on whether it's a single case label or a
1778          range label.  */
1779       size += 1;
1780       size += size_of_discr_value (&list->dw_discr_lower_bound);
1781       if (list->dw_discr_range != 0)
1782         size += size_of_discr_value (&list->dw_discr_upper_bound);
1783     }
1784   return size;
1785 }
1786
1787 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1788 static void get_ref_die_offset_label (char *, dw_die_ref);
1789 static unsigned long int get_ref_die_offset (dw_die_ref);
1790
1791 /* Output location description stack opcode's operands (if any).
1792    The for_eh_or_skip parameter controls whether register numbers are
1793    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1794    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1795    info).  This should be suppressed for the cases that have not been converted
1796    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1797
1798 static void
1799 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1800 {
1801   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1802   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1803
1804   switch (loc->dw_loc_opc)
1805     {
1806 #ifdef DWARF2_DEBUGGING_INFO
1807     case DW_OP_const2u:
1808     case DW_OP_const2s:
1809       dw2_asm_output_data (2, val1->v.val_int, NULL);
1810       break;
1811     case DW_OP_const4u:
1812       if (loc->dtprel)
1813         {
1814           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1815           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1816                                                val1->v.val_addr);
1817           fputc ('\n', asm_out_file);
1818           break;
1819         }
1820       /* FALLTHRU */
1821     case DW_OP_const4s:
1822       dw2_asm_output_data (4, val1->v.val_int, NULL);
1823       break;
1824     case DW_OP_const8u:
1825       if (loc->dtprel)
1826         {
1827           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1828           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1829                                                val1->v.val_addr);
1830           fputc ('\n', asm_out_file);
1831           break;
1832         }
1833       /* FALLTHRU */
1834     case DW_OP_const8s:
1835       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1836       dw2_asm_output_data (8, val1->v.val_int, NULL);
1837       break;
1838     case DW_OP_skip:
1839     case DW_OP_bra:
1840       {
1841         int offset;
1842
1843         gcc_assert (val1->val_class == dw_val_class_loc);
1844         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1845
1846         dw2_asm_output_data (2, offset, NULL);
1847       }
1848       break;
1849     case DW_OP_implicit_value:
1850       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1851       switch (val2->val_class)
1852         {
1853         case dw_val_class_const:
1854           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1855           break;
1856         case dw_val_class_vec:
1857           {
1858             unsigned int elt_size = val2->v.val_vec.elt_size;
1859             unsigned int len = val2->v.val_vec.length;
1860             unsigned int i;
1861             unsigned char *p;
1862
1863             if (elt_size > sizeof (HOST_WIDE_INT))
1864               {
1865                 elt_size /= 2;
1866                 len *= 2;
1867               }
1868             for (i = 0, p = val2->v.val_vec.array;
1869                  i < len;
1870                  i++, p += elt_size)
1871               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1872                                    "fp or vector constant word %u", i);
1873           }
1874           break;
1875         case dw_val_class_const_double:
1876           {
1877             unsigned HOST_WIDE_INT first, second;
1878
1879             if (WORDS_BIG_ENDIAN)
1880               {
1881                 first = val2->v.val_double.high;
1882                 second = val2->v.val_double.low;
1883               }
1884             else
1885               {
1886                 first = val2->v.val_double.low;
1887                 second = val2->v.val_double.high;
1888               }
1889             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1890                                  first, NULL);
1891             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1892                                  second, NULL);
1893           }
1894           break;
1895         case dw_val_class_wide_int:
1896           {
1897             int i;
1898             int len = get_full_len (*val2->v.val_wide);
1899             if (WORDS_BIG_ENDIAN)
1900               for (i = len - 1; i >= 0; --i)
1901                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1902                                      val2->v.val_wide->elt (i), NULL);
1903             else
1904               for (i = 0; i < len; ++i)
1905                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1906                                      val2->v.val_wide->elt (i), NULL);
1907           }
1908           break;
1909         case dw_val_class_addr:
1910           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1911           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1912           break;
1913         default:
1914           gcc_unreachable ();
1915         }
1916       break;
1917 #else
1918     case DW_OP_const2u:
1919     case DW_OP_const2s:
1920     case DW_OP_const4u:
1921     case DW_OP_const4s:
1922     case DW_OP_const8u:
1923     case DW_OP_const8s:
1924     case DW_OP_skip:
1925     case DW_OP_bra:
1926     case DW_OP_implicit_value:
1927       /* We currently don't make any attempt to make sure these are
1928          aligned properly like we do for the main unwind info, so
1929          don't support emitting things larger than a byte if we're
1930          only doing unwinding.  */
1931       gcc_unreachable ();
1932 #endif
1933     case DW_OP_const1u:
1934     case DW_OP_const1s:
1935       dw2_asm_output_data (1, val1->v.val_int, NULL);
1936       break;
1937     case DW_OP_constu:
1938       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1939       break;
1940     case DW_OP_consts:
1941       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1942       break;
1943     case DW_OP_pick:
1944       dw2_asm_output_data (1, val1->v.val_int, NULL);
1945       break;
1946     case DW_OP_plus_uconst:
1947       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1948       break;
1949     case DW_OP_breg0:
1950     case DW_OP_breg1:
1951     case DW_OP_breg2:
1952     case DW_OP_breg3:
1953     case DW_OP_breg4:
1954     case DW_OP_breg5:
1955     case DW_OP_breg6:
1956     case DW_OP_breg7:
1957     case DW_OP_breg8:
1958     case DW_OP_breg9:
1959     case DW_OP_breg10:
1960     case DW_OP_breg11:
1961     case DW_OP_breg12:
1962     case DW_OP_breg13:
1963     case DW_OP_breg14:
1964     case DW_OP_breg15:
1965     case DW_OP_breg16:
1966     case DW_OP_breg17:
1967     case DW_OP_breg18:
1968     case DW_OP_breg19:
1969     case DW_OP_breg20:
1970     case DW_OP_breg21:
1971     case DW_OP_breg22:
1972     case DW_OP_breg23:
1973     case DW_OP_breg24:
1974     case DW_OP_breg25:
1975     case DW_OP_breg26:
1976     case DW_OP_breg27:
1977     case DW_OP_breg28:
1978     case DW_OP_breg29:
1979     case DW_OP_breg30:
1980     case DW_OP_breg31:
1981       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1982       break;
1983     case DW_OP_regx:
1984       {
1985         unsigned r = val1->v.val_unsigned;
1986         if (for_eh_or_skip >= 0)
1987           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1988         gcc_assert (size_of_uleb128 (r) 
1989                     == size_of_uleb128 (val1->v.val_unsigned));
1990         dw2_asm_output_data_uleb128 (r, NULL);  
1991       }
1992       break;
1993     case DW_OP_fbreg:
1994       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1995       break;
1996     case DW_OP_bregx:
1997       {
1998         unsigned r = val1->v.val_unsigned;
1999         if (for_eh_or_skip >= 0)
2000           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2001         gcc_assert (size_of_uleb128 (r) 
2002                     == size_of_uleb128 (val1->v.val_unsigned));
2003         dw2_asm_output_data_uleb128 (r, NULL);  
2004         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2005       }
2006       break;
2007     case DW_OP_piece:
2008       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2009       break;
2010     case DW_OP_bit_piece:
2011       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2013       break;
2014     case DW_OP_deref_size:
2015     case DW_OP_xderef_size:
2016       dw2_asm_output_data (1, val1->v.val_int, NULL);
2017       break;
2018
2019     case DW_OP_addr:
2020       if (loc->dtprel)
2021         {
2022           if (targetm.asm_out.output_dwarf_dtprel)
2023             {
2024               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2025                                                    DWARF2_ADDR_SIZE,
2026                                                    val1->v.val_addr);
2027               fputc ('\n', asm_out_file);
2028             }
2029           else
2030             gcc_unreachable ();
2031         }
2032       else
2033         {
2034 #ifdef DWARF2_DEBUGGING_INFO
2035           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2036 #else
2037           gcc_unreachable ();
2038 #endif
2039         }
2040       break;
2041
2042     case DW_OP_GNU_addr_index:
2043     case DW_OP_GNU_const_index:
2044       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2045       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2046                                    "(index into .debug_addr)");
2047       break;
2048
2049     case DW_OP_call2:
2050     case DW_OP_call4:
2051       {
2052         unsigned long die_offset
2053           = get_ref_die_offset (val1->v.val_die_ref.die);
2054         /* Make sure the offset has been computed and that we can encode it as
2055            an operand.  */
2056         gcc_assert (die_offset > 0
2057                     && die_offset <= (loc->dw_loc_opc == DW_OP_call2)
2058                                      ? 0xffff
2059                                      : 0xffffffff);
2060         dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2061                              die_offset, NULL);
2062       }
2063       break;
2064
2065     case DW_OP_GNU_implicit_pointer:
2066       {
2067         char label[MAX_ARTIFICIAL_LABEL_BYTES
2068                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2069         gcc_assert (val1->val_class == dw_val_class_die_ref);
2070         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2071         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2072         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2073       }
2074       break;
2075
2076     case DW_OP_GNU_entry_value:
2077       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2078       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2079       break;
2080
2081     case DW_OP_GNU_const_type:
2082       {
2083         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2084         gcc_assert (o);
2085         dw2_asm_output_data_uleb128 (o, NULL);
2086         switch (val2->val_class)
2087           {
2088           case dw_val_class_const:
2089             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2090             dw2_asm_output_data (1, l, NULL);
2091             dw2_asm_output_data (l, val2->v.val_int, NULL);
2092             break;
2093           case dw_val_class_vec:
2094             {
2095               unsigned int elt_size = val2->v.val_vec.elt_size;
2096               unsigned int len = val2->v.val_vec.length;
2097               unsigned int i;
2098               unsigned char *p;
2099
2100               l = len * elt_size;
2101               dw2_asm_output_data (1, l, NULL);
2102               if (elt_size > sizeof (HOST_WIDE_INT))
2103                 {
2104                   elt_size /= 2;
2105                   len *= 2;
2106                 }
2107               for (i = 0, p = val2->v.val_vec.array;
2108                    i < len;
2109                    i++, p += elt_size)
2110                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2111                                      "fp or vector constant word %u", i);
2112             }
2113             break;
2114           case dw_val_class_const_double:
2115             {
2116               unsigned HOST_WIDE_INT first, second;
2117               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2118
2119               dw2_asm_output_data (1, 2 * l, NULL);
2120               if (WORDS_BIG_ENDIAN)
2121                 {
2122                   first = val2->v.val_double.high;
2123                   second = val2->v.val_double.low;
2124                 }
2125               else
2126                 {
2127                   first = val2->v.val_double.low;
2128                   second = val2->v.val_double.high;
2129                 }
2130               dw2_asm_output_data (l, first, NULL);
2131               dw2_asm_output_data (l, second, NULL);
2132             }
2133             break;
2134           case dw_val_class_wide_int:
2135             {
2136               int i;
2137               int len = get_full_len (*val2->v.val_wide);
2138               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2139
2140               dw2_asm_output_data (1, len * l, NULL);
2141               if (WORDS_BIG_ENDIAN)
2142                 for (i = len - 1; i >= 0; --i)
2143                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2144               else
2145                 for (i = 0; i < len; ++i)
2146                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2147             }
2148             break;
2149           default:
2150             gcc_unreachable ();
2151           }
2152       }
2153       break;
2154     case DW_OP_GNU_regval_type:
2155       {
2156         unsigned r = val1->v.val_unsigned;
2157         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2158         gcc_assert (o);
2159         if (for_eh_or_skip >= 0)
2160           {
2161             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162             gcc_assert (size_of_uleb128 (r)
2163                         == size_of_uleb128 (val1->v.val_unsigned));
2164           }
2165         dw2_asm_output_data_uleb128 (r, NULL);
2166         dw2_asm_output_data_uleb128 (o, NULL);
2167       }
2168       break;
2169     case DW_OP_GNU_deref_type:
2170       {
2171         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2172         gcc_assert (o);
2173         dw2_asm_output_data (1, val1->v.val_int, NULL);
2174         dw2_asm_output_data_uleb128 (o, NULL);
2175       }
2176       break;
2177     case DW_OP_GNU_convert:
2178     case DW_OP_GNU_reinterpret:
2179       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2180         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2181       else
2182         {
2183           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2184           gcc_assert (o);
2185           dw2_asm_output_data_uleb128 (o, NULL);
2186         }
2187       break;
2188
2189     case DW_OP_GNU_parameter_ref:
2190       {
2191         unsigned long o;
2192         gcc_assert (val1->val_class == dw_val_class_die_ref);
2193         o = get_ref_die_offset (val1->v.val_die_ref.die);
2194         dw2_asm_output_data (4, o, NULL);
2195       }
2196       break;
2197
2198     default:
2199       /* Other codes have no operands.  */
2200       break;
2201     }
2202 }
2203
2204 /* Output a sequence of location operations.  
2205    The for_eh_or_skip parameter controls whether register numbers are
2206    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2207    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2208    info).  This should be suppressed for the cases that have not been converted
2209    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2210
2211 void
2212 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2213 {
2214   for (; loc != NULL; loc = loc->dw_loc_next)
2215     {
2216       enum dwarf_location_atom opc = loc->dw_loc_opc;
2217       /* Output the opcode.  */
2218       if (for_eh_or_skip >= 0 
2219           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2220         {
2221           unsigned r = (opc - DW_OP_breg0);
2222           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2223           gcc_assert (r <= 31);
2224           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2225         }
2226       else if (for_eh_or_skip >= 0 
2227                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2228         {
2229           unsigned r = (opc - DW_OP_reg0);
2230           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2231           gcc_assert (r <= 31);
2232           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2233         }
2234
2235       dw2_asm_output_data (1, opc,
2236                              "%s", dwarf_stack_op_name (opc));
2237
2238       /* Output the operand(s) (if any).  */
2239       output_loc_operands (loc, for_eh_or_skip);
2240     }
2241 }
2242
2243 /* Output location description stack opcode's operands (if any).
2244    The output is single bytes on a line, suitable for .cfi_escape.  */
2245
2246 static void
2247 output_loc_operands_raw (dw_loc_descr_ref loc)
2248 {
2249   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2250   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2251
2252   switch (loc->dw_loc_opc)
2253     {
2254     case DW_OP_addr:
2255     case DW_OP_GNU_addr_index:
2256     case DW_OP_GNU_const_index:
2257     case DW_OP_implicit_value:
2258       /* We cannot output addresses in .cfi_escape, only bytes.  */
2259       gcc_unreachable ();
2260
2261     case DW_OP_const1u:
2262     case DW_OP_const1s:
2263     case DW_OP_pick:
2264     case DW_OP_deref_size:
2265     case DW_OP_xderef_size:
2266       fputc (',', asm_out_file);
2267       dw2_asm_output_data_raw (1, val1->v.val_int);
2268       break;
2269
2270     case DW_OP_const2u:
2271     case DW_OP_const2s:
2272       fputc (',', asm_out_file);
2273       dw2_asm_output_data_raw (2, val1->v.val_int);
2274       break;
2275
2276     case DW_OP_const4u:
2277     case DW_OP_const4s:
2278       fputc (',', asm_out_file);
2279       dw2_asm_output_data_raw (4, val1->v.val_int);
2280       break;
2281
2282     case DW_OP_const8u:
2283     case DW_OP_const8s:
2284       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_raw (8, val1->v.val_int);
2287       break;
2288
2289     case DW_OP_skip:
2290     case DW_OP_bra:
2291       {
2292         int offset;
2293
2294         gcc_assert (val1->val_class == dw_val_class_loc);
2295         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2296
2297         fputc (',', asm_out_file);
2298         dw2_asm_output_data_raw (2, offset);
2299       }
2300       break;
2301
2302     case DW_OP_regx:
2303       {
2304         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2305         gcc_assert (size_of_uleb128 (r) 
2306                     == size_of_uleb128 (val1->v.val_unsigned));
2307         fputc (',', asm_out_file);
2308         dw2_asm_output_data_uleb128_raw (r);
2309       }
2310       break;
2311       
2312     case DW_OP_constu:
2313     case DW_OP_plus_uconst:
2314     case DW_OP_piece:
2315       fputc (',', asm_out_file);
2316       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2317       break;
2318
2319     case DW_OP_bit_piece:
2320       fputc (',', asm_out_file);
2321       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2322       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2323       break;
2324
2325     case DW_OP_consts:
2326     case DW_OP_breg0:
2327     case DW_OP_breg1:
2328     case DW_OP_breg2:
2329     case DW_OP_breg3:
2330     case DW_OP_breg4:
2331     case DW_OP_breg5:
2332     case DW_OP_breg6:
2333     case DW_OP_breg7:
2334     case DW_OP_breg8:
2335     case DW_OP_breg9:
2336     case DW_OP_breg10:
2337     case DW_OP_breg11:
2338     case DW_OP_breg12:
2339     case DW_OP_breg13:
2340     case DW_OP_breg14:
2341     case DW_OP_breg15:
2342     case DW_OP_breg16:
2343     case DW_OP_breg17:
2344     case DW_OP_breg18:
2345     case DW_OP_breg19:
2346     case DW_OP_breg20:
2347     case DW_OP_breg21:
2348     case DW_OP_breg22:
2349     case DW_OP_breg23:
2350     case DW_OP_breg24:
2351     case DW_OP_breg25:
2352     case DW_OP_breg26:
2353     case DW_OP_breg27:
2354     case DW_OP_breg28:
2355     case DW_OP_breg29:
2356     case DW_OP_breg30:
2357     case DW_OP_breg31:
2358     case DW_OP_fbreg:
2359       fputc (',', asm_out_file);
2360       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2361       break;
2362
2363     case DW_OP_bregx:
2364       {
2365         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2366         gcc_assert (size_of_uleb128 (r) 
2367                     == size_of_uleb128 (val1->v.val_unsigned));
2368         fputc (',', asm_out_file);
2369         dw2_asm_output_data_uleb128_raw (r);
2370         fputc (',', asm_out_file);
2371         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2372       }
2373       break;
2374
2375     case DW_OP_GNU_implicit_pointer:
2376     case DW_OP_GNU_entry_value:
2377     case DW_OP_GNU_const_type:
2378     case DW_OP_GNU_regval_type:
2379     case DW_OP_GNU_deref_type:
2380     case DW_OP_GNU_convert:
2381     case DW_OP_GNU_reinterpret:
2382     case DW_OP_GNU_parameter_ref:
2383       gcc_unreachable ();
2384       break;
2385
2386     default:
2387       /* Other codes have no operands.  */
2388       break;
2389     }
2390 }
2391
2392 void
2393 output_loc_sequence_raw (dw_loc_descr_ref loc)
2394 {
2395   while (1)
2396     {
2397       enum dwarf_location_atom opc = loc->dw_loc_opc;
2398       /* Output the opcode.  */
2399       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2400         {
2401           unsigned r = (opc - DW_OP_breg0);
2402           r = DWARF2_FRAME_REG_OUT (r, 1);
2403           gcc_assert (r <= 31);
2404           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2405         }
2406       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2407         {
2408           unsigned r = (opc - DW_OP_reg0);
2409           r = DWARF2_FRAME_REG_OUT (r, 1);
2410           gcc_assert (r <= 31);
2411           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2412         }
2413       /* Output the opcode.  */
2414       fprintf (asm_out_file, "%#x", opc);
2415       output_loc_operands_raw (loc);
2416
2417       if (!loc->dw_loc_next)
2418         break;
2419       loc = loc->dw_loc_next;
2420
2421       fputc (',', asm_out_file);
2422     }
2423 }
2424
2425 /* This function builds a dwarf location descriptor sequence from a
2426    dw_cfa_location, adding the given OFFSET to the result of the
2427    expression.  */
2428
2429 struct dw_loc_descr_node *
2430 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2431 {
2432   struct dw_loc_descr_node *head, *tmp;
2433
2434   offset += cfa->offset;
2435
2436   if (cfa->indirect)
2437     {
2438       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2439       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2440       head->dw_loc_oprnd1.val_entry = NULL;
2441       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2442       add_loc_descr (&head, tmp);
2443       if (offset != 0)
2444         {
2445           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2446           add_loc_descr (&head, tmp);
2447         }
2448     }
2449   else
2450     head = new_reg_loc_descr (cfa->reg, offset);
2451
2452   return head;
2453 }
2454
2455 /* This function builds a dwarf location descriptor sequence for
2456    the address at OFFSET from the CFA when stack is aligned to
2457    ALIGNMENT byte.  */
2458
2459 struct dw_loc_descr_node *
2460 build_cfa_aligned_loc (dw_cfa_location *cfa,
2461                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2462 {
2463   struct dw_loc_descr_node *head;
2464   unsigned int dwarf_fp
2465     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2466
2467   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2468   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2469     {
2470       head = new_reg_loc_descr (dwarf_fp, 0);
2471       add_loc_descr (&head, int_loc_descriptor (alignment));
2472       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2473       loc_descr_plus_const (&head, offset);
2474     }
2475   else
2476     head = new_reg_loc_descr (dwarf_fp, offset);
2477   return head;
2478 }
2479 \f
2480 /* And now, the support for symbolic debugging information.  */
2481
2482 /* .debug_str support.  */
2483
2484 static void dwarf2out_init (const char *);
2485 static void dwarf2out_finish (const char *);
2486 static void dwarf2out_early_finish (void);
2487 static void dwarf2out_assembly_start (void);
2488 static void dwarf2out_define (unsigned int, const char *);
2489 static void dwarf2out_undef (unsigned int, const char *);
2490 static void dwarf2out_start_source_file (unsigned, const char *);
2491 static void dwarf2out_end_source_file (unsigned);
2492 static void dwarf2out_function_decl (tree);
2493 static void dwarf2out_begin_block (unsigned, unsigned);
2494 static void dwarf2out_end_block (unsigned, unsigned);
2495 static bool dwarf2out_ignore_block (const_tree);
2496 static void dwarf2out_early_global_decl (tree);
2497 static void dwarf2out_late_global_decl (tree);
2498 static void dwarf2out_type_decl (tree, int);
2499 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2500 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2501                                                  dw_die_ref);
2502 static void dwarf2out_abstract_function (tree);
2503 static void dwarf2out_var_location (rtx_insn *);
2504 static void dwarf2out_size_function (tree);
2505 static void dwarf2out_begin_function (tree);
2506 static void dwarf2out_end_function (unsigned int);
2507 static void dwarf2out_register_main_translation_unit (tree unit);
2508 static void dwarf2out_set_name (tree, tree);
2509
2510 /* The debug hooks structure.  */
2511
2512 const struct gcc_debug_hooks dwarf2_debug_hooks =
2513 {
2514   dwarf2out_init,
2515   dwarf2out_finish,
2516   dwarf2out_early_finish,
2517   dwarf2out_assembly_start,
2518   dwarf2out_define,
2519   dwarf2out_undef,
2520   dwarf2out_start_source_file,
2521   dwarf2out_end_source_file,
2522   dwarf2out_begin_block,
2523   dwarf2out_end_block,
2524   dwarf2out_ignore_block,
2525   dwarf2out_source_line,
2526   dwarf2out_begin_prologue,
2527 #if VMS_DEBUGGING_INFO
2528   dwarf2out_vms_end_prologue,
2529   dwarf2out_vms_begin_epilogue,
2530 #else
2531   debug_nothing_int_charstar,
2532   debug_nothing_int_charstar,
2533 #endif
2534   dwarf2out_end_epilogue,
2535   dwarf2out_begin_function,
2536   dwarf2out_end_function,       /* end_function */
2537   dwarf2out_register_main_translation_unit,
2538   dwarf2out_function_decl,      /* function_decl */
2539   dwarf2out_early_global_decl,
2540   dwarf2out_late_global_decl,
2541   dwarf2out_type_decl,          /* type_decl */
2542   dwarf2out_imported_module_or_decl,
2543   debug_nothing_tree,           /* deferred_inline_function */
2544   /* The DWARF 2 backend tries to reduce debugging bloat by not
2545      emitting the abstract description of inline functions until
2546      something tries to reference them.  */
2547   dwarf2out_abstract_function,  /* outlining_inline_function */
2548   debug_nothing_rtx_code_label, /* label */
2549   debug_nothing_int,            /* handle_pch */
2550   dwarf2out_var_location,
2551   dwarf2out_size_function,      /* size_function */
2552   dwarf2out_switch_text_section,
2553   dwarf2out_set_name,
2554   1,                            /* start_end_main_source_file */
2555   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2556 };
2557
2558 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2559 {
2560   dwarf2out_init,
2561   debug_nothing_charstar,
2562   debug_nothing_void,
2563   debug_nothing_void,
2564   debug_nothing_int_charstar,
2565   debug_nothing_int_charstar,
2566   debug_nothing_int_charstar,
2567   debug_nothing_int,
2568   debug_nothing_int_int,                 /* begin_block */
2569   debug_nothing_int_int,                 /* end_block */
2570   debug_true_const_tree,                 /* ignore_block */
2571   dwarf2out_source_line,         /* source_line */
2572   debug_nothing_int_charstar,            /* begin_prologue */
2573   debug_nothing_int_charstar,            /* end_prologue */
2574   debug_nothing_int_charstar,            /* begin_epilogue */
2575   debug_nothing_int_charstar,            /* end_epilogue */
2576   debug_nothing_tree,                    /* begin_function */
2577   debug_nothing_int,                     /* end_function */
2578   debug_nothing_tree,                    /* register_main_translation_unit */
2579   debug_nothing_tree,                    /* function_decl */
2580   debug_nothing_tree,                    /* early_global_decl */
2581   debug_nothing_tree,                    /* late_global_decl */
2582   debug_nothing_tree_int,                /* type_decl */
2583   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2584   debug_nothing_tree,                    /* deferred_inline_function */
2585   debug_nothing_tree,                    /* outlining_inline_function */
2586   debug_nothing_rtx_code_label,          /* label */
2587   debug_nothing_int,                     /* handle_pch */
2588   debug_nothing_rtx_insn,                /* var_location */
2589   debug_nothing_tree,                    /* size_function */
2590   debug_nothing_void,                    /* switch_text_section */
2591   debug_nothing_tree_tree,               /* set_name */
2592   0,                                     /* start_end_main_source_file */
2593   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2594 };
2595 \f
2596 /* NOTE: In the comments in this file, many references are made to
2597    "Debugging Information Entries".  This term is abbreviated as `DIE'
2598    throughout the remainder of this file.  */
2599
2600 /* An internal representation of the DWARF output is built, and then
2601    walked to generate the DWARF debugging info.  The walk of the internal
2602    representation is done after the entire program has been compiled.
2603    The types below are used to describe the internal representation.  */
2604
2605 /* Whether to put type DIEs into their own section .debug_types instead
2606    of making them part of the .debug_info section.  Only supported for
2607    Dwarf V4 or higher and the user didn't disable them through
2608    -fno-debug-types-section.  It is more efficient to put them in a
2609    separate comdat sections since the linker will then be able to
2610    remove duplicates.  But not all tools support .debug_types sections
2611    yet.  */
2612
2613 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2614
2615 /* Various DIE's use offsets relative to the beginning of the
2616    .debug_info section to refer to each other.  */
2617
2618 typedef long int dw_offset;
2619
2620 struct comdat_type_node;
2621
2622 /* The entries in the line_info table more-or-less mirror the opcodes
2623    that are used in the real dwarf line table.  Arrays of these entries
2624    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2625    supported.  */
2626
2627 enum dw_line_info_opcode {
2628   /* Emit DW_LNE_set_address; the operand is the label index.  */
2629   LI_set_address,
2630
2631   /* Emit a row to the matrix with the given line.  This may be done
2632      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2633      special opcodes.  */
2634   LI_set_line,
2635
2636   /* Emit a DW_LNS_set_file.  */
2637   LI_set_file,
2638
2639   /* Emit a DW_LNS_set_column.  */
2640   LI_set_column,
2641
2642   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2643   LI_negate_stmt,
2644
2645   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2646   LI_set_prologue_end,
2647   LI_set_epilogue_begin,
2648
2649   /* Emit a DW_LNE_set_discriminator.  */
2650   LI_set_discriminator
2651 };
2652
2653 typedef struct GTY(()) dw_line_info_struct {
2654   enum dw_line_info_opcode opcode;
2655   unsigned int val;
2656 } dw_line_info_entry;
2657
2658
2659 struct GTY(()) dw_line_info_table {
2660   /* The label that marks the end of this section.  */
2661   const char *end_label;
2662
2663   /* The values for the last row of the matrix, as collected in the table.
2664      These are used to minimize the changes to the next row.  */
2665   unsigned int file_num;
2666   unsigned int line_num;
2667   unsigned int column_num;
2668   int discrim_num;
2669   bool is_stmt;
2670   bool in_use;
2671
2672   vec<dw_line_info_entry, va_gc> *entries;
2673 };
2674
2675
2676 /* Each DIE attribute has a field specifying the attribute kind,
2677    a link to the next attribute in the chain, and an attribute value.
2678    Attributes are typically linked below the DIE they modify.  */
2679
2680 typedef struct GTY(()) dw_attr_struct {
2681   enum dwarf_attribute dw_attr;
2682   dw_val_node dw_attr_val;
2683 }
2684 dw_attr_node;
2685
2686
2687 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2688    The children of each node form a circular list linked by
2689    die_sib.  die_child points to the node *before* the "first" child node.  */
2690
2691 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2692   union die_symbol_or_type_node
2693     {
2694       const char * GTY ((tag ("0"))) die_symbol;
2695       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2696     }
2697   GTY ((desc ("%0.comdat_type_p"))) die_id;
2698   vec<dw_attr_node, va_gc> *die_attr;
2699   dw_die_ref die_parent;
2700   dw_die_ref die_child;
2701   dw_die_ref die_sib;
2702   dw_die_ref die_definition; /* ref from a specification to its definition */
2703   dw_offset die_offset;
2704   unsigned long die_abbrev;
2705   int die_mark;
2706   unsigned int decl_id;
2707   enum dwarf_tag die_tag;
2708   /* Die is used and must not be pruned as unused.  */
2709   BOOL_BITFIELD die_perennial_p : 1;
2710   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2711   /* Lots of spare bits.  */
2712 }
2713 die_node;
2714
2715 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2716 static bool early_dwarf;
2717 struct set_early_dwarf {
2718   bool saved;
2719   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2720   ~set_early_dwarf () { early_dwarf = saved; }
2721 };
2722
2723 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2724 #define FOR_EACH_CHILD(die, c, expr) do {       \
2725   c = die->die_child;                           \
2726   if (c) do {                                   \
2727     c = c->die_sib;                             \
2728     expr;                                       \
2729   } while (c != die->die_child);                \
2730 } while (0)
2731
2732 /* The pubname structure */
2733
2734 typedef struct GTY(()) pubname_struct {
2735   dw_die_ref die;
2736   const char *name;
2737 }
2738 pubname_entry;
2739
2740
2741 struct GTY(()) dw_ranges {
2742   /* If this is positive, it's a block number, otherwise it's a
2743      bitwise-negated index into dw_ranges_by_label.  */
2744   int num;
2745 };
2746
2747 /* A structure to hold a macinfo entry.  */
2748
2749 typedef struct GTY(()) macinfo_struct {
2750   unsigned char code;
2751   unsigned HOST_WIDE_INT lineno;
2752   const char *info;
2753 }
2754 macinfo_entry;
2755
2756
2757 struct GTY(()) dw_ranges_by_label {
2758   const char *begin;
2759   const char *end;
2760 };
2761
2762 /* The comdat type node structure.  */
2763 struct GTY(()) comdat_type_node
2764 {
2765   dw_die_ref root_die;
2766   dw_die_ref type_die;
2767   dw_die_ref skeleton_die;
2768   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2769   comdat_type_node *next;
2770 };
2771
2772 /* A list of DIEs for which we can't determine ancestry (parent_die
2773    field) just yet.  Later in dwarf2out_finish we will fill in the
2774    missing bits.  */
2775 typedef struct GTY(()) limbo_die_struct {
2776   dw_die_ref die;
2777   /* The tree for which this DIE was created.  We use this to
2778      determine ancestry later.  */
2779   tree created_for;
2780   struct limbo_die_struct *next;
2781 }
2782 limbo_die_node;
2783
2784 typedef struct skeleton_chain_struct
2785 {
2786   dw_die_ref old_die;
2787   dw_die_ref new_die;
2788   struct skeleton_chain_struct *parent;
2789 }
2790 skeleton_chain_node;
2791
2792 /* Define a macro which returns nonzero for a TYPE_DECL which was
2793    implicitly generated for a type.
2794
2795    Note that, unlike the C front-end (which generates a NULL named
2796    TYPE_DECL node for each complete tagged type, each array type,
2797    and each function type node created) the C++ front-end generates
2798    a _named_ TYPE_DECL node for each tagged type node created.
2799    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2800    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2801    front-end, but for each type, tagged or not.  */
2802
2803 #define TYPE_DECL_IS_STUB(decl)                         \
2804   (DECL_NAME (decl) == NULL_TREE                        \
2805    || (DECL_ARTIFICIAL (decl)                           \
2806        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2807            /* This is necessary for stub decls that     \
2808               appear in nested inline functions.  */    \
2809            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2810                && (decl_ultimate_origin (decl)          \
2811                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2812
2813 /* Information concerning the compilation unit's programming
2814    language, and compiler version.  */
2815
2816 /* Fixed size portion of the DWARF compilation unit header.  */
2817 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2818   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2819
2820 /* Fixed size portion of the DWARF comdat type unit header.  */
2821 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2822   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2823    + DWARF_OFFSET_SIZE)
2824
2825 /* Fixed size portion of public names info.  */
2826 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2827
2828 /* Fixed size portion of the address range info.  */
2829 #define DWARF_ARANGES_HEADER_SIZE                                       \
2830   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2831                 DWARF2_ADDR_SIZE * 2)                                   \
2832    - DWARF_INITIAL_LENGTH_SIZE)
2833
2834 /* Size of padding portion in the address range info.  It must be
2835    aligned to twice the pointer size.  */
2836 #define DWARF_ARANGES_PAD_SIZE \
2837   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2838                 DWARF2_ADDR_SIZE * 2)                              \
2839    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2840
2841 /* Use assembler line directives if available.  */
2842 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2843 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2844 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2845 #else
2846 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2847 #endif
2848 #endif
2849
2850 /* Minimum line offset in a special line info. opcode.
2851    This value was chosen to give a reasonable range of values.  */
2852 #define DWARF_LINE_BASE  -10
2853
2854 /* First special line opcode - leave room for the standard opcodes.  */
2855 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2856
2857 /* Range of line offsets in a special line info. opcode.  */
2858 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2859
2860 /* Flag that indicates the initial value of the is_stmt_start flag.
2861    In the present implementation, we do not mark any lines as
2862    the beginning of a source statement, because that information
2863    is not made available by the GCC front-end.  */
2864 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2865
2866 /* Maximum number of operations per instruction bundle.  */
2867 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2868 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2869 #endif
2870
2871 /* This location is used by calc_die_sizes() to keep track
2872    the offset of each DIE within the .debug_info section.  */
2873 static unsigned long next_die_offset;
2874
2875 /* Record the root of the DIE's built for the current compilation unit.  */
2876 static GTY(()) dw_die_ref single_comp_unit_die;
2877
2878 /* A list of type DIEs that have been separated into comdat sections.  */
2879 static GTY(()) comdat_type_node *comdat_type_list;
2880
2881 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2882 static GTY(()) limbo_die_node *limbo_die_list;
2883
2884 /* A list of DIEs for which we may have to generate
2885    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2886 static GTY(()) limbo_die_node *deferred_asm_name;
2887
2888 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2889 {
2890   typedef const char *compare_type;
2891
2892   static hashval_t hash (dwarf_file_data *);
2893   static bool equal (dwarf_file_data *, const char *);
2894 };
2895
2896 /* Filenames referenced by this compilation unit.  */
2897 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2898
2899 struct decl_die_hasher : ggc_ptr_hash<die_node>
2900 {
2901   typedef tree compare_type;
2902
2903   static hashval_t hash (die_node *);
2904   static bool equal (die_node *, tree);
2905 };
2906 /* A hash table of references to DIE's that describe declarations.
2907    The key is a DECL_UID() which is a unique number identifying each decl.  */
2908 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2909
2910 struct block_die_hasher : ggc_ptr_hash<die_struct>
2911 {
2912   static hashval_t hash (die_struct *);
2913   static bool equal (die_struct *, die_struct *);
2914 };
2915
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917    The key is DECL_UID() ^ die_parent.  */
2918 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2919
2920 typedef struct GTY(()) die_arg_entry_struct {
2921     dw_die_ref die;
2922     tree arg;
2923 } die_arg_entry;
2924
2925
2926 /* Node of the variable location list.  */
2927 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2928   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2929      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2930      in mode of the EXPR_LIST node and first EXPR_LIST operand
2931      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2932      location or NULL for padding.  For larger bitsizes,
2933      mode is 0 and first operand is a CONCAT with bitsize
2934      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2935      NULL as second operand.  */
2936   rtx GTY (()) loc;
2937   const char * GTY (()) label;
2938   struct var_loc_node * GTY (()) next;
2939 };
2940
2941 /* Variable location list.  */
2942 struct GTY ((for_user)) var_loc_list_def {
2943   struct var_loc_node * GTY (()) first;
2944
2945   /* Pointer to the last but one or last element of the
2946      chained list.  If the list is empty, both first and
2947      last are NULL, if the list contains just one node
2948      or the last node certainly is not redundant, it points
2949      to the last node, otherwise points to the last but one.
2950      Do not mark it for GC because it is marked through the chain.  */
2951   struct var_loc_node * GTY ((skip ("%h"))) last;
2952
2953   /* Pointer to the last element before section switch,
2954      if NULL, either sections weren't switched or first
2955      is after section switch.  */
2956   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2957
2958   /* DECL_UID of the variable decl.  */
2959   unsigned int decl_id;
2960 };
2961 typedef struct var_loc_list_def var_loc_list;
2962
2963 /* Call argument location list.  */
2964 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2965   rtx GTY (()) call_arg_loc_note;
2966   const char * GTY (()) label;
2967   tree GTY (()) block;
2968   bool tail_call_p;
2969   rtx GTY (()) symbol_ref;
2970   struct call_arg_loc_node * GTY (()) next;
2971 };
2972
2973
2974 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2975 {
2976   typedef const_tree compare_type;
2977
2978   static hashval_t hash (var_loc_list *);
2979   static bool equal (var_loc_list *, const_tree);
2980 };
2981
2982 /* Table of decl location linked lists.  */
2983 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2984
2985 /* Head and tail of call_arg_loc chain.  */
2986 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2987 static struct call_arg_loc_node *call_arg_loc_last;
2988
2989 /* Number of call sites in the current function.  */
2990 static int call_site_count = -1;
2991 /* Number of tail call sites in the current function.  */
2992 static int tail_call_site_count = -1;
2993
2994 /* A cached location list.  */
2995 struct GTY ((for_user)) cached_dw_loc_list_def {
2996   /* The DECL_UID of the decl that this entry describes.  */
2997   unsigned int decl_id;
2998
2999   /* The cached location list.  */
3000   dw_loc_list_ref loc_list;
3001 };
3002 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3003
3004 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3005 {
3006
3007   typedef const_tree compare_type;
3008   
3009   static hashval_t hash (cached_dw_loc_list *);
3010   static bool equal (cached_dw_loc_list *, const_tree);
3011 };
3012
3013 /* Table of cached location lists.  */
3014 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3015
3016 /* A pointer to the base of a list of references to DIE's that
3017    are uniquely identified by their tag, presence/absence of
3018    children DIE's, and list of attribute/value pairs.  */
3019 static GTY((length ("abbrev_die_table_allocated")))
3020   dw_die_ref *abbrev_die_table;
3021
3022 /* Number of elements currently allocated for abbrev_die_table.  */
3023 static GTY(()) unsigned abbrev_die_table_allocated;
3024
3025 /* Number of elements in abbrev_die_table currently in use.  */
3026 static GTY(()) unsigned abbrev_die_table_in_use;
3027
3028 /* A hash map to remember the stack usage for DWARF procedures.  The value
3029    stored is the stack size difference between before the DWARF procedure
3030    invokation and after it returned.  In other words, for a DWARF procedure
3031    that consumes N stack slots and that pushes M ones, this stores M - N.  */
3032 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3033
3034 /* Size (in elements) of increments by which we may expand the
3035    abbrev_die_table.  */
3036 #define ABBREV_DIE_TABLE_INCREMENT 256
3037
3038 /* A global counter for generating labels for line number data.  */
3039 static unsigned int line_info_label_num;
3040
3041 /* The current table to which we should emit line number information
3042    for the current function.  This will be set up at the beginning of
3043    assembly for the function.  */
3044 static dw_line_info_table *cur_line_info_table;
3045
3046 /* The two default tables of line number info.  */
3047 static GTY(()) dw_line_info_table *text_section_line_info;
3048 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3049
3050 /* The set of all non-default tables of line number info.  */
3051 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3052
3053 /* A flag to tell pubnames/types export if there is an info section to
3054    refer to.  */
3055 static bool info_section_emitted;
3056
3057 /* A pointer to the base of a table that contains a list of publicly
3058    accessible names.  */
3059 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3060
3061 /* A pointer to the base of a table that contains a list of publicly
3062    accessible types.  */
3063 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3064
3065 /* A pointer to the base of a table that contains a list of macro
3066    defines/undefines (and file start/end markers).  */
3067 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3068
3069 /* True if .debug_macinfo or .debug_macros section is going to be
3070    emitted.  */
3071 #define have_macinfo \
3072   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3073    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3074    && !macinfo_table->is_empty ())
3075
3076 /* Array of dies for which we should generate .debug_ranges info.  */
3077 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3078
3079 /* Number of elements currently allocated for ranges_table.  */
3080 static GTY(()) unsigned ranges_table_allocated;
3081
3082 /* Number of elements in ranges_table currently in use.  */
3083 static GTY(()) unsigned ranges_table_in_use;
3084
3085 /* Array of pairs of labels referenced in ranges_table.  */
3086 static GTY ((length ("ranges_by_label_allocated")))
3087      dw_ranges_by_label *ranges_by_label;
3088
3089 /* Number of elements currently allocated for ranges_by_label.  */
3090 static GTY(()) unsigned ranges_by_label_allocated;
3091
3092 /* Number of elements in ranges_by_label currently in use.  */
3093 static GTY(()) unsigned ranges_by_label_in_use;
3094
3095 /* Size (in elements) of increments by which we may expand the
3096    ranges_table.  */
3097 #define RANGES_TABLE_INCREMENT 64
3098
3099 /* Whether we have location lists that need outputting */
3100 static GTY(()) bool have_location_lists;
3101
3102 /* Unique label counter.  */
3103 static GTY(()) unsigned int loclabel_num;
3104
3105 /* Unique label counter for point-of-call tables.  */
3106 static GTY(()) unsigned int poc_label_num;
3107
3108 /* The last file entry emitted by maybe_emit_file().  */
3109 static GTY(()) struct dwarf_file_data * last_emitted_file;
3110
3111 /* Number of internal labels generated by gen_internal_sym().  */
3112 static GTY(()) int label_num;
3113
3114 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3115
3116 /* Instances of generic types for which we need to generate debug
3117    info that describe their generic parameters and arguments. That
3118    generation needs to happen once all types are properly laid out so
3119    we do it at the end of compilation.  */
3120 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3121
3122 /* Offset from the "steady-state frame pointer" to the frame base,
3123    within the current function.  */
3124 static HOST_WIDE_INT frame_pointer_fb_offset;
3125 static bool frame_pointer_fb_offset_valid;
3126
3127 static vec<dw_die_ref> base_types;
3128
3129 /* Flags to represent a set of attribute classes for attributes that represent
3130    a scalar value (bounds, pointers, ...).  */
3131 enum dw_scalar_form
3132 {
3133   dw_scalar_form_constant = 0x01,
3134   dw_scalar_form_exprloc = 0x02,
3135   dw_scalar_form_reference = 0x04
3136 };
3137
3138 /* Forward declarations for functions defined in this file.  */
3139
3140 static int is_pseudo_reg (const_rtx);
3141 static tree type_main_variant (tree);
3142 static int is_tagged_type (const_tree);
3143 static const char *dwarf_tag_name (unsigned);
3144 static const char *dwarf_attr_name (unsigned);
3145 static const char *dwarf_form_name (unsigned);
3146 static tree decl_ultimate_origin (const_tree);
3147 static tree decl_class_context (tree);
3148 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3149 static inline enum dw_val_class AT_class (dw_attr_node *);
3150 static inline unsigned int AT_index (dw_attr_node *);
3151 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3152 static inline unsigned AT_flag (dw_attr_node *);
3153 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3154 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3155 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3156 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3157 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3158                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3159 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3160                                unsigned int, unsigned char *);
3161 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3162 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3163 static inline const char *AT_string (dw_attr_node *);
3164 static enum dwarf_form AT_string_form (dw_attr_node *);
3165 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3166 static void add_AT_specification (dw_die_ref, dw_die_ref);
3167 static inline dw_die_ref AT_ref (dw_attr_node *);
3168 static inline int AT_ref_external (dw_attr_node *);
3169 static inline void set_AT_ref_external (dw_attr_node *, int);
3170 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3171 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3172 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3173 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3174                              dw_loc_list_ref);
3175 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3176 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3177 static void remove_addr_table_entry (addr_table_entry *);
3178 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3179 static inline rtx AT_addr (dw_attr_node *);
3180 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3181 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3182 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3183 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3184                            unsigned HOST_WIDE_INT);
3185 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3186                                unsigned long, bool);
3187 static inline const char *AT_lbl (dw_attr_node *);
3188 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3189 static const char *get_AT_low_pc (dw_die_ref);
3190 static const char *get_AT_hi_pc (dw_die_ref);
3191 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3192 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3193 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3194 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3195 static bool is_cxx (void);
3196 static bool is_fortran (void);
3197 static bool is_ada (void);
3198 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3199 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3200 static void add_child_die (dw_die_ref, dw_die_ref);
3201 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3202 static dw_die_ref lookup_type_die (tree);
3203 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3204 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3205 static void equate_type_number_to_die (tree, dw_die_ref);
3206 static dw_die_ref lookup_decl_die (tree);
3207 static var_loc_list *lookup_decl_loc (const_tree);
3208 static void equate_decl_number_to_die (tree, dw_die_ref);
3209 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3210 static void print_spaces (FILE *);
3211 static void print_die (dw_die_ref, FILE *);
3212 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3213 static dw_die_ref pop_compile_unit (dw_die_ref);
3214 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3215 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3216 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3217 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3218 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3219 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3220 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3221                                    struct md5_ctx *, int *);
3222 struct checksum_attributes;
3223 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3224 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3225 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3226 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3227 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3228 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3229 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3230 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3231 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3232 static void compute_section_prefix (dw_die_ref);
3233 static int is_type_die (dw_die_ref);
3234 static int is_comdat_die (dw_die_ref);
3235 static int is_symbol_die (dw_die_ref);
3236 static inline bool is_template_instantiation (dw_die_ref);
3237 static void assign_symbol_names (dw_die_ref);
3238 static void break_out_includes (dw_die_ref);
3239 static int is_declaration_die (dw_die_ref);
3240 static int should_move_die_to_comdat (dw_die_ref);
3241 static dw_die_ref clone_as_declaration (dw_die_ref);
3242 static dw_die_ref clone_die (dw_die_ref);
3243 static dw_die_ref clone_tree (dw_die_ref);
3244 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3245 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3246 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3247 static dw_die_ref generate_skeleton (dw_die_ref);
3248 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3249                                                          dw_die_ref,
3250                                                          dw_die_ref);
3251 static void break_out_comdat_types (dw_die_ref);
3252 static void copy_decls_for_unworthy_types (dw_die_ref);
3253
3254 static void add_sibling_attributes (dw_die_ref);
3255 static void output_location_lists (dw_die_ref);
3256 static int constant_size (unsigned HOST_WIDE_INT);
3257 static unsigned long size_of_die (dw_die_ref);
3258 static void calc_die_sizes (dw_die_ref);
3259 static void calc_base_type_die_sizes (void);
3260 static void mark_dies (dw_die_ref);
3261 static void unmark_dies (dw_die_ref);
3262 static void unmark_all_dies (dw_die_ref);
3263 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3264 static unsigned long size_of_aranges (void);
3265 static enum dwarf_form value_format (dw_attr_node *);
3266 static void output_value_format (dw_attr_node *);
3267 static void output_abbrev_section (void);
3268 static void output_die_abbrevs (unsigned long, dw_die_ref);
3269 static void output_die_symbol (dw_die_ref);
3270 static void output_die (dw_die_ref);
3271 static void output_compilation_unit_header (void);
3272 static void output_comp_unit (dw_die_ref, int);
3273 static void output_comdat_type_unit (comdat_type_node *);
3274 static const char *dwarf2_name (tree, int);
3275 static void add_pubname (tree, dw_die_ref);
3276 static void add_enumerator_pubname (const char *, dw_die_ref);
3277 static void add_pubname_string (const char *, dw_die_ref);
3278 static void add_pubtype (tree, dw_die_ref);
3279 static void output_pubnames (vec<pubname_entry, va_gc> *);
3280 static void output_aranges (void);
3281 static unsigned int add_ranges_num (int);
3282 static unsigned int add_ranges (const_tree);
3283 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3284                                   bool *, bool);
3285 static void output_ranges (void);
3286 static dw_line_info_table *new_line_info_table (void);
3287 static void output_line_info (bool);
3288 static void output_file_names (void);
3289 static dw_die_ref base_type_die (tree, bool);
3290 static int is_base_type (tree);
3291 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3292 static int decl_quals (const_tree);
3293 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3294 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3295 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3296 static int type_is_enum (const_tree);
3297 static unsigned int dbx_reg_number (const_rtx);
3298 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3299 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3300 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3301                                                 enum var_init_status);
3302 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3303                                                      enum var_init_status);
3304 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3305                                          enum var_init_status);
3306 static int is_based_loc (const_rtx);
3307 static bool resolve_one_addr (rtx *);
3308 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3309                                                enum var_init_status);
3310 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3311                                         enum var_init_status);
3312 struct loc_descr_context;
3313 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3314 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3315 static dw_loc_list_ref loc_list_from_tree (tree, int,
3316                                            const struct loc_descr_context *);
3317 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3318                                                   const struct loc_descr_context *);
3319 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3320 static tree field_type (const_tree);
3321 static unsigned int simple_type_align_in_bits (const_tree);
3322 static unsigned int simple_decl_align_in_bits (const_tree);
3323 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3324 struct vlr_context;
3325 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3326                                            HOST_WIDE_INT *);
3327 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3328                                          dw_loc_list_ref);
3329 static void add_data_member_location_attribute (dw_die_ref, tree,
3330                                                 struct vlr_context *);
3331 static bool add_const_value_attribute (dw_die_ref, rtx);
3332 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3333 static void insert_wide_int (const wide_int &, unsigned char *, int);
3334 static void insert_float (const_rtx, unsigned char *);
3335 static rtx rtl_for_decl_location (tree);
3336 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3337 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3338 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3339 static void add_name_attribute (dw_die_ref, const char *);
3340 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3341 static void add_comp_dir_attribute (dw_die_ref);
3342 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3343                              const struct loc_descr_context *);
3344 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3345                             const struct loc_descr_context *);
3346 static void add_subscript_info (dw_die_ref, tree, bool);
3347 static void add_byte_size_attribute (dw_die_ref, tree);
3348 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3349                                              struct vlr_context *);
3350 static void add_bit_size_attribute (dw_die_ref, tree);
3351 static void add_prototyped_attribute (dw_die_ref, tree);
3352 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3353 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3354 static void add_src_coords_attributes (dw_die_ref, tree);
3355 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3356 static void add_discr_value (dw_die_ref, dw_discr_value *);
3357 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3358 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3359 static void push_decl_scope (tree);
3360 static void pop_decl_scope (void);
3361 static dw_die_ref scope_die_for (tree, dw_die_ref);
3362 static inline int local_scope_p (dw_die_ref);
3363 static inline int class_scope_p (dw_die_ref);
3364 static inline int class_or_namespace_scope_p (dw_die_ref);
3365 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3366 static void add_calling_convention_attribute (dw_die_ref, tree);
3367 static const char *type_tag (const_tree);
3368 static tree member_declared_type (const_tree);
3369 #if 0
3370 static const char *decl_start_label (tree);
3371 #endif
3372 static void gen_array_type_die (tree, dw_die_ref);
3373 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3374 #if 0
3375 static void gen_entry_point_die (tree, dw_die_ref);
3376 #endif
3377 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3378 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3379 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3380 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3381 static void gen_formal_types_die (tree, dw_die_ref);
3382 static void gen_subprogram_die (tree, dw_die_ref);
3383 static void gen_variable_die (tree, tree, dw_die_ref);
3384 static void gen_const_die (tree, dw_die_ref);
3385 static void gen_label_die (tree, dw_die_ref);
3386 static void gen_lexical_block_die (tree, dw_die_ref);
3387 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3388 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3389 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3390 static dw_die_ref gen_compile_unit_die (const char *);
3391 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3392 static void gen_member_die (tree, dw_die_ref);
3393 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3394                                                 enum debug_info_usage);
3395 static void gen_subroutine_type_die (tree, dw_die_ref);
3396 static void gen_typedef_die (tree, dw_die_ref);
3397 static void gen_type_die (tree, dw_die_ref);
3398 static void gen_block_die (tree, dw_die_ref);
3399 static void decls_for_scope (tree, dw_die_ref);
3400 static bool is_naming_typedef_decl (const_tree);
3401 static inline dw_die_ref get_context_die (tree);
3402 static void gen_namespace_die (tree, dw_die_ref);
3403 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3404 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3405 static dw_die_ref force_decl_die (tree);
3406 static dw_die_ref force_type_die (tree);
3407 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3408 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3409 static struct dwarf_file_data * lookup_filename (const char *);
3410 static void retry_incomplete_types (void);
3411 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3412 static void gen_generic_params_dies (tree);
3413 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3414 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3415 static void splice_child_die (dw_die_ref, dw_die_ref);
3416 static int file_info_cmp (const void *, const void *);
3417 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3418                                      const char *, const char *);
3419 static void output_loc_list (dw_loc_list_ref);
3420 static char *gen_internal_sym (const char *);
3421 static bool want_pubnames (void);
3422
3423 static void prune_unmark_dies (dw_die_ref);
3424 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3425 static void prune_unused_types_mark (dw_die_ref, int);
3426 static void prune_unused_types_walk (dw_die_ref);
3427 static void prune_unused_types_walk_attribs (dw_die_ref);
3428 static void prune_unused_types_prune (dw_die_ref);
3429 static void prune_unused_types (void);
3430 static int maybe_emit_file (struct dwarf_file_data *fd);
3431 static inline const char *AT_vms_delta1 (dw_attr_node *);
3432 static inline const char *AT_vms_delta2 (dw_attr_node *);
3433 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3434                                      const char *, const char *);
3435 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3436 static void gen_remaining_tmpl_value_param_die_attribute (void);
3437 static bool generic_type_p (tree);
3438 static void schedule_generic_params_dies_gen (tree t);
3439 static void gen_scheduled_generic_parms_dies (void);
3440
3441 static const char *comp_dir_string (void);
3442
3443 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3444
3445 /* enum for tracking thread-local variables whose address is really an offset
3446    relative to the TLS pointer, which will need link-time relocation, but will
3447    not need relocation by the DWARF consumer.  */
3448
3449 enum dtprel_bool
3450 {
3451   dtprel_false = 0,
3452   dtprel_true = 1
3453 };
3454
3455 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3456    use DW_OP_const*.  For regular variables, which need both link-time
3457    relocation and consumer-level relocation (e.g., to account for shared objects
3458    loaded at a random address), we use DW_OP_addr*.  */
3459
3460 static inline enum dwarf_location_atom
3461 dw_addr_op (enum dtprel_bool dtprel)
3462 {
3463   if (dtprel == dtprel_true)
3464     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3465             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3466   else
3467     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3468 }
3469
3470 /* Return a pointer to a newly allocated address location description.  If
3471    dwarf_split_debug_info is true, then record the address with the appropriate
3472    relocation.  */
3473 static inline dw_loc_descr_ref
3474 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3475 {
3476   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3477
3478   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3479   ref->dw_loc_oprnd1.v.val_addr = addr;
3480   ref->dtprel = dtprel;
3481   if (dwarf_split_debug_info)
3482     ref->dw_loc_oprnd1.val_entry
3483         = add_addr_table_entry (addr,
3484                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3485   else
3486     ref->dw_loc_oprnd1.val_entry = NULL;
3487
3488   return ref;
3489 }
3490
3491 /* Section names used to hold DWARF debugging information.  */
3492
3493 #ifndef DEBUG_INFO_SECTION
3494 #define DEBUG_INFO_SECTION      ".debug_info"
3495 #endif
3496 #ifndef DEBUG_DWO_INFO_SECTION
3497 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3498 #endif
3499 #ifndef DEBUG_ABBREV_SECTION
3500 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3501 #endif
3502 #ifndef DEBUG_DWO_ABBREV_SECTION
3503 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3504 #endif
3505 #ifndef DEBUG_ARANGES_SECTION
3506 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3507 #endif
3508 #ifndef DEBUG_ADDR_SECTION
3509 #define DEBUG_ADDR_SECTION     ".debug_addr"
3510 #endif
3511 #ifndef DEBUG_NORM_MACINFO_SECTION
3512 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3513 #endif
3514 #ifndef DEBUG_DWO_MACINFO_SECTION
3515 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3516 #endif
3517 #ifndef DEBUG_MACINFO_SECTION
3518 #define DEBUG_MACINFO_SECTION                                           \
3519   (!dwarf_split_debug_info                                              \
3520    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3521 #endif
3522 #ifndef DEBUG_NORM_MACRO_SECTION
3523 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3524 #endif
3525 #ifndef DEBUG_DWO_MACRO_SECTION
3526 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3527 #endif
3528 #ifndef DEBUG_MACRO_SECTION
3529 #define DEBUG_MACRO_SECTION                                             \
3530   (!dwarf_split_debug_info                                              \
3531    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3532 #endif
3533 #ifndef DEBUG_LINE_SECTION
3534 #define DEBUG_LINE_SECTION      ".debug_line"
3535 #endif
3536 #ifndef DEBUG_DWO_LINE_SECTION
3537 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3538 #endif
3539 #ifndef DEBUG_LOC_SECTION
3540 #define DEBUG_LOC_SECTION       ".debug_loc"
3541 #endif
3542 #ifndef DEBUG_DWO_LOC_SECTION
3543 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3544 #endif
3545 #ifndef DEBUG_PUBNAMES_SECTION
3546 #define DEBUG_PUBNAMES_SECTION  \
3547   ((debug_generate_pub_sections == 2) \
3548    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3549 #endif
3550 #ifndef DEBUG_PUBTYPES_SECTION
3551 #define DEBUG_PUBTYPES_SECTION  \
3552   ((debug_generate_pub_sections == 2) \
3553    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3554 #endif
3555 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3556 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3557 #ifndef DEBUG_STR_OFFSETS_SECTION
3558 #define DEBUG_STR_OFFSETS_SECTION                                       \
3559   (!dwarf_split_debug_info                                              \
3560    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3561 #endif
3562 #ifndef DEBUG_STR_DWO_SECTION
3563 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3564 #endif
3565 #ifndef DEBUG_STR_SECTION
3566 #define DEBUG_STR_SECTION  ".debug_str"
3567 #endif
3568 #ifndef DEBUG_RANGES_SECTION
3569 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3570 #endif
3571
3572 /* Standard ELF section names for compiled code and data.  */
3573 #ifndef TEXT_SECTION_NAME
3574 #define TEXT_SECTION_NAME       ".text"
3575 #endif
3576
3577 /* Section flags for .debug_macinfo/.debug_macro section.  */
3578 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3579   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3580
3581 /* Section flags for .debug_str section.  */
3582 #define DEBUG_STR_SECTION_FLAGS                                 \
3583   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3584    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3585    : SECTION_DEBUG)
3586
3587 /* Section flags for .debug_str.dwo section.  */
3588 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3589
3590 /* Labels we insert at beginning sections we can reference instead of
3591    the section names themselves.  */
3592
3593 #ifndef TEXT_SECTION_LABEL
3594 #define TEXT_SECTION_LABEL                 "Ltext"
3595 #endif
3596 #ifndef COLD_TEXT_SECTION_LABEL
3597 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3598 #endif
3599 #ifndef DEBUG_LINE_SECTION_LABEL
3600 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3601 #endif
3602 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3603 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3604 #endif
3605 #ifndef DEBUG_INFO_SECTION_LABEL
3606 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3607 #endif
3608 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3609 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3610 #endif
3611 #ifndef DEBUG_ABBREV_SECTION_LABEL
3612 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3613 #endif
3614 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3615 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3616 #endif
3617 #ifndef DEBUG_ADDR_SECTION_LABEL
3618 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3619 #endif
3620 #ifndef DEBUG_LOC_SECTION_LABEL
3621 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3622 #endif
3623 #ifndef DEBUG_RANGES_SECTION_LABEL
3624 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3625 #endif
3626 #ifndef DEBUG_MACINFO_SECTION_LABEL
3627 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3628 #endif
3629 #ifndef DEBUG_MACRO_SECTION_LABEL
3630 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3631 #endif
3632 #define SKELETON_COMP_DIE_ABBREV 1
3633 #define SKELETON_TYPE_DIE_ABBREV 2
3634
3635 /* Definitions of defaults for formats and names of various special
3636    (artificial) labels which may be generated within this file (when the -g
3637    options is used and DWARF2_DEBUGGING_INFO is in effect.
3638    If necessary, these may be overridden from within the tm.h file, but
3639    typically, overriding these defaults is unnecessary.  */
3640
3641 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3642 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3643 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3644 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3645 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3646 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3647 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3655
3656 #ifndef TEXT_END_LABEL
3657 #define TEXT_END_LABEL          "Letext"
3658 #endif
3659 #ifndef COLD_END_LABEL
3660 #define COLD_END_LABEL          "Letext_cold"
3661 #endif
3662 #ifndef BLOCK_BEGIN_LABEL
3663 #define BLOCK_BEGIN_LABEL       "LBB"
3664 #endif
3665 #ifndef BLOCK_END_LABEL
3666 #define BLOCK_END_LABEL         "LBE"
3667 #endif
3668 #ifndef LINE_CODE_LABEL
3669 #define LINE_CODE_LABEL         "LM"
3670 #endif
3671
3672 \f
3673 /* Return the root of the DIE's built for the current compilation unit.  */
3674 static dw_die_ref
3675 comp_unit_die (void)
3676 {
3677   if (!single_comp_unit_die)
3678     single_comp_unit_die = gen_compile_unit_die (NULL);
3679   return single_comp_unit_die;
3680 }
3681
3682 /* We allow a language front-end to designate a function that is to be
3683    called to "demangle" any name before it is put into a DIE.  */
3684
3685 static const char *(*demangle_name_func) (const char *);
3686
3687 void
3688 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3689 {
3690   demangle_name_func = func;
3691 }
3692
3693 /* Test if rtl node points to a pseudo register.  */
3694
3695 static inline int
3696 is_pseudo_reg (const_rtx rtl)
3697 {
3698   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3699           || (GET_CODE (rtl) == SUBREG
3700               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3701 }
3702
3703 /* Return a reference to a type, with its const and volatile qualifiers
3704    removed.  */
3705
3706 static inline tree
3707 type_main_variant (tree type)
3708 {
3709   type = TYPE_MAIN_VARIANT (type);
3710
3711   /* ??? There really should be only one main variant among any group of
3712      variants of a given type (and all of the MAIN_VARIANT values for all
3713      members of the group should point to that one type) but sometimes the C
3714      front-end messes this up for array types, so we work around that bug
3715      here.  */
3716   if (TREE_CODE (type) == ARRAY_TYPE)
3717     while (type != TYPE_MAIN_VARIANT (type))
3718       type = TYPE_MAIN_VARIANT (type);
3719
3720   return type;
3721 }
3722
3723 /* Return nonzero if the given type node represents a tagged type.  */
3724
3725 static inline int
3726 is_tagged_type (const_tree type)
3727 {
3728   enum tree_code code = TREE_CODE (type);
3729
3730   return (code == RECORD_TYPE || code == UNION_TYPE
3731           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3732 }
3733
3734 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3735
3736 static void
3737 get_ref_die_offset_label (char *label, dw_die_ref ref)
3738 {
3739   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3740 }
3741
3742 /* Return die_offset of a DIE reference to a base type.  */
3743
3744 static unsigned long int
3745 get_base_type_offset (dw_die_ref ref)
3746 {
3747   if (ref->die_offset)
3748     return ref->die_offset;
3749   if (comp_unit_die ()->die_abbrev)
3750     {
3751       calc_base_type_die_sizes ();
3752       gcc_assert (ref->die_offset);
3753     }
3754   return ref->die_offset;
3755 }
3756
3757 /* Return die_offset of a DIE reference other than base type.  */
3758
3759 static unsigned long int
3760 get_ref_die_offset (dw_die_ref ref)
3761 {
3762   gcc_assert (ref->die_offset);
3763   return ref->die_offset;
3764 }
3765
3766 /* Convert a DIE tag into its string name.  */
3767
3768 static const char *
3769 dwarf_tag_name (unsigned int tag)
3770 {
3771   const char *name = get_DW_TAG_name (tag);
3772
3773   if (name != NULL)
3774     return name;
3775
3776   return "DW_TAG_<unknown>";
3777 }
3778
3779 /* Convert a DWARF attribute code into its string name.  */
3780
3781 static const char *
3782 dwarf_attr_name (unsigned int attr)
3783 {
3784   const char *name;
3785
3786   switch (attr)
3787     {
3788 #if VMS_DEBUGGING_INFO
3789     case DW_AT_HP_prologue:
3790       return "DW_AT_HP_prologue";
3791 #else
3792     case DW_AT_MIPS_loop_unroll_factor:
3793       return "DW_AT_MIPS_loop_unroll_factor";
3794 #endif
3795
3796 #if VMS_DEBUGGING_INFO
3797     case DW_AT_HP_epilogue:
3798       return "DW_AT_HP_epilogue";
3799 #else
3800     case DW_AT_MIPS_stride:
3801       return "DW_AT_MIPS_stride";
3802 #endif
3803     }
3804
3805   name = get_DW_AT_name (attr);
3806
3807   if (name != NULL)
3808     return name;
3809
3810   return "DW_AT_<unknown>";
3811 }
3812
3813 /* Convert a DWARF value form code into its string name.  */
3814
3815 static const char *
3816 dwarf_form_name (unsigned int form)
3817 {
3818   const char *name = get_DW_FORM_name (form);
3819
3820   if (name != NULL)
3821     return name;
3822
3823   return "DW_FORM_<unknown>";
3824 }
3825 \f
3826 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3827    instance of an inlined instance of a decl which is local to an inline
3828    function, so we have to trace all of the way back through the origin chain
3829    to find out what sort of node actually served as the original seed for the
3830    given block.  */
3831
3832 static tree
3833 decl_ultimate_origin (const_tree decl)
3834 {
3835   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3836     return NULL_TREE;
3837
3838   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3839      we're trying to output the abstract instance of this function.  */
3840   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3841     return NULL_TREE;
3842
3843   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3844      most distant ancestor, this should never happen.  */
3845   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3846
3847   return DECL_ABSTRACT_ORIGIN (decl);
3848 }
3849
3850 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3851    of a virtual function may refer to a base class, so we check the 'this'
3852    parameter.  */
3853
3854 static tree
3855 decl_class_context (tree decl)
3856 {
3857   tree context = NULL_TREE;
3858
3859   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3860     context = DECL_CONTEXT (decl);
3861   else
3862     context = TYPE_MAIN_VARIANT
3863       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3864
3865   if (context && !TYPE_P (context))
3866     context = NULL_TREE;
3867
3868   return context;
3869 }
3870 \f
3871 /* Add an attribute/value pair to a DIE.  */
3872
3873 static inline void
3874 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3875 {
3876   /* Maybe this should be an assert?  */
3877   if (die == NULL)
3878     return;
3879
3880   vec_safe_reserve (die->die_attr, 1);
3881   vec_safe_push (die->die_attr, *attr);
3882 }
3883
3884 static inline enum dw_val_class
3885 AT_class (dw_attr_node *a)
3886 {
3887   return a->dw_attr_val.val_class;
3888 }
3889
3890 /* Return the index for any attribute that will be referenced with a
3891    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3892    are stored in dw_attr_val.v.val_str for reference counting
3893    pruning.  */
3894
3895 static inline unsigned int
3896 AT_index (dw_attr_node *a)
3897 {
3898   if (AT_class (a) == dw_val_class_str)
3899     return a->dw_attr_val.v.val_str->index;
3900   else if (a->dw_attr_val.val_entry != NULL)
3901     return a->dw_attr_val.val_entry->index;
3902   return NOT_INDEXED;
3903 }
3904
3905 /* Add a flag value attribute to a DIE.  */
3906
3907 static inline void
3908 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3909 {
3910   dw_attr_node attr;
3911
3912   attr.dw_attr = attr_kind;
3913   attr.dw_attr_val.val_class = dw_val_class_flag;
3914   attr.dw_attr_val.val_entry = NULL;
3915   attr.dw_attr_val.v.val_flag = flag;
3916   add_dwarf_attr (die, &attr);
3917 }
3918
3919 static inline unsigned
3920 AT_flag (dw_attr_node *a)
3921 {
3922   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3923   return a->dw_attr_val.v.val_flag;
3924 }
3925
3926 /* Add a signed integer attribute value to a DIE.  */
3927
3928 static inline void
3929 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3930 {
3931   dw_attr_node attr;
3932
3933   attr.dw_attr = attr_kind;
3934   attr.dw_attr_val.val_class = dw_val_class_const;
3935   attr.dw_attr_val.val_entry = NULL;
3936   attr.dw_attr_val.v.val_int = int_val;
3937   add_dwarf_attr (die, &attr);
3938 }
3939
3940 static inline HOST_WIDE_INT
3941 AT_int (dw_attr_node *a)
3942 {
3943   gcc_assert (a && AT_class (a) == dw_val_class_const);
3944   return a->dw_attr_val.v.val_int;
3945 }
3946
3947 /* Add an unsigned integer attribute value to a DIE.  */
3948
3949 static inline void
3950 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3951                  unsigned HOST_WIDE_INT unsigned_val)
3952 {
3953   dw_attr_node attr;
3954
3955   attr.dw_attr = attr_kind;
3956   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3957   attr.dw_attr_val.val_entry = NULL;
3958   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3959   add_dwarf_attr (die, &attr);
3960 }
3961
3962 static inline unsigned HOST_WIDE_INT
3963 AT_unsigned (dw_attr_node *a)
3964 {
3965   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3966   return a->dw_attr_val.v.val_unsigned;
3967 }
3968
3969 /* Add an unsigned wide integer attribute value to a DIE.  */
3970
3971 static inline void
3972 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3973              const wide_int& w)
3974 {
3975   dw_attr_node attr;
3976
3977   attr.dw_attr = attr_kind;
3978   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3979   attr.dw_attr_val.val_entry = NULL;
3980   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3981   *attr.dw_attr_val.v.val_wide = w;
3982   add_dwarf_attr (die, &attr);
3983 }
3984
3985 /* Add an unsigned double integer attribute value to a DIE.  */
3986
3987 static inline void
3988 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3989                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3990 {
3991   dw_attr_node attr;
3992
3993   attr.dw_attr = attr_kind;
3994   attr.dw_attr_val.val_class = dw_val_class_const_double;
3995   attr.dw_attr_val.val_entry = NULL;
3996   attr.dw_attr_val.v.val_double.high = high;
3997   attr.dw_attr_val.v.val_double.low = low;
3998   add_dwarf_attr (die, &attr);
3999 }
4000
4001 /* Add a floating point attribute value to a DIE and return it.  */
4002
4003 static inline void
4004 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4005             unsigned int length, unsigned int elt_size, unsigned char *array)
4006 {
4007   dw_attr_node attr;
4008
4009   attr.dw_attr = attr_kind;
4010   attr.dw_attr_val.val_class = dw_val_class_vec;
4011   attr.dw_attr_val.val_entry = NULL;
4012   attr.dw_attr_val.v.val_vec.length = length;
4013   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4014   attr.dw_attr_val.v.val_vec.array = array;
4015   add_dwarf_attr (die, &attr);
4016 }
4017
4018 /* Add an 8-byte data attribute value to a DIE.  */
4019
4020 static inline void
4021 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4022               unsigned char data8[8])
4023 {
4024   dw_attr_node attr;
4025
4026   attr.dw_attr = attr_kind;
4027   attr.dw_attr_val.val_class = dw_val_class_data8;
4028   attr.dw_attr_val.val_entry = NULL;
4029   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4030   add_dwarf_attr (die, &attr);
4031 }
4032
4033 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
4034    dwarf_split_debug_info, address attributes in dies destined for the
4035    final executable have force_direct set to avoid using indexed
4036    references.  */
4037
4038 static inline void
4039 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4040                     bool force_direct)
4041 {
4042   dw_attr_node attr;
4043   char * lbl_id;
4044
4045   lbl_id = xstrdup (lbl_low);
4046   attr.dw_attr = DW_AT_low_pc;
4047   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4048   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4049   if (dwarf_split_debug_info && !force_direct)
4050     attr.dw_attr_val.val_entry
4051         = add_addr_table_entry (lbl_id, ate_kind_label);
4052   else
4053     attr.dw_attr_val.val_entry = NULL;
4054   add_dwarf_attr (die, &attr);
4055
4056   attr.dw_attr = DW_AT_high_pc;
4057   if (dwarf_version < 4)
4058     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4059   else
4060     attr.dw_attr_val.val_class = dw_val_class_high_pc;
4061   lbl_id = xstrdup (lbl_high);
4062   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4063   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4064       && dwarf_split_debug_info && !force_direct)
4065     attr.dw_attr_val.val_entry
4066         = add_addr_table_entry (lbl_id, ate_kind_label);
4067   else
4068     attr.dw_attr_val.val_entry = NULL;
4069   add_dwarf_attr (die, &attr);
4070 }
4071
4072 /* Hash and equality functions for debug_str_hash.  */
4073
4074 hashval_t
4075 indirect_string_hasher::hash (indirect_string_node *x)
4076 {
4077   return htab_hash_string (x->str);
4078 }
4079
4080 bool
4081 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4082 {
4083   return strcmp (x1->str, x2) == 0;
4084 }
4085
4086 /* Add STR to the given string hash table.  */
4087
4088 static struct indirect_string_node *
4089 find_AT_string_in_table (const char *str,
4090                          hash_table<indirect_string_hasher> *table)
4091 {
4092   struct indirect_string_node *node;
4093
4094   indirect_string_node **slot
4095     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4096   if (*slot == NULL)
4097     {
4098       node = ggc_cleared_alloc<indirect_string_node> ();
4099       node->str = ggc_strdup (str);
4100       *slot = node;
4101     }
4102   else
4103     node = *slot;
4104
4105   node->refcount++;
4106   return node;
4107 }
4108
4109 /* Add STR to the indirect string hash table.  */
4110
4111 static struct indirect_string_node *
4112 find_AT_string (const char *str)
4113 {
4114   if (! debug_str_hash)
4115     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4116
4117   return find_AT_string_in_table (str, debug_str_hash);
4118 }
4119
4120 /* Add a string attribute value to a DIE.  */
4121
4122 static inline void
4123 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4124 {
4125   dw_attr_node attr;
4126   struct indirect_string_node *node;
4127
4128   node = find_AT_string (str);
4129
4130   attr.dw_attr = attr_kind;
4131   attr.dw_attr_val.val_class = dw_val_class_str;
4132   attr.dw_attr_val.val_entry = NULL;
4133   attr.dw_attr_val.v.val_str = node;
4134   add_dwarf_attr (die, &attr);
4135 }
4136
4137 static inline const char *
4138 AT_string (dw_attr_node *a)
4139 {
4140   gcc_assert (a && AT_class (a) == dw_val_class_str);
4141   return a->dw_attr_val.v.val_str->str;
4142 }
4143
4144 /* Call this function directly to bypass AT_string_form's logic to put
4145    the string inline in the die. */
4146
4147 static void
4148 set_indirect_string (struct indirect_string_node *node)
4149 {
4150   char label[32];
4151   /* Already indirect is a no op.  */
4152   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4153     {
4154       gcc_assert (node->label);
4155       return;
4156     }
4157   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4158   ++dw2_string_counter;
4159   node->label = xstrdup (label);
4160
4161   if (!dwarf_split_debug_info)
4162     {
4163       node->form = DW_FORM_strp;
4164       node->index = NOT_INDEXED;
4165     }
4166   else
4167     {
4168       node->form = DW_FORM_GNU_str_index;
4169       node->index = NO_INDEX_ASSIGNED;
4170     }
4171 }
4172
4173 /* Find out whether a string should be output inline in DIE
4174    or out-of-line in .debug_str section.  */
4175
4176 static enum dwarf_form
4177 find_string_form (struct indirect_string_node *node)
4178 {
4179   unsigned int len;
4180
4181   if (node->form)
4182     return node->form;
4183
4184   len = strlen (node->str) + 1;
4185
4186   /* If the string is shorter or equal to the size of the reference, it is
4187      always better to put it inline.  */
4188   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4189     return node->form = DW_FORM_string;
4190
4191   /* If we cannot expect the linker to merge strings in .debug_str
4192      section, only put it into .debug_str if it is worth even in this
4193      single module.  */
4194   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4195       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4196       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4197     return node->form = DW_FORM_string;
4198
4199   set_indirect_string (node);
4200
4201   return node->form;
4202 }
4203
4204 /* Find out whether the string referenced from the attribute should be
4205    output inline in DIE or out-of-line in .debug_str section.  */
4206
4207 static enum dwarf_form
4208 AT_string_form (dw_attr_node *a)
4209 {
4210   gcc_assert (a && AT_class (a) == dw_val_class_str);
4211   return find_string_form (a->dw_attr_val.v.val_str);
4212 }
4213
4214 /* Add a DIE reference attribute value to a DIE.  */
4215
4216 static inline void
4217 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4218 {
4219   dw_attr_node attr;
4220   gcc_checking_assert (targ_die != NULL);
4221
4222   /* With LTO we can end up trying to reference something we didn't create
4223      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4224   if (targ_die == NULL)
4225     return;
4226
4227   attr.dw_attr = attr_kind;
4228   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4229   attr.dw_attr_val.val_entry = NULL;
4230   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4231   attr.dw_attr_val.v.val_die_ref.external = 0;
4232   add_dwarf_attr (die, &attr);
4233 }
4234
4235 /* Change DIE reference REF to point to NEW_DIE instead.  */
4236
4237 static inline void
4238 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4239 {
4240   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4241   ref->dw_attr_val.v.val_die_ref.die = new_die;
4242   ref->dw_attr_val.v.val_die_ref.external = 0;
4243 }
4244
4245 /* Add an AT_specification attribute to a DIE, and also make the back
4246    pointer from the specification to the definition.  */
4247
4248 static inline void
4249 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4250 {
4251   add_AT_die_ref (die, DW_AT_specification, targ_die);
4252   gcc_assert (!targ_die->die_definition);
4253   targ_die->die_definition = die;
4254 }
4255
4256 static inline dw_die_ref
4257 AT_ref (dw_attr_node *a)
4258 {
4259   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4260   return a->dw_attr_val.v.val_die_ref.die;
4261 }
4262
4263 static inline int
4264 AT_ref_external (dw_attr_node *a)
4265 {
4266   if (a && AT_class (a) == dw_val_class_die_ref)
4267     return a->dw_attr_val.v.val_die_ref.external;
4268
4269   return 0;
4270 }
4271
4272 static inline void
4273 set_AT_ref_external (dw_attr_node *a, int i)
4274 {
4275   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4276   a->dw_attr_val.v.val_die_ref.external = i;
4277 }
4278
4279 /* Add an FDE reference attribute value to a DIE.  */
4280
4281 static inline void
4282 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4283 {
4284   dw_attr_node attr;
4285
4286   attr.dw_attr = attr_kind;
4287   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4288   attr.dw_attr_val.val_entry = NULL;
4289   attr.dw_attr_val.v.val_fde_index = targ_fde;
4290   add_dwarf_attr (die, &attr);
4291 }
4292
4293 /* Add a location description attribute value to a DIE.  */
4294
4295 static inline void
4296 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4297 {
4298   dw_attr_node attr;
4299
4300   attr.dw_attr = attr_kind;
4301   attr.dw_attr_val.val_class = dw_val_class_loc;
4302   attr.dw_attr_val.val_entry = NULL;
4303   attr.dw_attr_val.v.val_loc = loc;
4304   add_dwarf_attr (die, &attr);
4305 }
4306
4307 static inline dw_loc_descr_ref
4308 AT_loc (dw_attr_node *a)
4309 {
4310   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4311   return a->dw_attr_val.v.val_loc;
4312 }
4313
4314 static inline void
4315 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4316 {
4317   dw_attr_node attr;
4318
4319   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4320     return;
4321
4322   attr.dw_attr = attr_kind;
4323   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4324   attr.dw_attr_val.val_entry = NULL;
4325   attr.dw_attr_val.v.val_loc_list = loc_list;
4326   add_dwarf_attr (die, &attr);
4327   have_location_lists = true;
4328 }
4329
4330 static inline dw_loc_list_ref
4331 AT_loc_list (dw_attr_node *a)
4332 {
4333   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4334   return a->dw_attr_val.v.val_loc_list;
4335 }
4336
4337 static inline dw_loc_list_ref *
4338 AT_loc_list_ptr (dw_attr_node *a)
4339 {
4340   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4341   return &a->dw_attr_val.v.val_loc_list;
4342 }
4343
4344 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4345 {
4346   static hashval_t hash (addr_table_entry *);
4347   static bool equal (addr_table_entry *, addr_table_entry *);
4348 };
4349
4350 /* Table of entries into the .debug_addr section.  */
4351
4352 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4353
4354 /* Hash an address_table_entry.  */
4355
4356 hashval_t
4357 addr_hasher::hash (addr_table_entry *a)
4358 {
4359   inchash::hash hstate;
4360   switch (a->kind)
4361     {
4362       case ate_kind_rtx:
4363         hstate.add_int (0);
4364         break;
4365       case ate_kind_rtx_dtprel:
4366         hstate.add_int (1);
4367         break;
4368       case ate_kind_label:
4369         return htab_hash_string (a->addr.label);
4370       default:
4371         gcc_unreachable ();
4372     }
4373   inchash::add_rtx (a->addr.rtl, hstate);
4374   return hstate.end ();
4375 }
4376
4377 /* Determine equality for two address_table_entries.  */
4378
4379 bool
4380 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4381 {
4382   if (a1->kind != a2->kind)
4383     return 0;
4384   switch (a1->kind)
4385     {
4386       case ate_kind_rtx:
4387       case ate_kind_rtx_dtprel:
4388         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4389       case ate_kind_label:
4390         return strcmp (a1->addr.label, a2->addr.label) == 0;
4391       default:
4392         gcc_unreachable ();
4393     }
4394 }
4395
4396 /* Initialize an addr_table_entry.  */
4397
4398 void
4399 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4400 {
4401   e->kind = kind;
4402   switch (kind)
4403     {
4404       case ate_kind_rtx:
4405       case ate_kind_rtx_dtprel:
4406         e->addr.rtl = (rtx) addr;
4407         break;
4408       case ate_kind_label:
4409         e->addr.label = (char *) addr;
4410         break;
4411     }
4412   e->refcount = 0;
4413   e->index = NO_INDEX_ASSIGNED;
4414 }
4415
4416 /* Add attr to the address table entry to the table.  Defer setting an
4417    index until output time.  */
4418
4419 static addr_table_entry *
4420 add_addr_table_entry (void *addr, enum ate_kind kind)
4421 {
4422   addr_table_entry *node;
4423   addr_table_entry finder;
4424
4425   gcc_assert (dwarf_split_debug_info);
4426   if (! addr_index_table)
4427     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4428   init_addr_table_entry (&finder, kind, addr);
4429   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4430
4431   if (*slot == HTAB_EMPTY_ENTRY)
4432     {
4433       node = ggc_cleared_alloc<addr_table_entry> ();
4434       init_addr_table_entry (node, kind, addr);
4435       *slot = node;
4436     }
4437   else
4438     node = *slot;
4439
4440   node->refcount++;
4441   return node;
4442 }
4443
4444 /* Remove an entry from the addr table by decrementing its refcount.
4445    Strictly, decrementing the refcount would be enough, but the
4446    assertion that the entry is actually in the table has found
4447    bugs.  */
4448
4449 static void
4450 remove_addr_table_entry (addr_table_entry *entry)
4451 {
4452   gcc_assert (dwarf_split_debug_info && addr_index_table);
4453   /* After an index is assigned, the table is frozen.  */
4454   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4455   entry->refcount--;
4456 }
4457
4458 /* Given a location list, remove all addresses it refers to from the
4459    address_table.  */
4460
4461 static void
4462 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4463 {
4464   for (; descr; descr = descr->dw_loc_next)
4465     if (descr->dw_loc_oprnd1.val_entry != NULL)
4466       {
4467         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4468         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4469       }
4470 }
4471
4472 /* A helper function for dwarf2out_finish called through
4473    htab_traverse.  Assign an addr_table_entry its index.  All entries
4474    must be collected into the table when this function is called,
4475    because the indexing code relies on htab_traverse to traverse nodes
4476    in the same order for each run. */
4477
4478 int
4479 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4480 {
4481   addr_table_entry *node = *h;
4482
4483   /* Don't index unreferenced nodes.  */
4484   if (node->refcount == 0)
4485     return 1;
4486
4487   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4488   node->index = *index;
4489   *index += 1;
4490
4491   return 1;
4492 }
4493
4494 /* Add an address constant attribute value to a DIE.  When using
4495    dwarf_split_debug_info, address attributes in dies destined for the
4496    final executable should be direct references--setting the parameter
4497    force_direct ensures this behavior.  */
4498
4499 static inline void
4500 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4501              bool force_direct)
4502 {
4503   dw_attr_node attr;
4504
4505   attr.dw_attr = attr_kind;
4506   attr.dw_attr_val.val_class = dw_val_class_addr;
4507   attr.dw_attr_val.v.val_addr = addr;
4508   if (dwarf_split_debug_info && !force_direct)
4509     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4510   else
4511     attr.dw_attr_val.val_entry = NULL;
4512   add_dwarf_attr (die, &attr);
4513 }
4514
4515 /* Get the RTX from to an address DIE attribute.  */
4516
4517 static inline rtx
4518 AT_addr (dw_attr_node *a)
4519 {
4520   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4521   return a->dw_attr_val.v.val_addr;
4522 }
4523
4524 /* Add a file attribute value to a DIE.  */
4525
4526 static inline void
4527 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4528              struct dwarf_file_data *fd)
4529 {
4530   dw_attr_node attr;
4531
4532   attr.dw_attr = attr_kind;
4533   attr.dw_attr_val.val_class = dw_val_class_file;
4534   attr.dw_attr_val.val_entry = NULL;
4535   attr.dw_attr_val.v.val_file = fd;
4536   add_dwarf_attr (die, &attr);
4537 }
4538
4539 /* Get the dwarf_file_data from a file DIE attribute.  */
4540
4541 static inline struct dwarf_file_data *
4542 AT_file (dw_attr_node *a)
4543 {
4544   gcc_assert (a && AT_class (a) == dw_val_class_file);
4545   return a->dw_attr_val.v.val_file;
4546 }
4547
4548 /* Add a vms delta attribute value to a DIE.  */
4549
4550 static inline void
4551 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4552                   const char *lbl1, const char *lbl2)
4553 {
4554   dw_attr_node attr;
4555
4556   attr.dw_attr = attr_kind;
4557   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4558   attr.dw_attr_val.val_entry = NULL;
4559   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4560   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4561   add_dwarf_attr (die, &attr);
4562 }
4563
4564 /* Add a label identifier attribute value to a DIE.  */
4565
4566 static inline void
4567 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4568                const char *lbl_id)
4569 {
4570   dw_attr_node attr;
4571
4572   attr.dw_attr = attr_kind;
4573   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4574   attr.dw_attr_val.val_entry = NULL;
4575   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4576   if (dwarf_split_debug_info)
4577     attr.dw_attr_val.val_entry
4578         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4579                                 ate_kind_label);
4580   add_dwarf_attr (die, &attr);
4581 }
4582
4583 /* Add a section offset attribute value to a DIE, an offset into the
4584    debug_line section.  */
4585
4586 static inline void
4587 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4588                 const char *label)
4589 {
4590   dw_attr_node attr;
4591
4592   attr.dw_attr = attr_kind;
4593   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4594   attr.dw_attr_val.val_entry = NULL;
4595   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4596   add_dwarf_attr (die, &attr);
4597 }
4598
4599 /* Add a section offset attribute value to a DIE, an offset into the
4600    debug_macinfo section.  */
4601
4602 static inline void
4603 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4604                const char *label)
4605 {
4606   dw_attr_node attr;
4607
4608   attr.dw_attr = attr_kind;
4609   attr.dw_attr_val.val_class = dw_val_class_macptr;
4610   attr.dw_attr_val.val_entry = NULL;
4611   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4612   add_dwarf_attr (die, &attr);
4613 }
4614
4615 /* Add an offset attribute value to a DIE.  */
4616
4617 static inline void
4618 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4619                unsigned HOST_WIDE_INT offset)
4620 {
4621   dw_attr_node attr;
4622
4623   attr.dw_attr = attr_kind;
4624   attr.dw_attr_val.val_class = dw_val_class_offset;
4625   attr.dw_attr_val.val_entry = NULL;
4626   attr.dw_attr_val.v.val_offset = offset;
4627   add_dwarf_attr (die, &attr);
4628 }
4629
4630 /* Add a range_list attribute value to a DIE.  When using
4631    dwarf_split_debug_info, address attributes in dies destined for the
4632    final executable should be direct references--setting the parameter
4633    force_direct ensures this behavior.  */
4634
4635 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4636 #define RELOCATED_OFFSET (NULL)
4637
4638 static void
4639 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4640                    long unsigned int offset, bool force_direct)
4641 {
4642   dw_attr_node attr;
4643
4644   attr.dw_attr = attr_kind;
4645   attr.dw_attr_val.val_class = dw_val_class_range_list;
4646   /* For the range_list attribute, use val_entry to store whether the
4647      offset should follow split-debug-info or normal semantics.  This
4648      value is read in output_range_list_offset.  */
4649   if (dwarf_split_debug_info && !force_direct)
4650     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4651   else
4652     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4653   attr.dw_attr_val.v.val_offset = offset;
4654   add_dwarf_attr (die, &attr);
4655 }
4656
4657 /* Return the start label of a delta attribute.  */
4658
4659 static inline const char *
4660 AT_vms_delta1 (dw_attr_node *a)
4661 {
4662   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4663   return a->dw_attr_val.v.val_vms_delta.lbl1;
4664 }
4665
4666 /* Return the end label of a delta attribute.  */
4667
4668 static inline const char *
4669 AT_vms_delta2 (dw_attr_node *a)
4670 {
4671   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4672   return a->dw_attr_val.v.val_vms_delta.lbl2;
4673 }
4674
4675 static inline const char *
4676 AT_lbl (dw_attr_node *a)
4677 {
4678   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4679                     || AT_class (a) == dw_val_class_lineptr
4680                     || AT_class (a) == dw_val_class_macptr
4681                     || AT_class (a) == dw_val_class_high_pc));
4682   return a->dw_attr_val.v.val_lbl_id;
4683 }
4684
4685 /* Get the attribute of type attr_kind.  */
4686
4687 static dw_attr_node *
4688 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4689 {
4690   dw_attr_node *a;
4691   unsigned ix;
4692   dw_die_ref spec = NULL;
4693
4694   if (! die)
4695     return NULL;
4696
4697   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4698     if (a->dw_attr == attr_kind)
4699       return a;
4700     else if (a->dw_attr == DW_AT_specification
4701              || a->dw_attr == DW_AT_abstract_origin)
4702       spec = AT_ref (a);
4703
4704   if (spec)
4705     return get_AT (spec, attr_kind);
4706
4707   return NULL;
4708 }
4709
4710 /* Returns the parent of the declaration of DIE.  */
4711
4712 static dw_die_ref
4713 get_die_parent (dw_die_ref die)
4714 {
4715   dw_die_ref t;
4716
4717   if (!die)
4718     return NULL;
4719
4720   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4721       || (t = get_AT_ref (die, DW_AT_specification)))
4722     die = t;
4723
4724   return die->die_parent;
4725 }
4726
4727 /* Return the "low pc" attribute value, typically associated with a subprogram
4728    DIE.  Return null if the "low pc" attribute is either not present, or if it
4729    cannot be represented as an assembler label identifier.  */
4730
4731 static inline const char *
4732 get_AT_low_pc (dw_die_ref die)
4733 {
4734   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4735
4736   return a ? AT_lbl (a) : NULL;
4737 }
4738
4739 /* Return the "high pc" attribute value, typically associated with a subprogram
4740    DIE.  Return null if the "high pc" attribute is either not present, or if it
4741    cannot be represented as an assembler label identifier.  */
4742
4743 static inline const char *
4744 get_AT_hi_pc (dw_die_ref die)
4745 {
4746   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4747
4748   return a ? AT_lbl (a) : NULL;
4749 }
4750
4751 /* Return the value of the string attribute designated by ATTR_KIND, or
4752    NULL if it is not present.  */
4753
4754 static inline const char *
4755 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4756 {
4757   dw_attr_node *a = get_AT (die, attr_kind);
4758
4759   return a ? AT_string (a) : NULL;
4760 }
4761
4762 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4763    if it is not present.  */
4764
4765 static inline int
4766 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4767 {
4768   dw_attr_node *a = get_AT (die, attr_kind);
4769
4770   return a ? AT_flag (a) : 0;
4771 }
4772
4773 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4774    if it is not present.  */
4775
4776 static inline unsigned
4777 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4778 {
4779   dw_attr_node *a = get_AT (die, attr_kind);
4780
4781   return a ? AT_unsigned (a) : 0;
4782 }
4783
4784 static inline dw_die_ref
4785 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4786 {
4787   dw_attr_node *a = get_AT (die, attr_kind);
4788
4789   return a ? AT_ref (a) : NULL;
4790 }
4791
4792 static inline struct dwarf_file_data *
4793 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4794 {
4795   dw_attr_node *a = get_AT (die, attr_kind);
4796
4797   return a ? AT_file (a) : NULL;
4798 }
4799
4800 /* Return TRUE if the language is C++.  */
4801
4802 static inline bool
4803 is_cxx (void)
4804 {
4805   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4806
4807   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4808           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4809 }
4810
4811 /* Return TRUE if the language is Java.  */
4812
4813 static inline bool
4814 is_java (void)
4815 {
4816   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4817
4818   return lang == DW_LANG_Java;
4819 }
4820
4821 /* Return TRUE if the language is Fortran.  */
4822
4823 static inline bool
4824 is_fortran (void)
4825 {
4826   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4827
4828   return (lang == DW_LANG_Fortran77
4829           || lang == DW_LANG_Fortran90
4830           || lang == DW_LANG_Fortran95
4831           || lang == DW_LANG_Fortran03
4832           || lang == DW_LANG_Fortran08);
4833 }
4834
4835 /* Return TRUE if the language is Ada.  */
4836
4837 static inline bool
4838 is_ada (void)
4839 {
4840   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4841
4842   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4843 }
4844
4845 /* Remove the specified attribute if present.  Return TRUE if removal
4846    was successful.  */
4847
4848 static bool
4849 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4850 {
4851   dw_attr_node *a;
4852   unsigned ix;
4853
4854   if (! die)
4855     return false;
4856
4857   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4858     if (a->dw_attr == attr_kind)
4859       {
4860         if (AT_class (a) == dw_val_class_str)
4861           if (a->dw_attr_val.v.val_str->refcount)
4862             a->dw_attr_val.v.val_str->refcount--;
4863
4864         /* vec::ordered_remove should help reduce the number of abbrevs
4865            that are needed.  */
4866         die->die_attr->ordered_remove (ix);
4867         return true;
4868       }
4869   return false;
4870 }
4871
4872 /* Remove CHILD from its parent.  PREV must have the property that
4873    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4874
4875 static void
4876 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4877 {
4878   gcc_assert (child->die_parent == prev->die_parent);
4879   gcc_assert (prev->die_sib == child);
4880   if (prev == child)
4881     {
4882       gcc_assert (child->die_parent->die_child == child);
4883       prev = NULL;
4884     }
4885   else
4886     prev->die_sib = child->die_sib;
4887   if (child->die_parent->die_child == child)
4888     child->die_parent->die_child = prev;
4889 }
4890
4891 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4892    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4893
4894 static void
4895 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4896 {
4897   dw_die_ref parent = old_child->die_parent;
4898
4899   gcc_assert (parent == prev->die_parent);
4900   gcc_assert (prev->die_sib == old_child);
4901
4902   new_child->die_parent = parent;
4903   if (prev == old_child)
4904     {
4905       gcc_assert (parent->die_child == old_child);
4906       new_child->die_sib = new_child;
4907     }
4908   else
4909     {
4910       prev->die_sib = new_child;
4911       new_child->die_sib = old_child->die_sib;
4912     }
4913   if (old_child->die_parent->die_child == old_child)
4914     old_child->die_parent->die_child = new_child;
4915 }
4916
4917 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4918
4919 static void
4920 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4921 {
4922   dw_die_ref c;
4923   new_parent->die_child = old_parent->die_child;
4924   old_parent->die_child = NULL;
4925   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4926 }
4927
4928 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4929    matches TAG.  */
4930
4931 static void
4932 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4933 {
4934   dw_die_ref c;
4935
4936   c = die->die_child;
4937   if (c) do {
4938     dw_die_ref prev = c;
4939     c = c->die_sib;
4940     while (c->die_tag == tag)
4941       {
4942         remove_child_with_prev (c, prev);
4943         c->die_parent = NULL;
4944         /* Might have removed every child.  */
4945         if (c == c->die_sib)
4946           return;
4947         c = c->die_sib;
4948       }
4949   } while (c != die->die_child);
4950 }
4951
4952 /* Add a CHILD_DIE as the last child of DIE.  */
4953
4954 static void
4955 add_child_die (dw_die_ref die, dw_die_ref child_die)
4956 {
4957   /* FIXME this should probably be an assert.  */
4958   if (! die || ! child_die)
4959     return;
4960   gcc_assert (die != child_die);
4961
4962   child_die->die_parent = die;
4963   if (die->die_child)
4964     {
4965       child_die->die_sib = die->die_child->die_sib;
4966       die->die_child->die_sib = child_die;
4967     }
4968   else
4969     child_die->die_sib = child_die;
4970   die->die_child = child_die;
4971 }
4972
4973 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE.  */
4974
4975 static void
4976 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4977                      dw_die_ref after_die)
4978 {
4979   gcc_assert (die
4980               && child_die
4981               && after_die
4982               && die->die_child
4983               && die != child_die);
4984
4985   child_die->die_parent = die;
4986   child_die->die_sib = after_die->die_sib;
4987   after_die->die_sib = child_die;
4988   if (die->die_child == after_die)
4989     die->die_child = child_die;
4990 }
4991
4992 /* Unassociate CHILD from its parent, and make its parent be
4993    NEW_PARENT.  */
4994
4995 static void
4996 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4997 {
4998   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4999     if (p->die_sib == child)
5000       {
5001         remove_child_with_prev (child, p);
5002         break;
5003       }
5004   add_child_die (new_parent, child);
5005 }
5006
5007 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5008    is the specification, to the end of PARENT's list of children.
5009    This is done by removing and re-adding it.  */
5010
5011 static void
5012 splice_child_die (dw_die_ref parent, dw_die_ref child)
5013 {
5014   /* We want the declaration DIE from inside the class, not the
5015      specification DIE at toplevel.  */
5016   if (child->die_parent != parent)
5017     {
5018       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5019
5020       if (tmp)
5021         child = tmp;
5022     }
5023
5024   gcc_assert (child->die_parent == parent
5025               || (child->die_parent
5026                   == get_AT_ref (parent, DW_AT_specification)));
5027
5028   reparent_child (child, parent);
5029 }
5030
5031 /* Create and return a new die with a parent of PARENT_DIE.  If
5032    PARENT_DIE is NULL, the new DIE is placed in limbo and an
5033    associated tree T must be supplied to determine parenthood
5034    later.  */
5035
5036 static inline dw_die_ref
5037 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5038 {
5039   dw_die_ref die = ggc_cleared_alloc<die_node> ();
5040
5041   die->die_tag = tag_value;
5042
5043   if (parent_die != NULL)
5044     add_child_die (parent_die, die);
5045   else
5046     {
5047       limbo_die_node *limbo_node;
5048
5049       /* No DIEs created after early dwarf should end up in limbo,
5050          because the limbo list should not persist past LTO
5051          streaming.  */
5052       if (tag_value != DW_TAG_compile_unit
5053           /* These are allowed because they're generated while
5054              breaking out COMDAT units late.  */
5055           && tag_value != DW_TAG_type_unit
5056           && !early_dwarf
5057           /* Allow nested functions to live in limbo because they will
5058              only temporarily live there, as decls_for_scope will fix
5059              them up.  */
5060           && (TREE_CODE (t) != FUNCTION_DECL
5061               || !decl_function_context (t))
5062           /* Same as nested functions above but for types.  Types that
5063              are local to a function will be fixed in
5064              decls_for_scope.  */
5065           && (!RECORD_OR_UNION_TYPE_P (t)
5066               || !TYPE_CONTEXT (t)
5067               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5068           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5069              especially in the ltrans stage, but once we implement LTO
5070              dwarf streaming, we should remove this exception.  */
5071           && !in_lto_p)
5072         {
5073           fprintf (stderr, "symbol ended up in limbo too late:");
5074           debug_generic_stmt (t);
5075           gcc_unreachable ();
5076         }
5077
5078       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5079       limbo_node->die = die;
5080       limbo_node->created_for = t;
5081       limbo_node->next = limbo_die_list;
5082       limbo_die_list = limbo_node;
5083     }
5084
5085   return die;
5086 }
5087
5088 /* Return the DIE associated with the given type specifier.  */
5089
5090 static inline dw_die_ref
5091 lookup_type_die (tree type)
5092 {
5093   return TYPE_SYMTAB_DIE (type);
5094 }
5095
5096 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5097    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5098    anonymous type instead the one of the naming typedef.  */
5099
5100 static inline dw_die_ref
5101 strip_naming_typedef (tree type, dw_die_ref type_die)
5102 {
5103   if (type
5104       && TREE_CODE (type) == RECORD_TYPE
5105       && type_die
5106       && type_die->die_tag == DW_TAG_typedef
5107       && is_naming_typedef_decl (TYPE_NAME (type)))
5108     type_die = get_AT_ref (type_die, DW_AT_type);
5109   return type_die;
5110 }
5111
5112 /* Like lookup_type_die, but if type is an anonymous type named by a
5113    typedef[1], return the DIE of the anonymous type instead the one of
5114    the naming typedef.  This is because in gen_typedef_die, we did
5115    equate the anonymous struct named by the typedef with the DIE of
5116    the naming typedef. So by default, lookup_type_die on an anonymous
5117    struct yields the DIE of the naming typedef.
5118
5119    [1]: Read the comment of is_naming_typedef_decl to learn about what
5120    a naming typedef is.  */
5121
5122 static inline dw_die_ref
5123 lookup_type_die_strip_naming_typedef (tree type)
5124 {
5125   dw_die_ref die = lookup_type_die (type);
5126   return strip_naming_typedef (type, die);
5127 }
5128
5129 /* Equate a DIE to a given type specifier.  */
5130
5131 static inline void
5132 equate_type_number_to_die (tree type, dw_die_ref type_die)
5133 {
5134   TYPE_SYMTAB_DIE (type) = type_die;
5135 }
5136
5137 /* Returns a hash value for X (which really is a die_struct).  */
5138
5139 inline hashval_t
5140 decl_die_hasher::hash (die_node *x)
5141 {
5142   return (hashval_t) x->decl_id;
5143 }
5144
5145 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5146
5147 inline bool
5148 decl_die_hasher::equal (die_node *x, tree y)
5149 {
5150   return (x->decl_id == DECL_UID (y));
5151 }
5152
5153 /* Return the DIE associated with a given declaration.  */
5154
5155 static inline dw_die_ref
5156 lookup_decl_die (tree decl)
5157 {
5158   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5159 }
5160
5161 /* Returns a hash value for X (which really is a var_loc_list).  */
5162
5163 inline hashval_t
5164 decl_loc_hasher::hash (var_loc_list *x)
5165 {
5166   return (hashval_t) x->decl_id;
5167 }
5168
5169 /* Return nonzero if decl_id of var_loc_list X is the same as
5170    UID of decl *Y.  */
5171
5172 inline bool
5173 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5174 {
5175   return (x->decl_id == DECL_UID (y));
5176 }
5177
5178 /* Return the var_loc list associated with a given declaration.  */
5179
5180 static inline var_loc_list *
5181 lookup_decl_loc (const_tree decl)
5182 {
5183   if (!decl_loc_table)
5184     return NULL;
5185   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5186 }
5187
5188 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5189
5190 inline hashval_t
5191 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5192 {
5193   return (hashval_t) x->decl_id;
5194 }
5195
5196 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5197    UID of decl *Y.  */
5198
5199 inline bool
5200 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5201 {
5202   return (x->decl_id == DECL_UID (y));
5203 }
5204
5205 /* Equate a DIE to a particular declaration.  */
5206
5207 static void
5208 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5209 {
5210   unsigned int decl_id = DECL_UID (decl);
5211
5212   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5213   decl_die->decl_id = decl_id;
5214 }
5215
5216 /* Return how many bits covers PIECE EXPR_LIST.  */
5217
5218 static HOST_WIDE_INT
5219 decl_piece_bitsize (rtx piece)
5220 {
5221   int ret = (int) GET_MODE (piece);
5222   if (ret)
5223     return ret;
5224   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5225               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5226   return INTVAL (XEXP (XEXP (piece, 0), 0));
5227 }
5228
5229 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5230
5231 static rtx *
5232 decl_piece_varloc_ptr (rtx piece)
5233 {
5234   if ((int) GET_MODE (piece))
5235     return &XEXP (piece, 0);
5236   else
5237     return &XEXP (XEXP (piece, 0), 1);
5238 }
5239
5240 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5241    Next is the chain of following piece nodes.  */
5242
5243 static rtx_expr_list *
5244 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5245 {
5246   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5247     return alloc_EXPR_LIST (bitsize, loc_note, next);
5248   else
5249     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5250                                                GEN_INT (bitsize),
5251                                                loc_note), next);
5252 }
5253
5254 /* Return rtx that should be stored into loc field for
5255    LOC_NOTE and BITPOS/BITSIZE.  */
5256
5257 static rtx
5258 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5259                       HOST_WIDE_INT bitsize)
5260 {
5261   if (bitsize != -1)
5262     {
5263       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5264       if (bitpos != 0)
5265         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5266     }
5267   return loc_note;
5268 }
5269
5270 /* This function either modifies location piece list *DEST in
5271    place (if SRC and INNER is NULL), or copies location piece list
5272    *SRC to *DEST while modifying it.  Location BITPOS is modified
5273    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5274    not copied and if needed some padding around it is added.
5275    When modifying in place, DEST should point to EXPR_LIST where
5276    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5277    to the start of the whole list and INNER points to the EXPR_LIST
5278    where earlier pieces cover PIECE_BITPOS bits.  */
5279
5280 static void
5281 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5282                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5283                    HOST_WIDE_INT bitsize, rtx loc_note)
5284 {
5285   HOST_WIDE_INT diff;
5286   bool copy = inner != NULL;
5287
5288   if (copy)
5289     {
5290       /* First copy all nodes preceding the current bitpos.  */
5291       while (src != inner)
5292         {
5293           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5294                                    decl_piece_bitsize (*src), NULL_RTX);
5295           dest = &XEXP (*dest, 1);
5296           src = &XEXP (*src, 1);
5297         }
5298     }
5299   /* Add padding if needed.  */
5300   if (bitpos != piece_bitpos)
5301     {
5302       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5303                                copy ? NULL_RTX : *dest);
5304       dest = &XEXP (*dest, 1);
5305     }
5306   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5307     {
5308       gcc_assert (!copy);
5309       /* A piece with correct bitpos and bitsize already exist,
5310          just update the location for it and return.  */
5311       *decl_piece_varloc_ptr (*dest) = loc_note;
5312       return;
5313     }
5314   /* Add the piece that changed.  */
5315   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5316   dest = &XEXP (*dest, 1);
5317   /* Skip over pieces that overlap it.  */
5318   diff = bitpos - piece_bitpos + bitsize;
5319   if (!copy)
5320     src = dest;
5321   while (diff > 0 && *src)
5322     {
5323       rtx piece = *src;
5324       diff -= decl_piece_bitsize (piece);
5325       if (copy)
5326         src = &XEXP (piece, 1);
5327       else
5328         {
5329           *src = XEXP (piece, 1);
5330           free_EXPR_LIST_node (piece);
5331         }
5332     }
5333   /* Add padding if needed.  */
5334   if (diff < 0 && *src)
5335     {
5336       if (!copy)
5337         dest = src;
5338       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5339       dest = &XEXP (*dest, 1);
5340     }
5341   if (!copy)
5342     return;
5343   /* Finally copy all nodes following it.  */
5344   while (*src)
5345     {
5346       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5347                                decl_piece_bitsize (*src), NULL_RTX);
5348       dest = &XEXP (*dest, 1);
5349       src = &XEXP (*src, 1);
5350     }
5351 }
5352
5353 /* Add a variable location node to the linked list for DECL.  */
5354
5355 static struct var_loc_node *
5356 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5357 {
5358   unsigned int decl_id;
5359   var_loc_list *temp;
5360   struct var_loc_node *loc = NULL;
5361   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5362
5363   if (TREE_CODE (decl) == VAR_DECL
5364       && DECL_HAS_DEBUG_EXPR_P (decl))
5365     {
5366       tree realdecl = DECL_DEBUG_EXPR (decl);
5367       if (handled_component_p (realdecl)
5368           || (TREE_CODE (realdecl) == MEM_REF
5369               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5370         {
5371           HOST_WIDE_INT maxsize;
5372           bool reverse;
5373           tree innerdecl
5374             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5375                                        &reverse);
5376           if (!DECL_P (innerdecl)
5377               || DECL_IGNORED_P (innerdecl)
5378               || TREE_STATIC (innerdecl)
5379               || bitsize <= 0
5380               || bitpos + bitsize > 256
5381               || bitsize != maxsize)
5382             return NULL;
5383           decl = innerdecl;
5384         }
5385     }
5386
5387   decl_id = DECL_UID (decl);
5388   var_loc_list **slot
5389     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5390   if (*slot == NULL)
5391     {
5392       temp = ggc_cleared_alloc<var_loc_list> ();
5393       temp->decl_id = decl_id;
5394       *slot = temp;
5395     }
5396   else
5397     temp = *slot;
5398
5399   /* For PARM_DECLs try to keep around the original incoming value,
5400      even if that means we'll emit a zero-range .debug_loc entry.  */
5401   if (temp->last
5402       && temp->first == temp->last
5403       && TREE_CODE (decl) == PARM_DECL
5404       && NOTE_P (temp->first->loc)
5405       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5406       && DECL_INCOMING_RTL (decl)
5407       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5408       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5409          == GET_CODE (DECL_INCOMING_RTL (decl))
5410       && prev_real_insn (temp->first->loc) == NULL_RTX
5411       && (bitsize != -1
5412           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5413                            NOTE_VAR_LOCATION_LOC (loc_note))
5414           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5415               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5416     {
5417       loc = ggc_cleared_alloc<var_loc_node> ();
5418       temp->first->next = loc;
5419       temp->last = loc;
5420       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5421     }
5422   else if (temp->last)
5423     {
5424       struct var_loc_node *last = temp->last, *unused = NULL;
5425       rtx *piece_loc = NULL, last_loc_note;
5426       HOST_WIDE_INT piece_bitpos = 0;
5427       if (last->next)
5428         {
5429           last = last->next;
5430           gcc_assert (last->next == NULL);
5431         }
5432       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5433         {
5434           piece_loc = &last->loc;
5435           do
5436             {
5437               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5438               if (piece_bitpos + cur_bitsize > bitpos)
5439                 break;
5440               piece_bitpos += cur_bitsize;
5441               piece_loc = &XEXP (*piece_loc, 1);
5442             }
5443           while (*piece_loc);
5444         }
5445       /* TEMP->LAST here is either pointer to the last but one or
5446          last element in the chained list, LAST is pointer to the
5447          last element.  */
5448       if (label && strcmp (last->label, label) == 0)
5449         {
5450           /* For SRA optimized variables if there weren't any real
5451              insns since last note, just modify the last node.  */
5452           if (piece_loc != NULL)
5453             {
5454               adjust_piece_list (piece_loc, NULL, NULL,
5455                                  bitpos, piece_bitpos, bitsize, loc_note);
5456               return NULL;
5457             }
5458           /* If the last note doesn't cover any instructions, remove it.  */
5459           if (temp->last != last)
5460             {
5461               temp->last->next = NULL;
5462               unused = last;
5463               last = temp->last;
5464               gcc_assert (strcmp (last->label, label) != 0);
5465             }
5466           else
5467             {
5468               gcc_assert (temp->first == temp->last
5469                           || (temp->first->next == temp->last
5470                               && TREE_CODE (decl) == PARM_DECL));
5471               memset (temp->last, '\0', sizeof (*temp->last));
5472               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5473               return temp->last;
5474             }
5475         }
5476       if (bitsize == -1 && NOTE_P (last->loc))
5477         last_loc_note = last->loc;
5478       else if (piece_loc != NULL
5479                && *piece_loc != NULL_RTX
5480                && piece_bitpos == bitpos
5481                && decl_piece_bitsize (*piece_loc) == bitsize)
5482         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5483       else
5484         last_loc_note = NULL_RTX;
5485       /* If the current location is the same as the end of the list,
5486          and either both or neither of the locations is uninitialized,
5487          we have nothing to do.  */
5488       if (last_loc_note == NULL_RTX
5489           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5490                             NOTE_VAR_LOCATION_LOC (loc_note)))
5491           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5492                != NOTE_VAR_LOCATION_STATUS (loc_note))
5493               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5494                    == VAR_INIT_STATUS_UNINITIALIZED)
5495                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5496                       == VAR_INIT_STATUS_UNINITIALIZED))))
5497         {
5498           /* Add LOC to the end of list and update LAST.  If the last
5499              element of the list has been removed above, reuse its
5500              memory for the new node, otherwise allocate a new one.  */
5501           if (unused)
5502             {
5503               loc = unused;
5504               memset (loc, '\0', sizeof (*loc));
5505             }
5506           else
5507             loc = ggc_cleared_alloc<var_loc_node> ();
5508           if (bitsize == -1 || piece_loc == NULL)
5509             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5510           else
5511             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5512                                bitpos, piece_bitpos, bitsize, loc_note);
5513           last->next = loc;
5514           /* Ensure TEMP->LAST will point either to the new last but one
5515              element of the chain, or to the last element in it.  */
5516           if (last != temp->last)
5517             temp->last = last;
5518         }
5519       else if (unused)
5520         ggc_free (unused);
5521     }
5522   else
5523     {
5524       loc = ggc_cleared_alloc<var_loc_node> ();
5525       temp->first = loc;
5526       temp->last = loc;
5527       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5528     }
5529   return loc;
5530 }
5531 \f
5532 /* Keep track of the number of spaces used to indent the
5533    output of the debugging routines that print the structure of
5534    the DIE internal representation.  */
5535 static int print_indent;
5536
5537 /* Indent the line the number of spaces given by print_indent.  */
5538
5539 static inline void
5540 print_spaces (FILE *outfile)
5541 {
5542   fprintf (outfile, "%*s", print_indent, "");
5543 }
5544
5545 /* Print a type signature in hex.  */
5546
5547 static inline void
5548 print_signature (FILE *outfile, char *sig)
5549 {
5550   int i;
5551
5552   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5553     fprintf (outfile, "%02x", sig[i] & 0xff);
5554 }
5555
5556 static inline void
5557 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5558 {
5559   if (discr_value->pos)
5560     fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5561   else
5562     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5563 }
5564
5565 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5566
5567 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5568    RECURSE, output location descriptor operations.  */
5569
5570 static void
5571 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5572 {
5573   switch (val->val_class)
5574     {
5575     case dw_val_class_addr:
5576       fprintf (outfile, "address");
5577       break;
5578     case dw_val_class_offset:
5579       fprintf (outfile, "offset");
5580       break;
5581     case dw_val_class_loc:
5582       fprintf (outfile, "location descriptor");
5583       if (val->v.val_loc == NULL)
5584         fprintf (outfile, " -> <null>\n");
5585       else if (recurse)
5586         {
5587           fprintf (outfile, ":\n");
5588           print_indent += 4;
5589           print_loc_descr (val->v.val_loc, outfile);
5590           print_indent -= 4;
5591         }
5592       else
5593         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5594       break;
5595     case dw_val_class_loc_list:
5596       fprintf (outfile, "location list -> label:%s",
5597                val->v.val_loc_list->ll_symbol);
5598       break;
5599     case dw_val_class_range_list:
5600       fprintf (outfile, "range list");
5601       break;
5602     case dw_val_class_const:
5603       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5604       break;
5605     case dw_val_class_unsigned_const:
5606       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5607       break;
5608     case dw_val_class_const_double:
5609       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5610                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5611                val->v.val_double.high,
5612                val->v.val_double.low);
5613       break;
5614     case dw_val_class_wide_int:
5615       {
5616         int i = val->v.val_wide->get_len ();
5617         fprintf (outfile, "constant (");
5618         gcc_assert (i > 0);
5619         if (val->v.val_wide->elt (i - 1) == 0)
5620           fprintf (outfile, "0x");
5621         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5622                  val->v.val_wide->elt (--i));
5623         while (--i >= 0)
5624           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5625                    val->v.val_wide->elt (i));
5626         fprintf (outfile, ")");
5627         break;
5628       }
5629     case dw_val_class_vec:
5630       fprintf (outfile, "floating-point or vector constant");
5631       break;
5632     case dw_val_class_flag:
5633       fprintf (outfile, "%u", val->v.val_flag);
5634       break;
5635     case dw_val_class_die_ref:
5636       if (val->v.val_die_ref.die != NULL)
5637         {
5638           dw_die_ref die = val->v.val_die_ref.die;
5639
5640           if (die->comdat_type_p)
5641             {
5642               fprintf (outfile, "die -> signature: ");
5643               print_signature (outfile,
5644                                die->die_id.die_type_node->signature);
5645             }
5646           else if (die->die_id.die_symbol)
5647             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5648           else
5649             fprintf (outfile, "die -> %ld", die->die_offset);
5650           fprintf (outfile, " (%p)", (void *) die);
5651         }
5652       else
5653         fprintf (outfile, "die -> <null>");
5654       break;
5655     case dw_val_class_vms_delta:
5656       fprintf (outfile, "delta: @slotcount(%s-%s)",
5657                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5658       break;
5659     case dw_val_class_lbl_id:
5660     case dw_val_class_lineptr:
5661     case dw_val_class_macptr:
5662     case dw_val_class_high_pc:
5663       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5664       break;
5665     case dw_val_class_str:
5666       if (val->v.val_str->str != NULL)
5667         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5668       else
5669         fprintf (outfile, "<null>");
5670       break;
5671     case dw_val_class_file:
5672       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5673                val->v.val_file->emitted_number);
5674       break;
5675     case dw_val_class_data8:
5676       {
5677         int i;
5678
5679         for (i = 0; i < 8; i++)
5680           fprintf (outfile, "%02x", val->v.val_data8[i]);
5681         break;
5682       }
5683     case dw_val_class_discr_value:
5684       print_discr_value (outfile, &val->v.val_discr_value);
5685       break;
5686     case dw_val_class_discr_list:
5687       for (dw_discr_list_ref node = val->v.val_discr_list;
5688            node != NULL;
5689            node = node->dw_discr_next)
5690         {
5691           if (node->dw_discr_range)
5692             {
5693               fprintf (outfile, " .. ");
5694               print_discr_value (outfile, &node->dw_discr_lower_bound);
5695               print_discr_value (outfile, &node->dw_discr_upper_bound);
5696             }
5697           else
5698             print_discr_value (outfile, &node->dw_discr_lower_bound);
5699
5700           if (node->dw_discr_next != NULL)
5701             fprintf (outfile, " | ");
5702         }
5703     default:
5704       break;
5705     }
5706 }
5707
5708 /* Likewise, for a DIE attribute.  */
5709
5710 static void
5711 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5712 {
5713   print_dw_val (&a->dw_attr_val, recurse, outfile);
5714 }
5715
5716
5717 /* Print the list of operands in the LOC location description to OUTFILE.  This
5718    routine is a debugging aid only.  */
5719
5720 static void
5721 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5722 {
5723   dw_loc_descr_ref l = loc;
5724
5725   if (loc == NULL)
5726     {
5727       print_spaces (outfile);
5728       fprintf (outfile, "<null>\n");
5729       return;
5730     }
5731
5732   for (l = loc; l != NULL; l = l->dw_loc_next)
5733     {
5734       print_spaces (outfile);
5735       fprintf (outfile, "(%p) %s",
5736                (void *) l,
5737                dwarf_stack_op_name (l->dw_loc_opc));
5738       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5739         {
5740           fprintf (outfile, " ");
5741           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5742         }
5743       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5744         {
5745           fprintf (outfile, ", ");
5746           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5747         }
5748       fprintf (outfile, "\n");
5749     }
5750 }
5751
5752 /* Print the information associated with a given DIE, and its children.
5753    This routine is a debugging aid only.  */
5754
5755 static void
5756 print_die (dw_die_ref die, FILE *outfile)
5757 {
5758   dw_attr_node *a;
5759   dw_die_ref c;
5760   unsigned ix;
5761
5762   print_spaces (outfile);
5763   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5764            die->die_offset, dwarf_tag_name (die->die_tag),
5765            (void*) die);
5766   print_spaces (outfile);
5767   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5768   fprintf (outfile, " offset: %ld", die->die_offset);
5769   fprintf (outfile, " mark: %d\n", die->die_mark);
5770
5771   if (die->comdat_type_p)
5772     {
5773       print_spaces (outfile);
5774       fprintf (outfile, "  signature: ");
5775       print_signature (outfile, die->die_id.die_type_node->signature);
5776       fprintf (outfile, "\n");
5777     }
5778
5779   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5780     {
5781       print_spaces (outfile);
5782       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5783
5784       print_attribute (a, true, outfile);
5785       fprintf (outfile, "\n");
5786     }
5787
5788   if (die->die_child != NULL)
5789     {
5790       print_indent += 4;
5791       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5792       print_indent -= 4;
5793     }
5794   if (print_indent == 0)
5795     fprintf (outfile, "\n");
5796 }
5797
5798 /* Print the list of operations in the LOC location description.  */
5799
5800 DEBUG_FUNCTION void
5801 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5802 {
5803   print_loc_descr (loc, stderr);
5804 }
5805
5806 /* Print the information collected for a given DIE.  */
5807
5808 DEBUG_FUNCTION void
5809 debug_dwarf_die (dw_die_ref die)
5810 {
5811   print_die (die, stderr);
5812 }
5813
5814 DEBUG_FUNCTION void
5815 debug (die_struct &ref)
5816 {
5817   print_die (&ref, stderr);
5818 }
5819
5820 DEBUG_FUNCTION void
5821 debug (die_struct *ptr)
5822 {
5823   if (ptr)
5824     debug (*ptr);
5825   else
5826     fprintf (stderr, "<nil>\n");
5827 }
5828
5829
5830 /* Print all DWARF information collected for the compilation unit.
5831    This routine is a debugging aid only.  */
5832
5833 DEBUG_FUNCTION void
5834 debug_dwarf (void)
5835 {
5836   print_indent = 0;
5837   print_die (comp_unit_die (), stderr);
5838 }
5839
5840 /* Sanity checks on DIEs.  */
5841
5842 static void
5843 check_die (dw_die_ref die)
5844 {
5845   unsigned ix;
5846   dw_attr_node *a;
5847   bool inline_found = false;
5848   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5849   int n_decl_line = 0, n_decl_file = 0;
5850   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5851     {
5852       switch (a->dw_attr)
5853         {
5854         case DW_AT_inline:
5855           if (a->dw_attr_val.v.val_unsigned)
5856             inline_found = true;
5857           break;
5858         case DW_AT_location:
5859           ++n_location;
5860           break;
5861         case DW_AT_low_pc:
5862           ++n_low_pc;
5863           break;
5864         case DW_AT_high_pc:
5865           ++n_high_pc;
5866           break;
5867         case DW_AT_artificial:
5868           ++n_artificial;
5869           break;
5870         case DW_AT_decl_line:
5871           ++n_decl_line;
5872           break;
5873         case DW_AT_decl_file:
5874           ++n_decl_file;
5875           break;
5876         default:
5877           break;
5878         }
5879     }
5880   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5881       || n_decl_line > 1 || n_decl_file > 1)
5882     {
5883       fprintf (stderr, "Duplicate attributes in DIE:\n");
5884       debug_dwarf_die (die);
5885       gcc_unreachable ();
5886     }
5887   if (inline_found)
5888     {
5889       /* A debugging information entry that is a member of an abstract
5890          instance tree [that has DW_AT_inline] should not contain any
5891          attributes which describe aspects of the subroutine which vary
5892          between distinct inlined expansions or distinct out-of-line
5893          expansions.  */
5894       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5895         gcc_assert (a->dw_attr != DW_AT_low_pc
5896                     && a->dw_attr != DW_AT_high_pc
5897                     && a->dw_attr != DW_AT_location
5898                     && a->dw_attr != DW_AT_frame_base
5899                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5900     }
5901 }
5902 \f
5903 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5904    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5905    DIE that marks the start of the DIEs for this include file.  */
5906
5907 static dw_die_ref
5908 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5909 {
5910   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5911   dw_die_ref new_unit = gen_compile_unit_die (filename);
5912
5913   new_unit->die_sib = old_unit;
5914   return new_unit;
5915 }
5916
5917 /* Close an include-file CU and reopen the enclosing one.  */
5918
5919 static dw_die_ref
5920 pop_compile_unit (dw_die_ref old_unit)
5921 {
5922   dw_die_ref new_unit = old_unit->die_sib;
5923
5924   old_unit->die_sib = NULL;
5925   return new_unit;
5926 }
5927
5928 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5929 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5930 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5931
5932 /* Calculate the checksum of a location expression.  */
5933
5934 static inline void
5935 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5936 {
5937   int tem;
5938   inchash::hash hstate;
5939   hashval_t hash;
5940
5941   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5942   CHECKSUM (tem);
5943   hash_loc_operands (loc, hstate);
5944   hash = hstate.end();
5945   CHECKSUM (hash);
5946 }
5947
5948 /* Calculate the checksum of an attribute.  */
5949
5950 static void
5951 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5952 {
5953   dw_loc_descr_ref loc;
5954   rtx r;
5955
5956   CHECKSUM (at->dw_attr);
5957
5958   /* We don't care that this was compiled with a different compiler
5959      snapshot; if the output is the same, that's what matters.  */
5960   if (at->dw_attr == DW_AT_producer)
5961     return;
5962
5963   switch (AT_class (at))
5964     {
5965     case dw_val_class_const:
5966       CHECKSUM (at->dw_attr_val.v.val_int);
5967       break;
5968     case dw_val_class_unsigned_const:
5969       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5970       break;
5971     case dw_val_class_const_double:
5972       CHECKSUM (at->dw_attr_val.v.val_double);
5973       break;
5974     case dw_val_class_wide_int:
5975       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5976                       get_full_len (*at->dw_attr_val.v.val_wide)
5977                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5978       break;
5979     case dw_val_class_vec:
5980       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5981                       (at->dw_attr_val.v.val_vec.length
5982                        * at->dw_attr_val.v.val_vec.elt_size));
5983       break;
5984     case dw_val_class_flag:
5985       CHECKSUM (at->dw_attr_val.v.val_flag);
5986       break;
5987     case dw_val_class_str:
5988       CHECKSUM_STRING (AT_string (at));
5989       break;
5990
5991     case dw_val_class_addr:
5992       r = AT_addr (at);
5993       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5994       CHECKSUM_STRING (XSTR (r, 0));
5995       break;
5996
5997     case dw_val_class_offset:
5998       CHECKSUM (at->dw_attr_val.v.val_offset);
5999       break;
6000
6001     case dw_val_class_loc:
6002       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6003         loc_checksum (loc, ctx);
6004       break;
6005
6006     case dw_val_class_die_ref:
6007       die_checksum (AT_ref (at), ctx, mark);
6008       break;
6009
6010     case dw_val_class_fde_ref:
6011     case dw_val_class_vms_delta:
6012     case dw_val_class_lbl_id:
6013     case dw_val_class_lineptr:
6014     case dw_val_class_macptr:
6015     case dw_val_class_high_pc:
6016       break;
6017
6018     case dw_val_class_file:
6019       CHECKSUM_STRING (AT_file (at)->filename);
6020       break;
6021
6022     case dw_val_class_data8:
6023       CHECKSUM (at->dw_attr_val.v.val_data8);
6024       break;
6025
6026     default:
6027       break;
6028     }
6029 }
6030
6031 /* Calculate the checksum of a DIE.  */
6032
6033 static void
6034 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6035 {
6036   dw_die_ref c;
6037   dw_attr_node *a;
6038   unsigned ix;
6039
6040   /* To avoid infinite recursion.  */
6041   if (die->die_mark)
6042     {
6043       CHECKSUM (die->die_mark);
6044       return;
6045     }
6046   die->die_mark = ++(*mark);
6047
6048   CHECKSUM (die->die_tag);
6049
6050   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6051     attr_checksum (a, ctx, mark);
6052
6053   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6054 }
6055
6056 #undef CHECKSUM
6057 #undef CHECKSUM_BLOCK
6058 #undef CHECKSUM_STRING
6059
6060 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
6061 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6062 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6063 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6064 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6065 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6066 #define CHECKSUM_ATTR(FOO) \
6067   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6068
6069 /* Calculate the checksum of a number in signed LEB128 format.  */
6070
6071 static void
6072 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6073 {
6074   unsigned char byte;
6075   bool more;
6076
6077   while (1)
6078     {
6079       byte = (value & 0x7f);
6080       value >>= 7;
6081       more = !((value == 0 && (byte & 0x40) == 0)
6082                 || (value == -1 && (byte & 0x40) != 0));
6083       if (more)
6084         byte |= 0x80;
6085       CHECKSUM (byte);
6086       if (!more)
6087         break;
6088     }
6089 }
6090
6091 /* Calculate the checksum of a number in unsigned LEB128 format.  */
6092
6093 static void
6094 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6095 {
6096   while (1)
6097     {
6098       unsigned char byte = (value & 0x7f);
6099       value >>= 7;
6100       if (value != 0)
6101         /* More bytes to follow.  */
6102         byte |= 0x80;
6103       CHECKSUM (byte);
6104       if (value == 0)
6105         break;
6106     }
6107 }
6108
6109 /* Checksum the context of the DIE.  This adds the names of any
6110    surrounding namespaces or structures to the checksum.  */
6111
6112 static void
6113 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6114 {
6115   const char *name;
6116   dw_die_ref spec;
6117   int tag = die->die_tag;
6118
6119   if (tag != DW_TAG_namespace
6120       && tag != DW_TAG_structure_type
6121       && tag != DW_TAG_class_type)
6122     return;
6123
6124   name = get_AT_string (die, DW_AT_name);
6125
6126   spec = get_AT_ref (die, DW_AT_specification);
6127   if (spec != NULL)
6128     die = spec;
6129
6130   if (die->die_parent != NULL)
6131     checksum_die_context (die->die_parent, ctx);
6132
6133   CHECKSUM_ULEB128 ('C');
6134   CHECKSUM_ULEB128 (tag);
6135   if (name != NULL)
6136     CHECKSUM_STRING (name);
6137 }
6138
6139 /* Calculate the checksum of a location expression.  */
6140
6141 static inline void
6142 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6143 {
6144   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6145      were emitted as a DW_FORM_sdata instead of a location expression.  */
6146   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6147     {
6148       CHECKSUM_ULEB128 (DW_FORM_sdata);
6149       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6150       return;
6151     }
6152
6153   /* Otherwise, just checksum the raw location expression.  */
6154   while (loc != NULL)
6155     {
6156       inchash::hash hstate;
6157       hashval_t hash;
6158
6159       CHECKSUM_ULEB128 (loc->dtprel);
6160       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6161       hash_loc_operands (loc, hstate);
6162       hash = hstate.end ();
6163       CHECKSUM (hash);
6164       loc = loc->dw_loc_next;
6165     }
6166 }
6167
6168 /* Calculate the checksum of an attribute.  */
6169
6170 static void
6171 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6172                        struct md5_ctx *ctx, int *mark)
6173 {
6174   dw_loc_descr_ref loc;
6175   rtx r;
6176
6177   if (AT_class (at) == dw_val_class_die_ref)
6178     {
6179       dw_die_ref target_die = AT_ref (at);
6180
6181       /* For pointer and reference types, we checksum only the (qualified)
6182          name of the target type (if there is a name).  For friend entries,
6183          we checksum only the (qualified) name of the target type or function.
6184          This allows the checksum to remain the same whether the target type
6185          is complete or not.  */
6186       if ((at->dw_attr == DW_AT_type
6187            && (tag == DW_TAG_pointer_type
6188                || tag == DW_TAG_reference_type
6189                || tag == DW_TAG_rvalue_reference_type
6190                || tag == DW_TAG_ptr_to_member_type))
6191           || (at->dw_attr == DW_AT_friend
6192               && tag == DW_TAG_friend))
6193         {
6194           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6195
6196           if (name_attr != NULL)
6197             {
6198               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6199
6200               if (decl == NULL)
6201                 decl = target_die;
6202               CHECKSUM_ULEB128 ('N');
6203               CHECKSUM_ULEB128 (at->dw_attr);
6204               if (decl->die_parent != NULL)
6205                 checksum_die_context (decl->die_parent, ctx);
6206               CHECKSUM_ULEB128 ('E');
6207               CHECKSUM_STRING (AT_string (name_attr));
6208               return;
6209             }
6210         }
6211
6212       /* For all other references to another DIE, we check to see if the
6213          target DIE has already been visited.  If it has, we emit a
6214          backward reference; if not, we descend recursively.  */
6215       if (target_die->die_mark > 0)
6216         {
6217           CHECKSUM_ULEB128 ('R');
6218           CHECKSUM_ULEB128 (at->dw_attr);
6219           CHECKSUM_ULEB128 (target_die->die_mark);
6220         }
6221       else
6222         {
6223           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6224
6225           if (decl == NULL)
6226             decl = target_die;
6227           target_die->die_mark = ++(*mark);
6228           CHECKSUM_ULEB128 ('T');
6229           CHECKSUM_ULEB128 (at->dw_attr);
6230           if (decl->die_parent != NULL)
6231             checksum_die_context (decl->die_parent, ctx);
6232           die_checksum_ordered (target_die, ctx, mark);
6233         }
6234       return;
6235     }
6236
6237   CHECKSUM_ULEB128 ('A');
6238   CHECKSUM_ULEB128 (at->dw_attr);
6239
6240   switch (AT_class (at))
6241     {
6242     case dw_val_class_const:
6243       CHECKSUM_ULEB128 (DW_FORM_sdata);
6244       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6245       break;
6246
6247     case dw_val_class_unsigned_const:
6248       CHECKSUM_ULEB128 (DW_FORM_sdata);
6249       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6250       break;
6251
6252     case dw_val_class_const_double:
6253       CHECKSUM_ULEB128 (DW_FORM_block);
6254       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6255       CHECKSUM (at->dw_attr_val.v.val_double);
6256       break;
6257
6258     case dw_val_class_wide_int:
6259       CHECKSUM_ULEB128 (DW_FORM_block);
6260       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6261                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6262       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6263                       get_full_len (*at->dw_attr_val.v.val_wide)
6264                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6265       break;
6266
6267     case dw_val_class_vec:
6268       CHECKSUM_ULEB128 (DW_FORM_block);
6269       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6270                         * at->dw_attr_val.v.val_vec.elt_size);
6271       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6272                       (at->dw_attr_val.v.val_vec.length
6273                        * at->dw_attr_val.v.val_vec.elt_size));
6274       break;
6275
6276     case dw_val_class_flag:
6277       CHECKSUM_ULEB128 (DW_FORM_flag);
6278       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6279       break;
6280
6281     case dw_val_class_str:
6282       CHECKSUM_ULEB128 (DW_FORM_string);
6283       CHECKSUM_STRING (AT_string (at));
6284       break;
6285
6286     case dw_val_class_addr:
6287       r = AT_addr (at);
6288       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6289       CHECKSUM_ULEB128 (DW_FORM_string);
6290       CHECKSUM_STRING (XSTR (r, 0));
6291       break;
6292
6293     case dw_val_class_offset:
6294       CHECKSUM_ULEB128 (DW_FORM_sdata);
6295       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6296       break;
6297
6298     case dw_val_class_loc:
6299       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6300         loc_checksum_ordered (loc, ctx);
6301       break;
6302
6303     case dw_val_class_fde_ref:
6304     case dw_val_class_lbl_id:
6305     case dw_val_class_lineptr:
6306     case dw_val_class_macptr:
6307     case dw_val_class_high_pc:
6308       break;
6309
6310     case dw_val_class_file:
6311       CHECKSUM_ULEB128 (DW_FORM_string);
6312       CHECKSUM_STRING (AT_file (at)->filename);
6313       break;
6314
6315     case dw_val_class_data8:
6316       CHECKSUM (at->dw_attr_val.v.val_data8);
6317       break;
6318
6319     default:
6320       break;
6321     }
6322 }
6323
6324 struct checksum_attributes
6325 {
6326   dw_attr_node *at_name;
6327   dw_attr_node *at_type;
6328   dw_attr_node *at_friend;
6329   dw_attr_node *at_accessibility;
6330   dw_attr_node *at_address_class;
6331   dw_attr_node *at_allocated;
6332   dw_attr_node *at_artificial;
6333   dw_attr_node *at_associated;
6334   dw_attr_node *at_binary_scale;
6335   dw_attr_node *at_bit_offset;
6336   dw_attr_node *at_bit_size;
6337   dw_attr_node *at_bit_stride;
6338   dw_attr_node *at_byte_size;
6339   dw_attr_node *at_byte_stride;
6340   dw_attr_node *at_const_value;
6341   dw_attr_node *at_containing_type;
6342   dw_attr_node *at_count;
6343   dw_attr_node *at_data_location;
6344   dw_attr_node *at_data_member_location;
6345   dw_attr_node *at_decimal_scale;
6346   dw_attr_node *at_decimal_sign;
6347   dw_attr_node *at_default_value;
6348   dw_attr_node *at_digit_count;
6349   dw_attr_node *at_discr;
6350   dw_attr_node *at_discr_list;
6351   dw_attr_node *at_discr_value;
6352   dw_attr_node *at_encoding;
6353   dw_attr_node *at_endianity;
6354   dw_attr_node *at_explicit;
6355   dw_attr_node *at_is_optional;
6356   dw_attr_node *at_location;
6357   dw_attr_node *at_lower_bound;
6358   dw_attr_node *at_mutable;
6359   dw_attr_node *at_ordering;
6360   dw_attr_node *at_picture_string;
6361   dw_attr_node *at_prototyped;
6362   dw_attr_node *at_small;
6363   dw_attr_node *at_segment;
6364   dw_attr_node *at_string_length;
6365   dw_attr_node *at_threads_scaled;
6366   dw_attr_node *at_upper_bound;
6367   dw_attr_node *at_use_location;
6368   dw_attr_node *at_use_UTF8;
6369   dw_attr_node *at_variable_parameter;
6370   dw_attr_node *at_virtuality;
6371   dw_attr_node *at_visibility;
6372   dw_attr_node *at_vtable_elem_location;
6373 };
6374
6375 /* Collect the attributes that we will want to use for the checksum.  */
6376
6377 static void
6378 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6379 {
6380   dw_attr_node *a;
6381   unsigned ix;
6382
6383   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6384     {
6385       switch (a->dw_attr)
6386         {
6387         case DW_AT_name:
6388           attrs->at_name = a;
6389           break;
6390         case DW_AT_type:
6391           attrs->at_type = a;
6392           break;
6393         case DW_AT_friend:
6394           attrs->at_friend = a;
6395           break;
6396         case DW_AT_accessibility:
6397           attrs->at_accessibility = a;
6398           break;
6399         case DW_AT_address_class:
6400           attrs->at_address_class = a;
6401           break;
6402         case DW_AT_allocated:
6403           attrs->at_allocated = a;
6404           break;
6405         case DW_AT_artificial:
6406           attrs->at_artificial = a;
6407           break;
6408         case DW_AT_associated:
6409           attrs->at_associated = a;
6410           break;
6411         case DW_AT_binary_scale:
6412           attrs->at_binary_scale = a;
6413           break;
6414         case DW_AT_bit_offset:
6415           attrs->at_bit_offset = a;
6416           break;
6417         case DW_AT_bit_size:
6418           attrs->at_bit_size = a;
6419           break;
6420         case DW_AT_bit_stride:
6421           attrs->at_bit_stride = a;
6422           break;
6423         case DW_AT_byte_size:
6424           attrs->at_byte_size = a;
6425           break;
6426         case DW_AT_byte_stride:
6427           attrs->at_byte_stride = a;
6428           break;
6429         case DW_AT_const_value:
6430           attrs->at_const_value = a;
6431           break;
6432         case DW_AT_containing_type:
6433           attrs->at_containing_type = a;
6434           break;
6435         case DW_AT_count:
6436           attrs->at_count = a;
6437           break;
6438         case DW_AT_data_location:
6439           attrs->at_data_location = a;
6440           break;
6441         case DW_AT_data_member_location:
6442           attrs->at_data_member_location = a;
6443           break;
6444         case DW_AT_decimal_scale:
6445           attrs->at_decimal_scale = a;
6446           break;
6447         case DW_AT_decimal_sign:
6448           attrs->at_decimal_sign = a;
6449           break;
6450         case DW_AT_default_value:
6451           attrs->at_default_value = a;
6452           break;
6453         case DW_AT_digit_count:
6454           attrs->at_digit_count = a;
6455           break;
6456         case DW_AT_discr:
6457           attrs->at_discr = a;
6458           break;
6459         case DW_AT_discr_list:
6460           attrs->at_discr_list = a;
6461           break;
6462         case DW_AT_discr_value:
6463           attrs->at_discr_value = a;
6464           break;
6465         case DW_AT_encoding:
6466           attrs->at_encoding = a;
6467           break;
6468         case DW_AT_endianity:
6469           attrs->at_endianity = a;
6470           break;
6471         case DW_AT_explicit:
6472           attrs->at_explicit = a;
6473           break;
6474         case DW_AT_is_optional:
6475           attrs->at_is_optional = a;
6476           break;
6477         case DW_AT_location:
6478           attrs->at_location = a;
6479           break;
6480         case DW_AT_lower_bound:
6481           attrs->at_lower_bound = a;
6482           break;
6483         case DW_AT_mutable:
6484           attrs->at_mutable = a;
6485           break;
6486         case DW_AT_ordering:
6487           attrs->at_ordering = a;
6488           break;
6489         case DW_AT_picture_string:
6490           attrs->at_picture_string = a;
6491           break;
6492         case DW_AT_prototyped:
6493           attrs->at_prototyped = a;
6494           break;
6495         case DW_AT_small:
6496           attrs->at_small = a;
6497           break;
6498         case DW_AT_segment:
6499           attrs->at_segment = a;
6500           break;
6501         case DW_AT_string_length:
6502           attrs->at_string_length = a;
6503           break;
6504         case DW_AT_threads_scaled:
6505           attrs->at_threads_scaled = a;
6506           break;
6507         case DW_AT_upper_bound:
6508           attrs->at_upper_bound = a;
6509           break;
6510         case DW_AT_use_location:
6511           attrs->at_use_location = a;
6512           break;
6513         case DW_AT_use_UTF8:
6514           attrs->at_use_UTF8 = a;
6515           break;
6516         case DW_AT_variable_parameter:
6517           attrs->at_variable_parameter = a;
6518           break;
6519         case DW_AT_virtuality:
6520           attrs->at_virtuality = a;
6521           break;
6522         case DW_AT_visibility:
6523           attrs->at_visibility = a;
6524           break;
6525         case DW_AT_vtable_elem_location:
6526           attrs->at_vtable_elem_location = a;
6527           break;
6528         default:
6529           break;
6530         }
6531     }
6532 }
6533
6534 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6535
6536 static void
6537 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6538 {
6539   dw_die_ref c;
6540   dw_die_ref decl;
6541   struct checksum_attributes attrs;
6542
6543   CHECKSUM_ULEB128 ('D');
6544   CHECKSUM_ULEB128 (die->die_tag);
6545
6546   memset (&attrs, 0, sizeof (attrs));
6547
6548   decl = get_AT_ref (die, DW_AT_specification);
6549   if (decl != NULL)
6550     collect_checksum_attributes (&attrs, decl);
6551   collect_checksum_attributes (&attrs, die);
6552
6553   CHECKSUM_ATTR (attrs.at_name);
6554   CHECKSUM_ATTR (attrs.at_accessibility);
6555   CHECKSUM_ATTR (attrs.at_address_class);
6556   CHECKSUM_ATTR (attrs.at_allocated);
6557   CHECKSUM_ATTR (attrs.at_artificial);
6558   CHECKSUM_ATTR (attrs.at_associated);
6559   CHECKSUM_ATTR (attrs.at_binary_scale);
6560   CHECKSUM_ATTR (attrs.at_bit_offset);
6561   CHECKSUM_ATTR (attrs.at_bit_size);
6562   CHECKSUM_ATTR (attrs.at_bit_stride);
6563   CHECKSUM_ATTR (attrs.at_byte_size);
6564   CHECKSUM_ATTR (attrs.at_byte_stride);
6565   CHECKSUM_ATTR (attrs.at_const_value);
6566   CHECKSUM_ATTR (attrs.at_containing_type);
6567   CHECKSUM_ATTR (attrs.at_count);
6568   CHECKSUM_ATTR (attrs.at_data_location);
6569   CHECKSUM_ATTR (attrs.at_data_member_location);
6570   CHECKSUM_ATTR (attrs.at_decimal_scale);
6571   CHECKSUM_ATTR (attrs.at_decimal_sign);
6572   CHECKSUM_ATTR (attrs.at_default_value);
6573   CHECKSUM_ATTR (attrs.at_digit_count);
6574   CHECKSUM_ATTR (attrs.at_discr);
6575   CHECKSUM_ATTR (attrs.at_discr_list);
6576   CHECKSUM_ATTR (attrs.at_discr_value);
6577   CHECKSUM_ATTR (attrs.at_encoding);
6578   CHECKSUM_ATTR (attrs.at_endianity);
6579   CHECKSUM_ATTR (attrs.at_explicit);
6580   CHECKSUM_ATTR (attrs.at_is_optional);
6581   CHECKSUM_ATTR (attrs.at_location);
6582   CHECKSUM_ATTR (attrs.at_lower_bound);
6583   CHECKSUM_ATTR (attrs.at_mutable);
6584   CHECKSUM_ATTR (attrs.at_ordering);
6585   CHECKSUM_ATTR (attrs.at_picture_string);
6586   CHECKSUM_ATTR (attrs.at_prototyped);
6587   CHECKSUM_ATTR (attrs.at_small);
6588   CHECKSUM_ATTR (attrs.at_segment);
6589   CHECKSUM_ATTR (attrs.at_string_length);
6590   CHECKSUM_ATTR (attrs.at_threads_scaled);
6591   CHECKSUM_ATTR (attrs.at_upper_bound);
6592   CHECKSUM_ATTR (attrs.at_use_location);
6593   CHECKSUM_ATTR (attrs.at_use_UTF8);
6594   CHECKSUM_ATTR (attrs.at_variable_parameter);
6595   CHECKSUM_ATTR (attrs.at_virtuality);
6596   CHECKSUM_ATTR (attrs.at_visibility);
6597   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6598   CHECKSUM_ATTR (attrs.at_type);
6599   CHECKSUM_ATTR (attrs.at_friend);
6600
6601   /* Checksum the child DIEs.  */
6602   c = die->die_child;
6603   if (c) do {
6604     dw_attr_node *name_attr;
6605
6606     c = c->die_sib;
6607     name_attr = get_AT (c, DW_AT_name);
6608     if (is_template_instantiation (c))
6609       {
6610         /* Ignore instantiations of member type and function templates.  */
6611       }
6612     else if (name_attr != NULL
6613              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6614       {
6615         /* Use a shallow checksum for named nested types and member
6616            functions.  */
6617         CHECKSUM_ULEB128 ('S');
6618         CHECKSUM_ULEB128 (c->die_tag);
6619         CHECKSUM_STRING (AT_string (name_attr));
6620       }
6621     else
6622       {
6623         /* Use a deep checksum for other children.  */
6624         /* Mark this DIE so it gets processed when unmarking.  */
6625         if (c->die_mark == 0)
6626           c->die_mark = -1;
6627         die_checksum_ordered (c, ctx, mark);
6628       }
6629   } while (c != die->die_child);
6630
6631   CHECKSUM_ULEB128 (0);
6632 }
6633
6634 /* Add a type name and tag to a hash.  */
6635 static void
6636 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6637 {
6638   CHECKSUM_ULEB128 (tag);
6639   CHECKSUM_STRING (name);
6640 }
6641
6642 #undef CHECKSUM
6643 #undef CHECKSUM_STRING
6644 #undef CHECKSUM_ATTR
6645 #undef CHECKSUM_LEB128
6646 #undef CHECKSUM_ULEB128
6647
6648 /* Generate the type signature for DIE.  This is computed by generating an
6649    MD5 checksum over the DIE's tag, its relevant attributes, and its
6650    children.  Attributes that are references to other DIEs are processed
6651    by recursion, using the MARK field to prevent infinite recursion.
6652    If the DIE is nested inside a namespace or another type, we also
6653    need to include that context in the signature.  The lower 64 bits
6654    of the resulting MD5 checksum comprise the signature.  */
6655
6656 static void
6657 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6658 {
6659   int mark;
6660   const char *name;
6661   unsigned char checksum[16];
6662   struct md5_ctx ctx;
6663   dw_die_ref decl;
6664   dw_die_ref parent;
6665
6666   name = get_AT_string (die, DW_AT_name);
6667   decl = get_AT_ref (die, DW_AT_specification);
6668   parent = get_die_parent (die);
6669
6670   /* First, compute a signature for just the type name (and its surrounding
6671      context, if any.  This is stored in the type unit DIE for link-time
6672      ODR (one-definition rule) checking.  */
6673
6674   if (is_cxx () && name != NULL)
6675     {
6676       md5_init_ctx (&ctx);
6677
6678       /* Checksum the names of surrounding namespaces and structures.  */
6679       if (parent != NULL)
6680         checksum_die_context (parent, &ctx);
6681
6682       /* Checksum the current DIE. */
6683       die_odr_checksum (die->die_tag, name, &ctx);
6684       md5_finish_ctx (&ctx, checksum);
6685
6686       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6687     }
6688
6689   /* Next, compute the complete type signature.  */
6690
6691   md5_init_ctx (&ctx);
6692   mark = 1;
6693   die->die_mark = mark;
6694
6695   /* Checksum the names of surrounding namespaces and structures.  */
6696   if (parent != NULL)
6697     checksum_die_context (parent, &ctx);
6698
6699   /* Checksum the DIE and its children.  */
6700   die_checksum_ordered (die, &ctx, &mark);
6701   unmark_all_dies (die);
6702   md5_finish_ctx (&ctx, checksum);
6703
6704   /* Store the signature in the type node and link the type DIE and the
6705      type node together.  */
6706   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6707           DWARF_TYPE_SIGNATURE_SIZE);
6708   die->comdat_type_p = true;
6709   die->die_id.die_type_node = type_node;
6710   type_node->type_die = die;
6711
6712   /* If the DIE is a specification, link its declaration to the type node
6713      as well.  */
6714   if (decl != NULL)
6715     {
6716       decl->comdat_type_p = true;
6717       decl->die_id.die_type_node = type_node;
6718     }
6719 }
6720
6721 /* Do the location expressions look same?  */
6722 static inline int
6723 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6724 {
6725   return loc1->dw_loc_opc == loc2->dw_loc_opc
6726          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6727          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6728 }
6729
6730 /* Do the values look the same?  */
6731 static int
6732 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6733 {
6734   dw_loc_descr_ref loc1, loc2;
6735   rtx r1, r2;
6736
6737   if (v1->val_class != v2->val_class)
6738     return 0;
6739
6740   switch (v1->val_class)
6741     {
6742     case dw_val_class_const:
6743       return v1->v.val_int == v2->v.val_int;
6744     case dw_val_class_unsigned_const:
6745       return v1->v.val_unsigned == v2->v.val_unsigned;
6746     case dw_val_class_const_double:
6747       return v1->v.val_double.high == v2->v.val_double.high
6748              && v1->v.val_double.low == v2->v.val_double.low;
6749     case dw_val_class_wide_int:
6750       return *v1->v.val_wide == *v2->v.val_wide;
6751     case dw_val_class_vec:
6752       if (v1->v.val_vec.length != v2->v.val_vec.length
6753           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6754         return 0;
6755       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6756                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6757         return 0;
6758       return 1;
6759     case dw_val_class_flag:
6760       return v1->v.val_flag == v2->v.val_flag;
6761     case dw_val_class_str:
6762       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6763
6764     case dw_val_class_addr:
6765       r1 = v1->v.val_addr;
6766       r2 = v2->v.val_addr;
6767       if (GET_CODE (r1) != GET_CODE (r2))
6768         return 0;
6769       return !rtx_equal_p (r1, r2);
6770
6771     case dw_val_class_offset:
6772       return v1->v.val_offset == v2->v.val_offset;
6773
6774     case dw_val_class_loc:
6775       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6776            loc1 && loc2;
6777            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6778         if (!same_loc_p (loc1, loc2, mark))
6779           return 0;
6780       return !loc1 && !loc2;
6781
6782     case dw_val_class_die_ref:
6783       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6784
6785     case dw_val_class_fde_ref:
6786     case dw_val_class_vms_delta:
6787     case dw_val_class_lbl_id:
6788     case dw_val_class_lineptr:
6789     case dw_val_class_macptr:
6790     case dw_val_class_high_pc:
6791       return 1;
6792
6793     case dw_val_class_file:
6794       return v1->v.val_file == v2->v.val_file;
6795
6796     case dw_val_class_data8:
6797       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6798
6799     default:
6800       return 1;
6801     }
6802 }
6803
6804 /* Do the attributes look the same?  */
6805
6806 static int
6807 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6808 {
6809   if (at1->dw_attr != at2->dw_attr)
6810     return 0;
6811
6812   /* We don't care that this was compiled with a different compiler
6813      snapshot; if the output is the same, that's what matters. */
6814   if (at1->dw_attr == DW_AT_producer)
6815     return 1;
6816
6817   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6818 }
6819
6820 /* Do the dies look the same?  */
6821
6822 static int
6823 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6824 {
6825   dw_die_ref c1, c2;
6826   dw_attr_node *a1;
6827   unsigned ix;
6828
6829   /* To avoid infinite recursion.  */
6830   if (die1->die_mark)
6831     return die1->die_mark == die2->die_mark;
6832   die1->die_mark = die2->die_mark = ++(*mark);
6833
6834   if (die1->die_tag != die2->die_tag)
6835     return 0;
6836
6837   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6838     return 0;
6839
6840   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6841     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6842       return 0;
6843
6844   c1 = die1->die_child;
6845   c2 = die2->die_child;
6846   if (! c1)
6847     {
6848       if (c2)
6849         return 0;
6850     }
6851   else
6852     for (;;)
6853       {
6854         if (!same_die_p (c1, c2, mark))
6855           return 0;
6856         c1 = c1->die_sib;
6857         c2 = c2->die_sib;
6858         if (c1 == die1->die_child)
6859           {
6860             if (c2 == die2->die_child)
6861               break;
6862             else
6863               return 0;
6864           }
6865     }
6866
6867   return 1;
6868 }
6869
6870 /* Do the dies look the same?  Wrapper around same_die_p.  */
6871
6872 static int
6873 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6874 {
6875   int mark = 0;
6876   int ret = same_die_p (die1, die2, &mark);
6877
6878   unmark_all_dies (die1);
6879   unmark_all_dies (die2);
6880
6881   return ret;
6882 }
6883
6884 /* The prefix to attach to symbols on DIEs in the current comdat debug
6885    info section.  */
6886 static const char *comdat_symbol_id;
6887
6888 /* The index of the current symbol within the current comdat CU.  */
6889 static unsigned int comdat_symbol_number;
6890
6891 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6892    children, and set comdat_symbol_id accordingly.  */
6893
6894 static void
6895 compute_section_prefix (dw_die_ref unit_die)
6896 {
6897   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6898   const char *base = die_name ? lbasename (die_name) : "anonymous";
6899   char *name = XALLOCAVEC (char, strlen (base) + 64);
6900   char *p;
6901   int i, mark;
6902   unsigned char checksum[16];
6903   struct md5_ctx ctx;
6904
6905   /* Compute the checksum of the DIE, then append part of it as hex digits to
6906      the name filename of the unit.  */
6907
6908   md5_init_ctx (&ctx);
6909   mark = 0;
6910   die_checksum (unit_die, &ctx, &mark);
6911   unmark_all_dies (unit_die);
6912   md5_finish_ctx (&ctx, checksum);
6913
6914   sprintf (name, "%s.", base);
6915   clean_symbol_name (name);
6916
6917   p = name + strlen (name);
6918   for (i = 0; i < 4; i++)
6919     {
6920       sprintf (p, "%.2x", checksum[i]);
6921       p += 2;
6922     }
6923
6924   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6925   comdat_symbol_number = 0;
6926 }
6927
6928 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6929
6930 static int
6931 is_type_die (dw_die_ref die)
6932 {
6933   switch (die->die_tag)
6934     {
6935     case DW_TAG_array_type:
6936     case DW_TAG_class_type:
6937     case DW_TAG_interface_type:
6938     case DW_TAG_enumeration_type:
6939     case DW_TAG_pointer_type:
6940     case DW_TAG_reference_type:
6941     case DW_TAG_rvalue_reference_type:
6942     case DW_TAG_string_type:
6943     case DW_TAG_structure_type:
6944     case DW_TAG_subroutine_type:
6945     case DW_TAG_union_type:
6946     case DW_TAG_ptr_to_member_type:
6947     case DW_TAG_set_type:
6948     case DW_TAG_subrange_type:
6949     case DW_TAG_base_type:
6950     case DW_TAG_const_type:
6951     case DW_TAG_file_type:
6952     case DW_TAG_packed_type:
6953     case DW_TAG_volatile_type:
6954     case DW_TAG_typedef:
6955       return 1;
6956     default:
6957       return 0;
6958     }
6959 }
6960
6961 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6962    Basically, we want to choose the bits that are likely to be shared between
6963    compilations (types) and leave out the bits that are specific to individual
6964    compilations (functions).  */
6965
6966 static int
6967 is_comdat_die (dw_die_ref c)
6968 {
6969   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6970      we do for stabs.  The advantage is a greater likelihood of sharing between
6971      objects that don't include headers in the same order (and therefore would
6972      put the base types in a different comdat).  jason 8/28/00 */
6973
6974   if (c->die_tag == DW_TAG_base_type)
6975     return 0;
6976
6977   if (c->die_tag == DW_TAG_pointer_type
6978       || c->die_tag == DW_TAG_reference_type
6979       || c->die_tag == DW_TAG_rvalue_reference_type
6980       || c->die_tag == DW_TAG_const_type
6981       || c->die_tag == DW_TAG_volatile_type)
6982     {
6983       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6984
6985       return t ? is_comdat_die (t) : 0;
6986     }
6987
6988   return is_type_die (c);
6989 }
6990
6991 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6992    compilation unit.  */
6993
6994 static int
6995 is_symbol_die (dw_die_ref c)
6996 {
6997   return (is_type_die (c)
6998           || is_declaration_die (c)
6999           || c->die_tag == DW_TAG_namespace
7000           || c->die_tag == DW_TAG_module);
7001 }
7002
7003 /* Returns true iff C is a compile-unit DIE.  */
7004
7005 static inline bool
7006 is_cu_die (dw_die_ref c)
7007 {
7008   return c && c->die_tag == DW_TAG_compile_unit;
7009 }
7010
7011 /* Returns true iff C is a unit DIE of some sort.  */
7012
7013 static inline bool
7014 is_unit_die (dw_die_ref c)
7015 {
7016   return c && (c->die_tag == DW_TAG_compile_unit
7017                || c->die_tag == DW_TAG_partial_unit
7018                || c->die_tag == DW_TAG_type_unit);
7019 }
7020
7021 /* Returns true iff C is a namespace DIE.  */
7022
7023 static inline bool
7024 is_namespace_die (dw_die_ref c)
7025 {
7026   return c && c->die_tag == DW_TAG_namespace;
7027 }
7028
7029 /* Returns true iff C is a class or structure DIE.  */
7030
7031 static inline bool
7032 is_class_die (dw_die_ref c)
7033 {
7034   return c && (c->die_tag == DW_TAG_class_type
7035                || c->die_tag == DW_TAG_structure_type);
7036 }
7037
7038 /* Return non-zero if this DIE is a template parameter.  */
7039
7040 static inline bool
7041 is_template_parameter (dw_die_ref die)
7042 {
7043   switch (die->die_tag)
7044     {
7045     case DW_TAG_template_type_param:
7046     case DW_TAG_template_value_param:
7047     case DW_TAG_GNU_template_template_param:
7048     case DW_TAG_GNU_template_parameter_pack:
7049       return true;
7050     default:
7051       return false;
7052     }
7053 }
7054
7055 /* Return non-zero if this DIE represents a template instantiation.  */
7056
7057 static inline bool
7058 is_template_instantiation (dw_die_ref die)
7059 {
7060   dw_die_ref c;
7061
7062   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7063     return false;
7064   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7065   return false;
7066 }
7067
7068 static char *
7069 gen_internal_sym (const char *prefix)
7070 {
7071   char buf[256];
7072
7073   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7074   return xstrdup (buf);
7075 }
7076
7077 /* Assign symbols to all worthy DIEs under DIE.  */
7078
7079 static void
7080 assign_symbol_names (dw_die_ref die)
7081 {
7082   dw_die_ref c;
7083
7084   if (is_symbol_die (die) && !die->comdat_type_p)
7085     {
7086       if (comdat_symbol_id)
7087         {
7088           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7089
7090           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7091                    comdat_symbol_id, comdat_symbol_number++);
7092           die->die_id.die_symbol = xstrdup (p);
7093         }
7094       else
7095         die->die_id.die_symbol = gen_internal_sym ("LDIE");
7096     }
7097
7098   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7099 }
7100
7101 struct cu_hash_table_entry
7102 {
7103   dw_die_ref cu;
7104   unsigned min_comdat_num, max_comdat_num;
7105   struct cu_hash_table_entry *next;
7106 };
7107
7108 /* Helpers to manipulate hash table of CUs.  */
7109
7110 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7111 {
7112   typedef die_struct *compare_type;
7113   static inline hashval_t hash (const cu_hash_table_entry *);
7114   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7115   static inline void remove (cu_hash_table_entry *);
7116 };
7117
7118 inline hashval_t
7119 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7120 {
7121   return htab_hash_string (entry->cu->die_id.die_symbol);
7122 }
7123
7124 inline bool
7125 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7126                                    const die_struct *entry2)
7127 {
7128   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7129 }
7130
7131 inline void
7132 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7133 {
7134   struct cu_hash_table_entry *next;
7135
7136   while (entry)
7137     {
7138       next = entry->next;
7139       free (entry);
7140       entry = next;
7141     }
7142 }
7143
7144 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7145
7146 /* Check whether we have already seen this CU and set up SYM_NUM
7147    accordingly.  */
7148 static int
7149 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7150 {
7151   struct cu_hash_table_entry dummy;
7152   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7153
7154   dummy.max_comdat_num = 0;
7155
7156   slot = htable->find_slot_with_hash (cu,
7157                                       htab_hash_string (cu->die_id.die_symbol),
7158                                       INSERT);
7159   entry = *slot;
7160
7161   for (; entry; last = entry, entry = entry->next)
7162     {
7163       if (same_die_p_wrap (cu, entry->cu))
7164         break;
7165     }
7166
7167   if (entry)
7168     {
7169       *sym_num = entry->min_comdat_num;
7170       return 1;
7171     }
7172
7173   entry = XCNEW (struct cu_hash_table_entry);
7174   entry->cu = cu;
7175   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7176   entry->next = *slot;
7177   *slot = entry;
7178
7179   return 0;
7180 }
7181
7182 /* Record SYM_NUM to record of CU in HTABLE.  */
7183 static void
7184 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7185                              unsigned int sym_num)
7186 {
7187   struct cu_hash_table_entry **slot, *entry;
7188
7189   slot = htable->find_slot_with_hash (cu,
7190                                       htab_hash_string (cu->die_id.die_symbol),
7191                                       NO_INSERT);
7192   entry = *slot;
7193
7194   entry->max_comdat_num = sym_num;
7195 }
7196
7197 /* Traverse the DIE (which is always comp_unit_die), and set up
7198    additional compilation units for each of the include files we see
7199    bracketed by BINCL/EINCL.  */
7200
7201 static void
7202 break_out_includes (dw_die_ref die)
7203 {
7204   dw_die_ref c;
7205   dw_die_ref unit = NULL;
7206   limbo_die_node *node, **pnode;
7207
7208   c = die->die_child;
7209   if (c) do {
7210     dw_die_ref prev = c;
7211     c = c->die_sib;
7212     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7213            || (unit && is_comdat_die (c)))
7214       {
7215         dw_die_ref next = c->die_sib;
7216
7217         /* This DIE is for a secondary CU; remove it from the main one.  */
7218         remove_child_with_prev (c, prev);
7219
7220         if (c->die_tag == DW_TAG_GNU_BINCL)
7221           unit = push_new_compile_unit (unit, c);
7222         else if (c->die_tag == DW_TAG_GNU_EINCL)
7223           unit = pop_compile_unit (unit);
7224         else
7225           add_child_die (unit, c);
7226         c = next;
7227         if (c == die->die_child)
7228           break;
7229       }
7230   } while (c != die->die_child);
7231
7232 #if 0
7233   /* We can only use this in debugging, since the frontend doesn't check
7234      to make sure that we leave every include file we enter.  */
7235   gcc_assert (!unit);
7236 #endif
7237
7238   assign_symbol_names (die);
7239   cu_hash_type cu_hash_table (10);
7240   for (node = limbo_die_list, pnode = &limbo_die_list;
7241        node;
7242        node = node->next)
7243     {
7244       int is_dupl;
7245
7246       compute_section_prefix (node->die);
7247       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7248                         &comdat_symbol_number);
7249       assign_symbol_names (node->die);
7250       if (is_dupl)
7251         *pnode = node->next;
7252       else
7253         {
7254           pnode = &node->next;
7255           record_comdat_symbol_number (node->die, &cu_hash_table,
7256                 comdat_symbol_number);
7257         }
7258     }
7259 }
7260
7261 /* Return non-zero if this DIE is a declaration.  */
7262
7263 static int
7264 is_declaration_die (dw_die_ref die)
7265 {
7266   dw_attr_node *a;
7267   unsigned ix;
7268
7269   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7270     if (a->dw_attr == DW_AT_declaration)
7271       return 1;
7272
7273   return 0;
7274 }
7275
7276 /* Return non-zero if this DIE is nested inside a subprogram.  */
7277
7278 static int
7279 is_nested_in_subprogram (dw_die_ref die)
7280 {
7281   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7282
7283   if (decl == NULL)
7284     decl = die;
7285   return local_scope_p (decl);
7286 }
7287
7288 /* Return non-zero if this DIE contains a defining declaration of a
7289    subprogram.  */
7290
7291 static int
7292 contains_subprogram_definition (dw_die_ref die)
7293 {
7294   dw_die_ref c;
7295
7296   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7297     return 1;
7298   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7299   return 0;
7300 }
7301
7302 /* Return non-zero if this is a type DIE that should be moved to a
7303    COMDAT .debug_types section.  */
7304
7305 static int
7306 should_move_die_to_comdat (dw_die_ref die)
7307 {
7308   switch (die->die_tag)
7309     {
7310     case DW_TAG_class_type:
7311     case DW_TAG_structure_type:
7312     case DW_TAG_enumeration_type:
7313     case DW_TAG_union_type:
7314       /* Don't move declarations, inlined instances, types nested in a
7315          subprogram, or types that contain subprogram definitions.  */
7316       if (is_declaration_die (die)
7317           || get_AT (die, DW_AT_abstract_origin)
7318           || is_nested_in_subprogram (die)
7319           || contains_subprogram_definition (die))
7320         return 0;
7321       return 1;
7322     case DW_TAG_array_type:
7323     case DW_TAG_interface_type:
7324     case DW_TAG_pointer_type:
7325     case DW_TAG_reference_type:
7326     case DW_TAG_rvalue_reference_type:
7327     case DW_TAG_string_type:
7328     case DW_TAG_subroutine_type:
7329     case DW_TAG_ptr_to_member_type:
7330     case DW_TAG_set_type:
7331     case DW_TAG_subrange_type:
7332     case DW_TAG_base_type:
7333     case DW_TAG_const_type:
7334     case DW_TAG_file_type:
7335     case DW_TAG_packed_type:
7336     case DW_TAG_volatile_type:
7337     case DW_TAG_typedef:
7338     default:
7339       return 0;
7340     }
7341 }
7342
7343 /* Make a clone of DIE.  */
7344
7345 static dw_die_ref
7346 clone_die (dw_die_ref die)
7347 {
7348   dw_die_ref clone;
7349   dw_attr_node *a;
7350   unsigned ix;
7351
7352   clone = ggc_cleared_alloc<die_node> ();
7353   clone->die_tag = die->die_tag;
7354
7355   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7356     add_dwarf_attr (clone, a);
7357
7358   return clone;
7359 }
7360
7361 /* Make a clone of the tree rooted at DIE.  */
7362
7363 static dw_die_ref
7364 clone_tree (dw_die_ref die)
7365 {
7366   dw_die_ref c;
7367   dw_die_ref clone = clone_die (die);
7368
7369   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7370
7371   return clone;
7372 }
7373
7374 /* Make a clone of DIE as a declaration.  */
7375
7376 static dw_die_ref
7377 clone_as_declaration (dw_die_ref die)
7378 {
7379   dw_die_ref clone;
7380   dw_die_ref decl;
7381   dw_attr_node *a;
7382   unsigned ix;
7383
7384   /* If the DIE is already a declaration, just clone it.  */
7385   if (is_declaration_die (die))
7386     return clone_die (die);
7387
7388   /* If the DIE is a specification, just clone its declaration DIE.  */
7389   decl = get_AT_ref (die, DW_AT_specification);
7390   if (decl != NULL)
7391     {
7392       clone = clone_die (decl);
7393       if (die->comdat_type_p)
7394         add_AT_die_ref (clone, DW_AT_signature, die);
7395       return clone;
7396     }
7397
7398   clone = ggc_cleared_alloc<die_node> ();
7399   clone->die_tag = die->die_tag;
7400
7401   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7402     {
7403       /* We don't want to copy over all attributes.
7404          For example we don't want DW_AT_byte_size because otherwise we will no
7405          longer have a declaration and GDB will treat it as a definition.  */
7406
7407       switch (a->dw_attr)
7408         {
7409         case DW_AT_abstract_origin:
7410         case DW_AT_artificial:
7411         case DW_AT_containing_type:
7412         case DW_AT_external:
7413         case DW_AT_name:
7414         case DW_AT_type:
7415         case DW_AT_virtuality:
7416         case DW_AT_linkage_name:
7417         case DW_AT_MIPS_linkage_name:
7418           add_dwarf_attr (clone, a);
7419           break;
7420         case DW_AT_byte_size:
7421         default:
7422           break;
7423         }
7424     }
7425
7426   if (die->comdat_type_p)
7427     add_AT_die_ref (clone, DW_AT_signature, die);
7428
7429   add_AT_flag (clone, DW_AT_declaration, 1);
7430   return clone;
7431 }
7432
7433
7434 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7435
7436 struct decl_table_entry
7437 {
7438   dw_die_ref orig;
7439   dw_die_ref copy;
7440 };
7441
7442 /* Helpers to manipulate hash table of copied declarations.  */
7443
7444 /* Hashtable helpers.  */
7445
7446 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7447 {
7448   typedef die_struct *compare_type;
7449   static inline hashval_t hash (const decl_table_entry *);
7450   static inline bool equal (const decl_table_entry *, const die_struct *);
7451 };
7452
7453 inline hashval_t
7454 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7455 {
7456   return htab_hash_pointer (entry->orig);
7457 }
7458
7459 inline bool
7460 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7461                                 const die_struct *entry2)
7462 {
7463   return entry1->orig == entry2;
7464 }
7465
7466 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7467
7468 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7469    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7470    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7471    to check if the ancestor has already been copied into UNIT.  */
7472
7473 static dw_die_ref
7474 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7475                     decl_hash_type *decl_table)
7476 {
7477   dw_die_ref parent = die->die_parent;
7478   dw_die_ref new_parent = unit;
7479   dw_die_ref copy;
7480   decl_table_entry **slot = NULL;
7481   struct decl_table_entry *entry = NULL;
7482
7483   if (decl_table)
7484     {
7485       /* Check if the entry has already been copied to UNIT.  */
7486       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7487                                               INSERT);
7488       if (*slot != HTAB_EMPTY_ENTRY)
7489         {
7490           entry = *slot;
7491           return entry->copy;
7492         }
7493
7494       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7495       entry = XCNEW (struct decl_table_entry);
7496       entry->orig = die;
7497       entry->copy = NULL;
7498       *slot = entry;
7499     }
7500
7501   if (parent != NULL)
7502     {
7503       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7504       if (spec != NULL)
7505         parent = spec;
7506       if (!is_unit_die (parent))
7507         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7508     }
7509
7510   copy = clone_as_declaration (die);
7511   add_child_die (new_parent, copy);
7512
7513   if (decl_table)
7514     {
7515       /* Record the pointer to the copy.  */
7516       entry->copy = copy;
7517     }
7518
7519   return copy;
7520 }
7521 /* Copy the declaration context to the new type unit DIE.  This includes
7522    any surrounding namespace or type declarations.  If the DIE has an
7523    AT_specification attribute, it also includes attributes and children
7524    attached to the specification, and returns a pointer to the original
7525    parent of the declaration DIE.  Returns NULL otherwise.  */
7526
7527 static dw_die_ref
7528 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7529 {
7530   dw_die_ref decl;
7531   dw_die_ref new_decl;
7532   dw_die_ref orig_parent = NULL;
7533
7534   decl = get_AT_ref (die, DW_AT_specification);
7535   if (decl == NULL)
7536     decl = die;
7537   else
7538     {
7539       unsigned ix;
7540       dw_die_ref c;
7541       dw_attr_node *a;
7542
7543       /* The original DIE will be changed to a declaration, and must
7544          be moved to be a child of the original declaration DIE.  */
7545       orig_parent = decl->die_parent;
7546
7547       /* Copy the type node pointer from the new DIE to the original
7548          declaration DIE so we can forward references later.  */
7549       decl->comdat_type_p = true;
7550       decl->die_id.die_type_node = die->die_id.die_type_node;
7551
7552       remove_AT (die, DW_AT_specification);
7553
7554       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7555         {
7556           if (a->dw_attr != DW_AT_name
7557               && a->dw_attr != DW_AT_declaration
7558               && a->dw_attr != DW_AT_external)
7559             add_dwarf_attr (die, a);
7560         }
7561
7562       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7563     }
7564
7565   if (decl->die_parent != NULL
7566       && !is_unit_die (decl->die_parent))
7567     {
7568       new_decl = copy_ancestor_tree (unit, decl, NULL);
7569       if (new_decl != NULL)
7570         {
7571           remove_AT (new_decl, DW_AT_signature);
7572           add_AT_specification (die, new_decl);
7573         }
7574     }
7575
7576   return orig_parent;
7577 }
7578
7579 /* Generate the skeleton ancestor tree for the given NODE, then clone
7580    the DIE and add the clone into the tree.  */
7581
7582 static void
7583 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7584 {
7585   if (node->new_die != NULL)
7586     return;
7587
7588   node->new_die = clone_as_declaration (node->old_die);
7589
7590   if (node->parent != NULL)
7591     {
7592       generate_skeleton_ancestor_tree (node->parent);
7593       add_child_die (node->parent->new_die, node->new_die);
7594     }
7595 }
7596
7597 /* Generate a skeleton tree of DIEs containing any declarations that are
7598    found in the original tree.  We traverse the tree looking for declaration
7599    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7600
7601 static void
7602 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7603 {
7604   skeleton_chain_node node;
7605   dw_die_ref c;
7606   dw_die_ref first;
7607   dw_die_ref prev = NULL;
7608   dw_die_ref next = NULL;
7609
7610   node.parent = parent;
7611
7612   first = c = parent->old_die->die_child;
7613   if (c)
7614     next = c->die_sib;
7615   if (c) do {
7616     if (prev == NULL || prev->die_sib == c)
7617       prev = c;
7618     c = next;
7619     next = (c == first ? NULL : c->die_sib);
7620     node.old_die = c;
7621     node.new_die = NULL;
7622     if (is_declaration_die (c))
7623       {
7624         if (is_template_instantiation (c))
7625           {
7626             /* Instantiated templates do not need to be cloned into the
7627                type unit.  Just move the DIE and its children back to
7628                the skeleton tree (in the main CU).  */
7629             remove_child_with_prev (c, prev);
7630             add_child_die (parent->new_die, c);
7631             c = prev;
7632           }
7633         else
7634           {
7635             /* Clone the existing DIE, move the original to the skeleton
7636                tree (which is in the main CU), and put the clone, with
7637                all the original's children, where the original came from
7638                (which is about to be moved to the type unit).  */
7639             dw_die_ref clone = clone_die (c);
7640             move_all_children (c, clone);
7641
7642             /* If the original has a DW_AT_object_pointer attribute,
7643                it would now point to a child DIE just moved to the
7644                cloned tree, so we need to remove that attribute from
7645                the original.  */
7646             remove_AT (c, DW_AT_object_pointer);
7647
7648             replace_child (c, clone, prev);
7649             generate_skeleton_ancestor_tree (parent);
7650             add_child_die (parent->new_die, c);
7651             node.new_die = c;
7652             c = clone;
7653           }
7654       }
7655     generate_skeleton_bottom_up (&node);
7656   } while (next != NULL);
7657 }
7658
7659 /* Wrapper function for generate_skeleton_bottom_up.  */
7660
7661 static dw_die_ref
7662 generate_skeleton (dw_die_ref die)
7663 {
7664   skeleton_chain_node node;
7665
7666   node.old_die = die;
7667   node.new_die = NULL;
7668   node.parent = NULL;
7669
7670   /* If this type definition is nested inside another type,
7671      and is not an instantiation of a template, always leave
7672      at least a declaration in its place.  */
7673   if (die->die_parent != NULL
7674       && is_type_die (die->die_parent)
7675       && !is_template_instantiation (die))
7676     node.new_die = clone_as_declaration (die);
7677
7678   generate_skeleton_bottom_up (&node);
7679   return node.new_die;
7680 }
7681
7682 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7683    declaration.  The original DIE is moved to a new compile unit so that
7684    existing references to it follow it to the new location.  If any of the
7685    original DIE's descendants is a declaration, we need to replace the
7686    original DIE with a skeleton tree and move the declarations back into the
7687    skeleton tree.  */
7688
7689 static dw_die_ref
7690 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7691                                        dw_die_ref prev)
7692 {
7693   dw_die_ref skeleton, orig_parent;
7694
7695   /* Copy the declaration context to the type unit DIE.  If the returned
7696      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7697      that DIE.  */
7698   orig_parent = copy_declaration_context (unit, child);
7699
7700   skeleton = generate_skeleton (child);
7701   if (skeleton == NULL)
7702     remove_child_with_prev (child, prev);
7703   else
7704     {
7705       skeleton->comdat_type_p = true;
7706       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7707
7708       /* If the original DIE was a specification, we need to put
7709          the skeleton under the parent DIE of the declaration.
7710          This leaves the original declaration in the tree, but
7711          it will be pruned later since there are no longer any
7712          references to it.  */
7713       if (orig_parent != NULL)
7714         {
7715           remove_child_with_prev (child, prev);
7716           add_child_die (orig_parent, skeleton);
7717         }
7718       else
7719         replace_child (child, skeleton, prev);
7720     }
7721
7722   return skeleton;
7723 }
7724
7725 static void
7726 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7727                                comdat_type_node *type_node,
7728                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7729
7730 /* Helper for copy_dwarf_procs_ref_in_dies.  Make a copy of the DIE DWARF
7731    procedure, put it under TYPE_NODE and return the copy.  Continue looking for
7732    DWARF procedure references in the DW_AT_location attribute.  */
7733
7734 static dw_die_ref
7735 copy_dwarf_procedure (dw_die_ref die,
7736                       comdat_type_node *type_node,
7737                       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7738 {
7739   /* We do this for COMDAT section, which is DWARFv4 specific, so
7740      DWARF procedure are always DW_TAG_dwarf_procedure DIEs (unlike
7741      DW_TAG_variable in DWARFv3).  */
7742   gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7743
7744   /* DWARF procedures are not supposed to have children...  */
7745   gcc_assert (die->die_child == NULL);
7746
7747   /* ... and they are supposed to have only one attribute: DW_AT_location.  */
7748   gcc_assert (vec_safe_length (die->die_attr) == 1
7749               && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7750
7751   /* Do not copy more than once DWARF procedures.  */
7752   bool existed;
7753   dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7754   if (existed)
7755     return die_copy;
7756
7757   die_copy = clone_die (die);
7758   add_child_die (type_node->root_die, die_copy);
7759   copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7760   return die_copy;
7761 }
7762
7763 /* Helper for copy_dwarf_procs_ref_in_dies.  Look for references to DWARF
7764    procedures in DIE's attributes.  */
7765
7766 static void
7767 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7768                                comdat_type_node *type_node,
7769                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7770 {
7771   dw_attr_node *a;
7772   unsigned i;
7773
7774   FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7775     {
7776       dw_loc_descr_ref loc;
7777
7778       if (a->dw_attr_val.val_class != dw_val_class_loc)
7779         continue;
7780
7781       for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7782         {
7783           switch (loc->dw_loc_opc)
7784             {
7785             case DW_OP_call2:
7786             case DW_OP_call4:
7787             case DW_OP_call_ref:
7788               gcc_assert (loc->dw_loc_oprnd1.val_class
7789                           == dw_val_class_die_ref);
7790               loc->dw_loc_oprnd1.v.val_die_ref.die
7791                 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7792                                         type_node,
7793                                         copied_dwarf_procs);
7794
7795             default:
7796               break;
7797             }
7798         }
7799     }
7800 }
7801
7802 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7803    rewrite references to point to the copies.
7804
7805    References are looked for in DIE's attributes and recursively in all its
7806    children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7807    mapping from old DWARF procedures to their copy. It is used not to copy
7808    twice the same DWARF procedure under TYPE_NODE.  */
7809
7810 static void
7811 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7812                               comdat_type_node *type_node,
7813                               hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7814 {
7815   dw_die_ref c;
7816
7817   copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7818   FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7819                                                         type_node,
7820                                                         copied_dwarf_procs));
7821 }
7822
7823 /* Traverse the DIE and set up additional .debug_types sections for each
7824    type worthy of being placed in a COMDAT section.  */
7825
7826 static void
7827 break_out_comdat_types (dw_die_ref die)
7828 {
7829   dw_die_ref c;
7830   dw_die_ref first;
7831   dw_die_ref prev = NULL;
7832   dw_die_ref next = NULL;
7833   dw_die_ref unit = NULL;
7834
7835   first = c = die->die_child;
7836   if (c)
7837     next = c->die_sib;
7838   if (c) do {
7839     if (prev == NULL || prev->die_sib == c)
7840       prev = c;
7841     c = next;
7842     next = (c == first ? NULL : c->die_sib);
7843     if (should_move_die_to_comdat (c))
7844       {
7845         dw_die_ref replacement;
7846         comdat_type_node *type_node;
7847
7848         /* Break out nested types into their own type units.  */
7849         break_out_comdat_types (c);
7850
7851         /* Create a new type unit DIE as the root for the new tree, and
7852            add it to the list of comdat types.  */
7853         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7854         add_AT_unsigned (unit, DW_AT_language,
7855                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7856         type_node = ggc_cleared_alloc<comdat_type_node> ();
7857         type_node->root_die = unit;
7858         type_node->next = comdat_type_list;
7859         comdat_type_list = type_node;
7860
7861         /* Generate the type signature.  */
7862         generate_type_signature (c, type_node);
7863
7864         /* Copy the declaration context, attributes, and children of the
7865            declaration into the new type unit DIE, then remove this DIE
7866            from the main CU (or replace it with a skeleton if necessary).  */
7867         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7868         type_node->skeleton_die = replacement;
7869
7870         /* Add the DIE to the new compunit.  */
7871         add_child_die (unit, c);
7872
7873         /* Types can reference DWARF procedures for type size or data location
7874            expressions.  Calls in DWARF expressions cannot target procedures
7875            that are not in the same section.  So we must copy DWARF procedures
7876            along with this type and then rewrite references to them.  */
7877         hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7878         copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7879
7880         if (replacement != NULL)
7881           c = replacement;
7882       }
7883     else if (c->die_tag == DW_TAG_namespace
7884              || c->die_tag == DW_TAG_class_type
7885              || c->die_tag == DW_TAG_structure_type
7886              || c->die_tag == DW_TAG_union_type)
7887       {
7888         /* Look for nested types that can be broken out.  */
7889         break_out_comdat_types (c);
7890       }
7891   } while (next != NULL);
7892 }
7893
7894 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7895    Enter all the cloned children into the hash table decl_table.  */
7896
7897 static dw_die_ref
7898 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7899 {
7900   dw_die_ref c;
7901   dw_die_ref clone;
7902   struct decl_table_entry *entry;
7903   decl_table_entry **slot;
7904
7905   if (die->die_tag == DW_TAG_subprogram)
7906     clone = clone_as_declaration (die);
7907   else
7908     clone = clone_die (die);
7909
7910   slot = decl_table->find_slot_with_hash (die,
7911                                           htab_hash_pointer (die), INSERT);
7912
7913   /* Assert that DIE isn't in the hash table yet.  If it would be there
7914      before, the ancestors would be necessarily there as well, therefore
7915      clone_tree_partial wouldn't be called.  */
7916   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7917
7918   entry = XCNEW (struct decl_table_entry);
7919   entry->orig = die;
7920   entry->copy = clone;
7921   *slot = entry;
7922
7923   if (die->die_tag != DW_TAG_subprogram)
7924     FOR_EACH_CHILD (die, c,
7925                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7926
7927   return clone;
7928 }
7929
7930 /* Walk the DIE and its children, looking for references to incomplete
7931    or trivial types that are unmarked (i.e., that are not in the current
7932    type_unit).  */
7933
7934 static void
7935 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7936 {
7937   dw_die_ref c;
7938   dw_attr_node *a;
7939   unsigned ix;
7940
7941   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7942     {
7943       if (AT_class (a) == dw_val_class_die_ref)
7944         {
7945           dw_die_ref targ = AT_ref (a);
7946           decl_table_entry **slot;
7947           struct decl_table_entry *entry;
7948
7949           if (targ->die_mark != 0 || targ->comdat_type_p)
7950             continue;
7951
7952           slot = decl_table->find_slot_with_hash (targ,
7953                                                   htab_hash_pointer (targ),
7954                                                   INSERT);
7955
7956           if (*slot != HTAB_EMPTY_ENTRY)
7957             {
7958               /* TARG has already been copied, so we just need to
7959                  modify the reference to point to the copy.  */
7960               entry = *slot;
7961               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7962             }
7963           else
7964             {
7965               dw_die_ref parent = unit;
7966               dw_die_ref copy = clone_die (targ);
7967
7968               /* Record in DECL_TABLE that TARG has been copied.
7969                  Need to do this now, before the recursive call,
7970                  because DECL_TABLE may be expanded and SLOT
7971                  would no longer be a valid pointer.  */
7972               entry = XCNEW (struct decl_table_entry);
7973               entry->orig = targ;
7974               entry->copy = copy;
7975               *slot = entry;
7976
7977               /* If TARG is not a declaration DIE, we need to copy its
7978                  children.  */
7979               if (!is_declaration_die (targ))
7980                 {
7981                   FOR_EACH_CHILD (
7982                       targ, c,
7983                       add_child_die (copy,
7984                                      clone_tree_partial (c, decl_table)));
7985                 }
7986
7987               /* Make sure the cloned tree is marked as part of the
7988                  type unit.  */
7989               mark_dies (copy);
7990
7991               /* If TARG has surrounding context, copy its ancestor tree
7992                  into the new type unit.  */
7993               if (targ->die_parent != NULL
7994                   && !is_unit_die (targ->die_parent))
7995                 parent = copy_ancestor_tree (unit, targ->die_parent,
7996                                              decl_table);
7997
7998               add_child_die (parent, copy);
7999               a->dw_attr_val.v.val_die_ref.die = copy;
8000
8001               /* Make sure the newly-copied DIE is walked.  If it was
8002                  installed in a previously-added context, it won't
8003                  get visited otherwise.  */
8004               if (parent != unit)
8005                 {
8006                   /* Find the highest point of the newly-added tree,
8007                      mark each node along the way, and walk from there.  */
8008                   parent->die_mark = 1;
8009                   while (parent->die_parent
8010                          && parent->die_parent->die_mark == 0)
8011                     {
8012                       parent = parent->die_parent;
8013                       parent->die_mark = 1;
8014                     }
8015                   copy_decls_walk (unit, parent, decl_table);
8016                 }
8017             }
8018         }
8019     }
8020
8021   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8022 }
8023
8024 /* Copy declarations for "unworthy" types into the new comdat section.
8025    Incomplete types, modified types, and certain other types aren't broken
8026    out into comdat sections of their own, so they don't have a signature,
8027    and we need to copy the declaration into the same section so that we
8028    don't have an external reference.  */
8029
8030 static void
8031 copy_decls_for_unworthy_types (dw_die_ref unit)
8032 {
8033   mark_dies (unit);
8034   decl_hash_type decl_table (10);
8035   copy_decls_walk (unit, unit, &decl_table);
8036   unmark_dies (unit);
8037 }
8038
8039 /* Traverse the DIE and add a sibling attribute if it may have the
8040    effect of speeding up access to siblings.  To save some space,
8041    avoid generating sibling attributes for DIE's without children.  */
8042
8043 static void
8044 add_sibling_attributes (dw_die_ref die)
8045 {
8046   dw_die_ref c;
8047
8048   if (! die->die_child)
8049     return;
8050
8051   if (die->die_parent && die != die->die_parent->die_child)
8052     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8053
8054   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8055 }
8056
8057 /* Output all location lists for the DIE and its children.  */
8058
8059 static void
8060 output_location_lists (dw_die_ref die)
8061 {
8062   dw_die_ref c;
8063   dw_attr_node *a;
8064   unsigned ix;
8065
8066   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8067     if (AT_class (a) == dw_val_class_loc_list)
8068       output_loc_list (AT_loc_list (a));
8069
8070   FOR_EACH_CHILD (die, c, output_location_lists (c));
8071 }
8072
8073 /* We want to limit the number of external references, because they are
8074    larger than local references: a relocation takes multiple words, and
8075    even a sig8 reference is always eight bytes, whereas a local reference
8076    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8077    So if we encounter multiple external references to the same type DIE, we
8078    make a local typedef stub for it and redirect all references there.
8079
8080    This is the element of the hash table for keeping track of these
8081    references.  */
8082
8083 struct external_ref
8084 {
8085   dw_die_ref type;
8086   dw_die_ref stub;
8087   unsigned n_refs;
8088 };
8089
8090 /* Hashtable helpers.  */
8091
8092 struct external_ref_hasher : free_ptr_hash <external_ref>
8093 {
8094   static inline hashval_t hash (const external_ref *);
8095   static inline bool equal (const external_ref *, const external_ref *);
8096 };
8097
8098 inline hashval_t
8099 external_ref_hasher::hash (const external_ref *r)
8100 {
8101   dw_die_ref die = r->type;
8102   hashval_t h = 0;
8103
8104   /* We can't use the address of the DIE for hashing, because
8105      that will make the order of the stub DIEs non-deterministic.  */
8106   if (! die->comdat_type_p)
8107     /* We have a symbol; use it to compute a hash.  */
8108     h = htab_hash_string (die->die_id.die_symbol);
8109   else
8110     {
8111       /* We have a type signature; use a subset of the bits as the hash.
8112          The 8-byte signature is at least as large as hashval_t.  */
8113       comdat_type_node *type_node = die->die_id.die_type_node;
8114       memcpy (&h, type_node->signature, sizeof (h));
8115     }
8116   return h;
8117 }
8118
8119 inline bool
8120 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8121 {
8122   return r1->type == r2->type;
8123 }
8124
8125 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8126
8127 /* Return a pointer to the external_ref for references to DIE.  */
8128
8129 static struct external_ref *
8130 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8131 {
8132   struct external_ref ref, *ref_p;
8133   external_ref **slot;
8134
8135   ref.type = die;
8136   slot = map->find_slot (&ref, INSERT);
8137   if (*slot != HTAB_EMPTY_ENTRY)
8138     return *slot;
8139
8140   ref_p = XCNEW (struct external_ref);
8141   ref_p->type = die;
8142   *slot = ref_p;
8143   return ref_p;
8144 }
8145
8146 /* Subroutine of optimize_external_refs, below.
8147
8148    If we see a type skeleton, record it as our stub.  If we see external
8149    references, remember how many we've seen.  */
8150
8151 static void
8152 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8153 {
8154   dw_die_ref c;
8155   dw_attr_node *a;
8156   unsigned ix;
8157   struct external_ref *ref_p;
8158
8159   if (is_type_die (die)
8160       && (c = get_AT_ref (die, DW_AT_signature)))
8161     {
8162       /* This is a local skeleton; use it for local references.  */
8163       ref_p = lookup_external_ref (map, c);
8164       ref_p->stub = die;
8165     }
8166
8167   /* Scan the DIE references, and remember any that refer to DIEs from
8168      other CUs (i.e. those which are not marked).  */
8169   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8170     if (AT_class (a) == dw_val_class_die_ref
8171         && (c = AT_ref (a))->die_mark == 0
8172         && is_type_die (c))
8173       {
8174         ref_p = lookup_external_ref (map, c);
8175         ref_p->n_refs++;
8176       }
8177
8178   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8179 }
8180
8181 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
8182    points to an external_ref, DATA is the CU we're processing.  If we don't
8183    already have a local stub, and we have multiple refs, build a stub.  */
8184
8185 int
8186 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8187 {
8188   struct external_ref *ref_p = *slot;
8189
8190   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8191     {
8192       /* We have multiple references to this type, so build a small stub.
8193          Both of these forms are a bit dodgy from the perspective of the
8194          DWARF standard, since technically they should have names.  */
8195       dw_die_ref cu = data;
8196       dw_die_ref type = ref_p->type;
8197       dw_die_ref stub = NULL;
8198
8199       if (type->comdat_type_p)
8200         {
8201           /* If we refer to this type via sig8, use AT_signature.  */
8202           stub = new_die (type->die_tag, cu, NULL_TREE);
8203           add_AT_die_ref (stub, DW_AT_signature, type);
8204         }
8205       else
8206         {
8207           /* Otherwise, use a typedef with no name.  */
8208           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8209           add_AT_die_ref (stub, DW_AT_type, type);
8210         }
8211
8212       stub->die_mark++;
8213       ref_p->stub = stub;
8214     }
8215   return 1;
8216 }
8217
8218 /* DIE is a unit; look through all the DIE references to see if there are
8219    any external references to types, and if so, create local stubs for
8220    them which will be applied in build_abbrev_table.  This is useful because
8221    references to local DIEs are smaller.  */
8222
8223 static external_ref_hash_type *
8224 optimize_external_refs (dw_die_ref die)
8225 {
8226   external_ref_hash_type *map = new external_ref_hash_type (10);
8227   optimize_external_refs_1 (die, map);
8228   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8229   return map;
8230 }
8231
8232 /* The format of each DIE (and its attribute value pairs) is encoded in an
8233    abbreviation table.  This routine builds the abbreviation table and assigns
8234    a unique abbreviation id for each abbreviation entry.  The children of each
8235    die are visited recursively.  */
8236
8237 static void
8238 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8239 {
8240   unsigned long abbrev_id;
8241   unsigned int n_alloc;
8242   dw_die_ref c;
8243   dw_attr_node *a;
8244   unsigned ix;
8245
8246   /* Scan the DIE references, and replace any that refer to
8247      DIEs from other CUs (i.e. those which are not marked) with
8248      the local stubs we built in optimize_external_refs.  */
8249   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8250     if (AT_class (a) == dw_val_class_die_ref
8251         && (c = AT_ref (a))->die_mark == 0)
8252       {
8253         struct external_ref *ref_p;
8254         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8255
8256         ref_p = lookup_external_ref (extern_map, c);
8257         if (ref_p->stub && ref_p->stub != die)
8258           change_AT_die_ref (a, ref_p->stub);
8259         else
8260           /* We aren't changing this reference, so mark it external.  */
8261           set_AT_ref_external (a, 1);
8262       }
8263
8264   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8265     {
8266       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8267       dw_attr_node *die_a, *abbrev_a;
8268       unsigned ix;
8269       bool ok = true;
8270
8271       if (abbrev->die_tag != die->die_tag)
8272         continue;
8273       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8274         continue;
8275
8276       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8277         continue;
8278
8279       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8280         {
8281           abbrev_a = &(*abbrev->die_attr)[ix];
8282           if ((abbrev_a->dw_attr != die_a->dw_attr)
8283               || (value_format (abbrev_a) != value_format (die_a)))
8284             {
8285               ok = false;
8286               break;
8287             }
8288         }
8289       if (ok)
8290         break;
8291     }
8292
8293   if (abbrev_id >= abbrev_die_table_in_use)
8294     {
8295       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8296         {
8297           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8298           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8299                                             n_alloc);
8300
8301           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8302                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8303           abbrev_die_table_allocated = n_alloc;
8304         }
8305
8306       ++abbrev_die_table_in_use;
8307       abbrev_die_table[abbrev_id] = die;
8308     }
8309
8310   die->die_abbrev = abbrev_id;
8311   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8312 }
8313 \f
8314 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8315
8316 static int
8317 constant_size (unsigned HOST_WIDE_INT value)
8318 {
8319   int log;
8320
8321   if (value == 0)
8322     log = 0;
8323   else
8324     log = floor_log2 (value);
8325
8326   log = log / 8;
8327   log = 1 << (floor_log2 (log) + 1);
8328
8329   return log;
8330 }
8331
8332 /* Return the size of a DIE as it is represented in the
8333    .debug_info section.  */
8334
8335 static unsigned long
8336 size_of_die (dw_die_ref die)
8337 {
8338   unsigned long size = 0;
8339   dw_attr_node *a;
8340   unsigned ix;
8341   enum dwarf_form form;
8342
8343   size += size_of_uleb128 (die->die_abbrev);
8344   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8345     {
8346       switch (AT_class (a))
8347         {
8348         case dw_val_class_addr:
8349           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8350             {
8351               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8352               size += size_of_uleb128 (AT_index (a));
8353             }
8354           else
8355             size += DWARF2_ADDR_SIZE;
8356           break;
8357         case dw_val_class_offset:
8358           size += DWARF_OFFSET_SIZE;
8359           break;
8360         case dw_val_class_loc:
8361           {
8362             unsigned long lsize = size_of_locs (AT_loc (a));
8363
8364             /* Block length.  */
8365             if (dwarf_version >= 4)
8366               size += size_of_uleb128 (lsize);
8367             else
8368               size += constant_size (lsize);
8369             size += lsize;
8370           }
8371           break;
8372         case dw_val_class_loc_list:
8373           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8374             {
8375               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8376               size += size_of_uleb128 (AT_index (a));
8377             }
8378           else
8379             size += DWARF_OFFSET_SIZE;
8380           break;
8381         case dw_val_class_range_list:
8382           size += DWARF_OFFSET_SIZE;
8383           break;
8384         case dw_val_class_const:
8385           size += size_of_sleb128 (AT_int (a));
8386           break;
8387         case dw_val_class_unsigned_const:
8388           {
8389             int csize = constant_size (AT_unsigned (a));
8390             if (dwarf_version == 3
8391                 && a->dw_attr == DW_AT_data_member_location
8392                 && csize >= 4)
8393               size += size_of_uleb128 (AT_unsigned (a));
8394             else
8395               size += csize;
8396           }
8397           break;
8398         case dw_val_class_const_double:
8399           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8400           if (HOST_BITS_PER_WIDE_INT >= 64)
8401             size++; /* block */
8402           break;
8403         case dw_val_class_wide_int:
8404           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8405                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8406           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8407               > 64)
8408             size++; /* block */
8409           break;
8410         case dw_val_class_vec:
8411           size += constant_size (a->dw_attr_val.v.val_vec.length
8412                                  * a->dw_attr_val.v.val_vec.elt_size)
8413                   + a->dw_attr_val.v.val_vec.length
8414                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8415           break;
8416         case dw_val_class_flag:
8417           if (dwarf_version >= 4)
8418             /* Currently all add_AT_flag calls pass in 1 as last argument,
8419                so DW_FORM_flag_present can be used.  If that ever changes,
8420                we'll need to use DW_FORM_flag and have some optimization
8421                in build_abbrev_table that will change those to
8422                DW_FORM_flag_present if it is set to 1 in all DIEs using
8423                the same abbrev entry.  */
8424             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8425           else
8426             size += 1;
8427           break;
8428         case dw_val_class_die_ref:
8429           if (AT_ref_external (a))
8430             {
8431               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8432                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8433                  is sized by target address length, whereas in DWARF3
8434                  it's always sized as an offset.  */
8435               if (use_debug_types)
8436                 size += DWARF_TYPE_SIGNATURE_SIZE;
8437               else if (dwarf_version == 2)
8438                 size += DWARF2_ADDR_SIZE;
8439               else
8440                 size += DWARF_OFFSET_SIZE;
8441             }
8442           else
8443             size += DWARF_OFFSET_SIZE;
8444           break;
8445         case dw_val_class_fde_ref:
8446           size += DWARF_OFFSET_SIZE;
8447           break;
8448         case dw_val_class_lbl_id:
8449           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8450             {
8451               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8452               size += size_of_uleb128 (AT_index (a));
8453             }
8454           else
8455             size += DWARF2_ADDR_SIZE;
8456           break;
8457         case dw_val_class_lineptr:
8458         case dw_val_class_macptr:
8459           size += DWARF_OFFSET_SIZE;
8460           break;
8461         case dw_val_class_str:
8462           form = AT_string_form (a);
8463           if (form == DW_FORM_strp)
8464             size += DWARF_OFFSET_SIZE;
8465          else if (form == DW_FORM_GNU_str_index)
8466             size += size_of_uleb128 (AT_index (a));
8467           else
8468             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8469           break;
8470         case dw_val_class_file:
8471           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8472           break;
8473         case dw_val_class_data8:
8474           size += 8;
8475           break;
8476         case dw_val_class_vms_delta:
8477           size += DWARF_OFFSET_SIZE;
8478           break;
8479         case dw_val_class_high_pc:
8480           size += DWARF2_ADDR_SIZE;
8481           break;
8482         case dw_val_class_discr_value:
8483           size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8484           break;
8485         case dw_val_class_discr_list:
8486             {
8487               unsigned block_size = size_of_discr_list (AT_discr_list (a));
8488
8489               /* This is a block, so we have the block length and then its
8490                  data.  */
8491               size += constant_size (block_size) + block_size;
8492             }
8493           break;
8494         default:
8495           gcc_unreachable ();
8496         }
8497     }
8498
8499   return size;
8500 }
8501
8502 /* Size the debugging information associated with a given DIE.  Visits the
8503    DIE's children recursively.  Updates the global variable next_die_offset, on
8504    each time through.  Uses the current value of next_die_offset to update the
8505    die_offset field in each DIE.  */
8506
8507 static void
8508 calc_die_sizes (dw_die_ref die)
8509 {
8510   dw_die_ref c;
8511
8512   gcc_assert (die->die_offset == 0
8513               || (unsigned long int) die->die_offset == next_die_offset);
8514   die->die_offset = next_die_offset;
8515   next_die_offset += size_of_die (die);
8516
8517   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8518
8519   if (die->die_child != NULL)
8520     /* Count the null byte used to terminate sibling lists.  */
8521     next_die_offset += 1;
8522 }
8523
8524 /* Size just the base type children at the start of the CU.
8525    This is needed because build_abbrev needs to size locs
8526    and sizing of type based stack ops needs to know die_offset
8527    values for the base types.  */
8528
8529 static void
8530 calc_base_type_die_sizes (void)
8531 {
8532   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8533   unsigned int i;
8534   dw_die_ref base_type;
8535 #if ENABLE_ASSERT_CHECKING
8536   dw_die_ref prev = comp_unit_die ()->die_child;
8537 #endif
8538
8539   die_offset += size_of_die (comp_unit_die ());
8540   for (i = 0; base_types.iterate (i, &base_type); i++)
8541     {
8542 #if ENABLE_ASSERT_CHECKING
8543       gcc_assert (base_type->die_offset == 0
8544                   && prev->die_sib == base_type
8545                   && base_type->die_child == NULL
8546                   && base_type->die_abbrev);
8547       prev = base_type;
8548 #endif
8549       base_type->die_offset = die_offset;
8550       die_offset += size_of_die (base_type);
8551     }
8552 }
8553
8554 /* Set the marks for a die and its children.  We do this so
8555    that we know whether or not a reference needs to use FORM_ref_addr; only
8556    DIEs in the same CU will be marked.  We used to clear out the offset
8557    and use that as the flag, but ran into ordering problems.  */
8558
8559 static void
8560 mark_dies (dw_die_ref die)
8561 {
8562   dw_die_ref c;
8563
8564   gcc_assert (!die->die_mark);
8565
8566   die->die_mark = 1;
8567   FOR_EACH_CHILD (die, c, mark_dies (c));
8568 }
8569
8570 /* Clear the marks for a die and its children.  */
8571
8572 static void
8573 unmark_dies (dw_die_ref die)
8574 {
8575   dw_die_ref c;
8576
8577   if (! use_debug_types)
8578     gcc_assert (die->die_mark);
8579
8580   die->die_mark = 0;
8581   FOR_EACH_CHILD (die, c, unmark_dies (c));
8582 }
8583
8584 /* Clear the marks for a die, its children and referred dies.  */
8585
8586 static void
8587 unmark_all_dies (dw_die_ref die)
8588 {
8589   dw_die_ref c;
8590   dw_attr_node *a;
8591   unsigned ix;
8592
8593   if (!die->die_mark)
8594     return;
8595   die->die_mark = 0;
8596
8597   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8598
8599   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8600     if (AT_class (a) == dw_val_class_die_ref)
8601       unmark_all_dies (AT_ref (a));
8602 }
8603
8604 /* Calculate if the entry should appear in the final output file.  It may be
8605    from a pruned a type.  */
8606
8607 static bool
8608 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8609 {
8610   /* By limiting gnu pubnames to definitions only, gold can generate a
8611      gdb index without entries for declarations, which don't include
8612      enough information to be useful.  */
8613   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8614     return false;
8615
8616   if (table == pubname_table)
8617     {
8618       /* Enumerator names are part of the pubname table, but the
8619          parent DW_TAG_enumeration_type die may have been pruned.
8620          Don't output them if that is the case.  */
8621       if (p->die->die_tag == DW_TAG_enumerator &&
8622           (p->die->die_parent == NULL
8623            || !p->die->die_parent->die_perennial_p))
8624         return false;
8625
8626       /* Everything else in the pubname table is included.  */
8627       return true;
8628     }
8629
8630   /* The pubtypes table shouldn't include types that have been
8631      pruned.  */
8632   return (p->die->die_offset != 0
8633           || !flag_eliminate_unused_debug_types);
8634 }
8635
8636 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8637    generated for the compilation unit.  */
8638
8639 static unsigned long
8640 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8641 {
8642   unsigned long size;
8643   unsigned i;
8644   pubname_entry *p;
8645   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8646
8647   size = DWARF_PUBNAMES_HEADER_SIZE;
8648   FOR_EACH_VEC_ELT (*names, i, p)
8649     if (include_pubname_in_output (names, p))
8650       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8651
8652   size += DWARF_OFFSET_SIZE;
8653   return size;
8654 }
8655
8656 /* Return the size of the information in the .debug_aranges section.  */
8657
8658 static unsigned long
8659 size_of_aranges (void)
8660 {
8661   unsigned long size;
8662
8663   size = DWARF_ARANGES_HEADER_SIZE;
8664
8665   /* Count the address/length pair for this compilation unit.  */
8666   if (text_section_used)
8667     size += 2 * DWARF2_ADDR_SIZE;
8668   if (cold_text_section_used)
8669     size += 2 * DWARF2_ADDR_SIZE;
8670   if (have_multiple_function_sections)
8671     {
8672       unsigned fde_idx;
8673       dw_fde_ref fde;
8674
8675       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8676         {
8677           if (DECL_IGNORED_P (fde->decl))
8678             continue;
8679           if (!fde->in_std_section)
8680             size += 2 * DWARF2_ADDR_SIZE;
8681           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8682             size += 2 * DWARF2_ADDR_SIZE;
8683         }
8684     }
8685
8686   /* Count the two zero words used to terminated the address range table.  */
8687   size += 2 * DWARF2_ADDR_SIZE;
8688   return size;
8689 }
8690 \f
8691 /* Select the encoding of an attribute value.  */
8692
8693 static enum dwarf_form
8694 value_format (dw_attr_node *a)
8695 {
8696   switch (AT_class (a))
8697     {
8698     case dw_val_class_addr:
8699       /* Only very few attributes allow DW_FORM_addr.  */
8700       switch (a->dw_attr)
8701         {
8702         case DW_AT_low_pc:
8703         case DW_AT_high_pc:
8704         case DW_AT_entry_pc:
8705         case DW_AT_trampoline:
8706           return (AT_index (a) == NOT_INDEXED
8707                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8708         default:
8709           break;
8710         }
8711       switch (DWARF2_ADDR_SIZE)
8712         {
8713         case 1:
8714           return DW_FORM_data1;
8715         case 2:
8716           return DW_FORM_data2;
8717         case 4:
8718           return DW_FORM_data4;
8719         case 8:
8720           return DW_FORM_data8;
8721         default:
8722           gcc_unreachable ();
8723         }
8724     case dw_val_class_range_list:
8725     case dw_val_class_loc_list:
8726       if (dwarf_version >= 4)
8727         return DW_FORM_sec_offset;
8728       /* FALLTHRU */
8729     case dw_val_class_vms_delta:
8730     case dw_val_class_offset:
8731       switch (DWARF_OFFSET_SIZE)
8732         {
8733         case 4:
8734           return DW_FORM_data4;
8735         case 8:
8736           return DW_FORM_data8;
8737         default:
8738           gcc_unreachable ();
8739         }
8740     case dw_val_class_loc:
8741       if (dwarf_version >= 4)
8742         return DW_FORM_exprloc;
8743       switch (constant_size (size_of_locs (AT_loc (a))))
8744         {
8745         case 1:
8746           return DW_FORM_block1;
8747         case 2:
8748           return DW_FORM_block2;
8749         case 4:
8750           return DW_FORM_block4;
8751         default:
8752           gcc_unreachable ();
8753         }
8754     case dw_val_class_const:
8755       return DW_FORM_sdata;
8756     case dw_val_class_unsigned_const:
8757       switch (constant_size (AT_unsigned (a)))
8758         {
8759         case 1:
8760           return DW_FORM_data1;
8761         case 2:
8762           return DW_FORM_data2;
8763         case 4:
8764           /* In DWARF3 DW_AT_data_member_location with
8765              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8766              constant, so we need to use DW_FORM_udata if we need
8767              a large constant.  */
8768           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8769             return DW_FORM_udata;
8770           return DW_FORM_data4;
8771         case 8:
8772           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8773             return DW_FORM_udata;
8774           return DW_FORM_data8;
8775         default:
8776           gcc_unreachable ();
8777         }
8778     case dw_val_class_const_double:
8779       switch (HOST_BITS_PER_WIDE_INT)
8780         {
8781         case 8:
8782           return DW_FORM_data2;
8783         case 16:
8784           return DW_FORM_data4;
8785         case 32:
8786           return DW_FORM_data8;
8787         case 64:
8788         default:
8789           return DW_FORM_block1;
8790         }
8791     case dw_val_class_wide_int:
8792       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8793         {
8794         case 8:
8795           return DW_FORM_data1;
8796         case 16:
8797           return DW_FORM_data2;
8798         case 32:
8799           return DW_FORM_data4;
8800         case 64:
8801           return DW_FORM_data8;
8802         default:
8803           return DW_FORM_block1;
8804         }
8805     case dw_val_class_vec:
8806       switch (constant_size (a->dw_attr_val.v.val_vec.length
8807                              * a->dw_attr_val.v.val_vec.elt_size))
8808         {
8809         case 1:
8810           return DW_FORM_block1;
8811         case 2:
8812           return DW_FORM_block2;
8813         case 4:
8814           return DW_FORM_block4;
8815         default:
8816           gcc_unreachable ();
8817         }
8818     case dw_val_class_flag:
8819       if (dwarf_version >= 4)
8820         {
8821           /* Currently all add_AT_flag calls pass in 1 as last argument,
8822              so DW_FORM_flag_present can be used.  If that ever changes,
8823              we'll need to use DW_FORM_flag and have some optimization
8824              in build_abbrev_table that will change those to
8825              DW_FORM_flag_present if it is set to 1 in all DIEs using
8826              the same abbrev entry.  */
8827           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8828           return DW_FORM_flag_present;
8829         }
8830       return DW_FORM_flag;
8831     case dw_val_class_die_ref:
8832       if (AT_ref_external (a))
8833         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8834       else
8835         return DW_FORM_ref;
8836     case dw_val_class_fde_ref:
8837       return DW_FORM_data;
8838     case dw_val_class_lbl_id:
8839       return (AT_index (a) == NOT_INDEXED
8840               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8841     case dw_val_class_lineptr:
8842     case dw_val_class_macptr:
8843       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8844     case dw_val_class_str:
8845       return AT_string_form (a);
8846     case dw_val_class_file:
8847       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8848         {
8849         case 1:
8850           return DW_FORM_data1;
8851         case 2:
8852           return DW_FORM_data2;
8853         case 4:
8854           return DW_FORM_data4;
8855         default:
8856           gcc_unreachable ();
8857         }
8858
8859     case dw_val_class_data8:
8860       return DW_FORM_data8;
8861
8862     case dw_val_class_high_pc:
8863       switch (DWARF2_ADDR_SIZE)
8864         {
8865         case 1:
8866           return DW_FORM_data1;
8867         case 2:
8868           return DW_FORM_data2;
8869         case 4:
8870           return DW_FORM_data4;
8871         case 8:
8872           return DW_FORM_data8;
8873         default:
8874           gcc_unreachable ();
8875         }
8876
8877     case dw_val_class_discr_value:
8878       return (a->dw_attr_val.v.val_discr_value.pos
8879               ? DW_FORM_udata
8880               : DW_FORM_sdata);
8881     case dw_val_class_discr_list:
8882       switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8883         {
8884         case 1:
8885           return DW_FORM_block1;
8886         case 2:
8887           return DW_FORM_block2;
8888         case 4:
8889           return DW_FORM_block4;
8890         default:
8891           gcc_unreachable ();
8892         }
8893
8894     default:
8895       gcc_unreachable ();
8896     }
8897 }
8898
8899 /* Output the encoding of an attribute value.  */
8900
8901 static void
8902 output_value_format (dw_attr_node *a)
8903 {
8904   enum dwarf_form form = value_format (a);
8905
8906   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8907 }
8908
8909 /* Given a die and id, produce the appropriate abbreviations.  */
8910
8911 static void
8912 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8913 {
8914   unsigned ix;
8915   dw_attr_node *a_attr;
8916
8917   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8918   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8919                                dwarf_tag_name (abbrev->die_tag));
8920
8921   if (abbrev->die_child != NULL)
8922     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8923   else
8924     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8925
8926   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8927     {
8928       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8929                                    dwarf_attr_name (a_attr->dw_attr));
8930       output_value_format (a_attr);
8931     }
8932
8933   dw2_asm_output_data (1, 0, NULL);
8934   dw2_asm_output_data (1, 0, NULL);
8935 }
8936
8937
8938 /* Output the .debug_abbrev section which defines the DIE abbreviation
8939    table.  */
8940
8941 static void
8942 output_abbrev_section (void)
8943 {
8944   unsigned long abbrev_id;
8945
8946   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8947     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8948
8949   /* Terminate the table.  */
8950   dw2_asm_output_data (1, 0, NULL);
8951 }
8952
8953 /* Output a symbol we can use to refer to this DIE from another CU.  */
8954
8955 static inline void
8956 output_die_symbol (dw_die_ref die)
8957 {
8958   const char *sym = die->die_id.die_symbol;
8959
8960   gcc_assert (!die->comdat_type_p);
8961
8962   if (sym == 0)
8963     return;
8964
8965   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8966     /* We make these global, not weak; if the target doesn't support
8967        .linkonce, it doesn't support combining the sections, so debugging
8968        will break.  */
8969     targetm.asm_out.globalize_label (asm_out_file, sym);
8970
8971   ASM_OUTPUT_LABEL (asm_out_file, sym);
8972 }
8973
8974 /* Return a new location list, given the begin and end range, and the
8975    expression.  */
8976
8977 static inline dw_loc_list_ref
8978 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8979               const char *section)
8980 {
8981   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8982
8983   retlist->begin = begin;
8984   retlist->begin_entry = NULL;
8985   retlist->end = end;
8986   retlist->expr = expr;
8987   retlist->section = section;
8988
8989   return retlist;
8990 }
8991
8992 /* Generate a new internal symbol for this location list node, if it
8993    hasn't got one yet.  */
8994
8995 static inline void
8996 gen_llsym (dw_loc_list_ref list)
8997 {
8998   gcc_assert (!list->ll_symbol);
8999   list->ll_symbol = gen_internal_sym ("LLST");
9000 }
9001
9002 /* Output the location list given to us.  */
9003
9004 static void
9005 output_loc_list (dw_loc_list_ref list_head)
9006 {
9007   dw_loc_list_ref curr = list_head;
9008
9009   if (list_head->emitted)
9010     return;
9011   list_head->emitted = true;
9012
9013   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9014
9015   /* Walk the location list, and output each range + expression.  */
9016   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9017     {
9018       unsigned long size;
9019       /* Don't output an entry that starts and ends at the same address.  */
9020       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9021         continue;
9022       size = size_of_locs (curr->expr);
9023       /* If the expression is too large, drop it on the floor.  We could
9024          perhaps put it into DW_TAG_dwarf_procedure and refer to that
9025          in the expression, but >= 64KB expressions for a single value
9026          in a single range are unlikely very useful.  */
9027       if (size > 0xffff)
9028         continue;
9029       if (dwarf_split_debug_info)
9030         {
9031           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9032                                "Location list start/length entry (%s)",
9033                                list_head->ll_symbol);
9034           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9035                                        "Location list range start index (%s)",
9036                                        curr->begin);
9037           /* The length field is 4 bytes.  If we ever need to support
9038             an 8-byte length, we can add a new DW_LLE code or fall back
9039             to DW_LLE_GNU_start_end_entry.  */
9040           dw2_asm_output_delta (4, curr->end, curr->begin,
9041                                 "Location list range length (%s)",
9042                                 list_head->ll_symbol);
9043         }
9044       else if (!have_multiple_function_sections)
9045         {
9046           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9047                                 "Location list begin address (%s)",
9048                                 list_head->ll_symbol);
9049           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9050                                 "Location list end address (%s)",
9051                                 list_head->ll_symbol);
9052         }
9053       else
9054         {
9055           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9056                                "Location list begin address (%s)",
9057                                list_head->ll_symbol);
9058           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9059                                "Location list end address (%s)",
9060                                list_head->ll_symbol);
9061         }
9062
9063       /* Output the block length for this list of location operations.  */
9064       gcc_assert (size <= 0xffff);
9065       dw2_asm_output_data (2, size, "%s", "Location expression size");
9066
9067       output_loc_sequence (curr->expr, -1);
9068     }
9069
9070   if (dwarf_split_debug_info)
9071     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9072                          "Location list terminator (%s)",
9073                          list_head->ll_symbol);
9074   else
9075     {
9076       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9077                            "Location list terminator begin (%s)",
9078                            list_head->ll_symbol);
9079       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9080                            "Location list terminator end (%s)",
9081                            list_head->ll_symbol);
9082     }
9083 }
9084
9085 /* Output a range_list offset into the debug_range section.  Emit a
9086    relocated reference if val_entry is NULL, otherwise, emit an
9087    indirect reference.  */
9088
9089 static void
9090 output_range_list_offset (dw_attr_node *a)
9091 {
9092   const char *name = dwarf_attr_name (a->dw_attr);
9093
9094   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9095     {
9096       char *p = strchr (ranges_section_label, '\0');
9097       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9098       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9099                              debug_ranges_section, "%s", name);
9100       *p = '\0';
9101     }
9102   else
9103     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9104                          "%s (offset from %s)", name, ranges_section_label);
9105 }
9106
9107 /* Output the offset into the debug_loc section.  */
9108
9109 static void
9110 output_loc_list_offset (dw_attr_node *a)
9111 {
9112   char *sym = AT_loc_list (a)->ll_symbol;
9113
9114   gcc_assert (sym);
9115   if (dwarf_split_debug_info)
9116     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9117                           "%s", dwarf_attr_name (a->dw_attr));
9118   else
9119     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9120                            "%s", dwarf_attr_name (a->dw_attr));
9121 }
9122
9123 /* Output an attribute's index or value appropriately.  */
9124
9125 static void
9126 output_attr_index_or_value (dw_attr_node *a)
9127 {
9128   const char *name = dwarf_attr_name (a->dw_attr);
9129
9130   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9131     {
9132       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9133       return;
9134     }
9135   switch (AT_class (a))
9136     {
9137       case dw_val_class_addr:
9138         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9139         break;
9140       case dw_val_class_high_pc:
9141       case dw_val_class_lbl_id:
9142         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9143         break;
9144       case dw_val_class_loc_list:
9145         output_loc_list_offset (a);
9146         break;
9147       default:
9148         gcc_unreachable ();
9149     }
9150 }
9151
9152 /* Output a type signature.  */
9153
9154 static inline void
9155 output_signature (const char *sig, const char *name)
9156 {
9157   int i;
9158
9159   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9160     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9161 }
9162
9163 /* Output a discriminant value.  */
9164
9165 static inline void
9166 output_discr_value (dw_discr_value *discr_value, const char *name)
9167 {
9168   if (discr_value->pos)
9169     dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9170   else
9171     dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9172 }
9173
9174 /* Output the DIE and its attributes.  Called recursively to generate
9175    the definitions of each child DIE.  */
9176
9177 static void
9178 output_die (dw_die_ref die)
9179 {
9180   dw_attr_node *a;
9181   dw_die_ref c;
9182   unsigned long size;
9183   unsigned ix;
9184
9185   /* If someone in another CU might refer to us, set up a symbol for
9186      them to point to.  */
9187   if (! die->comdat_type_p && die->die_id.die_symbol)
9188     output_die_symbol (die);
9189
9190   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9191                                (unsigned long)die->die_offset,
9192                                dwarf_tag_name (die->die_tag));
9193
9194   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9195     {
9196       const char *name = dwarf_attr_name (a->dw_attr);
9197
9198       switch (AT_class (a))
9199         {
9200         case dw_val_class_addr:
9201           output_attr_index_or_value (a);
9202           break;
9203
9204         case dw_val_class_offset:
9205           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9206                                "%s", name);
9207           break;
9208
9209         case dw_val_class_range_list:
9210           output_range_list_offset (a);
9211           break;
9212
9213         case dw_val_class_loc:
9214           size = size_of_locs (AT_loc (a));
9215
9216           /* Output the block length for this list of location operations.  */
9217           if (dwarf_version >= 4)
9218             dw2_asm_output_data_uleb128 (size, "%s", name);
9219           else
9220             dw2_asm_output_data (constant_size (size), size, "%s", name);
9221
9222           output_loc_sequence (AT_loc (a), -1);
9223           break;
9224
9225         case dw_val_class_const:
9226           /* ??? It would be slightly more efficient to use a scheme like is
9227              used for unsigned constants below, but gdb 4.x does not sign
9228              extend.  Gdb 5.x does sign extend.  */
9229           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9230           break;
9231
9232         case dw_val_class_unsigned_const:
9233           {
9234             int csize = constant_size (AT_unsigned (a));
9235             if (dwarf_version == 3
9236                 && a->dw_attr == DW_AT_data_member_location
9237                 && csize >= 4)
9238               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9239             else
9240               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9241           }
9242           break;
9243
9244         case dw_val_class_const_double:
9245           {
9246             unsigned HOST_WIDE_INT first, second;
9247
9248             if (HOST_BITS_PER_WIDE_INT >= 64)
9249               dw2_asm_output_data (1,
9250                                    HOST_BITS_PER_DOUBLE_INT
9251                                    / HOST_BITS_PER_CHAR,
9252                                    NULL);
9253
9254             if (WORDS_BIG_ENDIAN)
9255               {
9256                 first = a->dw_attr_val.v.val_double.high;
9257                 second = a->dw_attr_val.v.val_double.low;
9258               }
9259             else
9260               {
9261                 first = a->dw_attr_val.v.val_double.low;
9262                 second = a->dw_attr_val.v.val_double.high;
9263               }
9264
9265             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9266                                  first, "%s", name);
9267             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9268                                  second, NULL);
9269           }
9270           break;
9271
9272         case dw_val_class_wide_int:
9273           {
9274             int i;
9275             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9276             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9277             if (len * HOST_BITS_PER_WIDE_INT > 64)
9278               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9279                                    NULL);
9280
9281             if (WORDS_BIG_ENDIAN)
9282               for (i = len - 1; i >= 0; --i)
9283                 {
9284                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9285                                        "%s", name);
9286                   name = "";
9287                 }
9288             else
9289               for (i = 0; i < len; ++i)
9290                 {
9291                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9292                                        "%s", name);
9293                   name = "";
9294                 }
9295           }
9296           break;
9297
9298         case dw_val_class_vec:
9299           {
9300             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9301             unsigned int len = a->dw_attr_val.v.val_vec.length;
9302             unsigned int i;
9303             unsigned char *p;
9304
9305             dw2_asm_output_data (constant_size (len * elt_size),
9306                                  len * elt_size, "%s", name);
9307             if (elt_size > sizeof (HOST_WIDE_INT))
9308               {
9309                 elt_size /= 2;
9310                 len *= 2;
9311               }
9312             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9313                  i < len;
9314                  i++, p += elt_size)
9315               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9316                                    "fp or vector constant word %u", i);
9317             break;
9318           }
9319
9320         case dw_val_class_flag:
9321           if (dwarf_version >= 4)
9322             {
9323               /* Currently all add_AT_flag calls pass in 1 as last argument,
9324                  so DW_FORM_flag_present can be used.  If that ever changes,
9325                  we'll need to use DW_FORM_flag and have some optimization
9326                  in build_abbrev_table that will change those to
9327                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9328                  the same abbrev entry.  */
9329               gcc_assert (AT_flag (a) == 1);
9330               if (flag_debug_asm)
9331                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9332                          ASM_COMMENT_START, name);
9333               break;
9334             }
9335           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9336           break;
9337
9338         case dw_val_class_loc_list:
9339           output_attr_index_or_value (a);
9340           break;
9341
9342         case dw_val_class_die_ref:
9343           if (AT_ref_external (a))
9344             {
9345               if (AT_ref (a)->comdat_type_p)
9346                 {
9347                   comdat_type_node *type_node =
9348                     AT_ref (a)->die_id.die_type_node;
9349
9350                   gcc_assert (type_node);
9351                   output_signature (type_node->signature, name);
9352                 }
9353               else
9354                 {
9355                   const char *sym = AT_ref (a)->die_id.die_symbol;
9356                   int size;
9357
9358                   gcc_assert (sym);
9359                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9360                      length, whereas in DWARF3 it's always sized as an
9361                      offset.  */
9362                   if (dwarf_version == 2)
9363                     size = DWARF2_ADDR_SIZE;
9364                   else
9365                     size = DWARF_OFFSET_SIZE;
9366                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9367                                          name);
9368                 }
9369             }
9370           else
9371             {
9372               gcc_assert (AT_ref (a)->die_offset);
9373               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9374                                    "%s", name);
9375             }
9376           break;
9377
9378         case dw_val_class_fde_ref:
9379           {
9380             char l1[20];
9381
9382             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9383                                          a->dw_attr_val.v.val_fde_index * 2);
9384             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9385                                    "%s", name);
9386           }
9387           break;
9388
9389         case dw_val_class_vms_delta:
9390 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9391           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9392                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9393                                     "%s", name);
9394 #else
9395           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9396                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9397                                 "%s", name);
9398 #endif
9399           break;
9400
9401         case dw_val_class_lbl_id:
9402           output_attr_index_or_value (a);
9403           break;
9404
9405         case dw_val_class_lineptr:
9406           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9407                                  debug_line_section, "%s", name);
9408           break;
9409
9410         case dw_val_class_macptr:
9411           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9412                                  debug_macinfo_section, "%s", name);
9413           break;
9414
9415         case dw_val_class_str:
9416           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9417             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9418                                    a->dw_attr_val.v.val_str->label,
9419                                    debug_str_section,
9420                                    "%s: \"%s\"", name, AT_string (a));
9421           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9422             dw2_asm_output_data_uleb128 (AT_index (a),
9423                                          "%s: \"%s\"", name, AT_string (a));
9424           else
9425             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9426           break;
9427
9428         case dw_val_class_file:
9429           {
9430             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9431
9432             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9433                                  a->dw_attr_val.v.val_file->filename);
9434             break;
9435           }
9436
9437         case dw_val_class_data8:
9438           {
9439             int i;
9440
9441             for (i = 0; i < 8; i++)
9442               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9443                                    i == 0 ? "%s" : NULL, name);
9444             break;
9445           }
9446
9447         case dw_val_class_high_pc:
9448           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9449                                 get_AT_low_pc (die), "DW_AT_high_pc");
9450           break;
9451
9452         case dw_val_class_discr_value:
9453           output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9454           break;
9455
9456         case dw_val_class_discr_list:
9457           {
9458             dw_discr_list_ref list = AT_discr_list (a);
9459             const int size = size_of_discr_list (list);
9460
9461             /* This is a block, so output its length first.  */
9462             dw2_asm_output_data (constant_size (size), size,
9463                                  "%s: block size", name);
9464
9465             for (; list != NULL; list = list->dw_discr_next)
9466               {
9467                 /* One byte for the discriminant value descriptor, and then as
9468                    many LEB128 numbers as required.  */
9469                 if (list->dw_discr_range)
9470                   dw2_asm_output_data (1, DW_DSC_range,
9471                                        "%s: DW_DSC_range", name);
9472                 else
9473                   dw2_asm_output_data (1, DW_DSC_label,
9474                                        "%s: DW_DSC_label", name);
9475
9476                 output_discr_value (&list->dw_discr_lower_bound, name);
9477                 if (list->dw_discr_range)
9478                   output_discr_value (&list->dw_discr_upper_bound, name);
9479               }
9480             break;
9481           }
9482
9483         default:
9484           gcc_unreachable ();
9485         }
9486     }
9487
9488   FOR_EACH_CHILD (die, c, output_die (c));
9489
9490   /* Add null byte to terminate sibling list.  */
9491   if (die->die_child != NULL)
9492     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9493                          (unsigned long) die->die_offset);
9494 }
9495
9496 /* Output the compilation unit that appears at the beginning of the
9497    .debug_info section, and precedes the DIE descriptions.  */
9498
9499 static void
9500 output_compilation_unit_header (void)
9501 {
9502   /* We don't support actual DWARFv5 units yet, we just use some
9503      DWARFv5 draft DIE tags in DWARFv4 format.  */
9504   int ver = dwarf_version < 5 ? dwarf_version : 4;
9505
9506   if (!XCOFF_DEBUGGING_INFO)
9507     {
9508       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9509         dw2_asm_output_data (4, 0xffffffff,
9510           "Initial length escape value indicating 64-bit DWARF extension");
9511       dw2_asm_output_data (DWARF_OFFSET_SIZE,
9512                            next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9513                            "Length of Compilation Unit Info");
9514     }
9515
9516   dw2_asm_output_data (2, ver, "DWARF version number");
9517   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9518                          debug_abbrev_section,
9519                          "Offset Into Abbrev. Section");
9520   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9521 }
9522
9523 /* Output the compilation unit DIE and its children.  */
9524
9525 static void
9526 output_comp_unit (dw_die_ref die, int output_if_empty)
9527 {
9528   const char *secname, *oldsym;
9529   char *tmp;
9530
9531   /* Unless we are outputting main CU, we may throw away empty ones.  */
9532   if (!output_if_empty && die->die_child == NULL)
9533     return;
9534
9535   /* Even if there are no children of this DIE, we must output the information
9536      about the compilation unit.  Otherwise, on an empty translation unit, we
9537      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9538      will then complain when examining the file.  First mark all the DIEs in
9539      this CU so we know which get local refs.  */
9540   mark_dies (die);
9541
9542   external_ref_hash_type *extern_map = optimize_external_refs (die);
9543
9544   build_abbrev_table (die, extern_map);
9545
9546   delete extern_map;
9547
9548   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9549   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9550   calc_die_sizes (die);
9551
9552   oldsym = die->die_id.die_symbol;
9553   if (oldsym)
9554     {
9555       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9556
9557       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9558       secname = tmp;
9559       die->die_id.die_symbol = NULL;
9560       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9561     }
9562   else
9563     {
9564       switch_to_section (debug_info_section);
9565       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9566       info_section_emitted = true;
9567     }
9568
9569   /* Output debugging information.  */
9570   output_compilation_unit_header ();
9571   output_die (die);
9572
9573   /* Leave the marks on the main CU, so we can check them in
9574      output_pubnames.  */
9575   if (oldsym)
9576     {
9577       unmark_dies (die);
9578       die->die_id.die_symbol = oldsym;
9579     }
9580 }
9581
9582 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9583    and .debug_pubtypes.  This is configured per-target, but can be
9584    overridden by the -gpubnames or -gno-pubnames options.  */
9585
9586 static inline bool
9587 want_pubnames (void)
9588 {
9589   if (debug_info_level <= DINFO_LEVEL_TERSE)
9590     return false;
9591   if (debug_generate_pub_sections != -1)
9592     return debug_generate_pub_sections;
9593   return targetm.want_debug_pub_sections;
9594 }
9595
9596 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9597
9598 static void
9599 add_AT_pubnames (dw_die_ref die)
9600 {
9601   if (want_pubnames ())
9602     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9603 }
9604
9605 /* Add a string attribute value to a skeleton DIE.  */
9606
9607 static inline void
9608 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9609                         const char *str)
9610 {
9611   dw_attr_node attr;
9612   struct indirect_string_node *node;
9613
9614   if (! skeleton_debug_str_hash)
9615     skeleton_debug_str_hash
9616       = hash_table<indirect_string_hasher>::create_ggc (10);
9617
9618   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9619   find_string_form (node);
9620   if (node->form == DW_FORM_GNU_str_index)
9621     node->form = DW_FORM_strp;
9622
9623   attr.dw_attr = attr_kind;
9624   attr.dw_attr_val.val_class = dw_val_class_str;
9625   attr.dw_attr_val.val_entry = NULL;
9626   attr.dw_attr_val.v.val_str = node;
9627   add_dwarf_attr (die, &attr);
9628 }
9629
9630 /* Helper function to generate top-level dies for skeleton debug_info and
9631    debug_types.  */
9632
9633 static void
9634 add_top_level_skeleton_die_attrs (dw_die_ref die)
9635 {
9636   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9637   const char *comp_dir = comp_dir_string ();
9638
9639   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9640   if (comp_dir != NULL)
9641     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9642   add_AT_pubnames (die);
9643   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9644 }
9645
9646 /* Output skeleton debug sections that point to the dwo file.  */
9647
9648 static void
9649 output_skeleton_debug_sections (dw_die_ref comp_unit)
9650 {
9651   /* We don't support actual DWARFv5 units yet, we just use some
9652      DWARFv5 draft DIE tags in DWARFv4 format.  */
9653   int ver = dwarf_version < 5 ? dwarf_version : 4;
9654
9655   /* These attributes will be found in the full debug_info section.  */
9656   remove_AT (comp_unit, DW_AT_producer);
9657   remove_AT (comp_unit, DW_AT_language);
9658
9659   switch_to_section (debug_skeleton_info_section);
9660   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9661
9662   /* Produce the skeleton compilation-unit header.  This one differs enough from
9663      a normal CU header that it's better not to call output_compilation_unit
9664      header.  */
9665   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9666     dw2_asm_output_data (4, 0xffffffff,
9667       "Initial length escape value indicating 64-bit DWARF extension");
9668
9669   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9670                        DWARF_COMPILE_UNIT_HEADER_SIZE
9671                        - DWARF_INITIAL_LENGTH_SIZE
9672                        + size_of_die (comp_unit),
9673                       "Length of Compilation Unit Info");
9674   dw2_asm_output_data (2, ver, "DWARF version number");
9675   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9676                          debug_abbrev_section,
9677                          "Offset Into Abbrev. Section");
9678   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9679
9680   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9681   output_die (comp_unit);
9682
9683   /* Build the skeleton debug_abbrev section.  */
9684   switch_to_section (debug_skeleton_abbrev_section);
9685   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9686
9687   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9688
9689   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9690 }
9691
9692 /* Output a comdat type unit DIE and its children.  */
9693
9694 static void
9695 output_comdat_type_unit (comdat_type_node *node)
9696 {
9697   const char *secname;
9698   char *tmp;
9699   int i;
9700 #if defined (OBJECT_FORMAT_ELF)
9701   tree comdat_key;
9702 #endif
9703
9704   /* First mark all the DIEs in this CU so we know which get local refs.  */
9705   mark_dies (node->root_die);
9706
9707   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9708
9709   build_abbrev_table (node->root_die, extern_map);
9710
9711   delete extern_map;
9712   extern_map = NULL;
9713
9714   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9715   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9716   calc_die_sizes (node->root_die);
9717
9718 #if defined (OBJECT_FORMAT_ELF)
9719   if (!dwarf_split_debug_info)
9720     secname = ".debug_types";
9721   else
9722     secname = ".debug_types.dwo";
9723
9724   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9725   sprintf (tmp, "wt.");
9726   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9727     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9728   comdat_key = get_identifier (tmp);
9729   targetm.asm_out.named_section (secname,
9730                                  SECTION_DEBUG | SECTION_LINKONCE,
9731                                  comdat_key);
9732 #else
9733   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9734   sprintf (tmp, ".gnu.linkonce.wt.");
9735   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9736     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9737   secname = tmp;
9738   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9739 #endif
9740
9741   /* Output debugging information.  */
9742   output_compilation_unit_header ();
9743   output_signature (node->signature, "Type Signature");
9744   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9745                        "Offset to Type DIE");
9746   output_die (node->root_die);
9747
9748   unmark_dies (node->root_die);
9749 }
9750
9751 /* Return the DWARF2/3 pubname associated with a decl.  */
9752
9753 static const char *
9754 dwarf2_name (tree decl, int scope)
9755 {
9756   if (DECL_NAMELESS (decl))
9757     return NULL;
9758   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9759 }
9760
9761 /* Add a new entry to .debug_pubnames if appropriate.  */
9762
9763 static void
9764 add_pubname_string (const char *str, dw_die_ref die)
9765 {
9766   pubname_entry e;
9767
9768   e.die = die;
9769   e.name = xstrdup (str);
9770   vec_safe_push (pubname_table, e);
9771 }
9772
9773 static void
9774 add_pubname (tree decl, dw_die_ref die)
9775 {
9776   if (!want_pubnames ())
9777     return;
9778
9779   /* Don't add items to the table when we expect that the consumer will have
9780      just read the enclosing die.  For example, if the consumer is looking at a
9781      class_member, it will either be inside the class already, or will have just
9782      looked up the class to find the member.  Either way, searching the class is
9783      faster than searching the index.  */
9784   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9785       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9786     {
9787       const char *name = dwarf2_name (decl, 1);
9788
9789       if (name)
9790         add_pubname_string (name, die);
9791     }
9792 }
9793
9794 /* Add an enumerator to the pubnames section.  */
9795
9796 static void
9797 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9798 {
9799   pubname_entry e;
9800
9801   gcc_assert (scope_name);
9802   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9803   e.die = die;
9804   vec_safe_push (pubname_table, e);
9805 }
9806
9807 /* Add a new entry to .debug_pubtypes if appropriate.  */
9808
9809 static void
9810 add_pubtype (tree decl, dw_die_ref die)
9811 {
9812   pubname_entry e;
9813
9814   if (!want_pubnames ())
9815     return;
9816
9817   if ((TREE_PUBLIC (decl)
9818        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9819       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9820     {
9821       tree scope = NULL;
9822       const char *scope_name = "";
9823       const char *sep = is_cxx () ? "::" : ".";
9824       const char *name;
9825
9826       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9827       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9828         {
9829           scope_name = lang_hooks.dwarf_name (scope, 1);
9830           if (scope_name != NULL && scope_name[0] != '\0')
9831             scope_name = concat (scope_name, sep, NULL);
9832           else
9833             scope_name = "";
9834         }
9835
9836       if (TYPE_P (decl))
9837         name = type_tag (decl);
9838       else
9839         name = lang_hooks.dwarf_name (decl, 1);
9840
9841       /* If we don't have a name for the type, there's no point in adding
9842          it to the table.  */
9843       if (name != NULL && name[0] != '\0')
9844         {
9845           e.die = die;
9846           e.name = concat (scope_name, name, NULL);
9847           vec_safe_push (pubtype_table, e);
9848         }
9849
9850       /* Although it might be more consistent to add the pubinfo for the
9851          enumerators as their dies are created, they should only be added if the
9852          enum type meets the criteria above.  So rather than re-check the parent
9853          enum type whenever an enumerator die is created, just output them all
9854          here.  This isn't protected by the name conditional because anonymous
9855          enums don't have names.  */
9856       if (die->die_tag == DW_TAG_enumeration_type)
9857         {
9858           dw_die_ref c;
9859
9860           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9861         }
9862     }
9863 }
9864
9865 /* Output a single entry in the pubnames table.  */
9866
9867 static void
9868 output_pubname (dw_offset die_offset, pubname_entry *entry)
9869 {
9870   dw_die_ref die = entry->die;
9871   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9872
9873   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9874
9875   if (debug_generate_pub_sections == 2)
9876     {
9877       /* This logic follows gdb's method for determining the value of the flag
9878          byte.  */
9879       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9880       switch (die->die_tag)
9881       {
9882         case DW_TAG_typedef:
9883         case DW_TAG_base_type:
9884         case DW_TAG_subrange_type:
9885           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9886           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9887           break;
9888         case DW_TAG_enumerator:
9889           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9890                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9891           if (!is_cxx () && !is_java ())
9892             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9893           break;
9894         case DW_TAG_subprogram:
9895           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9896                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9897           if (!is_ada ())
9898             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9899           break;
9900         case DW_TAG_constant:
9901           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9902                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9903           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9904           break;
9905         case DW_TAG_variable:
9906           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9907                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9908           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9909           break;
9910         case DW_TAG_namespace:
9911         case DW_TAG_imported_declaration:
9912           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9913           break;
9914         case DW_TAG_class_type:
9915         case DW_TAG_interface_type:
9916         case DW_TAG_structure_type:
9917         case DW_TAG_union_type:
9918         case DW_TAG_enumeration_type:
9919           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9920           if (!is_cxx () && !is_java ())
9921             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9922           break;
9923         default:
9924           /* An unusual tag.  Leave the flag-byte empty.  */
9925           break;
9926       }
9927       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9928                            "GDB-index flags");
9929     }
9930
9931   dw2_asm_output_nstring (entry->name, -1, "external name");
9932 }
9933
9934
9935 /* Output the public names table used to speed up access to externally
9936    visible names; or the public types table used to find type definitions.  */
9937
9938 static void
9939 output_pubnames (vec<pubname_entry, va_gc> *names)
9940 {
9941   unsigned i;
9942   unsigned long pubnames_length = size_of_pubnames (names);
9943   pubname_entry *pub;
9944
9945   if (!XCOFF_DEBUGGING_INFO)
9946     {
9947       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9948         dw2_asm_output_data (4, 0xffffffff,
9949           "Initial length escape value indicating 64-bit DWARF extension");
9950       dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9951                            "Pub Info Length");
9952     }
9953
9954   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9955   dw2_asm_output_data (2, 2, "DWARF Version");
9956
9957   if (dwarf_split_debug_info)
9958     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9959                            debug_skeleton_info_section,
9960                            "Offset of Compilation Unit Info");
9961   else
9962     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9963                            debug_info_section,
9964                            "Offset of Compilation Unit Info");
9965   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9966                        "Compilation Unit Length");
9967
9968   FOR_EACH_VEC_ELT (*names, i, pub)
9969     {
9970       if (include_pubname_in_output (names, pub))
9971         {
9972           dw_offset die_offset = pub->die->die_offset;
9973
9974           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9975           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9976             gcc_assert (pub->die->die_mark);
9977
9978           /* If we're putting types in their own .debug_types sections,
9979              the .debug_pubtypes table will still point to the compile
9980              unit (not the type unit), so we want to use the offset of
9981              the skeleton DIE (if there is one).  */
9982           if (pub->die->comdat_type_p && names == pubtype_table)
9983             {
9984               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9985
9986               if (type_node != NULL)
9987                 die_offset = (type_node->skeleton_die != NULL
9988                               ? type_node->skeleton_die->die_offset
9989                               : comp_unit_die ()->die_offset);
9990             }
9991
9992           output_pubname (die_offset, pub);
9993         }
9994     }
9995
9996   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9997 }
9998
9999 /* Output public names and types tables if necessary.  */
10000
10001 static void
10002 output_pubtables (void)
10003 {
10004   if (!want_pubnames () || !info_section_emitted)
10005     return;
10006
10007   switch_to_section (debug_pubnames_section);
10008   output_pubnames (pubname_table);
10009   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10010      It shouldn't hurt to emit it always, since pure DWARF2 consumers
10011      simply won't look for the section.  */
10012   switch_to_section (debug_pubtypes_section);
10013   output_pubnames (pubtype_table);
10014 }
10015
10016
10017 /* Output the information that goes into the .debug_aranges table.
10018    Namely, define the beginning and ending address range of the
10019    text section generated for this compilation unit.  */
10020
10021 static void
10022 output_aranges (void)
10023 {
10024   unsigned i;
10025   unsigned long aranges_length = size_of_aranges ();
10026   
10027   if (!XCOFF_DEBUGGING_INFO)
10028     {
10029       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10030         dw2_asm_output_data (4, 0xffffffff,
10031           "Initial length escape value indicating 64-bit DWARF extension");
10032       dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10033                            "Length of Address Ranges Info");
10034     }
10035
10036   /* Version number for aranges is still 2, even up to DWARF5.  */
10037   dw2_asm_output_data (2, 2, "DWARF Version");
10038   if (dwarf_split_debug_info)
10039     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10040                            debug_skeleton_info_section,
10041                            "Offset of Compilation Unit Info");
10042   else
10043     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10044                            debug_info_section,
10045                            "Offset of Compilation Unit Info");
10046   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10047   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10048
10049   /* We need to align to twice the pointer size here.  */
10050   if (DWARF_ARANGES_PAD_SIZE)
10051     {
10052       /* Pad using a 2 byte words so that padding is correct for any
10053          pointer size.  */
10054       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10055                            2 * DWARF2_ADDR_SIZE);
10056       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10057         dw2_asm_output_data (2, 0, NULL);
10058     }
10059
10060   /* It is necessary not to output these entries if the sections were
10061      not used; if the sections were not used, the length will be 0 and
10062      the address may end up as 0 if the section is discarded by ld
10063      --gc-sections, leaving an invalid (0, 0) entry that can be
10064      confused with the terminator.  */
10065   if (text_section_used)
10066     {
10067       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10068       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10069                             text_section_label, "Length");
10070     }
10071   if (cold_text_section_used)
10072     {
10073       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10074                            "Address");
10075       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10076                             cold_text_section_label, "Length");
10077     }
10078
10079   if (have_multiple_function_sections)
10080     {
10081       unsigned fde_idx;
10082       dw_fde_ref fde;
10083
10084       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10085         {
10086           if (DECL_IGNORED_P (fde->decl))
10087             continue;
10088           if (!fde->in_std_section)
10089             {
10090               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10091                                    "Address");
10092               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10093                                     fde->dw_fde_begin, "Length");
10094             }
10095           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10096             {
10097               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10098                                    "Address");
10099               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10100                                     fde->dw_fde_second_begin, "Length");
10101             }
10102         }
10103     }
10104
10105   /* Output the terminator words.  */
10106   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10107   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10108 }
10109
10110 /* Add a new entry to .debug_ranges.  Return the offset at which it
10111    was placed.  */
10112
10113 static unsigned int
10114 add_ranges_num (int num)
10115 {
10116   unsigned int in_use = ranges_table_in_use;
10117
10118   if (in_use == ranges_table_allocated)
10119     {
10120       ranges_table_allocated += RANGES_TABLE_INCREMENT;
10121       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10122                                     ranges_table_allocated);
10123       memset (ranges_table + ranges_table_in_use, 0,
10124               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10125     }
10126
10127   ranges_table[in_use].num = num;
10128   ranges_table_in_use = in_use + 1;
10129
10130   return in_use * 2 * DWARF2_ADDR_SIZE;
10131 }
10132
10133 /* Add a new entry to .debug_ranges corresponding to a block, or a
10134    range terminator if BLOCK is NULL.  */
10135
10136 static unsigned int
10137 add_ranges (const_tree block)
10138 {
10139   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10140 }
10141
10142 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10143    When using dwarf_split_debug_info, address attributes in dies destined
10144    for the final executable should be direct references--setting the
10145    parameter force_direct ensures this behavior.  */
10146
10147 static void
10148 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10149                       bool *added, bool force_direct)
10150 {
10151   unsigned int in_use = ranges_by_label_in_use;
10152   unsigned int offset;
10153
10154   if (in_use == ranges_by_label_allocated)
10155     {
10156       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10157       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10158                                        ranges_by_label_allocated);
10159       memset (ranges_by_label + ranges_by_label_in_use, 0,
10160               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10161     }
10162
10163   ranges_by_label[in_use].begin = begin;
10164   ranges_by_label[in_use].end = end;
10165   ranges_by_label_in_use = in_use + 1;
10166
10167   offset = add_ranges_num (-(int)in_use - 1);
10168   if (!*added)
10169     {
10170       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10171       *added = true;
10172     }
10173 }
10174
10175 static void
10176 output_ranges (void)
10177 {
10178   unsigned i;
10179   static const char *const start_fmt = "Offset %#x";
10180   const char *fmt = start_fmt;
10181
10182   for (i = 0; i < ranges_table_in_use; i++)
10183     {
10184       int block_num = ranges_table[i].num;
10185
10186       if (block_num > 0)
10187         {
10188           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10189           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10190
10191           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10192           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10193
10194           /* If all code is in the text section, then the compilation
10195              unit base address defaults to DW_AT_low_pc, which is the
10196              base of the text section.  */
10197           if (!have_multiple_function_sections)
10198             {
10199               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10200                                     text_section_label,
10201                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10202               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10203                                     text_section_label, NULL);
10204             }
10205
10206           /* Otherwise, the compilation unit base address is zero,
10207              which allows us to use absolute addresses, and not worry
10208              about whether the target supports cross-section
10209              arithmetic.  */
10210           else
10211             {
10212               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10213                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10214               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10215             }
10216
10217           fmt = NULL;
10218         }
10219
10220       /* Negative block_num stands for an index into ranges_by_label.  */
10221       else if (block_num < 0)
10222         {
10223           int lab_idx = - block_num - 1;
10224
10225           if (!have_multiple_function_sections)
10226             {
10227               gcc_unreachable ();
10228 #if 0
10229               /* If we ever use add_ranges_by_labels () for a single
10230                  function section, all we have to do is to take out
10231                  the #if 0 above.  */
10232               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10233                                     ranges_by_label[lab_idx].begin,
10234                                     text_section_label,
10235                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10236               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10237                                     ranges_by_label[lab_idx].end,
10238                                     text_section_label, NULL);
10239 #endif
10240             }
10241           else
10242             {
10243               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10244                                    ranges_by_label[lab_idx].begin,
10245                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10246               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10247                                    ranges_by_label[lab_idx].end,
10248                                    NULL);
10249             }
10250         }
10251       else
10252         {
10253           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10254           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10255           fmt = start_fmt;
10256         }
10257     }
10258 }
10259
10260 /* Data structure containing information about input files.  */
10261 struct file_info
10262 {
10263   const char *path;     /* Complete file name.  */
10264   const char *fname;    /* File name part.  */
10265   int length;           /* Length of entire string.  */
10266   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
10267   int dir_idx;          /* Index in directory table.  */
10268 };
10269
10270 /* Data structure containing information about directories with source
10271    files.  */
10272 struct dir_info
10273 {
10274   const char *path;     /* Path including directory name.  */
10275   int length;           /* Path length.  */
10276   int prefix;           /* Index of directory entry which is a prefix.  */
10277   int count;            /* Number of files in this directory.  */
10278   int dir_idx;          /* Index of directory used as base.  */
10279 };
10280
10281 /* Callback function for file_info comparison.  We sort by looking at
10282    the directories in the path.  */
10283
10284 static int
10285 file_info_cmp (const void *p1, const void *p2)
10286 {
10287   const struct file_info *const s1 = (const struct file_info *) p1;
10288   const struct file_info *const s2 = (const struct file_info *) p2;
10289   const unsigned char *cp1;
10290   const unsigned char *cp2;
10291
10292   /* Take care of file names without directories.  We need to make sure that
10293      we return consistent values to qsort since some will get confused if
10294      we return the same value when identical operands are passed in opposite
10295      orders.  So if neither has a directory, return 0 and otherwise return
10296      1 or -1 depending on which one has the directory.  */
10297   if ((s1->path == s1->fname || s2->path == s2->fname))
10298     return (s2->path == s2->fname) - (s1->path == s1->fname);
10299
10300   cp1 = (const unsigned char *) s1->path;
10301   cp2 = (const unsigned char *) s2->path;
10302
10303   while (1)
10304     {
10305       ++cp1;
10306       ++cp2;
10307       /* Reached the end of the first path?  If so, handle like above.  */
10308       if ((cp1 == (const unsigned char *) s1->fname)
10309           || (cp2 == (const unsigned char *) s2->fname))
10310         return ((cp2 == (const unsigned char *) s2->fname)
10311                 - (cp1 == (const unsigned char *) s1->fname));
10312
10313       /* Character of current path component the same?  */
10314       else if (*cp1 != *cp2)
10315         return *cp1 - *cp2;
10316     }
10317 }
10318
10319 struct file_name_acquire_data
10320 {
10321   struct file_info *files;
10322   int used_files;
10323   int max_files;
10324 };
10325
10326 /* Traversal function for the hash table.  */
10327
10328 int
10329 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10330 {
10331   struct dwarf_file_data *d = *slot;
10332   struct file_info *fi;
10333   const char *f;
10334
10335   gcc_assert (fnad->max_files >= d->emitted_number);
10336
10337   if (! d->emitted_number)
10338     return 1;
10339
10340   gcc_assert (fnad->max_files != fnad->used_files);
10341
10342   fi = fnad->files + fnad->used_files++;
10343
10344   /* Skip all leading "./".  */
10345   f = d->filename;
10346   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10347     f += 2;
10348
10349   /* Create a new array entry.  */
10350   fi->path = f;
10351   fi->length = strlen (f);
10352   fi->file_idx = d;
10353
10354   /* Search for the file name part.  */
10355   f = strrchr (f, DIR_SEPARATOR);
10356 #if defined (DIR_SEPARATOR_2)
10357   {
10358     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10359
10360     if (g != NULL)
10361       {
10362         if (f == NULL || f < g)
10363           f = g;
10364       }
10365   }
10366 #endif
10367
10368   fi->fname = f == NULL ? fi->path : f + 1;
10369   return 1;
10370 }
10371
10372 /* Output the directory table and the file name table.  We try to minimize
10373    the total amount of memory needed.  A heuristic is used to avoid large
10374    slowdowns with many input files.  */
10375
10376 static void
10377 output_file_names (void)
10378 {
10379   struct file_name_acquire_data fnad;
10380   int numfiles;
10381   struct file_info *files;
10382   struct dir_info *dirs;
10383   int *saved;
10384   int *savehere;
10385   int *backmap;
10386   int ndirs;
10387   int idx_offset;
10388   int i;
10389
10390   if (!last_emitted_file)
10391     {
10392       dw2_asm_output_data (1, 0, "End directory table");
10393       dw2_asm_output_data (1, 0, "End file name table");
10394       return;
10395     }
10396
10397   numfiles = last_emitted_file->emitted_number;
10398
10399   /* Allocate the various arrays we need.  */
10400   files = XALLOCAVEC (struct file_info, numfiles);
10401   dirs = XALLOCAVEC (struct dir_info, numfiles);
10402
10403   fnad.files = files;
10404   fnad.used_files = 0;
10405   fnad.max_files = numfiles;
10406   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10407   gcc_assert (fnad.used_files == fnad.max_files);
10408
10409   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10410
10411   /* Find all the different directories used.  */
10412   dirs[0].path = files[0].path;
10413   dirs[0].length = files[0].fname - files[0].path;
10414   dirs[0].prefix = -1;
10415   dirs[0].count = 1;
10416   dirs[0].dir_idx = 0;
10417   files[0].dir_idx = 0;
10418   ndirs = 1;
10419
10420   for (i = 1; i < numfiles; i++)
10421     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10422         && memcmp (dirs[ndirs - 1].path, files[i].path,
10423                    dirs[ndirs - 1].length) == 0)
10424       {
10425         /* Same directory as last entry.  */
10426         files[i].dir_idx = ndirs - 1;
10427         ++dirs[ndirs - 1].count;
10428       }
10429     else
10430       {
10431         int j;
10432
10433         /* This is a new directory.  */
10434         dirs[ndirs].path = files[i].path;
10435         dirs[ndirs].length = files[i].fname - files[i].path;
10436         dirs[ndirs].count = 1;
10437         dirs[ndirs].dir_idx = ndirs;
10438         files[i].dir_idx = ndirs;
10439
10440         /* Search for a prefix.  */
10441         dirs[ndirs].prefix = -1;
10442         for (j = 0; j < ndirs; j++)
10443           if (dirs[j].length < dirs[ndirs].length
10444               && dirs[j].length > 1
10445               && (dirs[ndirs].prefix == -1
10446                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10447               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10448             dirs[ndirs].prefix = j;
10449
10450         ++ndirs;
10451       }
10452
10453   /* Now to the actual work.  We have to find a subset of the directories which
10454      allow expressing the file name using references to the directory table
10455      with the least amount of characters.  We do not do an exhaustive search
10456      where we would have to check out every combination of every single
10457      possible prefix.  Instead we use a heuristic which provides nearly optimal
10458      results in most cases and never is much off.  */
10459   saved = XALLOCAVEC (int, ndirs);
10460   savehere = XALLOCAVEC (int, ndirs);
10461
10462   memset (saved, '\0', ndirs * sizeof (saved[0]));
10463   for (i = 0; i < ndirs; i++)
10464     {
10465       int j;
10466       int total;
10467
10468       /* We can always save some space for the current directory.  But this
10469          does not mean it will be enough to justify adding the directory.  */
10470       savehere[i] = dirs[i].length;
10471       total = (savehere[i] - saved[i]) * dirs[i].count;
10472
10473       for (j = i + 1; j < ndirs; j++)
10474         {
10475           savehere[j] = 0;
10476           if (saved[j] < dirs[i].length)
10477             {
10478               /* Determine whether the dirs[i] path is a prefix of the
10479                  dirs[j] path.  */
10480               int k;
10481
10482               k = dirs[j].prefix;
10483               while (k != -1 && k != (int) i)
10484                 k = dirs[k].prefix;
10485
10486               if (k == (int) i)
10487                 {
10488                   /* Yes it is.  We can possibly save some memory by
10489                      writing the filenames in dirs[j] relative to
10490                      dirs[i].  */
10491                   savehere[j] = dirs[i].length;
10492                   total += (savehere[j] - saved[j]) * dirs[j].count;
10493                 }
10494             }
10495         }
10496
10497       /* Check whether we can save enough to justify adding the dirs[i]
10498          directory.  */
10499       if (total > dirs[i].length + 1)
10500         {
10501           /* It's worthwhile adding.  */
10502           for (j = i; j < ndirs; j++)
10503             if (savehere[j] > 0)
10504               {
10505                 /* Remember how much we saved for this directory so far.  */
10506                 saved[j] = savehere[j];
10507
10508                 /* Remember the prefix directory.  */
10509                 dirs[j].dir_idx = i;
10510               }
10511         }
10512     }
10513
10514   /* Emit the directory name table.  */
10515   idx_offset = dirs[0].length > 0 ? 1 : 0;
10516   for (i = 1 - idx_offset; i < ndirs; i++)
10517     dw2_asm_output_nstring (dirs[i].path,
10518                             dirs[i].length
10519                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10520                             "Directory Entry: %#x", i + idx_offset);
10521
10522   dw2_asm_output_data (1, 0, "End directory table");
10523
10524   /* We have to emit them in the order of emitted_number since that's
10525      used in the debug info generation.  To do this efficiently we
10526      generate a back-mapping of the indices first.  */
10527   backmap = XALLOCAVEC (int, numfiles);
10528   for (i = 0; i < numfiles; i++)
10529     backmap[files[i].file_idx->emitted_number - 1] = i;
10530
10531   /* Now write all the file names.  */
10532   for (i = 0; i < numfiles; i++)
10533     {
10534       int file_idx = backmap[i];
10535       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10536
10537 #ifdef VMS_DEBUGGING_INFO
10538 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10539
10540       /* Setting these fields can lead to debugger miscomparisons,
10541          but VMS Debug requires them to be set correctly.  */
10542
10543       int ver;
10544       long long cdt;
10545       long siz;
10546       int maxfilelen = strlen (files[file_idx].path)
10547                                + dirs[dir_idx].length
10548                                + MAX_VMS_VERSION_LEN + 1;
10549       char *filebuf = XALLOCAVEC (char, maxfilelen);
10550
10551       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10552       snprintf (filebuf, maxfilelen, "%s;%d",
10553                 files[file_idx].path + dirs[dir_idx].length, ver);
10554
10555       dw2_asm_output_nstring
10556         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10557
10558       /* Include directory index.  */
10559       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10560
10561       /* Modification time.  */
10562       dw2_asm_output_data_uleb128
10563         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10564           ? cdt : 0,
10565          NULL);
10566
10567       /* File length in bytes.  */
10568       dw2_asm_output_data_uleb128
10569         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10570           ? siz : 0,
10571          NULL);
10572 #else
10573       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10574                               "File Entry: %#x", (unsigned) i + 1);
10575
10576       /* Include directory index.  */
10577       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10578
10579       /* Modification time.  */
10580       dw2_asm_output_data_uleb128 (0, NULL);
10581
10582       /* File length in bytes.  */
10583       dw2_asm_output_data_uleb128 (0, NULL);
10584 #endif /* VMS_DEBUGGING_INFO */
10585     }
10586
10587   dw2_asm_output_data (1, 0, "End file name table");
10588 }
10589
10590
10591 /* Output one line number table into the .debug_line section.  */
10592
10593 static void
10594 output_one_line_info_table (dw_line_info_table *table)
10595 {
10596   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10597   unsigned int current_line = 1;
10598   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10599   dw_line_info_entry *ent;
10600   size_t i;
10601
10602   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10603     {
10604       switch (ent->opcode)
10605         {
10606         case LI_set_address:
10607           /* ??? Unfortunately, we have little choice here currently, and
10608              must always use the most general form.  GCC does not know the
10609              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10610              ports do have length attributes which will give an upper bound
10611              on the address range.  We could perhaps use length attributes
10612              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10613           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10614
10615           /* This can handle any delta.  This takes
10616              4+DWARF2_ADDR_SIZE bytes.  */
10617           dw2_asm_output_data (1, 0, "set address %s", line_label);
10618           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10619           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10620           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10621           break;
10622
10623         case LI_set_line:
10624           if (ent->val == current_line)
10625             {
10626               /* We still need to start a new row, so output a copy insn.  */
10627               dw2_asm_output_data (1, DW_LNS_copy,
10628                                    "copy line %u", current_line);
10629             }
10630           else
10631             {
10632               int line_offset = ent->val - current_line;
10633               int line_delta = line_offset - DWARF_LINE_BASE;
10634
10635               current_line = ent->val;
10636               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10637                 {
10638                   /* This can handle deltas from -10 to 234, using the current
10639                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10640                      This takes 1 byte.  */
10641                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10642                                        "line %u", current_line);
10643                 }
10644               else
10645                 {
10646                   /* This can handle any delta.  This takes at least 4 bytes,
10647                      depending on the value being encoded.  */
10648                   dw2_asm_output_data (1, DW_LNS_advance_line,
10649                                        "advance to line %u", current_line);
10650                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10651                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10652                 }
10653             }
10654           break;
10655
10656         case LI_set_file:
10657           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10658           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10659           break;
10660
10661         case LI_set_column:
10662           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10663           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10664           break;
10665
10666         case LI_negate_stmt:
10667           current_is_stmt = !current_is_stmt;
10668           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10669                                "is_stmt %d", current_is_stmt);
10670           break;
10671
10672         case LI_set_prologue_end:
10673           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10674                                "set prologue end");
10675           break;
10676           
10677         case LI_set_epilogue_begin:
10678           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10679                                "set epilogue begin");
10680           break;
10681
10682         case LI_set_discriminator:
10683           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10684           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10685           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10686           dw2_asm_output_data_uleb128 (ent->val, NULL);
10687           break;
10688         }
10689     }
10690
10691   /* Emit debug info for the address of the end of the table.  */
10692   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10693   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10694   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10695   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10696
10697   dw2_asm_output_data (1, 0, "end sequence");
10698   dw2_asm_output_data_uleb128 (1, NULL);
10699   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10700 }
10701
10702 /* Output the source line number correspondence information.  This
10703    information goes into the .debug_line section.  */
10704
10705 static void
10706 output_line_info (bool prologue_only)
10707 {
10708   char l1[20], l2[20], p1[20], p2[20];
10709   /* We don't support DWARFv5 line tables yet.  */
10710   int ver = dwarf_version < 5 ? dwarf_version : 4;
10711   bool saw_one = false;
10712   int opc;
10713
10714   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10715   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10716   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10717   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10718
10719   if (!XCOFF_DEBUGGING_INFO)
10720     {
10721       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10722         dw2_asm_output_data (4, 0xffffffff,
10723           "Initial length escape value indicating 64-bit DWARF extension");
10724       dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10725                             "Length of Source Line Info");
10726     }
10727
10728   ASM_OUTPUT_LABEL (asm_out_file, l1);
10729
10730   dw2_asm_output_data (2, ver, "DWARF Version");
10731   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10732   ASM_OUTPUT_LABEL (asm_out_file, p1);
10733
10734   /* Define the architecture-dependent minimum instruction length (in bytes).
10735      In this implementation of DWARF, this field is used for information
10736      purposes only.  Since GCC generates assembly language, we have no
10737      a priori knowledge of how many instruction bytes are generated for each
10738      source line, and therefore can use only the DW_LNE_set_address and
10739      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10740      this as '1', which is "correct enough" for all architectures,
10741      and don't let the target override.  */
10742   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10743
10744   if (ver >= 4)
10745     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10746                          "Maximum Operations Per Instruction");
10747   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10748                        "Default is_stmt_start flag");
10749   dw2_asm_output_data (1, DWARF_LINE_BASE,
10750                        "Line Base Value (Special Opcodes)");
10751   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10752                        "Line Range Value (Special Opcodes)");
10753   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10754                        "Special Opcode Base");
10755
10756   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10757     {
10758       int n_op_args;
10759       switch (opc)
10760         {
10761         case DW_LNS_advance_pc:
10762         case DW_LNS_advance_line:
10763         case DW_LNS_set_file:
10764         case DW_LNS_set_column:
10765         case DW_LNS_fixed_advance_pc:
10766         case DW_LNS_set_isa:
10767           n_op_args = 1;
10768           break;
10769         default:
10770           n_op_args = 0;
10771           break;
10772         }
10773
10774       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10775                            opc, n_op_args);
10776     }
10777
10778   /* Write out the information about the files we use.  */
10779   output_file_names ();
10780   ASM_OUTPUT_LABEL (asm_out_file, p2);
10781   if (prologue_only)
10782     {
10783       /* Output the marker for the end of the line number info.  */
10784       ASM_OUTPUT_LABEL (asm_out_file, l2);
10785       return;
10786     }
10787
10788   if (separate_line_info)
10789     {
10790       dw_line_info_table *table;
10791       size_t i;
10792
10793       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10794         if (table->in_use)
10795           {
10796             output_one_line_info_table (table);
10797             saw_one = true;
10798           }
10799     }
10800   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10801     {
10802       output_one_line_info_table (cold_text_section_line_info);
10803       saw_one = true;
10804     }
10805
10806   /* ??? Some Darwin linkers crash on a .debug_line section with no
10807      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10808      sufficient -- the address column must also be initialized.
10809      Make sure to output at least one set_address/end_sequence pair,
10810      choosing .text since that section is always present.  */
10811   if (text_section_line_info->in_use || !saw_one)
10812     output_one_line_info_table (text_section_line_info);
10813
10814   /* Output the marker for the end of the line number info.  */
10815   ASM_OUTPUT_LABEL (asm_out_file, l2);
10816 }
10817 \f
10818 /* Return true if DW_AT_endianity should be emitted according to REVERSE.  */
10819
10820 static inline bool
10821 need_endianity_attribute_p (bool reverse)
10822 {
10823   return reverse && (dwarf_version >= 3 || !dwarf_strict);
10824 }
10825
10826 /* Given a pointer to a tree node for some base type, return a pointer to
10827    a DIE that describes the given type.  REVERSE is true if the type is
10828    to be interpreted in the reverse storage order wrt the target order.
10829
10830    This routine must only be called for GCC type nodes that correspond to
10831    Dwarf base (fundamental) types.  */
10832
10833 static dw_die_ref
10834 base_type_die (tree type, bool reverse)
10835 {
10836   dw_die_ref base_type_result;
10837   enum dwarf_type encoding;
10838   bool fpt_used = false;
10839   struct fixed_point_type_info fpt_info;
10840   tree type_bias = NULL_TREE;
10841
10842   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10843     return 0;
10844
10845   /* If this is a subtype that should not be emitted as a subrange type,
10846      use the base type.  See subrange_type_for_debug_p.  */
10847   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10848     type = TREE_TYPE (type);
10849
10850   switch (TREE_CODE (type))
10851     {
10852     case INTEGER_TYPE:
10853       if ((dwarf_version >= 4 || !dwarf_strict)
10854           && TYPE_NAME (type)
10855           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10856           && DECL_IS_BUILTIN (TYPE_NAME (type))
10857           && DECL_NAME (TYPE_NAME (type)))
10858         {
10859           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10860           if (strcmp (name, "char16_t") == 0
10861               || strcmp (name, "char32_t") == 0)
10862             {
10863               encoding = DW_ATE_UTF;
10864               break;
10865             }
10866         }
10867       if ((dwarf_version >= 3 || !dwarf_strict)
10868           && lang_hooks.types.get_fixed_point_type_info)
10869         {
10870           memset (&fpt_info, 0, sizeof (fpt_info));
10871           if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10872             {
10873               fpt_used = true;
10874               encoding = ((TYPE_UNSIGNED (type))
10875                           ? DW_ATE_unsigned_fixed
10876                           : DW_ATE_signed_fixed);
10877               break;
10878             }
10879         }
10880       if (TYPE_STRING_FLAG (type))
10881         {
10882           if (TYPE_UNSIGNED (type))
10883             encoding = DW_ATE_unsigned_char;
10884           else
10885             encoding = DW_ATE_signed_char;
10886         }
10887       else if (TYPE_UNSIGNED (type))
10888         encoding = DW_ATE_unsigned;
10889       else
10890         encoding = DW_ATE_signed;
10891
10892       if (!dwarf_strict
10893           && lang_hooks.types.get_type_bias)
10894         type_bias = lang_hooks.types.get_type_bias (type);
10895       break;
10896
10897     case REAL_TYPE:
10898       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10899         {
10900           if (dwarf_version >= 3 || !dwarf_strict)
10901             encoding = DW_ATE_decimal_float;
10902           else
10903             encoding = DW_ATE_lo_user;
10904         }
10905       else
10906         encoding = DW_ATE_float;
10907       break;
10908
10909     case FIXED_POINT_TYPE:
10910       if (!(dwarf_version >= 3 || !dwarf_strict))
10911         encoding = DW_ATE_lo_user;
10912       else if (TYPE_UNSIGNED (type))
10913         encoding = DW_ATE_unsigned_fixed;
10914       else
10915         encoding = DW_ATE_signed_fixed;
10916       break;
10917
10918       /* Dwarf2 doesn't know anything about complex ints, so use
10919          a user defined type for it.  */
10920     case COMPLEX_TYPE:
10921       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10922         encoding = DW_ATE_complex_float;
10923       else
10924         encoding = DW_ATE_lo_user;
10925       break;
10926
10927     case BOOLEAN_TYPE:
10928       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10929       encoding = DW_ATE_boolean;
10930       break;
10931
10932     default:
10933       /* No other TREE_CODEs are Dwarf fundamental types.  */
10934       gcc_unreachable ();
10935     }
10936
10937   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10938
10939   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10940                    int_size_in_bytes (type));
10941   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10942
10943   if (need_endianity_attribute_p (reverse))
10944     add_AT_unsigned (base_type_result, DW_AT_endianity,
10945                      BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10946
10947   if (fpt_used)
10948     {
10949       switch (fpt_info.scale_factor_kind)
10950         {
10951         case fixed_point_scale_factor_binary:
10952           add_AT_int (base_type_result, DW_AT_binary_scale,
10953                       fpt_info.scale_factor.binary);
10954           break;
10955
10956         case fixed_point_scale_factor_decimal:
10957           add_AT_int (base_type_result, DW_AT_decimal_scale,
10958                       fpt_info.scale_factor.decimal);
10959           break;
10960
10961         case fixed_point_scale_factor_arbitrary:
10962           /* Arbitrary scale factors cannot be described in standard DWARF,
10963              yet.  */
10964           if (!dwarf_strict)
10965             {
10966               /* Describe the scale factor as a rational constant.  */
10967               const dw_die_ref scale_factor
10968                 = new_die (DW_TAG_constant, comp_unit_die (), type);
10969
10970               add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10971                                fpt_info.scale_factor.arbitrary.numerator);
10972               add_AT_int (scale_factor, DW_AT_GNU_denominator,
10973                           fpt_info.scale_factor.arbitrary.denominator);
10974
10975               add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10976             }
10977           break;
10978
10979         default:
10980           gcc_unreachable ();
10981         }
10982     }
10983
10984   if (type_bias)
10985     add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10986                      dw_scalar_form_constant
10987                      | dw_scalar_form_exprloc
10988                      | dw_scalar_form_reference,
10989                      NULL);
10990
10991   add_pubtype (type, base_type_result);
10992
10993   return base_type_result;
10994 }
10995
10996 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10997    named 'auto' in its type: return true for it, false otherwise.  */
10998
10999 static inline bool
11000 is_cxx_auto (tree type)
11001 {
11002   if (is_cxx ())
11003     {
11004       tree name = TYPE_IDENTIFIER (type);
11005       if (name == get_identifier ("auto")
11006           || name == get_identifier ("decltype(auto)"))
11007         return true;
11008     }
11009   return false;
11010 }
11011
11012 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11013    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
11014
11015 static inline int
11016 is_base_type (tree type)
11017 {
11018   switch (TREE_CODE (type))
11019     {
11020     case ERROR_MARK:
11021     case VOID_TYPE:
11022     case INTEGER_TYPE:
11023     case REAL_TYPE:
11024     case FIXED_POINT_TYPE:
11025     case COMPLEX_TYPE:
11026     case BOOLEAN_TYPE:
11027     case POINTER_BOUNDS_TYPE:
11028       return 1;
11029
11030     case ARRAY_TYPE:
11031     case RECORD_TYPE:
11032     case UNION_TYPE:
11033     case QUAL_UNION_TYPE:
11034     case ENUMERAL_TYPE:
11035     case FUNCTION_TYPE:
11036     case METHOD_TYPE:
11037     case POINTER_TYPE:
11038     case REFERENCE_TYPE:
11039     case NULLPTR_TYPE:
11040     case OFFSET_TYPE:
11041     case LANG_TYPE:
11042     case VECTOR_TYPE:
11043       return 0;
11044
11045     default:
11046       if (is_cxx_auto (type))
11047         return 0;
11048       gcc_unreachable ();
11049     }
11050
11051   return 0;
11052 }
11053
11054 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11055    node, return the size in bits for the type if it is a constant, or else
11056    return the alignment for the type if the type's size is not constant, or
11057    else return BITS_PER_WORD if the type actually turns out to be an
11058    ERROR_MARK node.  */
11059
11060 static inline unsigned HOST_WIDE_INT
11061 simple_type_size_in_bits (const_tree type)
11062 {
11063   if (TREE_CODE (type) == ERROR_MARK)
11064     return BITS_PER_WORD;
11065   else if (TYPE_SIZE (type) == NULL_TREE)
11066     return 0;
11067   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11068     return tree_to_uhwi (TYPE_SIZE (type));
11069   else
11070     return TYPE_ALIGN (type);
11071 }
11072
11073 /* Similarly, but return an offset_int instead of UHWI.  */
11074
11075 static inline offset_int
11076 offset_int_type_size_in_bits (const_tree type)
11077 {
11078   if (TREE_CODE (type) == ERROR_MARK)
11079     return BITS_PER_WORD;
11080   else if (TYPE_SIZE (type) == NULL_TREE)
11081     return 0;
11082   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11083     return wi::to_offset (TYPE_SIZE (type));
11084   else
11085     return TYPE_ALIGN (type);
11086 }
11087
11088 /*  Given a pointer to a tree node for a subrange type, return a pointer
11089     to a DIE that describes the given type.  */
11090
11091 static dw_die_ref
11092 subrange_type_die (tree type, tree low, tree high, tree bias,
11093                    dw_die_ref context_die)
11094 {
11095   dw_die_ref subrange_die;
11096   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11097
11098   if (context_die == NULL)
11099     context_die = comp_unit_die ();
11100
11101   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11102
11103   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11104     {
11105       /* The size of the subrange type and its base type do not match,
11106          so we need to generate a size attribute for the subrange type.  */
11107       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11108     }
11109
11110   if (low)
11111     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11112   if (high)
11113     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11114   if (bias && !dwarf_strict)
11115     add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11116                      dw_scalar_form_constant
11117                      | dw_scalar_form_exprloc
11118                      | dw_scalar_form_reference,
11119                      NULL);
11120
11121   return subrange_die;
11122 }
11123
11124 /* Returns the (const and/or volatile) cv_qualifiers associated with
11125    the decl node.  This will normally be augmented with the
11126    cv_qualifiers of the underlying type in add_type_attribute.  */
11127
11128 static int
11129 decl_quals (const_tree decl)
11130 {
11131   return ((TREE_READONLY (decl)
11132            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11133           | (TREE_THIS_VOLATILE (decl)
11134              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11135 }
11136
11137 /* Determine the TYPE whose qualifiers match the largest strict subset
11138    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
11139    qualifiers outside QUAL_MASK.  */
11140
11141 static int
11142 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11143 {
11144   tree t;
11145   int best_rank = 0, best_qual = 0, max_rank;
11146
11147   type_quals &= qual_mask;
11148   max_rank = popcount_hwi (type_quals) - 1;
11149
11150   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11151        t = TYPE_NEXT_VARIANT (t))
11152     {
11153       int q = TYPE_QUALS (t) & qual_mask;
11154
11155       if ((q & type_quals) == q && q != type_quals
11156           && check_base_type (t, type))
11157         {
11158           int rank = popcount_hwi (q);
11159
11160           if (rank > best_rank)
11161             {
11162               best_rank = rank;
11163               best_qual = q;
11164             }
11165         }
11166     }
11167
11168   return best_qual;
11169 }
11170
11171 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11172 static const dwarf_qual_info_t dwarf_qual_info[] =
11173 {
11174   { TYPE_QUAL_CONST, DW_TAG_const_type },
11175   { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11176   { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11177   { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11178 };
11179 static const unsigned int dwarf_qual_info_size
11180   = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11181
11182 /* If DIE is a qualified DIE of some base DIE with the same parent,
11183    return the base DIE, otherwise return NULL.  Set MASK to the
11184    qualifiers added compared to the returned DIE.  */
11185
11186 static dw_die_ref
11187 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11188 {
11189   unsigned int i;
11190   for (i = 0; i < dwarf_qual_info_size; i++)
11191     if (die->die_tag == dwarf_qual_info[i].t)
11192       break;
11193   if (i == dwarf_qual_info_size)
11194     return NULL;
11195   if (vec_safe_length (die->die_attr) != 1)
11196     return NULL;
11197   dw_die_ref type = get_AT_ref (die, DW_AT_type);
11198   if (type == NULL || type->die_parent != die->die_parent)
11199     return NULL;
11200   *mask |= dwarf_qual_info[i].q;
11201   if (depth)
11202     {
11203       dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11204       if (ret)
11205         return ret;
11206     }
11207   return type;
11208 }
11209
11210 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11211    entry that chains the modifiers specified by CV_QUALS in front of the
11212    given type.  REVERSE is true if the type is to be interpreted in the
11213    reverse storage order wrt the target order.  */
11214
11215 static dw_die_ref
11216 modified_type_die (tree type, int cv_quals, bool reverse,
11217                    dw_die_ref context_die)
11218 {
11219   enum tree_code code = TREE_CODE (type);
11220   dw_die_ref mod_type_die;
11221   dw_die_ref sub_die = NULL;
11222   tree item_type = NULL;
11223   tree qualified_type;
11224   tree name, low, high;
11225   dw_die_ref mod_scope;
11226   /* Only these cv-qualifiers are currently handled.  */
11227   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11228                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11229
11230   if (code == ERROR_MARK)
11231     return NULL;
11232
11233   if (lang_hooks.types.get_debug_type)
11234     {
11235       tree debug_type = lang_hooks.types.get_debug_type (type);
11236
11237       if (debug_type != NULL_TREE && debug_type != type)
11238         return modified_type_die (debug_type, cv_quals, reverse, context_die);
11239     }
11240
11241   cv_quals &= cv_qual_mask;
11242
11243   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11244      tag modifier (and not an attribute) old consumers won't be able
11245      to handle it.  */
11246   if (dwarf_version < 3)
11247     cv_quals &= ~TYPE_QUAL_RESTRICT;
11248
11249   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
11250   if (dwarf_version < 5)
11251     cv_quals &= ~TYPE_QUAL_ATOMIC;
11252
11253   /* See if we already have the appropriately qualified variant of
11254      this type.  */
11255   qualified_type = get_qualified_type (type, cv_quals);
11256
11257   if (qualified_type == sizetype
11258       && TYPE_NAME (qualified_type)
11259       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11260     {
11261       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11262
11263       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11264                            && TYPE_PRECISION (t)
11265                            == TYPE_PRECISION (qualified_type)
11266                            && TYPE_UNSIGNED (t)
11267                            == TYPE_UNSIGNED (qualified_type));
11268       qualified_type = t;
11269     }
11270
11271   /* If we do, then we can just use its DIE, if it exists.  */
11272   if (qualified_type)
11273     {
11274       mod_type_die = lookup_type_die (qualified_type);
11275
11276       /* DW_AT_endianity doesn't come from a qualifier on the type.  */
11277       if (mod_type_die
11278           && (!need_endianity_attribute_p (reverse)
11279               || !is_base_type (type)
11280               || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11281         return mod_type_die;
11282     }
11283
11284   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11285
11286   /* Handle C typedef types.  */
11287   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11288       && !DECL_ARTIFICIAL (name))
11289     {
11290       tree dtype = TREE_TYPE (name);
11291
11292       if (qualified_type == dtype)
11293         {
11294           /* For a named type, use the typedef.  */
11295           gen_type_die (qualified_type, context_die);
11296           return lookup_type_die (qualified_type);
11297         }
11298       else
11299         {
11300           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11301           dquals &= cv_qual_mask;
11302           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11303               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11304             /* cv-unqualified version of named type.  Just use
11305                the unnamed type to which it refers.  */
11306             return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11307                                       reverse, context_die);
11308           /* Else cv-qualified version of named type; fall through.  */
11309         }
11310     }
11311
11312   mod_scope = scope_die_for (type, context_die);
11313
11314   if (cv_quals)
11315     {
11316       int sub_quals = 0, first_quals = 0;
11317       unsigned i;
11318       dw_die_ref first = NULL, last = NULL;
11319
11320       /* Determine a lesser qualified type that most closely matches
11321          this one.  Then generate DW_TAG_* entries for the remaining
11322          qualifiers.  */
11323       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11324                                                   cv_qual_mask);
11325       if (sub_quals && use_debug_types)
11326         {
11327           bool needed = false;
11328           /* If emitting type units, make sure the order of qualifiers
11329              is canonical.  Thus, start from unqualified type if
11330              an earlier qualifier is missing in sub_quals, but some later
11331              one is present there.  */
11332           for (i = 0; i < dwarf_qual_info_size; i++)
11333             if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11334               needed = true;
11335             else if (needed && (dwarf_qual_info[i].q & cv_quals))
11336               {
11337                 sub_quals = 0;
11338                 break;
11339               }
11340         }
11341       mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11342       if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11343         {
11344           /* As not all intermediate qualified DIEs have corresponding
11345              tree types, ensure that qualified DIEs in the same scope
11346              as their DW_AT_type are emitted after their DW_AT_type,
11347              only with other qualified DIEs for the same type possibly
11348              in between them.  Determine the range of such qualified
11349              DIEs now (first being the base type, last being corresponding
11350              last qualified DIE for it).  */
11351           unsigned int count = 0;
11352           first = qualified_die_p (mod_type_die, &first_quals,
11353                                    dwarf_qual_info_size);
11354           if (first == NULL)
11355             first = mod_type_die;
11356           gcc_assert ((first_quals & ~sub_quals) == 0);
11357           for (count = 0, last = first;
11358                count < (1U << dwarf_qual_info_size);
11359                count++, last = last->die_sib)
11360             {
11361               int quals = 0;
11362               if (last == mod_scope->die_child)
11363                 break;
11364               if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11365                   != first)
11366                 break;
11367             }
11368         }
11369
11370       for (i = 0; i < dwarf_qual_info_size; i++)
11371         if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11372           {
11373             dw_die_ref d;
11374             if (first && first != last)
11375               {
11376                 for (d = first->die_sib; ; d = d->die_sib)
11377                   {
11378                     int quals = 0;
11379                     qualified_die_p (d, &quals, dwarf_qual_info_size);
11380                     if (quals == (first_quals | dwarf_qual_info[i].q))
11381                       break;
11382                     if (d == last)
11383                       {
11384                         d = NULL;
11385                         break;
11386                       }
11387                   }
11388                 if (d)
11389                   {
11390                     mod_type_die = d;
11391                     continue;
11392                   }
11393               }
11394             if (first)
11395               {
11396                 d = ggc_cleared_alloc<die_node> ();
11397                 d->die_tag = dwarf_qual_info[i].t;
11398                 add_child_die_after (mod_scope, d, last);
11399                 last = d;
11400               }
11401             else
11402               d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11403             if (mod_type_die)
11404               add_AT_die_ref (d, DW_AT_type, mod_type_die);
11405             mod_type_die = d;
11406             first_quals |= dwarf_qual_info[i].q;
11407           }
11408     }
11409   else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11410     {
11411       dwarf_tag tag = DW_TAG_pointer_type;
11412       if (code == REFERENCE_TYPE)
11413         {
11414           if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11415             tag = DW_TAG_rvalue_reference_type;
11416           else
11417             tag = DW_TAG_reference_type;
11418         }
11419       mod_type_die = new_die (tag, mod_scope, type);
11420
11421       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11422                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
11423       item_type = TREE_TYPE (type);
11424
11425       addr_space_t as = TYPE_ADDR_SPACE (item_type);
11426       if (!ADDR_SPACE_GENERIC_P (as))
11427         {
11428           int action = targetm.addr_space.debug (as);
11429           if (action >= 0)
11430             {
11431               /* Positive values indicate an address_class.  */
11432               add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11433             }
11434           else
11435             {
11436               /* Negative values indicate an (inverted) segment base reg.  */
11437               dw_loc_descr_ref d
11438                 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11439               add_AT_loc (mod_type_die, DW_AT_segment, d);
11440             }
11441         }
11442     }
11443   else if (code == INTEGER_TYPE
11444            && TREE_TYPE (type) != NULL_TREE
11445            && subrange_type_for_debug_p (type, &low, &high))
11446     {
11447       tree bias = NULL_TREE;
11448       if (lang_hooks.types.get_type_bias)
11449         bias = lang_hooks.types.get_type_bias (type);
11450       mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11451       item_type = TREE_TYPE (type);
11452     }
11453   else if (is_base_type (type))
11454     mod_type_die = base_type_die (type, reverse);
11455   else
11456     {
11457       gen_type_die (type, context_die);
11458
11459       /* We have to get the type_main_variant here (and pass that to the
11460          `lookup_type_die' routine) because the ..._TYPE node we have
11461          might simply be a *copy* of some original type node (where the
11462          copy was created to help us keep track of typedef names) and
11463          that copy might have a different TYPE_UID from the original
11464          ..._TYPE node.  */
11465       if (TREE_CODE (type) != VECTOR_TYPE)
11466         return lookup_type_die (type_main_variant (type));
11467       else
11468         /* Vectors have the debugging information in the type,
11469            not the main variant.  */
11470         return lookup_type_die (type);
11471     }
11472
11473   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
11474      don't output a DW_TAG_typedef, since there isn't one in the
11475      user's program; just attach a DW_AT_name to the type.
11476      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11477      if the base type already has the same name.  */
11478   if (name
11479       && ((TREE_CODE (name) != TYPE_DECL
11480            && (qualified_type == TYPE_MAIN_VARIANT (type)
11481                || (cv_quals == TYPE_UNQUALIFIED)))
11482           || (TREE_CODE (name) == TYPE_DECL
11483               && TREE_TYPE (name) == qualified_type
11484               && DECL_NAME (name))))
11485     {
11486       if (TREE_CODE (name) == TYPE_DECL)
11487         /* Could just call add_name_and_src_coords_attributes here,
11488            but since this is a builtin type it doesn't have any
11489            useful source coordinates anyway.  */
11490         name = DECL_NAME (name);
11491       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11492     }
11493   /* This probably indicates a bug.  */
11494   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11495     {
11496       name = TYPE_IDENTIFIER (type);
11497       add_name_attribute (mod_type_die,
11498                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
11499     }
11500
11501   if (qualified_type)
11502     equate_type_number_to_die (qualified_type, mod_type_die);
11503
11504   if (item_type)
11505     /* We must do this after the equate_type_number_to_die call, in case
11506        this is a recursive type.  This ensures that the modified_type_die
11507        recursion will terminate even if the type is recursive.  Recursive
11508        types are possible in Ada.  */
11509     sub_die = modified_type_die (item_type,
11510                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
11511                                  reverse,
11512                                  context_die);
11513
11514   if (sub_die != NULL)
11515     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11516
11517   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11518   if (TYPE_ARTIFICIAL (type))
11519     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11520
11521   return mod_type_die;
11522 }
11523
11524 /* Generate DIEs for the generic parameters of T.
11525    T must be either a generic type or a generic function.
11526    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
11527
11528 static void
11529 gen_generic_params_dies (tree t)
11530 {
11531   tree parms, args;
11532   int parms_num, i;
11533   dw_die_ref die = NULL;
11534   int non_default;
11535
11536   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11537     return;
11538
11539   if (TYPE_P (t))
11540     die = lookup_type_die (t);
11541   else if (DECL_P (t))
11542     die = lookup_decl_die (t);
11543
11544   gcc_assert (die);
11545
11546   parms = lang_hooks.get_innermost_generic_parms (t);
11547   if (!parms)
11548     /* T has no generic parameter. It means T is neither a generic type
11549        or function. End of story.  */
11550     return;
11551
11552   parms_num = TREE_VEC_LENGTH (parms);
11553   args = lang_hooks.get_innermost_generic_args (t);
11554   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11555     non_default = int_cst_value (TREE_CHAIN (args));
11556   else
11557     non_default = TREE_VEC_LENGTH (args);
11558   for (i = 0; i < parms_num; i++)
11559     {
11560       tree parm, arg, arg_pack_elems;
11561       dw_die_ref parm_die;
11562
11563       parm = TREE_VEC_ELT (parms, i);
11564       arg = TREE_VEC_ELT (args, i);
11565       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11566       gcc_assert (parm && TREE_VALUE (parm) && arg);
11567
11568       if (parm && TREE_VALUE (parm) && arg)
11569         {
11570           /* If PARM represents a template parameter pack,
11571              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11572              by DW_TAG_template_*_parameter DIEs for the argument
11573              pack elements of ARG. Note that ARG would then be
11574              an argument pack.  */
11575           if (arg_pack_elems)
11576             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11577                                                     arg_pack_elems,
11578                                                     die);
11579           else
11580             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11581                                               true /* emit name */, die);
11582           if (i >= non_default)
11583             add_AT_flag (parm_die, DW_AT_default_value, 1);
11584         }
11585     }
11586 }
11587
11588 /* Create and return a DIE for PARM which should be
11589    the representation of a generic type parameter.
11590    For instance, in the C++ front end, PARM would be a template parameter.
11591    ARG is the argument to PARM.
11592    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11593    name of the PARM.
11594    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11595    as a child node.  */
11596
11597 static dw_die_ref
11598 generic_parameter_die (tree parm, tree arg,
11599                        bool emit_name_p,
11600                        dw_die_ref parent_die)
11601 {
11602   dw_die_ref tmpl_die = NULL;
11603   const char *name = NULL;
11604
11605   if (!parm || !DECL_NAME (parm) || !arg)
11606     return NULL;
11607
11608   /* We support non-type generic parameters and arguments,
11609      type generic parameters and arguments, as well as
11610      generic generic parameters (a.k.a. template template parameters in C++)
11611      and arguments.  */
11612   if (TREE_CODE (parm) == PARM_DECL)
11613     /* PARM is a nontype generic parameter  */
11614     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11615   else if (TREE_CODE (parm) == TYPE_DECL)
11616     /* PARM is a type generic parameter.  */
11617     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11618   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11619     /* PARM is a generic generic parameter.
11620        Its DIE is a GNU extension. It shall have a
11621        DW_AT_name attribute to represent the name of the template template
11622        parameter, and a DW_AT_GNU_template_name attribute to represent the
11623        name of the template template argument.  */
11624     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11625                         parent_die, parm);
11626   else
11627     gcc_unreachable ();
11628
11629   if (tmpl_die)
11630     {
11631       tree tmpl_type;
11632
11633       /* If PARM is a generic parameter pack, it means we are
11634          emitting debug info for a template argument pack element.
11635          In other terms, ARG is a template argument pack element.
11636          In that case, we don't emit any DW_AT_name attribute for
11637          the die.  */
11638       if (emit_name_p)
11639         {
11640           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11641           gcc_assert (name);
11642           add_AT_string (tmpl_die, DW_AT_name, name);
11643         }
11644
11645       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11646         {
11647           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11648              TMPL_DIE should have a child DW_AT_type attribute that is set
11649              to the type of the argument to PARM, which is ARG.
11650              If PARM is a type generic parameter, TMPL_DIE should have a
11651              child DW_AT_type that is set to ARG.  */
11652           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11653           add_type_attribute (tmpl_die, tmpl_type,
11654                               (TREE_THIS_VOLATILE (tmpl_type)
11655                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11656                               false, parent_die);
11657         }
11658       else
11659         {
11660           /* So TMPL_DIE is a DIE representing a
11661              a generic generic template parameter, a.k.a template template
11662              parameter in C++ and arg is a template.  */
11663
11664           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11665              to the name of the argument.  */
11666           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11667           if (name)
11668             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11669         }
11670
11671       if (TREE_CODE (parm) == PARM_DECL)
11672         /* So PARM is a non-type generic parameter.
11673            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11674            attribute of TMPL_DIE which value represents the value
11675            of ARG.
11676            We must be careful here:
11677            The value of ARG might reference some function decls.
11678            We might currently be emitting debug info for a generic
11679            type and types are emitted before function decls, we don't
11680            know if the function decls referenced by ARG will actually be
11681            emitted after cgraph computations.
11682            So must defer the generation of the DW_AT_const_value to
11683            after cgraph is ready.  */
11684         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11685     }
11686
11687   return tmpl_die;
11688 }
11689
11690 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11691    PARM_PACK must be a template parameter pack. The returned DIE
11692    will be child DIE of PARENT_DIE.  */
11693
11694 static dw_die_ref
11695 template_parameter_pack_die (tree parm_pack,
11696                              tree parm_pack_args,
11697                              dw_die_ref parent_die)
11698 {
11699   dw_die_ref die;
11700   int j;
11701
11702   gcc_assert (parent_die && parm_pack);
11703
11704   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11705   add_name_and_src_coords_attributes (die, parm_pack);
11706   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11707     generic_parameter_die (parm_pack,
11708                            TREE_VEC_ELT (parm_pack_args, j),
11709                            false /* Don't emit DW_AT_name */,
11710                            die);
11711   return die;
11712 }
11713
11714 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11715    an enumerated type.  */
11716
11717 static inline int
11718 type_is_enum (const_tree type)
11719 {
11720   return TREE_CODE (type) == ENUMERAL_TYPE;
11721 }
11722
11723 /* Return the DBX register number described by a given RTL node.  */
11724
11725 static unsigned int
11726 dbx_reg_number (const_rtx rtl)
11727 {
11728   unsigned regno = REGNO (rtl);
11729
11730   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11731
11732 #ifdef LEAF_REG_REMAP
11733   if (crtl->uses_only_leaf_regs)
11734     {
11735       int leaf_reg = LEAF_REG_REMAP (regno);
11736       if (leaf_reg != -1)
11737         regno = (unsigned) leaf_reg;
11738     }
11739 #endif
11740
11741   regno = DBX_REGISTER_NUMBER (regno);
11742   gcc_assert (regno != INVALID_REGNUM);
11743   return regno;
11744 }
11745
11746 /* Optionally add a DW_OP_piece term to a location description expression.
11747    DW_OP_piece is only added if the location description expression already
11748    doesn't end with DW_OP_piece.  */
11749
11750 static void
11751 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11752 {
11753   dw_loc_descr_ref loc;
11754
11755   if (*list_head != NULL)
11756     {
11757       /* Find the end of the chain.  */
11758       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11759         ;
11760
11761       if (loc->dw_loc_opc != DW_OP_piece)
11762         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11763     }
11764 }
11765
11766 /* Return a location descriptor that designates a machine register or
11767    zero if there is none.  */
11768
11769 static dw_loc_descr_ref
11770 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11771 {
11772   rtx regs;
11773
11774   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11775     return 0;
11776
11777   /* We only use "frame base" when we're sure we're talking about the
11778      post-prologue local stack frame.  We do this by *not* running
11779      register elimination until this point, and recognizing the special
11780      argument pointer and soft frame pointer rtx's.
11781      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11782   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11783       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11784     {
11785       dw_loc_descr_ref result = NULL;
11786
11787       if (dwarf_version >= 4 || !dwarf_strict)
11788         {
11789           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11790                                        initialized);
11791           if (result)
11792             add_loc_descr (&result,
11793                            new_loc_descr (DW_OP_stack_value, 0, 0));
11794         }
11795       return result;
11796     }
11797
11798   regs = targetm.dwarf_register_span (rtl);
11799
11800   if (REG_NREGS (rtl) > 1 || regs)
11801     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11802   else
11803     {
11804       unsigned int dbx_regnum = dbx_reg_number (rtl);
11805       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11806         return 0;
11807       return one_reg_loc_descriptor (dbx_regnum, initialized);
11808     }
11809 }
11810
11811 /* Return a location descriptor that designates a machine register for
11812    a given hard register number.  */
11813
11814 static dw_loc_descr_ref
11815 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11816 {
11817   dw_loc_descr_ref reg_loc_descr;
11818
11819   if (regno <= 31)
11820     reg_loc_descr
11821       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11822   else
11823     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11824
11825   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11826     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11827
11828   return reg_loc_descr;
11829 }
11830
11831 /* Given an RTL of a register, return a location descriptor that
11832    designates a value that spans more than one register.  */
11833
11834 static dw_loc_descr_ref
11835 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11836                              enum var_init_status initialized)
11837 {
11838   int size, i;
11839   dw_loc_descr_ref loc_result = NULL;
11840
11841   /* Simple, contiguous registers.  */
11842   if (regs == NULL_RTX)
11843     {
11844       unsigned reg = REGNO (rtl);
11845       int nregs;
11846
11847 #ifdef LEAF_REG_REMAP
11848       if (crtl->uses_only_leaf_regs)
11849         {
11850           int leaf_reg = LEAF_REG_REMAP (reg);
11851           if (leaf_reg != -1)
11852             reg = (unsigned) leaf_reg;
11853         }
11854 #endif
11855
11856       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11857       nregs = REG_NREGS (rtl);
11858
11859       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11860
11861       loc_result = NULL;
11862       while (nregs--)
11863         {
11864           dw_loc_descr_ref t;
11865
11866           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11867                                       VAR_INIT_STATUS_INITIALIZED);
11868           add_loc_descr (&loc_result, t);
11869           add_loc_descr_op_piece (&loc_result, size);
11870           ++reg;
11871         }
11872       return loc_result;
11873     }
11874
11875   /* Now onto stupid register sets in non contiguous locations.  */
11876
11877   gcc_assert (GET_CODE (regs) == PARALLEL);
11878
11879   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11880   loc_result = NULL;
11881
11882   for (i = 0; i < XVECLEN (regs, 0); ++i)
11883     {
11884       dw_loc_descr_ref t;
11885
11886       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11887                                   VAR_INIT_STATUS_INITIALIZED);
11888       add_loc_descr (&loc_result, t);
11889       add_loc_descr_op_piece (&loc_result, size);
11890     }
11891
11892   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11893     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11894   return loc_result;
11895 }
11896
11897 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11898
11899 /* Return a location descriptor that designates a constant i,
11900    as a compound operation from constant (i >> shift), constant shift
11901    and DW_OP_shl.  */
11902
11903 static dw_loc_descr_ref
11904 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11905 {
11906   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11907   add_loc_descr (&ret, int_loc_descriptor (shift));
11908   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11909   return ret;
11910 }
11911
11912 /* Return a location descriptor that designates a constant.  */
11913
11914 static dw_loc_descr_ref
11915 int_loc_descriptor (HOST_WIDE_INT i)
11916 {
11917   enum dwarf_location_atom op;
11918
11919   /* Pick the smallest representation of a constant, rather than just
11920      defaulting to the LEB encoding.  */
11921   if (i >= 0)
11922     {
11923       int clz = clz_hwi (i);
11924       int ctz = ctz_hwi (i);
11925       if (i <= 31)
11926         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11927       else if (i <= 0xff)
11928         op = DW_OP_const1u;
11929       else if (i <= 0xffff)
11930         op = DW_OP_const2u;
11931       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11932                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11933         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11934            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11935            while DW_OP_const4u is 5 bytes.  */
11936         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11937       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11938                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11939         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11940            while DW_OP_const4u is 5 bytes.  */
11941         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11942       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11943         op = DW_OP_const4u;
11944       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11945                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11946         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11947            while DW_OP_constu of constant >= 0x100000000 takes at least
11948            6 bytes.  */
11949         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11950       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11951                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11952                   >= HOST_BITS_PER_WIDE_INT)
11953         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11954            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11955            while DW_OP_constu takes in this case at least 6 bytes.  */
11956         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11957       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11958                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11959                && size_of_uleb128 (i) > 6)
11960         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11961         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11962       else
11963         op = DW_OP_constu;
11964     }
11965   else
11966     {
11967       if (i >= -0x80)
11968         op = DW_OP_const1s;
11969       else if (i >= -0x8000)
11970         op = DW_OP_const2s;
11971       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11972         {
11973           if (size_of_int_loc_descriptor (i) < 5)
11974             {
11975               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11976               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11977               return ret;
11978             }
11979           op = DW_OP_const4s;
11980         }
11981       else
11982         {
11983           if (size_of_int_loc_descriptor (i)
11984               < (unsigned long) 1 + size_of_sleb128 (i))
11985             {
11986               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11987               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11988               return ret;
11989             }
11990           op = DW_OP_consts;
11991         }
11992     }
11993
11994   return new_loc_descr (op, i, 0);
11995 }
11996
11997 /* Likewise, for unsigned constants.  */
11998
11999 static dw_loc_descr_ref
12000 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12001 {
12002   const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12003   const unsigned HOST_WIDE_INT max_uint
12004     = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12005
12006   /* If possible, use the clever signed constants handling.  */
12007   if (i <= max_int)
12008     return int_loc_descriptor ((HOST_WIDE_INT) i);
12009
12010   /* Here, we are left with positive numbers that cannot be represented as
12011      HOST_WIDE_INT, i.e.:
12012          max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12013
12014      Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12015      whereas may be better to output a negative integer: thanks to integer
12016      wrapping, we know that:
12017          x = x - 2 ** DWARF2_ADDR_SIZE
12018            = x - 2 * (max (HOST_WIDE_INT) + 1)
12019      So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12020      small negative integers.  Let's try that in cases it will clearly improve
12021      the encoding: there is no gain turning DW_OP_const4u into
12022      DW_OP_const4s.  */
12023   if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12024       && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12025           || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12026     {
12027       const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12028
12029       /* Now, -1 <  first_shift <= max (HOST_WIDE_INT)
12030          i.e.  0 <= first_shift <= max (HOST_WIDE_INT).  */
12031       const HOST_WIDE_INT second_shift
12032         = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12033
12034       /* So we finally have:
12035               -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12036          i.e.  min (HOST_WIDE_INT)     <= second_shift <  0.  */
12037       return int_loc_descriptor (second_shift);
12038     }
12039
12040   /* Last chance: fallback to a simple constant operation.  */
12041   return new_loc_descr
12042      ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12043       ? DW_OP_const4u
12044       : DW_OP_const8u,
12045       i, 0);
12046 }
12047
12048 /* Generate and return a location description that computes the unsigned
12049    comparison of the two stack top entries (a OP b where b is the top-most
12050    entry and a is the second one).  The KIND of comparison can be LT_EXPR,
12051    LE_EXPR, GT_EXPR or GE_EXPR.  */
12052
12053 static dw_loc_descr_ref
12054 uint_comparison_loc_list (enum tree_code kind)
12055 {
12056   enum dwarf_location_atom op, flip_op;
12057   dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12058
12059   switch (kind)
12060     {
12061     case LT_EXPR:
12062       op = DW_OP_lt;
12063       break;
12064     case LE_EXPR:
12065       op = DW_OP_le;
12066       break;
12067     case GT_EXPR:
12068       op = DW_OP_gt;
12069       break;
12070     case GE_EXPR:
12071       op = DW_OP_ge;
12072       break;
12073     default:
12074       gcc_unreachable ();
12075     }
12076
12077   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12078   jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12079
12080   /* Until DWARFv4, operations all work on signed integers.  It is nevertheless
12081      possible to perform unsigned comparisons: we just have to distinguish
12082      three cases:
12083
12084        1. when a and b have the same sign (as signed integers); then we should
12085           return: a OP(signed) b;
12086
12087        2. when a is a negative signed integer while b is a positive one, then a
12088           is a greater unsigned integer than b; likewise when a and b's roles
12089           are flipped.
12090
12091      So first, compare the sign of the two operands.  */
12092   ret = new_loc_descr (DW_OP_over, 0, 0);
12093   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12094   add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12095   /* If they have different signs (i.e. they have different sign bits), then
12096      the stack top value has now the sign bit set and thus it's smaller than
12097      zero.  */
12098   add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12099   add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12100   add_loc_descr (&ret, bra_node);
12101
12102   /* We are in case 1.  At this point, we know both operands have the same
12103      sign, to it's safe to use the built-in signed comparison.  */
12104   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12105   add_loc_descr (&ret, jmp_node);
12106
12107   /* We are in case 2.  Here, we know both operands do not have the same sign,
12108      so we have to flip the signed comparison.  */
12109   flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12110   tmp = new_loc_descr (flip_op, 0, 0);
12111   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12112   bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12113   add_loc_descr (&ret, tmp);
12114
12115   /* This dummy operation is necessary to make the two branches join.  */
12116   tmp = new_loc_descr (DW_OP_nop, 0, 0);
12117   jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12118   jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12119   add_loc_descr (&ret, tmp);
12120
12121   return ret;
12122 }
12123
12124 /* Likewise, but takes the location description lists (might be destructive on
12125    them).  Return NULL if either is NULL or if concatenation fails.  */
12126
12127 static dw_loc_list_ref
12128 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12129                                enum tree_code kind)
12130 {
12131   if (left == NULL || right == NULL)
12132     return NULL;
12133
12134   add_loc_list (&left, right);
12135   if (left == NULL)
12136     return NULL;
12137
12138   add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12139   return left;
12140 }
12141
12142 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12143    without actually allocating it.  */
12144
12145 static unsigned long
12146 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12147 {
12148   return size_of_int_loc_descriptor (i >> shift)
12149          + size_of_int_loc_descriptor (shift)
12150          + 1;
12151 }
12152
12153 /* Return size_of_locs (int_loc_descriptor (i)) without
12154    actually allocating it.  */
12155
12156 static unsigned long
12157 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12158 {
12159   unsigned long s;
12160
12161   if (i >= 0)
12162     {
12163       int clz, ctz;
12164       if (i <= 31)
12165         return 1;
12166       else if (i <= 0xff)
12167         return 2;
12168       else if (i <= 0xffff)
12169         return 3;
12170       clz = clz_hwi (i);
12171       ctz = ctz_hwi (i);
12172       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12173           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12174         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12175                                                     - clz - 5);
12176       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12177                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12178         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12179                                                     - clz - 8);
12180       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12181         return 5;
12182       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12183       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12184           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12185         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12186                                                     - clz - 8);
12187       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12188                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12189         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12190                                                     - clz - 16);
12191       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12192                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12193                && s > 6)
12194         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12195                                                     - clz - 32);
12196       else
12197         return 1 + s;
12198     }
12199   else
12200     {
12201       if (i >= -0x80)
12202         return 2;
12203       else if (i >= -0x8000)
12204         return 3;
12205       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12206         {
12207           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12208             {
12209               s = size_of_int_loc_descriptor (-i) + 1;
12210               if (s < 5)
12211                 return s;
12212             }
12213           return 5;
12214         }
12215       else
12216         {
12217           unsigned long r = 1 + size_of_sleb128 (i);
12218           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12219             {
12220               s = size_of_int_loc_descriptor (-i) + 1;
12221               if (s < r)
12222                 return s;
12223             }
12224           return r;
12225         }
12226     }
12227 }
12228
12229 /* Return loc description representing "address" of integer value.
12230    This can appear only as toplevel expression.  */
12231
12232 static dw_loc_descr_ref
12233 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12234 {
12235   int litsize;
12236   dw_loc_descr_ref loc_result = NULL;
12237
12238   if (!(dwarf_version >= 4 || !dwarf_strict))
12239     return NULL;
12240
12241   litsize = size_of_int_loc_descriptor (i);
12242   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12243      is more compact.  For DW_OP_stack_value we need:
12244      litsize + 1 (DW_OP_stack_value)
12245      and for DW_OP_implicit_value:
12246      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12247   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12248     {
12249       loc_result = int_loc_descriptor (i);
12250       add_loc_descr (&loc_result,
12251                      new_loc_descr (DW_OP_stack_value, 0, 0));
12252       return loc_result;
12253     }
12254
12255   loc_result = new_loc_descr (DW_OP_implicit_value,
12256                               size, 0);
12257   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12258   loc_result->dw_loc_oprnd2.v.val_int = i;
12259   return loc_result;
12260 }
12261
12262 /* Return a location descriptor that designates a base+offset location.  */
12263
12264 static dw_loc_descr_ref
12265 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12266                  enum var_init_status initialized)
12267 {
12268   unsigned int regno;
12269   dw_loc_descr_ref result;
12270   dw_fde_ref fde = cfun->fde;
12271
12272   /* We only use "frame base" when we're sure we're talking about the
12273      post-prologue local stack frame.  We do this by *not* running
12274      register elimination until this point, and recognizing the special
12275      argument pointer and soft frame pointer rtx's.  */
12276   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12277     {
12278       rtx elim = (ira_use_lra_p
12279                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12280                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
12281
12282       if (elim != reg)
12283         {
12284           if (GET_CODE (elim) == PLUS)
12285             {
12286               offset += INTVAL (XEXP (elim, 1));
12287               elim = XEXP (elim, 0);
12288             }
12289           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12290                        && (elim == hard_frame_pointer_rtx
12291                            || elim == stack_pointer_rtx))
12292                       || elim == (frame_pointer_needed
12293                                   ? hard_frame_pointer_rtx
12294                                   : stack_pointer_rtx));
12295
12296           /* If drap register is used to align stack, use frame
12297              pointer + offset to access stack variables.  If stack
12298              is aligned without drap, use stack pointer + offset to
12299              access stack variables.  */
12300           if (crtl->stack_realign_tried
12301               && reg == frame_pointer_rtx)
12302             {
12303               int base_reg
12304                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12305                                       ? HARD_FRAME_POINTER_REGNUM
12306                                       : REGNO (elim));
12307               return new_reg_loc_descr (base_reg, offset);
12308             }
12309
12310           gcc_assert (frame_pointer_fb_offset_valid);
12311           offset += frame_pointer_fb_offset;
12312           return new_loc_descr (DW_OP_fbreg, offset, 0);
12313         }
12314     }
12315
12316   regno = REGNO (reg);
12317 #ifdef LEAF_REG_REMAP
12318   if (crtl->uses_only_leaf_regs)
12319     {
12320       int leaf_reg = LEAF_REG_REMAP (regno);
12321       if (leaf_reg != -1)
12322         regno = (unsigned) leaf_reg;
12323     }
12324 #endif
12325   regno = DWARF_FRAME_REGNUM (regno);
12326
12327   if (!optimize && fde
12328       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12329     {
12330       /* Use cfa+offset to represent the location of arguments passed
12331          on the stack when drap is used to align stack.
12332          Only do this when not optimizing, for optimized code var-tracking
12333          is supposed to track where the arguments live and the register
12334          used as vdrap or drap in some spot might be used for something
12335          else in other part of the routine.  */
12336       return new_loc_descr (DW_OP_fbreg, offset, 0);
12337     }
12338
12339   if (regno <= 31)
12340     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12341                             offset, 0);
12342   else
12343     result = new_loc_descr (DW_OP_bregx, regno, offset);
12344
12345   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12346     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12347
12348   return result;
12349 }
12350
12351 /* Return true if this RTL expression describes a base+offset calculation.  */
12352
12353 static inline int
12354 is_based_loc (const_rtx rtl)
12355 {
12356   return (GET_CODE (rtl) == PLUS
12357           && ((REG_P (XEXP (rtl, 0))
12358                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12359                && CONST_INT_P (XEXP (rtl, 1)))));
12360 }
12361
12362 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12363    failed.  */
12364
12365 static dw_loc_descr_ref
12366 tls_mem_loc_descriptor (rtx mem)
12367 {
12368   tree base;
12369   dw_loc_descr_ref loc_result;
12370
12371   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12372     return NULL;
12373
12374   base = get_base_address (MEM_EXPR (mem));
12375   if (base == NULL
12376       || TREE_CODE (base) != VAR_DECL
12377       || !DECL_THREAD_LOCAL_P (base))
12378     return NULL;
12379
12380   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12381   if (loc_result == NULL)
12382     return NULL;
12383
12384   if (MEM_OFFSET (mem))
12385     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12386
12387   return loc_result;
12388 }
12389
12390 /* Output debug info about reason why we failed to expand expression as dwarf
12391    expression.  */
12392
12393 static void
12394 expansion_failed (tree expr, rtx rtl, char const *reason)
12395 {
12396   if (dump_file && (dump_flags & TDF_DETAILS))
12397     {
12398       fprintf (dump_file, "Failed to expand as dwarf: ");
12399       if (expr)
12400         print_generic_expr (dump_file, expr, dump_flags);
12401       if (rtl)
12402         {
12403           fprintf (dump_file, "\n");
12404           print_rtl (dump_file, rtl);
12405         }
12406       fprintf (dump_file, "\nReason: %s\n", reason);
12407     }
12408 }
12409
12410 /* Helper function for const_ok_for_output.  */
12411
12412 static bool
12413 const_ok_for_output_1 (rtx rtl)
12414 {
12415   if (GET_CODE (rtl) == UNSPEC)
12416     {
12417       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12418          we can't express it in the debug info.  */
12419       /* Don't complain about TLS UNSPECs, those are just too hard to
12420          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
12421          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12422          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
12423       if (flag_checking
12424           && (XVECLEN (rtl, 0) == 0
12425               || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12426               || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12427         inform (current_function_decl
12428                 ? DECL_SOURCE_LOCATION (current_function_decl)
12429                 : UNKNOWN_LOCATION,
12430 #if NUM_UNSPEC_VALUES > 0
12431                 "non-delegitimized UNSPEC %s (%d) found in variable location",
12432                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12433                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12434                 XINT (rtl, 1));
12435 #else
12436                 "non-delegitimized UNSPEC %d found in variable location",
12437                 XINT (rtl, 1));
12438 #endif
12439       expansion_failed (NULL_TREE, rtl,
12440                         "UNSPEC hasn't been delegitimized.\n");
12441       return false;
12442     }
12443
12444   if (targetm.const_not_ok_for_debug_p (rtl))
12445     {
12446       expansion_failed (NULL_TREE, rtl,
12447                         "Expression rejected for debug by the backend.\n");
12448       return false;
12449     }
12450
12451   /* FIXME: Refer to PR60655. It is possible for simplification
12452      of rtl expressions in var tracking to produce such expressions.
12453      We should really identify / validate expressions
12454      enclosed in CONST that can be handled by assemblers on various
12455      targets and only handle legitimate cases here.  */
12456   if (GET_CODE (rtl) != SYMBOL_REF)
12457     {
12458       if (GET_CODE (rtl) == NOT)
12459         return false;
12460       return true;
12461     }
12462
12463   if (CONSTANT_POOL_ADDRESS_P (rtl))
12464     {
12465       bool marked;
12466       get_pool_constant_mark (rtl, &marked);
12467       /* If all references to this pool constant were optimized away,
12468          it was not output and thus we can't represent it.  */
12469       if (!marked)
12470         {
12471           expansion_failed (NULL_TREE, rtl,
12472                             "Constant was removed from constant pool.\n");
12473           return false;
12474         }
12475     }
12476
12477   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12478     return false;
12479
12480   /* Avoid references to external symbols in debug info, on several targets
12481      the linker might even refuse to link when linking a shared library,
12482      and in many other cases the relocations for .debug_info/.debug_loc are
12483      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12484      to be defined within the same shared library or executable are fine.  */
12485   if (SYMBOL_REF_EXTERNAL_P (rtl))
12486     {
12487       tree decl = SYMBOL_REF_DECL (rtl);
12488
12489       if (decl == NULL || !targetm.binds_local_p (decl))
12490         {
12491           expansion_failed (NULL_TREE, rtl,
12492                             "Symbol not defined in current TU.\n");
12493           return false;
12494         }
12495     }
12496
12497   return true;
12498 }
12499
12500 /* Return true if constant RTL can be emitted in DW_OP_addr or
12501    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12502    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12503
12504 static bool
12505 const_ok_for_output (rtx rtl)
12506 {
12507   if (GET_CODE (rtl) == SYMBOL_REF)
12508     return const_ok_for_output_1 (rtl);
12509
12510   if (GET_CODE (rtl) == CONST)
12511     {
12512       subrtx_var_iterator::array_type array;
12513       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12514         if (!const_ok_for_output_1 (*iter))
12515           return false;
12516       return true;
12517     }
12518
12519   return true;
12520 }
12521
12522 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12523    if possible, NULL otherwise.  */
12524
12525 static dw_die_ref
12526 base_type_for_mode (machine_mode mode, bool unsignedp)
12527 {
12528   dw_die_ref type_die;
12529   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12530
12531   if (type == NULL)
12532     return NULL;
12533   switch (TREE_CODE (type))
12534     {
12535     case INTEGER_TYPE:
12536     case REAL_TYPE:
12537       break;
12538     default:
12539       return NULL;
12540     }
12541   type_die = lookup_type_die (type);
12542   if (!type_die)
12543     type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12544                                   comp_unit_die ());
12545   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12546     return NULL;
12547   return type_die;
12548 }
12549
12550 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12551    type matching MODE, or, if MODE is narrower than or as wide as
12552    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
12553    possible.  */
12554
12555 static dw_loc_descr_ref
12556 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12557 {
12558   machine_mode outer_mode = mode;
12559   dw_die_ref type_die;
12560   dw_loc_descr_ref cvt;
12561
12562   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12563     {
12564       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12565       return op;
12566     }
12567   type_die = base_type_for_mode (outer_mode, 1);
12568   if (type_die == NULL)
12569     return NULL;
12570   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12571   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12572   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12573   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12574   add_loc_descr (&op, cvt);
12575   return op;
12576 }
12577
12578 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
12579
12580 static dw_loc_descr_ref
12581 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12582                         dw_loc_descr_ref op1)
12583 {
12584   dw_loc_descr_ref ret = op0;
12585   add_loc_descr (&ret, op1);
12586   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12587   if (STORE_FLAG_VALUE != 1)
12588     {
12589       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12590       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12591     }
12592   return ret;
12593 }
12594
12595 /* Return location descriptor for signed comparison OP RTL.  */
12596
12597 static dw_loc_descr_ref
12598 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12599                          machine_mode mem_mode)
12600 {
12601   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12602   dw_loc_descr_ref op0, op1;
12603   int shift;
12604
12605   if (op_mode == VOIDmode)
12606     op_mode = GET_MODE (XEXP (rtl, 1));
12607   if (op_mode == VOIDmode)
12608     return NULL;
12609
12610   if (dwarf_strict
12611       && (GET_MODE_CLASS (op_mode) != MODE_INT
12612           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12613     return NULL;
12614
12615   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12616                             VAR_INIT_STATUS_INITIALIZED);
12617   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12618                             VAR_INIT_STATUS_INITIALIZED);
12619
12620   if (op0 == NULL || op1 == NULL)
12621     return NULL;
12622
12623   if (GET_MODE_CLASS (op_mode) != MODE_INT
12624       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12625     return compare_loc_descriptor (op, op0, op1);
12626
12627   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12628     {
12629       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12630       dw_loc_descr_ref cvt;
12631
12632       if (type_die == NULL)
12633         return NULL;
12634       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12635       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12636       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12637       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12638       add_loc_descr (&op0, cvt);
12639       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12640       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12641       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12642       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12643       add_loc_descr (&op1, cvt);
12644       return compare_loc_descriptor (op, op0, op1);
12645     }
12646
12647   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12648   /* For eq/ne, if the operands are known to be zero-extended,
12649      there is no need to do the fancy shifting up.  */
12650   if (op == DW_OP_eq || op == DW_OP_ne)
12651     {
12652       dw_loc_descr_ref last0, last1;
12653       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12654         ;
12655       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12656         ;
12657       /* deref_size zero extends, and for constants we can check
12658          whether they are zero extended or not.  */
12659       if (((last0->dw_loc_opc == DW_OP_deref_size
12660             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12661            || (CONST_INT_P (XEXP (rtl, 0))
12662                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12663                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12664           && ((last1->dw_loc_opc == DW_OP_deref_size
12665                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12666               || (CONST_INT_P (XEXP (rtl, 1))
12667                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12668                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12669         return compare_loc_descriptor (op, op0, op1);
12670
12671       /* EQ/NE comparison against constant in narrower type than
12672          DWARF2_ADDR_SIZE can be performed either as
12673          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12674          DW_OP_{eq,ne}
12675          or
12676          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12677          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
12678       if (CONST_INT_P (XEXP (rtl, 1))
12679           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12680           && (size_of_int_loc_descriptor (shift) + 1
12681               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12682               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12683                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12684                                                & GET_MODE_MASK (op_mode))))
12685         {
12686           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12687           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12688           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12689                                     & GET_MODE_MASK (op_mode));
12690           return compare_loc_descriptor (op, op0, op1);
12691         }
12692     }
12693   add_loc_descr (&op0, int_loc_descriptor (shift));
12694   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12695   if (CONST_INT_P (XEXP (rtl, 1)))
12696     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12697   else
12698     {
12699       add_loc_descr (&op1, int_loc_descriptor (shift));
12700       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12701     }
12702   return compare_loc_descriptor (op, op0, op1);
12703 }
12704
12705 /* Return location descriptor for unsigned comparison OP RTL.  */
12706
12707 static dw_loc_descr_ref
12708 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12709                          machine_mode mem_mode)
12710 {
12711   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12712   dw_loc_descr_ref op0, op1;
12713
12714   if (op_mode == VOIDmode)
12715     op_mode = GET_MODE (XEXP (rtl, 1));
12716   if (op_mode == VOIDmode)
12717     return NULL;
12718   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12719     return NULL;
12720
12721   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12722     return NULL;
12723
12724   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12725                             VAR_INIT_STATUS_INITIALIZED);
12726   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12727                             VAR_INIT_STATUS_INITIALIZED);
12728
12729   if (op0 == NULL || op1 == NULL)
12730     return NULL;
12731
12732   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12733     {
12734       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12735       dw_loc_descr_ref last0, last1;
12736       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12737         ;
12738       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12739         ;
12740       if (CONST_INT_P (XEXP (rtl, 0)))
12741         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12742       /* deref_size zero extends, so no need to mask it again.  */
12743       else if (last0->dw_loc_opc != DW_OP_deref_size
12744                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12745         {
12746           add_loc_descr (&op0, int_loc_descriptor (mask));
12747           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12748         }
12749       if (CONST_INT_P (XEXP (rtl, 1)))
12750         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12751       /* deref_size zero extends, so no need to mask it again.  */
12752       else if (last1->dw_loc_opc != DW_OP_deref_size
12753                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12754         {
12755           add_loc_descr (&op1, int_loc_descriptor (mask));
12756           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12757         }
12758     }
12759   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12760     {
12761       HOST_WIDE_INT bias = 1;
12762       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12763       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12764       if (CONST_INT_P (XEXP (rtl, 1)))
12765         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12766                                   + INTVAL (XEXP (rtl, 1)));
12767       else
12768         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12769                                             bias, 0));
12770     }
12771   return compare_loc_descriptor (op, op0, op1);
12772 }
12773
12774 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12775
12776 static dw_loc_descr_ref
12777 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12778                        machine_mode mem_mode)
12779 {
12780   enum dwarf_location_atom op;
12781   dw_loc_descr_ref op0, op1, ret;
12782   dw_loc_descr_ref bra_node, drop_node;
12783
12784   if (dwarf_strict
12785       && (GET_MODE_CLASS (mode) != MODE_INT
12786           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12787     return NULL;
12788
12789   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12790                             VAR_INIT_STATUS_INITIALIZED);
12791   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12792                             VAR_INIT_STATUS_INITIALIZED);
12793
12794   if (op0 == NULL || op1 == NULL)
12795     return NULL;
12796
12797   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12798   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12799   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12800   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12801     {
12802       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12803         {
12804           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12805           add_loc_descr (&op0, int_loc_descriptor (mask));
12806           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12807           add_loc_descr (&op1, int_loc_descriptor (mask));
12808           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12809         }
12810       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12811         {
12812           HOST_WIDE_INT bias = 1;
12813           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12814           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12815           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12816         }
12817     }
12818   else if (GET_MODE_CLASS (mode) == MODE_INT
12819            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12820     {
12821       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12822       add_loc_descr (&op0, int_loc_descriptor (shift));
12823       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12824       add_loc_descr (&op1, int_loc_descriptor (shift));
12825       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12826     }
12827   else if (GET_MODE_CLASS (mode) == MODE_INT
12828            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12829     {
12830       dw_die_ref type_die = base_type_for_mode (mode, 0);
12831       dw_loc_descr_ref cvt;
12832       if (type_die == NULL)
12833         return NULL;
12834       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12835       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12836       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12837       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12838       add_loc_descr (&op0, cvt);
12839       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12840       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12841       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12842       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12843       add_loc_descr (&op1, cvt);
12844     }
12845
12846   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12847     op = DW_OP_lt;
12848   else
12849     op = DW_OP_gt;
12850   ret = op0;
12851   add_loc_descr (&ret, op1);
12852   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12853   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12854   add_loc_descr (&ret, bra_node);
12855   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12856   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12857   add_loc_descr (&ret, drop_node);
12858   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12859   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12860   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12861       && GET_MODE_CLASS (mode) == MODE_INT
12862       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12863     ret = convert_descriptor_to_mode (mode, ret);
12864   return ret;
12865 }
12866
12867 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12868    but after converting arguments to type_die, afterwards
12869    convert back to unsigned.  */
12870
12871 static dw_loc_descr_ref
12872 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12873              machine_mode mode, machine_mode mem_mode)
12874 {
12875   dw_loc_descr_ref cvt, op0, op1;
12876
12877   if (type_die == NULL)
12878     return NULL;
12879   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12880                             VAR_INIT_STATUS_INITIALIZED);
12881   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12882                             VAR_INIT_STATUS_INITIALIZED);
12883   if (op0 == NULL || op1 == NULL)
12884     return NULL;
12885   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12886   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12887   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12888   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12889   add_loc_descr (&op0, cvt);
12890   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12891   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12892   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12893   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12894   add_loc_descr (&op1, cvt);
12895   add_loc_descr (&op0, op1);
12896   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12897   return convert_descriptor_to_mode (mode, op0);
12898 }
12899
12900 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12901    const0 is DW_OP_lit0 or corresponding typed constant,
12902    const1 is DW_OP_lit1 or corresponding typed constant
12903    and constMSB is constant with just the MSB bit set
12904    for the mode):
12905        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12906    L1: const0 DW_OP_swap
12907    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12908        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12909    L3: DW_OP_drop
12910    L4: DW_OP_nop
12911
12912    CTZ is similar:
12913        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12914    L1: const0 DW_OP_swap
12915    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12916        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12917    L3: DW_OP_drop
12918    L4: DW_OP_nop
12919
12920    FFS is similar:
12921        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12922    L1: const1 DW_OP_swap
12923    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12924        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12925    L3: DW_OP_drop
12926    L4: DW_OP_nop  */
12927
12928 static dw_loc_descr_ref
12929 clz_loc_descriptor (rtx rtl, machine_mode mode,
12930                     machine_mode mem_mode)
12931 {
12932   dw_loc_descr_ref op0, ret, tmp;
12933   HOST_WIDE_INT valv;
12934   dw_loc_descr_ref l1jump, l1label;
12935   dw_loc_descr_ref l2jump, l2label;
12936   dw_loc_descr_ref l3jump, l3label;
12937   dw_loc_descr_ref l4jump, l4label;
12938   rtx msb;
12939
12940   if (GET_MODE_CLASS (mode) != MODE_INT
12941       || GET_MODE (XEXP (rtl, 0)) != mode)
12942     return NULL;
12943
12944   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12945                             VAR_INIT_STATUS_INITIALIZED);
12946   if (op0 == NULL)
12947     return NULL;
12948   ret = op0;
12949   if (GET_CODE (rtl) == CLZ)
12950     {
12951       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12952         valv = GET_MODE_BITSIZE (mode);
12953     }
12954   else if (GET_CODE (rtl) == FFS)
12955     valv = 0;
12956   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12957     valv = GET_MODE_BITSIZE (mode);
12958   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12959   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12960   add_loc_descr (&ret, l1jump);
12961   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12962   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12963                             VAR_INIT_STATUS_INITIALIZED);
12964   if (tmp == NULL)
12965     return NULL;
12966   add_loc_descr (&ret, tmp);
12967   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12968   add_loc_descr (&ret, l4jump);
12969   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12970                                 ? const1_rtx : const0_rtx,
12971                                 mode, mem_mode,
12972                                 VAR_INIT_STATUS_INITIALIZED);
12973   if (l1label == NULL)
12974     return NULL;
12975   add_loc_descr (&ret, l1label);
12976   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12977   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12978   add_loc_descr (&ret, l2label);
12979   if (GET_CODE (rtl) != CLZ)
12980     msb = const1_rtx;
12981   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12982     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12983                    << (GET_MODE_BITSIZE (mode) - 1));
12984   else
12985     msb = immed_wide_int_const
12986       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12987                             GET_MODE_PRECISION (mode)), mode);
12988   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12989     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12990                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12991                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12992   else
12993     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12994                               VAR_INIT_STATUS_INITIALIZED);
12995   if (tmp == NULL)
12996     return NULL;
12997   add_loc_descr (&ret, tmp);
12998   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12999   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13000   add_loc_descr (&ret, l3jump);
13001   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13002                             VAR_INIT_STATUS_INITIALIZED);
13003   if (tmp == NULL)
13004     return NULL;
13005   add_loc_descr (&ret, tmp);
13006   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13007                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
13008   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13009   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13010   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13011   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13012   add_loc_descr (&ret, l2jump);
13013   l3label = new_loc_descr (DW_OP_drop, 0, 0);
13014   add_loc_descr (&ret, l3label);
13015   l4label = new_loc_descr (DW_OP_nop, 0, 0);
13016   add_loc_descr (&ret, l4label);
13017   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13018   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13019   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13020   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13021   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13022   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13023   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13024   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13025   return ret;
13026 }
13027
13028 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13029    const1 is DW_OP_lit1 or corresponding typed constant):
13030        const0 DW_OP_swap
13031    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13032        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13033    L2: DW_OP_drop
13034
13035    PARITY is similar:
13036    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13037        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13038    L2: DW_OP_drop  */
13039
13040 static dw_loc_descr_ref
13041 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13042                          machine_mode mem_mode)
13043 {
13044   dw_loc_descr_ref op0, ret, tmp;
13045   dw_loc_descr_ref l1jump, l1label;
13046   dw_loc_descr_ref l2jump, l2label;
13047
13048   if (GET_MODE_CLASS (mode) != MODE_INT
13049       || GET_MODE (XEXP (rtl, 0)) != mode)
13050     return NULL;
13051
13052   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13053                             VAR_INIT_STATUS_INITIALIZED);
13054   if (op0 == NULL)
13055     return NULL;
13056   ret = op0;
13057   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13058                             VAR_INIT_STATUS_INITIALIZED);
13059   if (tmp == NULL)
13060     return NULL;
13061   add_loc_descr (&ret, tmp);
13062   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13063   l1label = new_loc_descr (DW_OP_dup, 0, 0);
13064   add_loc_descr (&ret, l1label);
13065   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13066   add_loc_descr (&ret, l2jump);
13067   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13068   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13069   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13070                             VAR_INIT_STATUS_INITIALIZED);
13071   if (tmp == NULL)
13072     return NULL;
13073   add_loc_descr (&ret, tmp);
13074   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13075   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13076                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
13077   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13078   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13079                             VAR_INIT_STATUS_INITIALIZED);
13080   add_loc_descr (&ret, tmp);
13081   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13082   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13083   add_loc_descr (&ret, l1jump);
13084   l2label = new_loc_descr (DW_OP_drop, 0, 0);
13085   add_loc_descr (&ret, l2label);
13086   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13087   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13088   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13089   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13090   return ret;
13091 }
13092
13093 /* BSWAP (constS is initial shift count, either 56 or 24):
13094        constS const0
13095    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13096        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13097        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13098        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13099    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
13100
13101 static dw_loc_descr_ref
13102 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13103                       machine_mode mem_mode)
13104 {
13105   dw_loc_descr_ref op0, ret, tmp;
13106   dw_loc_descr_ref l1jump, l1label;
13107   dw_loc_descr_ref l2jump, l2label;
13108
13109   if (GET_MODE_CLASS (mode) != MODE_INT
13110       || BITS_PER_UNIT != 8
13111       || (GET_MODE_BITSIZE (mode) != 32
13112           &&  GET_MODE_BITSIZE (mode) != 64))
13113     return NULL;
13114
13115   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13116                             VAR_INIT_STATUS_INITIALIZED);
13117   if (op0 == NULL)
13118     return NULL;
13119
13120   ret = op0;
13121   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13122                             mode, mem_mode,
13123                             VAR_INIT_STATUS_INITIALIZED);
13124   if (tmp == NULL)
13125     return NULL;
13126   add_loc_descr (&ret, tmp);
13127   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13128                             VAR_INIT_STATUS_INITIALIZED);
13129   if (tmp == NULL)
13130     return NULL;
13131   add_loc_descr (&ret, tmp);
13132   l1label = new_loc_descr (DW_OP_pick, 2, 0);
13133   add_loc_descr (&ret, l1label);
13134   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13135                             mode, mem_mode,
13136                             VAR_INIT_STATUS_INITIALIZED);
13137   add_loc_descr (&ret, tmp);
13138   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13139   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13140   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13141   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13142                             VAR_INIT_STATUS_INITIALIZED);
13143   if (tmp == NULL)
13144     return NULL;
13145   add_loc_descr (&ret, tmp);
13146   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13147   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13148   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13149   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13150   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13151   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13152   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13153                             VAR_INIT_STATUS_INITIALIZED);
13154   add_loc_descr (&ret, tmp);
13155   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13156   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13157   add_loc_descr (&ret, l2jump);
13158   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13159                             VAR_INIT_STATUS_INITIALIZED);
13160   add_loc_descr (&ret, tmp);
13161   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13162   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13163   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13164   add_loc_descr (&ret, l1jump);
13165   l2label = new_loc_descr (DW_OP_drop, 0, 0);
13166   add_loc_descr (&ret, l2label);
13167   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13168   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13169   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13170   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13171   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13172   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13173   return ret;
13174 }
13175
13176 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13177    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13178    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13179    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13180
13181    ROTATERT is similar:
13182    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13183    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13184    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
13185
13186 static dw_loc_descr_ref
13187 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13188                        machine_mode mem_mode)
13189 {
13190   rtx rtlop1 = XEXP (rtl, 1);
13191   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13192   int i;
13193
13194   if (GET_MODE_CLASS (mode) != MODE_INT)
13195     return NULL;
13196
13197   if (GET_MODE (rtlop1) != VOIDmode
13198       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13199     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13200   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13201                             VAR_INIT_STATUS_INITIALIZED);
13202   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13203                             VAR_INIT_STATUS_INITIALIZED);
13204   if (op0 == NULL || op1 == NULL)
13205     return NULL;
13206   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13207     for (i = 0; i < 2; i++)
13208       {
13209         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13210           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13211                                         mode, mem_mode,
13212                                         VAR_INIT_STATUS_INITIALIZED);
13213         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13214           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13215                                    ? DW_OP_const4u
13216                                    : HOST_BITS_PER_WIDE_INT == 64
13217                                    ? DW_OP_const8u : DW_OP_constu,
13218                                    GET_MODE_MASK (mode), 0);
13219         else
13220           mask[i] = NULL;
13221         if (mask[i] == NULL)
13222           return NULL;
13223         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13224       }
13225   ret = op0;
13226   add_loc_descr (&ret, op1);
13227   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13228   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13229   if (GET_CODE (rtl) == ROTATERT)
13230     {
13231       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13232       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13233                                           GET_MODE_BITSIZE (mode), 0));
13234     }
13235   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13236   if (mask[0] != NULL)
13237     add_loc_descr (&ret, mask[0]);
13238   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13239   if (mask[1] != NULL)
13240     {
13241       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13242       add_loc_descr (&ret, mask[1]);
13243       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13244     }
13245   if (GET_CODE (rtl) == ROTATE)
13246     {
13247       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13248       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13249                                           GET_MODE_BITSIZE (mode), 0));
13250     }
13251   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13252   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13253   return ret;
13254 }
13255
13256 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
13257    for DEBUG_PARAMETER_REF RTL.  */
13258
13259 static dw_loc_descr_ref
13260 parameter_ref_descriptor (rtx rtl)
13261 {
13262   dw_loc_descr_ref ret;
13263   dw_die_ref ref;
13264
13265   if (dwarf_strict)
13266     return NULL;
13267   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13268   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13269   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13270   if (ref)
13271     {
13272       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13273       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13274       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13275     }
13276   else
13277     {
13278       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13279       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13280     }
13281   return ret;
13282 }
13283
13284 /* The following routine converts the RTL for a variable or parameter
13285    (resident in memory) into an equivalent Dwarf representation of a
13286    mechanism for getting the address of that same variable onto the top of a
13287    hypothetical "address evaluation" stack.
13288
13289    When creating memory location descriptors, we are effectively transforming
13290    the RTL for a memory-resident object into its Dwarf postfix expression
13291    equivalent.  This routine recursively descends an RTL tree, turning
13292    it into Dwarf postfix code as it goes.
13293
13294    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13295
13296    MEM_MODE is the mode of the memory reference, needed to handle some
13297    autoincrement addressing modes.
13298
13299    Return 0 if we can't represent the location.  */
13300
13301 dw_loc_descr_ref
13302 mem_loc_descriptor (rtx rtl, machine_mode mode,
13303                     machine_mode mem_mode,
13304                     enum var_init_status initialized)
13305 {
13306   dw_loc_descr_ref mem_loc_result = NULL;
13307   enum dwarf_location_atom op;
13308   dw_loc_descr_ref op0, op1;
13309   rtx inner = NULL_RTX;
13310
13311   if (mode == VOIDmode)
13312     mode = GET_MODE (rtl);
13313
13314   /* Note that for a dynamically sized array, the location we will generate a
13315      description of here will be the lowest numbered location which is
13316      actually within the array.  That's *not* necessarily the same as the
13317      zeroth element of the array.  */
13318
13319   rtl = targetm.delegitimize_address (rtl);
13320
13321   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13322     return NULL;
13323
13324   switch (GET_CODE (rtl))
13325     {
13326     case POST_INC:
13327     case POST_DEC:
13328     case POST_MODIFY:
13329       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13330
13331     case SUBREG:
13332       /* The case of a subreg may arise when we have a local (register)
13333          variable or a formal (register) parameter which doesn't quite fill
13334          up an entire register.  For now, just assume that it is
13335          legitimate to make the Dwarf info refer to the whole register which
13336          contains the given subreg.  */
13337       if (!subreg_lowpart_p (rtl))
13338         break;
13339       inner = SUBREG_REG (rtl);
13340     case TRUNCATE:
13341       if (inner == NULL_RTX)
13342         inner = XEXP (rtl, 0);
13343       if (GET_MODE_CLASS (mode) == MODE_INT
13344           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
13345           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13346 #ifdef POINTERS_EXTEND_UNSIGNED
13347               || (mode == Pmode && mem_mode != VOIDmode)
13348 #endif
13349              )
13350           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13351         {
13352           mem_loc_result = mem_loc_descriptor (inner,
13353                                                GET_MODE (inner),
13354                                                mem_mode, initialized);
13355           break;
13356         }
13357       if (dwarf_strict)
13358         break;
13359       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13360         break;
13361       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13362           && (GET_MODE_CLASS (mode) != MODE_INT
13363               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
13364         break;
13365       else
13366         {
13367           dw_die_ref type_die;
13368           dw_loc_descr_ref cvt;
13369
13370           mem_loc_result = mem_loc_descriptor (inner,
13371                                                GET_MODE (inner),
13372                                                mem_mode, initialized);
13373           if (mem_loc_result == NULL)
13374             break;
13375           type_die = base_type_for_mode (mode,
13376                                          GET_MODE_CLASS (mode) == MODE_INT);
13377           if (type_die == NULL)
13378             {
13379               mem_loc_result = NULL;
13380               break;
13381             }
13382           if (GET_MODE_SIZE (mode)
13383               != GET_MODE_SIZE (GET_MODE (inner)))
13384             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13385           else
13386             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13387           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13388           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13389           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13390           add_loc_descr (&mem_loc_result, cvt);
13391           if (GET_MODE_CLASS (mode) == MODE_INT
13392               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13393             {
13394               /* Convert it to untyped afterwards.  */
13395               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13396               add_loc_descr (&mem_loc_result, cvt);
13397             }
13398         }
13399       break;
13400
13401     case REG:
13402       if (GET_MODE_CLASS (mode) != MODE_INT
13403           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13404               && rtl != arg_pointer_rtx
13405               && rtl != frame_pointer_rtx
13406 #ifdef POINTERS_EXTEND_UNSIGNED
13407               && (mode != Pmode || mem_mode == VOIDmode)
13408 #endif
13409               ))
13410         {
13411           dw_die_ref type_die;
13412           unsigned int dbx_regnum;
13413
13414           if (dwarf_strict)
13415             break;
13416           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13417             break;
13418           type_die = base_type_for_mode (mode,
13419                                          GET_MODE_CLASS (mode) == MODE_INT);
13420           if (type_die == NULL)
13421             break;
13422
13423           dbx_regnum = dbx_reg_number (rtl);
13424           if (dbx_regnum == IGNORED_DWARF_REGNUM)
13425             break;
13426           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13427                                           dbx_regnum, 0);
13428           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13429           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13430           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13431           break;
13432         }
13433       /* Whenever a register number forms a part of the description of the
13434          method for calculating the (dynamic) address of a memory resident
13435          object, DWARF rules require the register number be referred to as
13436          a "base register".  This distinction is not based in any way upon
13437          what category of register the hardware believes the given register
13438          belongs to.  This is strictly DWARF terminology we're dealing with
13439          here. Note that in cases where the location of a memory-resident
13440          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13441          OP_CONST (0)) the actual DWARF location descriptor that we generate
13442          may just be OP_BASEREG (basereg).  This may look deceptively like
13443          the object in question was allocated to a register (rather than in
13444          memory) so DWARF consumers need to be aware of the subtle
13445          distinction between OP_REG and OP_BASEREG.  */
13446       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13447         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13448       else if (stack_realign_drap
13449                && crtl->drap_reg
13450                && crtl->args.internal_arg_pointer == rtl
13451                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13452         {
13453           /* If RTL is internal_arg_pointer, which has been optimized
13454              out, use DRAP instead.  */
13455           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13456                                             VAR_INIT_STATUS_INITIALIZED);
13457         }
13458       break;
13459
13460     case SIGN_EXTEND:
13461     case ZERO_EXTEND:
13462       if (GET_MODE_CLASS (mode) != MODE_INT)
13463         break;
13464       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13465                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13466       if (op0 == 0)
13467         break;
13468       else if (GET_CODE (rtl) == ZERO_EXTEND
13469                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13470                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13471                   < HOST_BITS_PER_WIDE_INT
13472                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13473                   to expand zero extend as two shifts instead of
13474                   masking.  */
13475                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13476         {
13477           machine_mode imode = GET_MODE (XEXP (rtl, 0));
13478           mem_loc_result = op0;
13479           add_loc_descr (&mem_loc_result,
13480                          int_loc_descriptor (GET_MODE_MASK (imode)));
13481           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13482         }
13483       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13484         {
13485           int shift = DWARF2_ADDR_SIZE
13486                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13487           shift *= BITS_PER_UNIT;
13488           if (GET_CODE (rtl) == SIGN_EXTEND)
13489             op = DW_OP_shra;
13490           else
13491             op = DW_OP_shr;
13492           mem_loc_result = op0;
13493           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13494           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13495           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13496           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13497         }
13498       else if (!dwarf_strict)
13499         {
13500           dw_die_ref type_die1, type_die2;
13501           dw_loc_descr_ref cvt;
13502
13503           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13504                                           GET_CODE (rtl) == ZERO_EXTEND);
13505           if (type_die1 == NULL)
13506             break;
13507           type_die2 = base_type_for_mode (mode, 1);
13508           if (type_die2 == NULL)
13509             break;
13510           mem_loc_result = op0;
13511           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13512           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13513           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13514           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13515           add_loc_descr (&mem_loc_result, cvt);
13516           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13517           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13518           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13519           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13520           add_loc_descr (&mem_loc_result, cvt);
13521         }
13522       break;
13523
13524     case MEM:
13525       {
13526         rtx new_rtl = avoid_constant_pool_reference (rtl);
13527         if (new_rtl != rtl)
13528           {
13529             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13530                                                  initialized);
13531             if (mem_loc_result != NULL)
13532               return mem_loc_result;
13533           }
13534       }
13535       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13536                                            get_address_mode (rtl), mode,
13537                                            VAR_INIT_STATUS_INITIALIZED);
13538       if (mem_loc_result == NULL)
13539         mem_loc_result = tls_mem_loc_descriptor (rtl);
13540       if (mem_loc_result != NULL)
13541         {
13542           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13543               || GET_MODE_CLASS (mode) != MODE_INT)
13544             {
13545               dw_die_ref type_die;
13546               dw_loc_descr_ref deref;
13547
13548               if (dwarf_strict)
13549                 return NULL;
13550               type_die
13551                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
13552               if (type_die == NULL)
13553                 return NULL;
13554               deref = new_loc_descr (DW_OP_GNU_deref_type,
13555                                      GET_MODE_SIZE (mode), 0);
13556               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13557               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13558               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13559               add_loc_descr (&mem_loc_result, deref);
13560             }
13561           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13562             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13563           else
13564             add_loc_descr (&mem_loc_result,
13565                            new_loc_descr (DW_OP_deref_size,
13566                                           GET_MODE_SIZE (mode), 0));
13567         }
13568       break;
13569
13570     case LO_SUM:
13571       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13572
13573     case LABEL_REF:
13574       /* Some ports can transform a symbol ref into a label ref, because
13575          the symbol ref is too far away and has to be dumped into a constant
13576          pool.  */
13577     case CONST:
13578     case SYMBOL_REF:
13579       if ((GET_MODE_CLASS (mode) != MODE_INT
13580            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
13581           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13582 #ifdef POINTERS_EXTEND_UNSIGNED
13583               && (mode != Pmode || mem_mode == VOIDmode)
13584 #endif
13585               ))
13586         break;
13587       if (GET_CODE (rtl) == SYMBOL_REF
13588           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13589         {
13590           dw_loc_descr_ref temp;
13591
13592           /* If this is not defined, we have no way to emit the data.  */
13593           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13594             break;
13595
13596           temp = new_addr_loc_descr (rtl, dtprel_true);
13597
13598           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13599           add_loc_descr (&mem_loc_result, temp);
13600
13601           break;
13602         }
13603
13604       if (!const_ok_for_output (rtl))
13605         {
13606           if (GET_CODE (rtl) == CONST)
13607             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13608                                                  initialized);
13609           break;
13610         }
13611
13612     symref:
13613       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13614       vec_safe_push (used_rtx_array, rtl);
13615       break;
13616
13617     case CONCAT:
13618     case CONCATN:
13619     case VAR_LOCATION:
13620     case DEBUG_IMPLICIT_PTR:
13621       expansion_failed (NULL_TREE, rtl,
13622                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13623       return 0;
13624
13625     case ENTRY_VALUE:
13626       if (dwarf_strict)
13627         return NULL;
13628       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13629         {
13630           if (GET_MODE_CLASS (mode) != MODE_INT
13631               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13632             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13633                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13634           else
13635             {
13636               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13637               if (dbx_regnum == IGNORED_DWARF_REGNUM)
13638                 return NULL;
13639               op0 = one_reg_loc_descriptor (dbx_regnum,
13640                                             VAR_INIT_STATUS_INITIALIZED);
13641             }
13642         }
13643       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13644                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13645         {
13646           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13647                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13648           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13649             return NULL;
13650         }
13651       else
13652         gcc_unreachable ();
13653       if (op0 == NULL)
13654         return NULL;
13655       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13656       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13657       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13658       break;
13659
13660     case DEBUG_PARAMETER_REF:
13661       mem_loc_result = parameter_ref_descriptor (rtl);
13662       break;
13663
13664     case PRE_MODIFY:
13665       /* Extract the PLUS expression nested inside and fall into
13666          PLUS code below.  */
13667       rtl = XEXP (rtl, 1);
13668       goto plus;
13669
13670     case PRE_INC:
13671     case PRE_DEC:
13672       /* Turn these into a PLUS expression and fall into the PLUS code
13673          below.  */
13674       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13675                           gen_int_mode (GET_CODE (rtl) == PRE_INC
13676                                         ? GET_MODE_UNIT_SIZE (mem_mode)
13677                                         : -GET_MODE_UNIT_SIZE (mem_mode),
13678                                         mode));
13679
13680       /* ... fall through ...  */
13681
13682     case PLUS:
13683     plus:
13684       if (is_based_loc (rtl)
13685           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13686               || XEXP (rtl, 0) == arg_pointer_rtx
13687               || XEXP (rtl, 0) == frame_pointer_rtx)
13688           && GET_MODE_CLASS (mode) == MODE_INT)
13689         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13690                                           INTVAL (XEXP (rtl, 1)),
13691                                           VAR_INIT_STATUS_INITIALIZED);
13692       else
13693         {
13694           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13695                                                VAR_INIT_STATUS_INITIALIZED);
13696           if (mem_loc_result == 0)
13697             break;
13698
13699           if (CONST_INT_P (XEXP (rtl, 1))
13700               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13701             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13702           else
13703             {
13704               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13705                                         VAR_INIT_STATUS_INITIALIZED);
13706               if (op1 == 0)
13707                 return NULL;
13708               add_loc_descr (&mem_loc_result, op1);
13709               add_loc_descr (&mem_loc_result,
13710                              new_loc_descr (DW_OP_plus, 0, 0));
13711             }
13712         }
13713       break;
13714
13715     /* If a pseudo-reg is optimized away, it is possible for it to
13716        be replaced with a MEM containing a multiply or shift.  */
13717     case MINUS:
13718       op = DW_OP_minus;
13719       goto do_binop;
13720
13721     case MULT:
13722       op = DW_OP_mul;
13723       goto do_binop;
13724
13725     case DIV:
13726       if (!dwarf_strict
13727           && GET_MODE_CLASS (mode) == MODE_INT
13728           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13729         {
13730           mem_loc_result = typed_binop (DW_OP_div, rtl,
13731                                         base_type_for_mode (mode, 0),
13732                                         mode, mem_mode);
13733           break;
13734         }
13735       op = DW_OP_div;
13736       goto do_binop;
13737
13738     case UMOD:
13739       op = DW_OP_mod;
13740       goto do_binop;
13741
13742     case ASHIFT:
13743       op = DW_OP_shl;
13744       goto do_shift;
13745
13746     case ASHIFTRT:
13747       op = DW_OP_shra;
13748       goto do_shift;
13749
13750     case LSHIFTRT:
13751       op = DW_OP_shr;
13752       goto do_shift;
13753
13754     do_shift:
13755       if (GET_MODE_CLASS (mode) != MODE_INT)
13756         break;
13757       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13758                                 VAR_INIT_STATUS_INITIALIZED);
13759       {
13760         rtx rtlop1 = XEXP (rtl, 1);
13761         if (GET_MODE (rtlop1) != VOIDmode
13762             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13763                < GET_MODE_BITSIZE (mode))
13764           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13765         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13766                                   VAR_INIT_STATUS_INITIALIZED);
13767       }
13768
13769       if (op0 == 0 || op1 == 0)
13770         break;
13771
13772       mem_loc_result = op0;
13773       add_loc_descr (&mem_loc_result, op1);
13774       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13775       break;
13776
13777     case AND:
13778       op = DW_OP_and;
13779       goto do_binop;
13780
13781     case IOR:
13782       op = DW_OP_or;
13783       goto do_binop;
13784
13785     case XOR:
13786       op = DW_OP_xor;
13787       goto do_binop;
13788
13789     do_binop:
13790       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13791                                 VAR_INIT_STATUS_INITIALIZED);
13792       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13793                                 VAR_INIT_STATUS_INITIALIZED);
13794
13795       if (op0 == 0 || op1 == 0)
13796         break;
13797
13798       mem_loc_result = op0;
13799       add_loc_descr (&mem_loc_result, op1);
13800       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13801       break;
13802
13803     case MOD:
13804       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13805         {
13806           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13807                                         base_type_for_mode (mode, 0),
13808                                         mode, mem_mode);
13809           break;
13810         }
13811
13812       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13813                                 VAR_INIT_STATUS_INITIALIZED);
13814       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13815                                 VAR_INIT_STATUS_INITIALIZED);
13816
13817       if (op0 == 0 || op1 == 0)
13818         break;
13819
13820       mem_loc_result = op0;
13821       add_loc_descr (&mem_loc_result, op1);
13822       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13823       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13824       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13825       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13826       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13827       break;
13828
13829     case UDIV:
13830       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13831         {
13832           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13833             {
13834               op = DW_OP_div;
13835               goto do_binop;
13836             }
13837           mem_loc_result = typed_binop (DW_OP_div, rtl,
13838                                         base_type_for_mode (mode, 1),
13839                                         mode, mem_mode);
13840         }
13841       break;
13842
13843     case NOT:
13844       op = DW_OP_not;
13845       goto do_unop;
13846
13847     case ABS:
13848       op = DW_OP_abs;
13849       goto do_unop;
13850
13851     case NEG:
13852       op = DW_OP_neg;
13853       goto do_unop;
13854
13855     do_unop:
13856       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13857                                 VAR_INIT_STATUS_INITIALIZED);
13858
13859       if (op0 == 0)
13860         break;
13861
13862       mem_loc_result = op0;
13863       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13864       break;
13865
13866     case CONST_INT:
13867       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13868 #ifdef POINTERS_EXTEND_UNSIGNED
13869           || (mode == Pmode
13870               && mem_mode != VOIDmode
13871               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13872 #endif
13873           )
13874         {
13875           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13876           break;
13877         }
13878       if (!dwarf_strict
13879           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13880               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13881         {
13882           dw_die_ref type_die = base_type_for_mode (mode, 1);
13883           machine_mode amode;
13884           if (type_die == NULL)
13885             return NULL;
13886           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13887                                  MODE_INT, 0);
13888           if (INTVAL (rtl) >= 0
13889               && amode != BLKmode
13890               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13891               /* const DW_OP_GNU_convert <XXX> vs.
13892                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13893               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13894                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13895             {
13896               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13897               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13898               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13899               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13900               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13901               add_loc_descr (&mem_loc_result, op0);
13902               return mem_loc_result;
13903             }
13904           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13905                                           INTVAL (rtl));
13906           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13907           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13908           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13909           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13910             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13911           else
13912             {
13913               mem_loc_result->dw_loc_oprnd2.val_class
13914                 = dw_val_class_const_double;
13915               mem_loc_result->dw_loc_oprnd2.v.val_double
13916                 = double_int::from_shwi (INTVAL (rtl));
13917             }
13918         }
13919       break;
13920
13921     case CONST_DOUBLE:
13922       if (!dwarf_strict)
13923         {
13924           dw_die_ref type_die;
13925
13926           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13927              CONST_DOUBLE rtx could represent either a large integer
13928              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13929              the value is always a floating point constant.
13930
13931              When it is an integer, a CONST_DOUBLE is used whenever
13932              the constant requires 2 HWIs to be adequately represented.
13933              We output CONST_DOUBLEs as blocks.  */
13934           if (mode == VOIDmode
13935               || (GET_MODE (rtl) == VOIDmode
13936                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13937             break;
13938           type_die = base_type_for_mode (mode,
13939                                          GET_MODE_CLASS (mode) == MODE_INT);
13940           if (type_die == NULL)
13941             return NULL;
13942           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13943           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13944           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13945           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13946 #if TARGET_SUPPORTS_WIDE_INT == 0
13947           if (!SCALAR_FLOAT_MODE_P (mode))
13948             {
13949               mem_loc_result->dw_loc_oprnd2.val_class
13950                 = dw_val_class_const_double;
13951               mem_loc_result->dw_loc_oprnd2.v.val_double
13952                 = rtx_to_double_int (rtl);
13953             }
13954           else
13955 #endif
13956             {
13957               unsigned int length = GET_MODE_SIZE (mode);
13958               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13959
13960               insert_float (rtl, array);
13961               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13962               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13963               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13964               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13965             }
13966         }
13967       break;
13968
13969     case CONST_WIDE_INT:
13970       if (!dwarf_strict)
13971         {
13972           dw_die_ref type_die;
13973
13974           type_die = base_type_for_mode (mode,
13975                                          GET_MODE_CLASS (mode) == MODE_INT);
13976           if (type_die == NULL)
13977             return NULL;
13978           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13979           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13980           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13981           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13982           mem_loc_result->dw_loc_oprnd2.val_class
13983             = dw_val_class_wide_int;
13984           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13985           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13986         }
13987       break;
13988
13989     case EQ:
13990       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13991       break;
13992
13993     case GE:
13994       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13995       break;
13996
13997     case GT:
13998       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13999       break;
14000
14001     case LE:
14002       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14003       break;
14004
14005     case LT:
14006       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14007       break;
14008
14009     case NE:
14010       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14011       break;
14012
14013     case GEU:
14014       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14015       break;
14016
14017     case GTU:
14018       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14019       break;
14020
14021     case LEU:
14022       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14023       break;
14024
14025     case LTU:
14026       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14027       break;
14028
14029     case UMIN:
14030     case UMAX:
14031       if (GET_MODE_CLASS (mode) != MODE_INT)
14032         break;
14033       /* FALLTHRU */
14034     case SMIN:
14035     case SMAX:
14036       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14037       break;
14038
14039     case ZERO_EXTRACT:
14040     case SIGN_EXTRACT:
14041       if (CONST_INT_P (XEXP (rtl, 1))
14042           && CONST_INT_P (XEXP (rtl, 2))
14043           && ((unsigned) INTVAL (XEXP (rtl, 1))
14044               + (unsigned) INTVAL (XEXP (rtl, 2))
14045               <= GET_MODE_BITSIZE (mode))
14046           && GET_MODE_CLASS (mode) == MODE_INT
14047           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14048           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14049         {
14050           int shift, size;
14051           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14052                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14053           if (op0 == 0)
14054             break;
14055           if (GET_CODE (rtl) == SIGN_EXTRACT)
14056             op = DW_OP_shra;
14057           else
14058             op = DW_OP_shr;
14059           mem_loc_result = op0;
14060           size = INTVAL (XEXP (rtl, 1));
14061           shift = INTVAL (XEXP (rtl, 2));
14062           if (BITS_BIG_ENDIAN)
14063             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14064                     - shift - size;
14065           if (shift + size != (int) DWARF2_ADDR_SIZE)
14066             {
14067               add_loc_descr (&mem_loc_result,
14068                              int_loc_descriptor (DWARF2_ADDR_SIZE
14069                                                  - shift - size));
14070               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14071             }
14072           if (size != (int) DWARF2_ADDR_SIZE)
14073             {
14074               add_loc_descr (&mem_loc_result,
14075                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14076               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14077             }
14078         }
14079       break;
14080
14081     case IF_THEN_ELSE:
14082       {
14083         dw_loc_descr_ref op2, bra_node, drop_node;
14084         op0 = mem_loc_descriptor (XEXP (rtl, 0),
14085                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
14086                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
14087                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
14088         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14089                                   VAR_INIT_STATUS_INITIALIZED);
14090         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14091                                   VAR_INIT_STATUS_INITIALIZED);
14092         if (op0 == NULL || op1 == NULL || op2 == NULL)
14093           break;
14094
14095         mem_loc_result = op1;
14096         add_loc_descr (&mem_loc_result, op2);
14097         add_loc_descr (&mem_loc_result, op0);
14098         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14099         add_loc_descr (&mem_loc_result, bra_node);
14100         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14101         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14102         add_loc_descr (&mem_loc_result, drop_node);
14103         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14104         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14105       }
14106       break;
14107
14108     case FLOAT_EXTEND:
14109     case FLOAT_TRUNCATE:
14110     case FLOAT:
14111     case UNSIGNED_FLOAT:
14112     case FIX:
14113     case UNSIGNED_FIX:
14114       if (!dwarf_strict)
14115         {
14116           dw_die_ref type_die;
14117           dw_loc_descr_ref cvt;
14118
14119           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14120                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14121           if (op0 == NULL)
14122             break;
14123           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14124               && (GET_CODE (rtl) == FLOAT
14125                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14126                      <= DWARF2_ADDR_SIZE))
14127             {
14128               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14129                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
14130               if (type_die == NULL)
14131                 break;
14132               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14133               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14134               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14135               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14136               add_loc_descr (&op0, cvt);
14137             }
14138           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14139           if (type_die == NULL)
14140             break;
14141           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14142           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14143           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14144           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14145           add_loc_descr (&op0, cvt);
14146           if (GET_MODE_CLASS (mode) == MODE_INT
14147               && (GET_CODE (rtl) == FIX
14148                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14149             {
14150               op0 = convert_descriptor_to_mode (mode, op0);
14151               if (op0 == NULL)
14152                 break;
14153             }
14154           mem_loc_result = op0;
14155         }
14156       break;
14157
14158     case CLZ:
14159     case CTZ:
14160     case FFS:
14161       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14162       break;
14163
14164     case POPCOUNT:
14165     case PARITY:
14166       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14167       break;
14168
14169     case BSWAP:
14170       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14171       break;
14172
14173     case ROTATE:
14174     case ROTATERT:
14175       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14176       break;
14177
14178     case COMPARE:
14179       /* In theory, we could implement the above.  */
14180       /* DWARF cannot represent the unsigned compare operations
14181          natively.  */
14182     case SS_MULT:
14183     case US_MULT:
14184     case SS_DIV:
14185     case US_DIV:
14186     case SS_PLUS:
14187     case US_PLUS:
14188     case SS_MINUS:
14189     case US_MINUS:
14190     case SS_NEG:
14191     case US_NEG:
14192     case SS_ABS:
14193     case SS_ASHIFT:
14194     case US_ASHIFT:
14195     case SS_TRUNCATE:
14196     case US_TRUNCATE:
14197     case UNORDERED:
14198     case ORDERED:
14199     case UNEQ:
14200     case UNGE:
14201     case UNGT:
14202     case UNLE:
14203     case UNLT:
14204     case LTGT:
14205     case FRACT_CONVERT:
14206     case UNSIGNED_FRACT_CONVERT:
14207     case SAT_FRACT:
14208     case UNSIGNED_SAT_FRACT:
14209     case SQRT:
14210     case ASM_OPERANDS:
14211     case VEC_MERGE:
14212     case VEC_SELECT:
14213     case VEC_CONCAT:
14214     case VEC_DUPLICATE:
14215     case UNSPEC:
14216     case HIGH:
14217     case FMA:
14218     case STRICT_LOW_PART:
14219     case CONST_VECTOR:
14220     case CONST_FIXED:
14221     case CLRSB:
14222     case CLOBBER:
14223       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14224          can't express it in the debug info.  This can happen e.g. with some
14225          TLS UNSPECs.  */
14226       break;
14227
14228     case CONST_STRING:
14229       resolve_one_addr (&rtl);
14230       goto symref;
14231
14232     default:
14233       if (flag_checking)
14234         {
14235           print_rtl (stderr, rtl);
14236           gcc_unreachable ();
14237         }
14238       break;
14239     }
14240
14241   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14242     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14243
14244   return mem_loc_result;
14245 }
14246
14247 /* Return a descriptor that describes the concatenation of two locations.
14248    This is typically a complex variable.  */
14249
14250 static dw_loc_descr_ref
14251 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14252 {
14253   dw_loc_descr_ref cc_loc_result = NULL;
14254   dw_loc_descr_ref x0_ref
14255     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14256   dw_loc_descr_ref x1_ref
14257     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14258
14259   if (x0_ref == 0 || x1_ref == 0)
14260     return 0;
14261
14262   cc_loc_result = x0_ref;
14263   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14264
14265   add_loc_descr (&cc_loc_result, x1_ref);
14266   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14267
14268   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14269     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14270
14271   return cc_loc_result;
14272 }
14273
14274 /* Return a descriptor that describes the concatenation of N
14275    locations.  */
14276
14277 static dw_loc_descr_ref
14278 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14279 {
14280   unsigned int i;
14281   dw_loc_descr_ref cc_loc_result = NULL;
14282   unsigned int n = XVECLEN (concatn, 0);
14283
14284   for (i = 0; i < n; ++i)
14285     {
14286       dw_loc_descr_ref ref;
14287       rtx x = XVECEXP (concatn, 0, i);
14288
14289       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14290       if (ref == NULL)
14291         return NULL;
14292
14293       add_loc_descr (&cc_loc_result, ref);
14294       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14295     }
14296
14297   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14298     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14299
14300   return cc_loc_result;
14301 }
14302
14303 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14304    for DEBUG_IMPLICIT_PTR RTL.  */
14305
14306 static dw_loc_descr_ref
14307 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14308 {
14309   dw_loc_descr_ref ret;
14310   dw_die_ref ref;
14311
14312   if (dwarf_strict)
14313     return NULL;
14314   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14315               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14316               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14317   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14318   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14319   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14320   if (ref)
14321     {
14322       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14323       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14324       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14325     }
14326   else
14327     {
14328       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14329       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14330     }
14331   return ret;
14332 }
14333
14334 /* Output a proper Dwarf location descriptor for a variable or parameter
14335    which is either allocated in a register or in a memory location.  For a
14336    register, we just generate an OP_REG and the register number.  For a
14337    memory location we provide a Dwarf postfix expression describing how to
14338    generate the (dynamic) address of the object onto the address stack.
14339
14340    MODE is mode of the decl if this loc_descriptor is going to be used in
14341    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14342    allowed, VOIDmode otherwise.
14343
14344    If we don't know how to describe it, return 0.  */
14345
14346 static dw_loc_descr_ref
14347 loc_descriptor (rtx rtl, machine_mode mode,
14348                 enum var_init_status initialized)
14349 {
14350   dw_loc_descr_ref loc_result = NULL;
14351
14352   switch (GET_CODE (rtl))
14353     {
14354     case SUBREG:
14355       /* The case of a subreg may arise when we have a local (register)
14356          variable or a formal (register) parameter which doesn't quite fill
14357          up an entire register.  For now, just assume that it is
14358          legitimate to make the Dwarf info refer to the whole register which
14359          contains the given subreg.  */
14360       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14361         loc_result = loc_descriptor (SUBREG_REG (rtl),
14362                                      GET_MODE (SUBREG_REG (rtl)), initialized);
14363       else
14364         goto do_default;
14365       break;
14366
14367     case REG:
14368       loc_result = reg_loc_descriptor (rtl, initialized);
14369       break;
14370
14371     case MEM:
14372       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14373                                        GET_MODE (rtl), initialized);
14374       if (loc_result == NULL)
14375         loc_result = tls_mem_loc_descriptor (rtl);
14376       if (loc_result == NULL)
14377         {
14378           rtx new_rtl = avoid_constant_pool_reference (rtl);
14379           if (new_rtl != rtl)
14380             loc_result = loc_descriptor (new_rtl, mode, initialized);
14381         }
14382       break;
14383
14384     case CONCAT:
14385       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14386                                           initialized);
14387       break;
14388
14389     case CONCATN:
14390       loc_result = concatn_loc_descriptor (rtl, initialized);
14391       break;
14392
14393     case VAR_LOCATION:
14394       /* Single part.  */
14395       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14396         {
14397           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14398           if (GET_CODE (loc) == EXPR_LIST)
14399             loc = XEXP (loc, 0);
14400           loc_result = loc_descriptor (loc, mode, initialized);
14401           break;
14402         }
14403
14404       rtl = XEXP (rtl, 1);
14405       /* FALLTHRU */
14406
14407     case PARALLEL:
14408       {
14409         rtvec par_elems = XVEC (rtl, 0);
14410         int num_elem = GET_NUM_ELEM (par_elems);
14411         machine_mode mode;
14412         int i;
14413
14414         /* Create the first one, so we have something to add to.  */
14415         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14416                                      VOIDmode, initialized);
14417         if (loc_result == NULL)
14418           return NULL;
14419         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14420         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14421         for (i = 1; i < num_elem; i++)
14422           {
14423             dw_loc_descr_ref temp;
14424
14425             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14426                                    VOIDmode, initialized);
14427             if (temp == NULL)
14428               return NULL;
14429             add_loc_descr (&loc_result, temp);
14430             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14431             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14432           }
14433       }
14434       break;
14435
14436     case CONST_INT:
14437       if (mode != VOIDmode && mode != BLKmode)
14438         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14439                                                     INTVAL (rtl));
14440       break;
14441
14442     case CONST_DOUBLE:
14443       if (mode == VOIDmode)
14444         mode = GET_MODE (rtl);
14445
14446       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14447         {
14448           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14449
14450           /* Note that a CONST_DOUBLE rtx could represent either an integer
14451              or a floating-point constant.  A CONST_DOUBLE is used whenever
14452              the constant requires more than one word in order to be
14453              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14454           loc_result = new_loc_descr (DW_OP_implicit_value,
14455                                       GET_MODE_SIZE (mode), 0);
14456 #if TARGET_SUPPORTS_WIDE_INT == 0
14457           if (!SCALAR_FLOAT_MODE_P (mode))
14458             {
14459               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14460               loc_result->dw_loc_oprnd2.v.val_double
14461                 = rtx_to_double_int (rtl);
14462             }
14463           else
14464 #endif
14465             {
14466               unsigned int length = GET_MODE_SIZE (mode);
14467               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14468
14469               insert_float (rtl, array);
14470               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14471               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14472               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14473               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14474             }
14475         }
14476       break;
14477
14478     case CONST_WIDE_INT:
14479       if (mode == VOIDmode)
14480         mode = GET_MODE (rtl);
14481
14482       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14483         {
14484           loc_result = new_loc_descr (DW_OP_implicit_value,
14485                                       GET_MODE_SIZE (mode), 0);
14486           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14487           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14488           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14489         }
14490       break;
14491
14492     case CONST_VECTOR:
14493       if (mode == VOIDmode)
14494         mode = GET_MODE (rtl);
14495
14496       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14497         {
14498           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14499           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14500           unsigned char *array
14501             = ggc_vec_alloc<unsigned char> (length * elt_size);
14502           unsigned int i;
14503           unsigned char *p;
14504           machine_mode imode = GET_MODE_INNER (mode);
14505
14506           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14507           switch (GET_MODE_CLASS (mode))
14508             {
14509             case MODE_VECTOR_INT:
14510               for (i = 0, p = array; i < length; i++, p += elt_size)
14511                 {
14512                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14513                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14514                 }
14515               break;
14516
14517             case MODE_VECTOR_FLOAT:
14518               for (i = 0, p = array; i < length; i++, p += elt_size)
14519                 {
14520                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14521                   insert_float (elt, p);
14522                 }
14523               break;
14524
14525             default:
14526               gcc_unreachable ();
14527             }
14528
14529           loc_result = new_loc_descr (DW_OP_implicit_value,
14530                                       length * elt_size, 0);
14531           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14532           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14533           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14534           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14535         }
14536       break;
14537
14538     case CONST:
14539       if (mode == VOIDmode
14540           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14541           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14542           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14543         {
14544           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14545           break;
14546         }
14547       /* FALLTHROUGH */
14548     case SYMBOL_REF:
14549       if (!const_ok_for_output (rtl))
14550         break;
14551     case LABEL_REF:
14552       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14553           && (dwarf_version >= 4 || !dwarf_strict))
14554         {
14555          loc_result = new_addr_loc_descr (rtl, dtprel_false);
14556           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14557           vec_safe_push (used_rtx_array, rtl);
14558         }
14559       break;
14560
14561     case DEBUG_IMPLICIT_PTR:
14562       loc_result = implicit_ptr_descriptor (rtl, 0);
14563       break;
14564
14565     case PLUS:
14566       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14567           && CONST_INT_P (XEXP (rtl, 1)))
14568         {
14569           loc_result
14570             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14571           break;
14572         }
14573       /* FALLTHRU */
14574     do_default:
14575     default:
14576       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14577            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14578            && dwarf_version >= 4)
14579           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14580         {
14581           /* Value expression.  */
14582           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14583           if (loc_result)
14584             add_loc_descr (&loc_result,
14585                            new_loc_descr (DW_OP_stack_value, 0, 0));
14586         }
14587       break;
14588     }
14589
14590   return loc_result;
14591 }
14592
14593 /* We need to figure out what section we should use as the base for the
14594    address ranges where a given location is valid.
14595    1. If this particular DECL has a section associated with it, use that.
14596    2. If this function has a section associated with it, use that.
14597    3. Otherwise, use the text section.
14598    XXX: If you split a variable across multiple sections, we won't notice.  */
14599
14600 static const char *
14601 secname_for_decl (const_tree decl)
14602 {
14603   const char *secname;
14604
14605   if (VAR_OR_FUNCTION_DECL_P (decl)
14606       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14607       && DECL_SECTION_NAME (decl))
14608     secname = DECL_SECTION_NAME (decl);
14609   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14610     secname = DECL_SECTION_NAME (current_function_decl);
14611   else if (cfun && in_cold_section_p)
14612     secname = crtl->subsections.cold_section_label;
14613   else
14614     secname = text_section_label;
14615
14616   return secname;
14617 }
14618
14619 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14620
14621 static bool
14622 decl_by_reference_p (tree decl)
14623 {
14624   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14625            || TREE_CODE (decl) == VAR_DECL)
14626           && DECL_BY_REFERENCE (decl));
14627 }
14628
14629 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14630    for VARLOC.  */
14631
14632 static dw_loc_descr_ref
14633 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14634                enum var_init_status initialized)
14635 {
14636   int have_address = 0;
14637   dw_loc_descr_ref descr;
14638   machine_mode mode;
14639
14640   if (want_address != 2)
14641     {
14642       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14643       /* Single part.  */
14644       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14645         {
14646           varloc = PAT_VAR_LOCATION_LOC (varloc);
14647           if (GET_CODE (varloc) == EXPR_LIST)
14648             varloc = XEXP (varloc, 0);
14649           mode = GET_MODE (varloc);
14650           if (MEM_P (varloc))
14651             {
14652               rtx addr = XEXP (varloc, 0);
14653               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14654                                           mode, initialized);
14655               if (descr)
14656                 have_address = 1;
14657               else
14658                 {
14659                   rtx x = avoid_constant_pool_reference (varloc);
14660                   if (x != varloc)
14661                     descr = mem_loc_descriptor (x, mode, VOIDmode,
14662                                                 initialized);
14663                 }
14664             }
14665           else
14666             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14667         }
14668       else
14669         return 0;
14670     }
14671   else
14672     {
14673       if (GET_CODE (varloc) == VAR_LOCATION)
14674         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14675       else
14676         mode = DECL_MODE (loc);
14677       descr = loc_descriptor (varloc, mode, initialized);
14678       have_address = 1;
14679     }
14680
14681   if (!descr)
14682     return 0;
14683
14684   if (want_address == 2 && !have_address
14685       && (dwarf_version >= 4 || !dwarf_strict))
14686     {
14687       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14688         {
14689           expansion_failed (loc, NULL_RTX,
14690                             "DWARF address size mismatch");
14691           return 0;
14692         }
14693       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14694       have_address = 1;
14695     }
14696   /* Show if we can't fill the request for an address.  */
14697   if (want_address && !have_address)
14698     {
14699       expansion_failed (loc, NULL_RTX,
14700                         "Want address and only have value");
14701       return 0;
14702     }
14703
14704   /* If we've got an address and don't want one, dereference.  */
14705   if (!want_address && have_address)
14706     {
14707       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14708       enum dwarf_location_atom op;
14709
14710       if (size > DWARF2_ADDR_SIZE || size == -1)
14711         {
14712           expansion_failed (loc, NULL_RTX,
14713                             "DWARF address size mismatch");
14714           return 0;
14715         }
14716       else if (size == DWARF2_ADDR_SIZE)
14717         op = DW_OP_deref;
14718       else
14719         op = DW_OP_deref_size;
14720
14721       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14722     }
14723
14724   return descr;
14725 }
14726
14727 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14728    if it is not possible.  */
14729
14730 static dw_loc_descr_ref
14731 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14732 {
14733   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14734     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14735   else if (dwarf_version >= 3 || !dwarf_strict)
14736     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14737   else
14738     return NULL;
14739 }
14740
14741 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14742    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14743
14744 static dw_loc_descr_ref
14745 dw_sra_loc_expr (tree decl, rtx loc)
14746 {
14747   rtx p;
14748   unsigned HOST_WIDE_INT padsize = 0;
14749   dw_loc_descr_ref descr, *descr_tail;
14750   unsigned HOST_WIDE_INT decl_size;
14751   rtx varloc;
14752   enum var_init_status initialized;
14753
14754   if (DECL_SIZE (decl) == NULL
14755       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14756     return NULL;
14757
14758   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14759   descr = NULL;
14760   descr_tail = &descr;
14761
14762   for (p = loc; p; p = XEXP (p, 1))
14763     {
14764       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14765       rtx loc_note = *decl_piece_varloc_ptr (p);
14766       dw_loc_descr_ref cur_descr;
14767       dw_loc_descr_ref *tail, last = NULL;
14768       unsigned HOST_WIDE_INT opsize = 0;
14769
14770       if (loc_note == NULL_RTX
14771           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14772         {
14773           padsize += bitsize;
14774           continue;
14775         }
14776       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14777       varloc = NOTE_VAR_LOCATION (loc_note);
14778       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14779       if (cur_descr == NULL)
14780         {
14781           padsize += bitsize;
14782           continue;
14783         }
14784
14785       /* Check that cur_descr either doesn't use
14786          DW_OP_*piece operations, or their sum is equal
14787          to bitsize.  Otherwise we can't embed it.  */
14788       for (tail = &cur_descr; *tail != NULL;
14789            tail = &(*tail)->dw_loc_next)
14790         if ((*tail)->dw_loc_opc == DW_OP_piece)
14791           {
14792             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14793                       * BITS_PER_UNIT;
14794             last = *tail;
14795           }
14796         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14797           {
14798             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14799             last = *tail;
14800           }
14801
14802       if (last != NULL && opsize != bitsize)
14803         {
14804           padsize += bitsize;
14805           /* Discard the current piece of the descriptor and release any
14806              addr_table entries it uses.  */
14807           remove_loc_list_addr_table_entries (cur_descr);
14808           continue;
14809         }
14810
14811       /* If there is a hole, add DW_OP_*piece after empty DWARF
14812          expression, which means that those bits are optimized out.  */
14813       if (padsize)
14814         {
14815           if (padsize > decl_size)
14816             {
14817               remove_loc_list_addr_table_entries (cur_descr);
14818               goto discard_descr;
14819             }
14820           decl_size -= padsize;
14821           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14822           if (*descr_tail == NULL)
14823             {
14824               remove_loc_list_addr_table_entries (cur_descr);
14825               goto discard_descr;
14826             }
14827           descr_tail = &(*descr_tail)->dw_loc_next;
14828           padsize = 0;
14829         }
14830       *descr_tail = cur_descr;
14831       descr_tail = tail;
14832       if (bitsize > decl_size)
14833         goto discard_descr;
14834       decl_size -= bitsize;
14835       if (last == NULL)
14836         {
14837           HOST_WIDE_INT offset = 0;
14838           if (GET_CODE (varloc) == VAR_LOCATION
14839               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14840             {
14841               varloc = PAT_VAR_LOCATION_LOC (varloc);
14842               if (GET_CODE (varloc) == EXPR_LIST)
14843                 varloc = XEXP (varloc, 0);
14844             }
14845           do 
14846             {
14847               if (GET_CODE (varloc) == CONST
14848                   || GET_CODE (varloc) == SIGN_EXTEND
14849                   || GET_CODE (varloc) == ZERO_EXTEND)
14850                 varloc = XEXP (varloc, 0);
14851               else if (GET_CODE (varloc) == SUBREG)
14852                 varloc = SUBREG_REG (varloc);
14853               else
14854                 break;
14855             }
14856           while (1);
14857           /* DW_OP_bit_size offset should be zero for register
14858              or implicit location descriptions and empty location
14859              descriptions, but for memory addresses needs big endian
14860              adjustment.  */
14861           if (MEM_P (varloc))
14862             {
14863               unsigned HOST_WIDE_INT memsize
14864                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14865               if (memsize != bitsize)
14866                 {
14867                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14868                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14869                     goto discard_descr;
14870                   if (memsize < bitsize)
14871                     goto discard_descr;
14872                   if (BITS_BIG_ENDIAN)
14873                     offset = memsize - bitsize;
14874                 }
14875             }
14876
14877           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14878           if (*descr_tail == NULL)
14879             goto discard_descr;
14880           descr_tail = &(*descr_tail)->dw_loc_next;
14881         }
14882     }
14883
14884   /* If there were any non-empty expressions, add padding till the end of
14885      the decl.  */
14886   if (descr != NULL && decl_size != 0)
14887     {
14888       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14889       if (*descr_tail == NULL)
14890         goto discard_descr;
14891     }
14892   return descr;
14893
14894 discard_descr:
14895   /* Discard the descriptor and release any addr_table entries it uses.  */
14896   remove_loc_list_addr_table_entries (descr);
14897   return NULL;
14898 }
14899
14900 /* Return the dwarf representation of the location list LOC_LIST of
14901    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14902    function.  */
14903
14904 static dw_loc_list_ref
14905 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14906 {
14907   const char *endname, *secname;
14908   rtx varloc;
14909   enum var_init_status initialized;
14910   struct var_loc_node *node;
14911   dw_loc_descr_ref descr;
14912   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14913   dw_loc_list_ref list = NULL;
14914   dw_loc_list_ref *listp = &list;
14915
14916   /* Now that we know what section we are using for a base,
14917      actually construct the list of locations.
14918      The first location information is what is passed to the
14919      function that creates the location list, and the remaining
14920      locations just get added on to that list.
14921      Note that we only know the start address for a location
14922      (IE location changes), so to build the range, we use
14923      the range [current location start, next location start].
14924      This means we have to special case the last node, and generate
14925      a range of [last location start, end of function label].  */
14926
14927   secname = secname_for_decl (decl);
14928
14929   for (node = loc_list->first; node; node = node->next)
14930     if (GET_CODE (node->loc) == EXPR_LIST
14931         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14932       {
14933         if (GET_CODE (node->loc) == EXPR_LIST)
14934           {
14935             /* This requires DW_OP_{,bit_}piece, which is not usable
14936                inside DWARF expressions.  */
14937             if (want_address != 2)
14938               continue;
14939             descr = dw_sra_loc_expr (decl, node->loc);
14940             if (descr == NULL)
14941               continue;
14942           }
14943         else
14944           {
14945             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14946             varloc = NOTE_VAR_LOCATION (node->loc);
14947             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14948           }
14949         if (descr)
14950           {
14951             bool range_across_switch = false;
14952             /* If section switch happens in between node->label
14953                and node->next->label (or end of function) and
14954                we can't emit it as a single entry list,
14955                emit two ranges, first one ending at the end
14956                of first partition and second one starting at the
14957                beginning of second partition.  */
14958             if (node == loc_list->last_before_switch
14959                 && (node != loc_list->first || loc_list->first->next)
14960                 && current_function_decl)
14961               {
14962                 endname = cfun->fde->dw_fde_end;
14963                 range_across_switch = true;
14964               }
14965             /* The variable has a location between NODE->LABEL and
14966                NODE->NEXT->LABEL.  */
14967             else if (node->next)
14968               endname = node->next->label;
14969             /* If the variable has a location at the last label
14970                it keeps its location until the end of function.  */
14971             else if (!current_function_decl)
14972               endname = text_end_label;
14973             else
14974               {
14975                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14976                                              current_function_funcdef_no);
14977                 endname = ggc_strdup (label_id);
14978               }
14979
14980             *listp = new_loc_list (descr, node->label, endname, secname);
14981             if (TREE_CODE (decl) == PARM_DECL
14982                 && node == loc_list->first
14983                 && NOTE_P (node->loc)
14984                 && strcmp (node->label, endname) == 0)
14985               (*listp)->force = true;
14986             listp = &(*listp)->dw_loc_next;
14987
14988             if (range_across_switch)
14989               {
14990                 if (GET_CODE (node->loc) == EXPR_LIST)
14991                   descr = dw_sra_loc_expr (decl, node->loc);
14992                 else
14993                   {
14994                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14995                     varloc = NOTE_VAR_LOCATION (node->loc);
14996                     descr = dw_loc_list_1 (decl, varloc, want_address,
14997                                            initialized);
14998                   }
14999                 gcc_assert (descr);
15000                 /* The variable has a location between NODE->LABEL and
15001                    NODE->NEXT->LABEL.  */
15002                 if (node->next)
15003                   endname = node->next->label;
15004                 else
15005                   endname = cfun->fde->dw_fde_second_end;
15006                 *listp = new_loc_list (descr,
15007                                        cfun->fde->dw_fde_second_begin,
15008                                        endname, secname);
15009                 listp = &(*listp)->dw_loc_next;
15010               }
15011           }
15012       }
15013
15014   /* Try to avoid the overhead of a location list emitting a location
15015      expression instead, but only if we didn't have more than one
15016      location entry in the first place.  If some entries were not
15017      representable, we don't want to pretend a single entry that was
15018      applies to the entire scope in which the variable is
15019      available.  */
15020   if (list && loc_list->first->next)
15021     gen_llsym (list);
15022
15023   return list;
15024 }
15025
15026 /* Return if the loc_list has only single element and thus can be represented
15027    as location description.   */
15028
15029 static bool
15030 single_element_loc_list_p (dw_loc_list_ref list)
15031 {
15032   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15033   return !list->ll_symbol;
15034 }
15035
15036 /* To each location in list LIST add loc descr REF.  */
15037
15038 static void
15039 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15040 {
15041   dw_loc_descr_ref copy;
15042   add_loc_descr (&list->expr, ref);
15043   list = list->dw_loc_next;
15044   while (list)
15045     {
15046       copy = ggc_alloc<dw_loc_descr_node> ();
15047       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15048       add_loc_descr (&list->expr, copy);
15049       while (copy->dw_loc_next)
15050         {
15051           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15052           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15053           copy->dw_loc_next = new_copy;
15054           copy = new_copy;
15055         }
15056       list = list->dw_loc_next;
15057     }
15058 }
15059
15060 /* Given two lists RET and LIST
15061    produce location list that is result of adding expression in LIST
15062    to expression in RET on each position in program.
15063    Might be destructive on both RET and LIST.
15064
15065    TODO: We handle only simple cases of RET or LIST having at most one
15066    element. General case would inolve sorting the lists in program order
15067    and merging them that will need some additional work.
15068    Adding that will improve quality of debug info especially for SRA-ed
15069    structures.  */
15070
15071 static void
15072 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15073 {
15074   if (!list)
15075     return;
15076   if (!*ret)
15077     {
15078       *ret = list;
15079       return;
15080     }
15081   if (!list->dw_loc_next)
15082     {
15083       add_loc_descr_to_each (*ret, list->expr);
15084       return;
15085     }
15086   if (!(*ret)->dw_loc_next)
15087     {
15088       add_loc_descr_to_each (list, (*ret)->expr);
15089       *ret = list;
15090       return;
15091     }
15092   expansion_failed (NULL_TREE, NULL_RTX,
15093                     "Don't know how to merge two non-trivial"
15094                     " location lists.\n");
15095   *ret = NULL;
15096   return;
15097 }
15098
15099 /* LOC is constant expression.  Try a luck, look it up in constant
15100    pool and return its loc_descr of its address.  */
15101
15102 static dw_loc_descr_ref
15103 cst_pool_loc_descr (tree loc)
15104 {
15105   /* Get an RTL for this, if something has been emitted.  */
15106   rtx rtl = lookup_constant_def (loc);
15107
15108   if (!rtl || !MEM_P (rtl))
15109     {
15110       gcc_assert (!rtl);
15111       return 0;
15112     }
15113   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15114
15115   /* TODO: We might get more coverage if we was actually delaying expansion
15116      of all expressions till end of compilation when constant pools are fully
15117      populated.  */
15118   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15119     {
15120       expansion_failed (loc, NULL_RTX,
15121                         "CST value in contant pool but not marked.");
15122       return 0;
15123     }
15124   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15125                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15126 }
15127
15128 /* Return dw_loc_list representing address of addr_expr LOC
15129    by looking for inner INDIRECT_REF expression and turning
15130    it into simple arithmetics.
15131
15132    See loc_list_from_tree for the meaning of CONTEXT.  */
15133
15134 static dw_loc_list_ref
15135 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15136                                                    const loc_descr_context *context)
15137 {
15138   tree obj, offset;
15139   HOST_WIDE_INT bitsize, bitpos, bytepos;
15140   machine_mode mode;
15141   int unsignedp, reversep, volatilep = 0;
15142   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15143
15144   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15145                              &bitsize, &bitpos, &offset, &mode,
15146                              &unsignedp, &reversep, &volatilep, false);
15147   STRIP_NOPS (obj);
15148   if (bitpos % BITS_PER_UNIT)
15149     {
15150       expansion_failed (loc, NULL_RTX, "bitfield access");
15151       return 0;
15152     }
15153   if (!INDIRECT_REF_P (obj))
15154     {
15155       expansion_failed (obj,
15156                         NULL_RTX, "no indirect ref in inner refrence");
15157       return 0;
15158     }
15159   if (!offset && !bitpos)
15160     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15161                                    context);
15162   else if (toplev
15163            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15164            && (dwarf_version >= 4 || !dwarf_strict))
15165     {
15166       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15167       if (!list_ret)
15168         return 0;
15169       if (offset)
15170         {
15171           /* Variable offset.  */
15172           list_ret1 = loc_list_from_tree (offset, 0, context);
15173           if (list_ret1 == 0)
15174             return 0;
15175           add_loc_list (&list_ret, list_ret1);
15176           if (!list_ret)
15177             return 0;
15178           add_loc_descr_to_each (list_ret,
15179                                  new_loc_descr (DW_OP_plus, 0, 0));
15180         }
15181       bytepos = bitpos / BITS_PER_UNIT;
15182       if (bytepos > 0)
15183         add_loc_descr_to_each (list_ret,
15184                                new_loc_descr (DW_OP_plus_uconst,
15185                                               bytepos, 0));
15186       else if (bytepos < 0)
15187         loc_list_plus_const (list_ret, bytepos);
15188       add_loc_descr_to_each (list_ret,
15189                              new_loc_descr (DW_OP_stack_value, 0, 0));
15190     }
15191   return list_ret;
15192 }
15193
15194 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15195    all operations from LOC are nops, move to the last one.  Insert in NOPS all
15196    operations that are skipped.  */
15197
15198 static void
15199 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15200                           hash_set<dw_loc_descr_ref> &nops)
15201 {
15202   while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15203     {
15204       nops.add (loc);
15205       loc = loc->dw_loc_next;
15206     }
15207 }
15208
15209 /* Helper for loc_descr_without_nops: free the location description operation
15210    P.  */
15211
15212 bool
15213 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15214 {
15215   ggc_free (loc);
15216   return true;
15217 }
15218
15219 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15220    finishes LOC.  */
15221
15222 static void
15223 loc_descr_without_nops (dw_loc_descr_ref &loc)
15224 {
15225   if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15226     return;
15227
15228   /* Set of all DW_OP_nop operations we remove.  */
15229   hash_set<dw_loc_descr_ref> nops;
15230
15231   /* First, strip all prefix NOP operations in order to keep the head of the
15232      operations list.  */
15233   loc_descr_to_next_no_nop (loc, nops);
15234
15235   for (dw_loc_descr_ref cur = loc; cur != NULL;)
15236     {
15237       /* For control flow operations: strip "prefix" nops in destination
15238          labels.  */
15239       if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15240         loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15241       if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15242         loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15243
15244       /* Do the same for the operations that follow, then move to the next
15245          iteration.  */
15246       if (cur->dw_loc_next != NULL)
15247         loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15248       cur = cur->dw_loc_next;
15249     }
15250
15251   nops.traverse<void *, free_loc_descr> (NULL);
15252 }
15253
15254
15255 struct dwarf_procedure_info;
15256
15257 /* Helper structure for location descriptions generation.  */
15258 struct loc_descr_context
15259 {
15260   /* The type that is implicitly referenced by DW_OP_push_object_address, or
15261      NULL_TREE if DW_OP_push_object_address in invalid for this location
15262      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
15263   tree context_type;
15264   /* The ..._DECL node that should be translated as a
15265      DW_OP_push_object_address operation.  */
15266   tree base_decl;
15267   /* Information about the DWARF procedure we are currently generating. NULL if
15268      we are not generating a DWARF procedure.  */
15269   struct dwarf_procedure_info *dpi;
15270 };
15271
15272 /* DWARF procedures generation
15273
15274    DWARF expressions (aka. location descriptions) are used to encode variable
15275    things such as sizes or offsets.  Such computations can have redundant parts
15276    that can be factorized in order to reduce the size of the output debug
15277    information.  This is the whole point of DWARF procedures.
15278
15279    Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15280    already factorized into functions ("size functions") in order to handle very
15281    big and complex types.  Such functions are quite simple: they have integral
15282    arguments, they return an integral result and their body contains only a
15283    return statement with arithmetic expressions.  This is the only kind of
15284    function we are interested in translating into DWARF procedures, here.
15285
15286    DWARF expressions and DWARF procedure are executed using a stack, so we have
15287    to define some calling convention for them to interact.  Let's say that:
15288
15289    - Before calling a DWARF procedure, DWARF expressions must push on the stack
15290      all arguments in reverse order (right-to-left) so that when the DWARF
15291      procedure execution starts, the first argument is the top of the stack.
15292
15293    - Then, when returning, the DWARF procedure must have consumed all arguments
15294      on the stack, must have pushed the result and touched nothing else.
15295
15296    - Each integral argument and the result are integral types can be hold in a
15297      single stack slot.
15298
15299    - We call "frame offset" the number of stack slots that are "under DWARF
15300      procedure control": it includes the arguments slots, the temporaries and
15301      the result slot. Thus, it is equal to the number of arguments when the
15302      procedure execution starts and must be equal to one (the result) when it
15303      returns.  */
15304
15305 /* Helper structure used when generating operations for a DWARF procedure.  */
15306 struct dwarf_procedure_info
15307 {
15308   /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15309      currently translated.  */
15310   tree fndecl;
15311   /* The number of arguments FNDECL takes.  */
15312   unsigned args_count;
15313 };
15314
15315 /* Return a pointer to a newly created DIE node for a DWARF procedure.  Add
15316    LOCATION as its DW_AT_location attribute.  If FNDECL is not NULL_TREE,
15317    equate it to this DIE.  */
15318
15319 static dw_die_ref
15320 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15321                     dw_die_ref parent_die)
15322 {
15323   const bool dwarf_proc_supported = dwarf_version >= 4;
15324   dw_die_ref dwarf_proc_die;
15325
15326   if ((dwarf_version < 3 && dwarf_strict)
15327       || location == NULL)
15328     return NULL;
15329
15330   dwarf_proc_die  = new_die (dwarf_proc_supported
15331                              ? DW_TAG_dwarf_procedure
15332                              : DW_TAG_variable,
15333                              parent_die,
15334                              fndecl);
15335   if (fndecl)
15336     equate_decl_number_to_die (fndecl, dwarf_proc_die);
15337   if (!dwarf_proc_supported)
15338     add_AT_flag (dwarf_proc_die, DW_AT_artificial, 1);
15339   add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15340   return dwarf_proc_die;
15341 }
15342
15343 /* Return whether TYPE is a supported type as a DWARF procedure argument
15344    type or return type (we handle only scalar types and pointer types that
15345    aren't wider than the DWARF expression evaluation stack.  */
15346
15347 static bool
15348 is_handled_procedure_type (tree type)
15349 {
15350   return ((INTEGRAL_TYPE_P (type)
15351            || TREE_CODE (type) == OFFSET_TYPE
15352            || TREE_CODE (type) == POINTER_TYPE)
15353           && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15354 }
15355
15356 /* Helper for resolve_args_picking.  Stop when coming across VISITED nodes.  */
15357
15358 static bool
15359 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15360                         struct dwarf_procedure_info *dpi,
15361                         hash_set<dw_loc_descr_ref> &visited)
15362 {
15363   /* The "frame_offset" identifier is already used to name a macro... */
15364   unsigned frame_offset_ = initial_frame_offset;
15365   dw_loc_descr_ref l;
15366
15367   for (l = loc; l != NULL;)
15368     {
15369       /* If we already met this node, there is nothing to compute anymore.  */
15370       if (visited.add (l))
15371         {
15372 #if ENABLE_CHECKING
15373           /* Make sure that the stack size is consistent wherever the execution
15374              flow comes from.  */
15375           gcc_assert ((unsigned) l->dw_loc_frame_offset == frame_offset_);
15376 #endif
15377           break;
15378         }
15379 #if ENABLE_CHECKING
15380       l->dw_loc_frame_offset = frame_offset_;
15381 #endif
15382
15383       /* If needed, relocate the picking offset with respect to the frame
15384          offset. */
15385       if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15386         {
15387           /* frame_offset_ is the size of the current stack frame, including
15388              incoming arguments. Besides, the arguments are pushed
15389              right-to-left.  Thus, in order to access the Nth argument from
15390              this operation node, the picking has to skip temporaries *plus*
15391              one stack slot per argument (0 for the first one, 1 for the second
15392              one, etc.).
15393
15394              The targetted argument number (N) is already set as the operand,
15395              and the number of temporaries can be computed with:
15396                frame_offsets_ - dpi->args_count */
15397           l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15398
15399           /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)...  */
15400           if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15401             return false;
15402         }
15403
15404       /* Update frame_offset according to the effect the current operation has
15405          on the stack.  */
15406       switch (l->dw_loc_opc)
15407         {
15408         case DW_OP_deref:
15409         case DW_OP_swap:
15410         case DW_OP_rot:
15411         case DW_OP_abs:
15412         case DW_OP_not:
15413         case DW_OP_plus_uconst:
15414         case DW_OP_skip:
15415         case DW_OP_reg0:
15416         case DW_OP_reg1:
15417         case DW_OP_reg2:
15418         case DW_OP_reg3:
15419         case DW_OP_reg4:
15420         case DW_OP_reg5:
15421         case DW_OP_reg6:
15422         case DW_OP_reg7:
15423         case DW_OP_reg8:
15424         case DW_OP_reg9:
15425         case DW_OP_reg10:
15426         case DW_OP_reg11:
15427         case DW_OP_reg12:
15428         case DW_OP_reg13:
15429         case DW_OP_reg14:
15430         case DW_OP_reg15:
15431         case DW_OP_reg16:
15432         case DW_OP_reg17:
15433         case DW_OP_reg18:
15434         case DW_OP_reg19:
15435         case DW_OP_reg20:
15436         case DW_OP_reg21:
15437         case DW_OP_reg22:
15438         case DW_OP_reg23:
15439         case DW_OP_reg24:
15440         case DW_OP_reg25:
15441         case DW_OP_reg26:
15442         case DW_OP_reg27:
15443         case DW_OP_reg28:
15444         case DW_OP_reg29:
15445         case DW_OP_reg30:
15446         case DW_OP_reg31:
15447         case DW_OP_bregx:
15448         case DW_OP_piece:
15449         case DW_OP_deref_size:
15450         case DW_OP_nop:
15451         case DW_OP_form_tls_address:
15452         case DW_OP_bit_piece:
15453         case DW_OP_implicit_value:
15454         case DW_OP_stack_value:
15455           break;
15456
15457         case DW_OP_addr:
15458         case DW_OP_const1u:
15459         case DW_OP_const1s:
15460         case DW_OP_const2u:
15461         case DW_OP_const2s:
15462         case DW_OP_const4u:
15463         case DW_OP_const4s:
15464         case DW_OP_const8u:
15465         case DW_OP_const8s:
15466         case DW_OP_constu:
15467         case DW_OP_consts:
15468         case DW_OP_dup:
15469         case DW_OP_over:
15470         case DW_OP_pick:
15471         case DW_OP_lit0:
15472         case DW_OP_lit1:
15473         case DW_OP_lit2:
15474         case DW_OP_lit3:
15475         case DW_OP_lit4:
15476         case DW_OP_lit5:
15477         case DW_OP_lit6:
15478         case DW_OP_lit7:
15479         case DW_OP_lit8:
15480         case DW_OP_lit9:
15481         case DW_OP_lit10:
15482         case DW_OP_lit11:
15483         case DW_OP_lit12:
15484         case DW_OP_lit13:
15485         case DW_OP_lit14:
15486         case DW_OP_lit15:
15487         case DW_OP_lit16:
15488         case DW_OP_lit17:
15489         case DW_OP_lit18:
15490         case DW_OP_lit19:
15491         case DW_OP_lit20:
15492         case DW_OP_lit21:
15493         case DW_OP_lit22:
15494         case DW_OP_lit23:
15495         case DW_OP_lit24:
15496         case DW_OP_lit25:
15497         case DW_OP_lit26:
15498         case DW_OP_lit27:
15499         case DW_OP_lit28:
15500         case DW_OP_lit29:
15501         case DW_OP_lit30:
15502         case DW_OP_lit31:
15503         case DW_OP_breg0:
15504         case DW_OP_breg1:
15505         case DW_OP_breg2:
15506         case DW_OP_breg3:
15507         case DW_OP_breg4:
15508         case DW_OP_breg5:
15509         case DW_OP_breg6:
15510         case DW_OP_breg7:
15511         case DW_OP_breg8:
15512         case DW_OP_breg9:
15513         case DW_OP_breg10:
15514         case DW_OP_breg11:
15515         case DW_OP_breg12:
15516         case DW_OP_breg13:
15517         case DW_OP_breg14:
15518         case DW_OP_breg15:
15519         case DW_OP_breg16:
15520         case DW_OP_breg17:
15521         case DW_OP_breg18:
15522         case DW_OP_breg19:
15523         case DW_OP_breg20:
15524         case DW_OP_breg21:
15525         case DW_OP_breg22:
15526         case DW_OP_breg23:
15527         case DW_OP_breg24:
15528         case DW_OP_breg25:
15529         case DW_OP_breg26:
15530         case DW_OP_breg27:
15531         case DW_OP_breg28:
15532         case DW_OP_breg29:
15533         case DW_OP_breg30:
15534         case DW_OP_breg31:
15535         case DW_OP_fbreg:
15536         case DW_OP_push_object_address:
15537         case DW_OP_call_frame_cfa:
15538           ++frame_offset_;
15539           break;
15540
15541         case DW_OP_drop:
15542         case DW_OP_xderef:
15543         case DW_OP_and:
15544         case DW_OP_div:
15545         case DW_OP_minus:
15546         case DW_OP_mod:
15547         case DW_OP_mul:
15548         case DW_OP_neg:
15549         case DW_OP_or:
15550         case DW_OP_plus:
15551         case DW_OP_shl:
15552         case DW_OP_shr:
15553         case DW_OP_shra:
15554         case DW_OP_xor:
15555         case DW_OP_bra:
15556         case DW_OP_eq:
15557         case DW_OP_ge:
15558         case DW_OP_gt:
15559         case DW_OP_le:
15560         case DW_OP_lt:
15561         case DW_OP_ne:
15562         case DW_OP_regx:
15563         case DW_OP_xderef_size:
15564           --frame_offset_;
15565           break;
15566
15567         case DW_OP_call2:
15568         case DW_OP_call4:
15569         case DW_OP_call_ref:
15570           {
15571             dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15572             int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15573
15574             if (stack_usage == NULL)
15575               return false;
15576             frame_offset += *stack_usage;
15577             break;
15578           }
15579
15580         case DW_OP_GNU_push_tls_address:
15581         case DW_OP_GNU_uninit:
15582         case DW_OP_GNU_encoded_addr:
15583         case DW_OP_GNU_implicit_pointer:
15584         case DW_OP_GNU_entry_value:
15585         case DW_OP_GNU_const_type:
15586         case DW_OP_GNU_regval_type:
15587         case DW_OP_GNU_deref_type:
15588         case DW_OP_GNU_convert:
15589         case DW_OP_GNU_reinterpret:
15590         case DW_OP_GNU_parameter_ref:
15591           /* loc_list_from_tree will probably not output these operations for
15592              size functions, so assume they will not appear here.  */
15593           /* Fall through...  */
15594
15595         default:
15596           gcc_unreachable ();
15597         }
15598
15599       /* Now, follow the control flow (except subroutine calls).  */
15600       switch (l->dw_loc_opc)
15601         {
15602         case DW_OP_bra:
15603           if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15604                                        visited))
15605             return false;
15606           /* Fall through... */
15607
15608         case DW_OP_skip:
15609           l = l->dw_loc_oprnd1.v.val_loc;
15610           break;
15611
15612         case DW_OP_stack_value:
15613           return true;
15614
15615         default:
15616           l = l->dw_loc_next;
15617           break;
15618         }
15619     }
15620
15621   return true;
15622 }
15623
15624 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15625    operations) in order to resolve the operand of DW_OP_pick operations that
15626    target DWARF procedure arguments (DPI).  Stop at already visited nodes.
15627    INITIAL_FRAME_OFFSET is the frame offset *before* LOC is executed.  Return
15628    if all relocations were successful.  */
15629
15630 static bool
15631 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15632                       struct dwarf_procedure_info *dpi)
15633 {
15634   hash_set<dw_loc_descr_ref> visited;
15635
15636   return resolve_args_picking_1 (loc, initial_frame_offset, dpi, visited);
15637 }
15638
15639 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15640    Return NULL if it is not possible.  */
15641
15642 static dw_die_ref
15643 function_to_dwarf_procedure (tree fndecl)
15644 {
15645   struct loc_descr_context ctx;
15646   struct dwarf_procedure_info dpi;
15647   dw_die_ref dwarf_proc_die;
15648   tree tree_body = DECL_SAVED_TREE (fndecl);
15649   dw_loc_descr_ref loc_body, epilogue;
15650
15651   tree cursor;
15652   unsigned i;
15653
15654   /* Do not generate multiple DWARF procedures for the same function
15655      declaration.  */
15656   dwarf_proc_die = lookup_decl_die (fndecl);
15657   if (dwarf_proc_die != NULL)
15658     return dwarf_proc_die;
15659
15660   /* DWARF procedures are available starting with the DWARFv3 standard, but
15661      it's the DWARFv4 standard that introduces the DW_TAG_dwarf_procedure
15662      DIE.  */
15663   if (dwarf_version < 3 && dwarf_strict)
15664     return NULL;
15665
15666   /* We handle only functions for which we still have a body, that return a
15667      supported type and that takes arguments with supported types.  Note that
15668      there is no point translating functions that return nothing.  */
15669   if (tree_body == NULL_TREE
15670       || DECL_RESULT (fndecl) == NULL_TREE
15671       || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15672     return NULL;
15673
15674   for (cursor = DECL_ARGUMENTS (fndecl);
15675        cursor != NULL_TREE;
15676        cursor = TREE_CHAIN (cursor))
15677     if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15678       return NULL;
15679
15680   /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)).  */
15681   if (TREE_CODE (tree_body) != RETURN_EXPR)
15682     return NULL;
15683   tree_body = TREE_OPERAND (tree_body, 0);
15684   if (TREE_CODE (tree_body) != MODIFY_EXPR
15685       || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15686     return NULL;
15687   tree_body = TREE_OPERAND (tree_body, 1);
15688
15689   /* Try to translate the body expression itself.  Note that this will probably
15690      cause an infinite recursion if its call graph has a cycle.  This is very
15691      unlikely for size functions, however, so don't bother with such things at
15692      the moment.  */
15693   ctx.context_type = NULL_TREE;
15694   ctx.base_decl = NULL_TREE;
15695   ctx.dpi = &dpi;
15696   dpi.fndecl = fndecl;
15697   dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15698   loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15699   if (!loc_body)
15700     return NULL;
15701
15702   /* After evaluating all operands in "loc_body", we should still have on the
15703      stack all arguments plus the desired function result (top of the stack).
15704      Generate code in order to keep only the result in our stack frame.  */
15705   epilogue = NULL;
15706   for (i = 0; i < dpi.args_count; ++i)
15707     {
15708       dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15709       op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15710       op_couple->dw_loc_next->dw_loc_next = epilogue;
15711       epilogue = op_couple;
15712     }
15713   add_loc_descr (&loc_body, epilogue);
15714   if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15715     return NULL;
15716
15717   /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15718      because they are considered useful.  Now there is an epilogue, they are
15719      not anymore, so give it another try.   */
15720   loc_descr_without_nops (loc_body);
15721
15722   /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15723      a DW_TAG_dwarf_procedure, so we may have a conflict, here.  It's unlikely,
15724      though, given that size functions do not come from source, so they should
15725      not have a dedicated DW_TAG_subprogram DIE.  */
15726   dwarf_proc_die
15727     = new_dwarf_proc_die (loc_body, fndecl,
15728                           get_context_die (DECL_CONTEXT (fndecl)));
15729
15730   /* The called DWARF procedure consumes one stack slot per argument and
15731      returns one stack slot.  */
15732   dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15733
15734   return dwarf_proc_die;
15735 }
15736
15737
15738 /* Generate Dwarf location list representing LOC.
15739    If WANT_ADDRESS is false, expression computing LOC will be computed
15740    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15741    if WANT_ADDRESS is 2, expression computing address useable in location
15742      will be returned (i.e. DW_OP_reg can be used
15743      to refer to register values).
15744
15745    CONTEXT provides information to customize the location descriptions
15746    generation.  Its context_type field specifies what type is implicitly
15747    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
15748    will not be generated.
15749
15750    Its DPI field determines whether we are generating a DWARF expression for a
15751    DWARF procedure, so PARM_DECL references are processed specifically.
15752
15753    If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15754    and dpi fields were null.  */
15755
15756 static dw_loc_list_ref
15757 loc_list_from_tree_1 (tree loc, int want_address,
15758                       const struct loc_descr_context *context)
15759 {
15760   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15761   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15762   int have_address = 0;
15763   enum dwarf_location_atom op;
15764
15765   /* ??? Most of the time we do not take proper care for sign/zero
15766      extending the values properly.  Hopefully this won't be a real
15767      problem...  */
15768
15769   if (context != NULL
15770       && context->base_decl == loc
15771       && want_address == 0)
15772     {
15773       if (dwarf_version >= 3 || !dwarf_strict)
15774         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15775                              NULL, NULL, NULL);
15776       else
15777         return NULL;
15778     }
15779
15780   switch (TREE_CODE (loc))
15781     {
15782     case ERROR_MARK:
15783       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15784       return 0;
15785
15786     case PLACEHOLDER_EXPR:
15787       /* This case involves extracting fields from an object to determine the
15788          position of other fields. It is supposed to appear only as the first
15789          operand of COMPONENT_REF nodes and to reference precisely the type
15790          that the context allows.  */
15791       if (context != NULL
15792           && TREE_TYPE (loc) == context->context_type
15793           && want_address >= 1)
15794         {
15795           if (dwarf_version >= 3 || !dwarf_strict)
15796             {
15797               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15798               have_address = 1;
15799               break;
15800             }
15801           else
15802             return NULL;
15803         }
15804       else
15805         expansion_failed (loc, NULL_RTX,
15806                           "PLACEHOLDER_EXPR for an unexpected type");
15807       break;
15808
15809     case CALL_EXPR:
15810         {
15811           const int nargs = call_expr_nargs (loc);
15812           tree callee = get_callee_fndecl (loc);
15813           int i;
15814           dw_die_ref dwarf_proc;
15815
15816           if (callee == NULL_TREE)
15817             goto call_expansion_failed;
15818
15819           /* We handle only functions that return an integer.  */
15820           if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15821             goto call_expansion_failed;
15822
15823           dwarf_proc = function_to_dwarf_procedure (callee);
15824           if (dwarf_proc == NULL)
15825             goto call_expansion_failed;
15826
15827           /* Evaluate arguments right-to-left so that the first argument will
15828              be the top-most one on the stack.  */
15829           for (i = nargs - 1; i >= 0; --i)
15830             {
15831               dw_loc_descr_ref loc_descr
15832                 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15833                                             context);
15834
15835               if (loc_descr == NULL)
15836                 goto call_expansion_failed;
15837
15838               add_loc_descr (&ret, loc_descr);
15839             }
15840
15841           ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15842           ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15843           ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15844           ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15845           add_loc_descr (&ret, ret1);
15846           break;
15847
15848         call_expansion_failed:
15849           expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15850           /* There are no opcodes for these operations.  */
15851           return 0;
15852         }
15853
15854     case PREINCREMENT_EXPR:
15855     case PREDECREMENT_EXPR:
15856     case POSTINCREMENT_EXPR:
15857     case POSTDECREMENT_EXPR:
15858       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15859       /* There are no opcodes for these operations.  */
15860       return 0;
15861
15862     case ADDR_EXPR:
15863       /* If we already want an address, see if there is INDIRECT_REF inside
15864          e.g. for &this->field.  */
15865       if (want_address)
15866         {
15867           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15868                        (loc, want_address == 2, context);
15869           if (list_ret)
15870             have_address = 1;
15871           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15872                    && (ret = cst_pool_loc_descr (loc)))
15873             have_address = 1;
15874         }
15875         /* Otherwise, process the argument and look for the address.  */
15876       if (!list_ret && !ret)
15877         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15878       else
15879         {
15880           if (want_address)
15881             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15882           return NULL;
15883         }
15884       break;
15885
15886     case VAR_DECL:
15887       if (DECL_THREAD_LOCAL_P (loc))
15888         {
15889           rtx rtl;
15890          enum dwarf_location_atom tls_op;
15891          enum dtprel_bool dtprel = dtprel_false;
15892
15893           if (targetm.have_tls)
15894             {
15895               /* If this is not defined, we have no way to emit the
15896                  data.  */
15897               if (!targetm.asm_out.output_dwarf_dtprel)
15898                 return 0;
15899
15900                /* The way DW_OP_GNU_push_tls_address is specified, we
15901                   can only look up addresses of objects in the current
15902                   module.  We used DW_OP_addr as first op, but that's
15903                   wrong, because DW_OP_addr is relocated by the debug
15904                   info consumer, while DW_OP_GNU_push_tls_address
15905                   operand shouldn't be.  */
15906               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15907                 return 0;
15908              dtprel = dtprel_true;
15909              tls_op = DW_OP_GNU_push_tls_address;
15910             }
15911           else
15912             {
15913               if (!targetm.emutls.debug_form_tls_address
15914                   || !(dwarf_version >= 3 || !dwarf_strict))
15915                 return 0;
15916               /* We stuffed the control variable into the DECL_VALUE_EXPR
15917                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15918                  no longer appear in gimple code.  We used the control
15919                  variable in specific so that we could pick it up here.  */
15920               loc = DECL_VALUE_EXPR (loc);
15921               tls_op = DW_OP_form_tls_address;
15922             }
15923
15924           rtl = rtl_for_decl_location (loc);
15925           if (rtl == NULL_RTX)
15926             return 0;
15927
15928           if (!MEM_P (rtl))
15929             return 0;
15930           rtl = XEXP (rtl, 0);
15931           if (! CONSTANT_P (rtl))
15932             return 0;
15933
15934           ret = new_addr_loc_descr (rtl, dtprel);
15935           ret1 = new_loc_descr (tls_op, 0, 0);
15936           add_loc_descr (&ret, ret1);
15937
15938           have_address = 1;
15939           break;
15940         }
15941       /* FALLTHRU */
15942
15943     case PARM_DECL:
15944       if (context != NULL && context->dpi != NULL
15945           && DECL_CONTEXT (loc) == context->dpi->fndecl)
15946         {
15947           /* We are generating code for a DWARF procedure and we want to access
15948              one of its arguments: find the appropriate argument offset and let
15949              the resolve_args_picking pass compute the offset that complies
15950              with the stack frame size.  */
15951           unsigned i = 0;
15952           tree cursor;
15953
15954           for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15955                cursor != NULL_TREE && cursor != loc;
15956                cursor = TREE_CHAIN (cursor), ++i)
15957             ;
15958           /* If we are translating a DWARF procedure, all referenced parameters
15959              must belong to the current function.  */
15960           gcc_assert (cursor != NULL_TREE);
15961
15962           ret = new_loc_descr (DW_OP_pick, i, 0);
15963           ret->frame_offset_rel = 1;
15964           break;
15965         }
15966       /* FALLTHRU */
15967
15968     case RESULT_DECL:
15969       if (DECL_HAS_VALUE_EXPR_P (loc))
15970         return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15971                                      want_address, context);
15972       /* FALLTHRU */
15973
15974     case FUNCTION_DECL:
15975       {
15976         rtx rtl;
15977         var_loc_list *loc_list = lookup_decl_loc (loc);
15978
15979         if (loc_list && loc_list->first)
15980           {
15981             list_ret = dw_loc_list (loc_list, loc, want_address);
15982             have_address = want_address != 0;
15983             break;
15984           }
15985         rtl = rtl_for_decl_location (loc);
15986         if (rtl == NULL_RTX)
15987           {
15988             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15989             return 0;
15990           }
15991         else if (CONST_INT_P (rtl))
15992           {
15993             HOST_WIDE_INT val = INTVAL (rtl);
15994             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15995               val &= GET_MODE_MASK (DECL_MODE (loc));
15996             ret = int_loc_descriptor (val);
15997           }
15998         else if (GET_CODE (rtl) == CONST_STRING)
15999           {
16000             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16001             return 0;
16002           }
16003         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16004           ret = new_addr_loc_descr (rtl, dtprel_false);
16005         else
16006           {
16007             machine_mode mode, mem_mode;
16008
16009             /* Certain constructs can only be represented at top-level.  */
16010             if (want_address == 2)
16011               {
16012                 ret = loc_descriptor (rtl, VOIDmode,
16013                                       VAR_INIT_STATUS_INITIALIZED);
16014                 have_address = 1;
16015               }
16016             else
16017               {
16018                 mode = GET_MODE (rtl);
16019                 mem_mode = VOIDmode;
16020                 if (MEM_P (rtl))
16021                   {
16022                     mem_mode = mode;
16023                     mode = get_address_mode (rtl);
16024                     rtl = XEXP (rtl, 0);
16025                     have_address = 1;
16026                   }
16027                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16028                                           VAR_INIT_STATUS_INITIALIZED);
16029               }
16030             if (!ret)
16031               expansion_failed (loc, rtl,
16032                                 "failed to produce loc descriptor for rtl");
16033           }
16034       }
16035       break;
16036
16037     case MEM_REF:
16038       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16039         {
16040           have_address = 1;
16041           goto do_plus;
16042         }
16043       /* Fallthru.  */
16044     case INDIRECT_REF:
16045       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16046       have_address = 1;
16047       break;
16048
16049     case TARGET_MEM_REF:
16050     case SSA_NAME:
16051     case DEBUG_EXPR_DECL:
16052       return NULL;
16053
16054     case COMPOUND_EXPR:
16055       return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16056                                    context);
16057
16058     CASE_CONVERT:
16059     case VIEW_CONVERT_EXPR:
16060     case SAVE_EXPR:
16061     case MODIFY_EXPR:
16062     case NON_LVALUE_EXPR:
16063       return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16064                                    context);
16065
16066     case COMPONENT_REF:
16067     case BIT_FIELD_REF:
16068     case ARRAY_REF:
16069     case ARRAY_RANGE_REF:
16070     case REALPART_EXPR:
16071     case IMAGPART_EXPR:
16072       {
16073         tree obj, offset;
16074         HOST_WIDE_INT bitsize, bitpos, bytepos;
16075         machine_mode mode;
16076         int unsignedp, reversep, volatilep = 0;
16077
16078         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16079                                    &unsignedp, &reversep, &volatilep, false);
16080
16081         gcc_assert (obj != loc);
16082
16083         list_ret = loc_list_from_tree_1 (obj,
16084                                          want_address == 2
16085                                          && !bitpos && !offset ? 2 : 1,
16086                                          context);
16087         /* TODO: We can extract value of the small expression via shifting even
16088            for nonzero bitpos.  */
16089         if (list_ret == 0)
16090           return 0;
16091         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16092           {
16093             expansion_failed (loc, NULL_RTX,
16094                               "bitfield access");
16095             return 0;
16096           }
16097
16098         if (offset != NULL_TREE)
16099           {
16100             /* Variable offset.  */
16101             list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16102             if (list_ret1 == 0)
16103               return 0;
16104             add_loc_list (&list_ret, list_ret1);
16105             if (!list_ret)
16106               return 0;
16107             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16108           }
16109
16110         bytepos = bitpos / BITS_PER_UNIT;
16111         if (bytepos > 0)
16112           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16113         else if (bytepos < 0)
16114           loc_list_plus_const (list_ret, bytepos);
16115
16116         have_address = 1;
16117         break;
16118       }
16119
16120     case INTEGER_CST:
16121       if ((want_address || !tree_fits_shwi_p (loc))
16122           && (ret = cst_pool_loc_descr (loc)))
16123         have_address = 1;
16124       else if (want_address == 2
16125                && tree_fits_shwi_p (loc)
16126                && (ret = address_of_int_loc_descriptor
16127                            (int_size_in_bytes (TREE_TYPE (loc)),
16128                             tree_to_shwi (loc))))
16129         have_address = 1;
16130       else if (tree_fits_shwi_p (loc))
16131         ret = int_loc_descriptor (tree_to_shwi (loc));
16132       else if (tree_fits_uhwi_p (loc))
16133         ret = uint_loc_descriptor (tree_to_uhwi (loc));
16134       else
16135         {
16136           expansion_failed (loc, NULL_RTX,
16137                             "Integer operand is not host integer");
16138           return 0;
16139         }
16140       break;
16141
16142     case CONSTRUCTOR:
16143     case REAL_CST:
16144     case STRING_CST:
16145     case COMPLEX_CST:
16146       if ((ret = cst_pool_loc_descr (loc)))
16147         have_address = 1;
16148       else
16149       /* We can construct small constants here using int_loc_descriptor.  */
16150         expansion_failed (loc, NULL_RTX,
16151                           "constructor or constant not in constant pool");
16152       break;
16153
16154     case TRUTH_AND_EXPR:
16155     case TRUTH_ANDIF_EXPR:
16156     case BIT_AND_EXPR:
16157       op = DW_OP_and;
16158       goto do_binop;
16159
16160     case TRUTH_XOR_EXPR:
16161     case BIT_XOR_EXPR:
16162       op = DW_OP_xor;
16163       goto do_binop;
16164
16165     case TRUTH_OR_EXPR:
16166     case TRUTH_ORIF_EXPR:
16167     case BIT_IOR_EXPR:
16168       op = DW_OP_or;
16169       goto do_binop;
16170
16171     case FLOOR_DIV_EXPR:
16172     case CEIL_DIV_EXPR:
16173     case ROUND_DIV_EXPR:
16174     case TRUNC_DIV_EXPR:
16175     case EXACT_DIV_EXPR:
16176       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16177         return 0;
16178       op = DW_OP_div;
16179       goto do_binop;
16180
16181     case MINUS_EXPR:
16182       op = DW_OP_minus;
16183       goto do_binop;
16184
16185     case FLOOR_MOD_EXPR:
16186     case CEIL_MOD_EXPR:
16187     case ROUND_MOD_EXPR:
16188     case TRUNC_MOD_EXPR:
16189       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16190         {
16191           op = DW_OP_mod;
16192           goto do_binop;
16193         }
16194       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16195       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16196       if (list_ret == 0 || list_ret1 == 0)
16197         return 0;
16198
16199       add_loc_list (&list_ret, list_ret1);
16200       if (list_ret == 0)
16201         return 0;
16202       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16203       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16204       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16205       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16206       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16207       break;
16208
16209     case MULT_EXPR:
16210       op = DW_OP_mul;
16211       goto do_binop;
16212
16213     case LSHIFT_EXPR:
16214       op = DW_OP_shl;
16215       goto do_binop;
16216
16217     case RSHIFT_EXPR:
16218       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16219       goto do_binop;
16220
16221     case POINTER_PLUS_EXPR:
16222     case PLUS_EXPR:
16223     do_plus:
16224       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16225         {
16226           /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16227              smarter to encode their opposite.  The DW_OP_plus_uconst operation
16228              takes 1 + X bytes, X being the size of the ULEB128 addend.  On the
16229              other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16230              bytes, Y being the size of the operation that pushes the opposite
16231              of the addend.  So let's choose the smallest representation.  */
16232           const tree tree_addend = TREE_OPERAND (loc, 1);
16233           offset_int wi_addend;
16234           HOST_WIDE_INT shwi_addend;
16235           dw_loc_descr_ref loc_naddend;
16236
16237           list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16238           if (list_ret == 0)
16239             return 0;
16240
16241           /* Try to get the literal to push.  It is the opposite of the addend,
16242              so as we rely on wrapping during DWARF evaluation, first decode
16243              the literal as a "DWARF-sized" signed number.  */
16244           wi_addend = wi::to_offset (tree_addend);
16245           wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16246           shwi_addend = wi_addend.to_shwi ();
16247           loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16248                         ? int_loc_descriptor (-shwi_addend)
16249                         : NULL;
16250
16251           if (loc_naddend != NULL
16252               && ((unsigned) size_of_uleb128 (shwi_addend)
16253                   > size_of_loc_descr (loc_naddend)))
16254             {
16255               add_loc_descr_to_each (list_ret, loc_naddend);
16256               add_loc_descr_to_each (list_ret,
16257                                      new_loc_descr (DW_OP_minus, 0, 0));
16258             }
16259           else
16260             {
16261               for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16262                 {
16263                   loc_naddend = loc_cur;
16264                   loc_cur = loc_cur->dw_loc_next;
16265                   ggc_free (loc_naddend);
16266                 }
16267               loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16268             }
16269           break;
16270         }
16271
16272       op = DW_OP_plus;
16273       goto do_binop;
16274
16275     case LE_EXPR:
16276       op = DW_OP_le;
16277       goto do_comp_binop;
16278
16279     case GE_EXPR:
16280       op = DW_OP_ge;
16281       goto do_comp_binop;
16282
16283     case LT_EXPR:
16284       op = DW_OP_lt;
16285       goto do_comp_binop;
16286
16287     case GT_EXPR:
16288       op = DW_OP_gt;
16289       goto do_comp_binop;
16290
16291     do_comp_binop:
16292       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16293         {
16294           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16295           list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16296           list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16297                                                     TREE_CODE (loc));
16298           break;
16299         }
16300       else
16301         goto do_binop;
16302
16303     case EQ_EXPR:
16304       op = DW_OP_eq;
16305       goto do_binop;
16306
16307     case NE_EXPR:
16308       op = DW_OP_ne;
16309       goto do_binop;
16310
16311     do_binop:
16312       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16313       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16314       if (list_ret == 0 || list_ret1 == 0)
16315         return 0;
16316
16317       add_loc_list (&list_ret, list_ret1);
16318       if (list_ret == 0)
16319         return 0;
16320       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16321       break;
16322
16323     case TRUTH_NOT_EXPR:
16324     case BIT_NOT_EXPR:
16325       op = DW_OP_not;
16326       goto do_unop;
16327
16328     case ABS_EXPR:
16329       op = DW_OP_abs;
16330       goto do_unop;
16331
16332     case NEGATE_EXPR:
16333       op = DW_OP_neg;
16334       goto do_unop;
16335
16336     do_unop:
16337       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16338       if (list_ret == 0)
16339         return 0;
16340
16341       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16342       break;
16343
16344     case MIN_EXPR:
16345     case MAX_EXPR:
16346       {
16347         const enum tree_code code =
16348           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16349
16350         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16351                       build2 (code, integer_type_node,
16352                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16353                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16354       }
16355
16356       /* ... fall through ...  */
16357
16358     case COND_EXPR:
16359       {
16360         dw_loc_descr_ref lhs
16361           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16362         dw_loc_list_ref rhs
16363           = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16364         dw_loc_descr_ref bra_node, jump_node, tmp;
16365
16366         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16367         if (list_ret == 0 || lhs == 0 || rhs == 0)
16368           return 0;
16369
16370         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16371         add_loc_descr_to_each (list_ret, bra_node);
16372
16373         add_loc_list (&list_ret, rhs);
16374         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16375         add_loc_descr_to_each (list_ret, jump_node);
16376
16377         add_loc_descr_to_each (list_ret, lhs);
16378         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16379         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16380
16381         /* ??? Need a node to point the skip at.  Use a nop.  */
16382         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16383         add_loc_descr_to_each (list_ret, tmp);
16384         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16385         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16386       }
16387       break;
16388
16389     case FIX_TRUNC_EXPR:
16390       return 0;
16391
16392     default:
16393       /* Leave front-end specific codes as simply unknown.  This comes
16394          up, for instance, with the C STMT_EXPR.  */
16395       if ((unsigned int) TREE_CODE (loc)
16396           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16397         {
16398           expansion_failed (loc, NULL_RTX,
16399                             "language specific tree node");
16400           return 0;
16401         }
16402
16403       /* Otherwise this is a generic code; we should just lists all of
16404          these explicitly.  We forgot one.  */
16405       if (flag_checking)
16406         gcc_unreachable ();
16407
16408       /* In a release build, we want to degrade gracefully: better to
16409          generate incomplete debugging information than to crash.  */
16410       return NULL;
16411     }
16412
16413   if (!ret && !list_ret)
16414     return 0;
16415
16416   if (want_address == 2 && !have_address
16417       && (dwarf_version >= 4 || !dwarf_strict))
16418     {
16419       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16420         {
16421           expansion_failed (loc, NULL_RTX,
16422                             "DWARF address size mismatch");
16423           return 0;
16424         }
16425       if (ret)
16426         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16427       else
16428         add_loc_descr_to_each (list_ret,
16429                                new_loc_descr (DW_OP_stack_value, 0, 0));
16430       have_address = 1;
16431     }
16432   /* Show if we can't fill the request for an address.  */
16433   if (want_address && !have_address)
16434     {
16435       expansion_failed (loc, NULL_RTX,
16436                         "Want address and only have value");
16437       return 0;
16438     }
16439
16440   gcc_assert (!ret || !list_ret);
16441
16442   /* If we've got an address and don't want one, dereference.  */
16443   if (!want_address && have_address)
16444     {
16445       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16446
16447       if (size > DWARF2_ADDR_SIZE || size == -1)
16448         {
16449           expansion_failed (loc, NULL_RTX,
16450                             "DWARF address size mismatch");
16451           return 0;
16452         }
16453       else if (size == DWARF2_ADDR_SIZE)
16454         op = DW_OP_deref;
16455       else
16456         op = DW_OP_deref_size;
16457
16458       if (ret)
16459         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16460       else
16461         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16462     }
16463   if (ret)
16464     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16465
16466   return list_ret;
16467 }
16468
16469 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16470    expressions.  */
16471
16472 static dw_loc_list_ref
16473 loc_list_from_tree (tree loc, int want_address,
16474                     const struct loc_descr_context *context)
16475 {
16476   dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16477
16478   for (dw_loc_list_ref loc_cur = result;
16479        loc_cur != NULL; loc_cur =
16480        loc_cur->dw_loc_next)
16481     loc_descr_without_nops (loc_cur->expr);
16482   return result;
16483 }
16484
16485 /* Same as above but return only single location expression.  */
16486 static dw_loc_descr_ref
16487 loc_descriptor_from_tree (tree loc, int want_address,
16488                           const struct loc_descr_context *context)
16489 {
16490   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16491   if (!ret)
16492     return NULL;
16493   if (ret->dw_loc_next)
16494     {
16495       expansion_failed (loc, NULL_RTX,
16496                         "Location list where only loc descriptor needed");
16497       return NULL;
16498     }
16499   return ret->expr;
16500 }
16501
16502 /* Given a value, round it up to the lowest multiple of `boundary'
16503    which is not less than the value itself.  */
16504
16505 static inline HOST_WIDE_INT
16506 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16507 {
16508   return (((value + boundary - 1) / boundary) * boundary);
16509 }
16510
16511 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16512    pointer to the declared type for the relevant field variable, or return
16513    `integer_type_node' if the given node turns out to be an
16514    ERROR_MARK node.  */
16515
16516 static inline tree
16517 field_type (const_tree decl)
16518 {
16519   tree type;
16520
16521   if (TREE_CODE (decl) == ERROR_MARK)
16522     return integer_type_node;
16523
16524   type = DECL_BIT_FIELD_TYPE (decl);
16525   if (type == NULL_TREE)
16526     type = TREE_TYPE (decl);
16527
16528   return type;
16529 }
16530
16531 /* Given a pointer to a tree node, return the alignment in bits for
16532    it, or else return BITS_PER_WORD if the node actually turns out to
16533    be an ERROR_MARK node.  */
16534
16535 static inline unsigned
16536 simple_type_align_in_bits (const_tree type)
16537 {
16538   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16539 }
16540
16541 static inline unsigned
16542 simple_decl_align_in_bits (const_tree decl)
16543 {
16544   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16545 }
16546
16547 /* Return the result of rounding T up to ALIGN.  */
16548
16549 static inline offset_int
16550 round_up_to_align (const offset_int &t, unsigned int align)
16551 {
16552   return wi::udiv_trunc (t + align - 1, align) * align;
16553 }
16554
16555 /* Compute the size of TYPE in bytes.  If possible, return NULL and store the
16556    size as an integer constant in CST_SIZE.  Otherwise, if possible, return a
16557    DWARF expression that computes the size.  Return NULL and set CST_SIZE to -1
16558    if we fail to return the size in one of these two forms.  */
16559
16560 static dw_loc_descr_ref
16561 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16562 {
16563   tree tree_size;
16564   struct loc_descr_context ctx;
16565
16566   /* Return a constant integer in priority, if possible.  */
16567   *cst_size = int_size_in_bytes (type);
16568   if (*cst_size != -1)
16569     return NULL;
16570
16571   ctx.context_type = const_cast<tree> (type);
16572   ctx.base_decl = NULL_TREE;
16573   ctx.dpi = NULL;
16574
16575   type = TYPE_MAIN_VARIANT (type);
16576   tree_size = TYPE_SIZE_UNIT (type);
16577   return ((tree_size != NULL_TREE)
16578           ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16579           : NULL);
16580 }
16581
16582 /* Helper structure for RECORD_TYPE processing.  */
16583 struct vlr_context
16584 {
16585   /* Root RECORD_TYPE.  It is needed to generate data member location
16586      descriptions in variable-length records (VLR), but also to cope with
16587      variants, which are composed of nested structures multiplexed with
16588      QUAL_UNION_TYPE nodes.  Each time such a structure is passed to a
16589      function processing a FIELD_DECL, it is required to be non null.  */
16590   tree struct_type;
16591   /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16592      QUAL_UNION_TYPE), this holds an expression that computes the offset for
16593      this variant part as part of the root record (in storage units).  For
16594      regular records, it must be NULL_TREE.  */
16595   tree variant_part_offset;
16596 };
16597
16598 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16599    addressed byte of the "containing object" for the given FIELD_DECL. If
16600    possible, return a native constant through CST_OFFSET (in which case NULL is
16601    returned); otherwise return a DWARF expression that computes the offset.
16602
16603    Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16604    that offset is, either because the argument turns out to be a pointer to an
16605    ERROR_MARK node, or because the offset expression is too complex for us.
16606
16607    CTX is required: see the comment for VLR_CONTEXT.  */
16608
16609 static dw_loc_descr_ref
16610 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16611                    HOST_WIDE_INT *cst_offset)
16612 {
16613   offset_int object_offset_in_bits;
16614   offset_int object_offset_in_bytes;
16615   offset_int bitpos_int;
16616   bool is_byte_offset_cst, is_bit_offset_cst;
16617   tree tree_result;
16618   dw_loc_list_ref loc_result;
16619
16620   *cst_offset = 0;
16621
16622   if (TREE_CODE (decl) == ERROR_MARK)
16623     return NULL;
16624   else
16625     gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16626
16627   is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16628   is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16629
16630   /* We cannot handle variable bit offsets at the moment, so abort if it's the
16631      case.  */
16632   if (is_bit_offset_cst)
16633     return NULL;
16634
16635 #ifdef PCC_BITFIELD_TYPE_MATTERS
16636   /* We used to handle only constant offsets in all cases.  Now, we handle
16637      properly dynamic byte offsets only when PCC bitfield type doesn't
16638      matter.  */
16639   if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16640     {
16641       tree type;
16642       tree field_size_tree;
16643       offset_int deepest_bitpos;
16644       offset_int field_size_in_bits;
16645       unsigned int type_align_in_bits;
16646       unsigned int decl_align_in_bits;
16647       offset_int type_size_in_bits;
16648
16649       bitpos_int = wi::to_offset (bit_position (decl));
16650       type = field_type (decl);
16651       type_size_in_bits = offset_int_type_size_in_bits (type);
16652       type_align_in_bits = simple_type_align_in_bits (type);
16653
16654       field_size_tree = DECL_SIZE (decl);
16655
16656       /* The size could be unspecified if there was an error, or for
16657          a flexible array member.  */
16658       if (!field_size_tree)
16659         field_size_tree = bitsize_zero_node;
16660
16661       /* If the size of the field is not constant, use the type size.  */
16662       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16663         field_size_in_bits = wi::to_offset (field_size_tree);
16664       else
16665         field_size_in_bits = type_size_in_bits;
16666
16667       decl_align_in_bits = simple_decl_align_in_bits (decl);
16668
16669       /* The GCC front-end doesn't make any attempt to keep track of the
16670          starting bit offset (relative to the start of the containing
16671          structure type) of the hypothetical "containing object" for a
16672          bit-field.  Thus, when computing the byte offset value for the
16673          start of the "containing object" of a bit-field, we must deduce
16674          this information on our own. This can be rather tricky to do in
16675          some cases.  For example, handling the following structure type
16676          definition when compiling for an i386/i486 target (which only
16677          aligns long long's to 32-bit boundaries) can be very tricky:
16678
16679          struct S { int field1; long long field2:31; };
16680
16681          Fortunately, there is a simple rule-of-thumb which can be used
16682          in such cases.  When compiling for an i386/i486, GCC will
16683          allocate 8 bytes for the structure shown above.  It decides to
16684          do this based upon one simple rule for bit-field allocation.
16685          GCC allocates each "containing object" for each bit-field at
16686          the first (i.e. lowest addressed) legitimate alignment boundary
16687          (based upon the required minimum alignment for the declared
16688          type of the field) which it can possibly use, subject to the
16689          condition that there is still enough available space remaining
16690          in the containing object (when allocated at the selected point)
16691          to fully accommodate all of the bits of the bit-field itself.
16692
16693          This simple rule makes it obvious why GCC allocates 8 bytes for
16694          each object of the structure type shown above.  When looking
16695          for a place to allocate the "containing object" for `field2',
16696          the compiler simply tries to allocate a 64-bit "containing
16697          object" at each successive 32-bit boundary (starting at zero)
16698          until it finds a place to allocate that 64- bit field such that
16699          at least 31 contiguous (and previously unallocated) bits remain
16700          within that selected 64 bit field.  (As it turns out, for the
16701          example above, the compiler finds it is OK to allocate the
16702          "containing object" 64-bit field at bit-offset zero within the
16703          structure type.)
16704
16705          Here we attempt to work backwards from the limited set of facts
16706          we're given, and we try to deduce from those facts, where GCC
16707          must have believed that the containing object started (within
16708          the structure type). The value we deduce is then used (by the
16709          callers of this routine) to generate DW_AT_location and
16710          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16711          the case of DW_AT_location, regular fields as well).  */
16712
16713       /* Figure out the bit-distance from the start of the structure to
16714          the "deepest" bit of the bit-field.  */
16715       deepest_bitpos = bitpos_int + field_size_in_bits;
16716
16717       /* This is the tricky part.  Use some fancy footwork to deduce
16718          where the lowest addressed bit of the containing object must
16719          be.  */
16720       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16721
16722       /* Round up to type_align by default.  This works best for
16723          bitfields.  */
16724       object_offset_in_bits
16725         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16726
16727       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16728         {
16729           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16730
16731           /* Round up to decl_align instead.  */
16732           object_offset_in_bits
16733             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16734         }
16735     }
16736 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16737
16738   tree_result = byte_position (decl);
16739   if (ctx->variant_part_offset != NULL_TREE)
16740     tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16741                                 ctx->variant_part_offset, tree_result));
16742
16743   /* If the byte offset is a constant, it's simplier to handle a native
16744      constant rather than a DWARF expression.  */
16745   if (TREE_CODE (tree_result) == INTEGER_CST)
16746     {
16747       *cst_offset = wi::to_offset (tree_result).to_shwi ();
16748       return NULL;
16749     }
16750   struct loc_descr_context loc_ctx = {
16751     ctx->struct_type, /* context_type */
16752     NULL_TREE,        /* base_decl */
16753     NULL              /* dpi */
16754   };
16755   loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16756
16757   /* We want a DWARF expression: abort if we only have a location list with
16758      multiple elements.  */
16759   if (!loc_result || !single_element_loc_list_p (loc_result))
16760     return NULL;
16761   else
16762     return loc_result->expr;
16763 }
16764 \f
16765 /* The following routines define various Dwarf attributes and any data
16766    associated with them.  */
16767
16768 /* Add a location description attribute value to a DIE.
16769
16770    This emits location attributes suitable for whole variables and
16771    whole parameters.  Note that the location attributes for struct fields are
16772    generated by the routine `data_member_location_attribute' below.  */
16773
16774 static inline void
16775 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16776                              dw_loc_list_ref descr)
16777 {
16778   if (descr == 0)
16779     return;
16780   if (single_element_loc_list_p (descr))
16781     add_AT_loc (die, attr_kind, descr->expr);
16782   else
16783     add_AT_loc_list (die, attr_kind, descr);
16784 }
16785
16786 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16787
16788 static void
16789 add_accessibility_attribute (dw_die_ref die, tree decl)
16790 {
16791   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16792      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16793      the default has always been DW_ACCESS_public.  */
16794   if (TREE_PROTECTED (decl))
16795     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16796   else if (TREE_PRIVATE (decl))
16797     {
16798       if (dwarf_version == 2
16799           || die->die_parent == NULL
16800           || die->die_parent->die_tag != DW_TAG_class_type)
16801         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16802     }
16803   else if (dwarf_version > 2
16804            && die->die_parent
16805            && die->die_parent->die_tag == DW_TAG_class_type)
16806     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16807 }
16808
16809 /* Attach the specialized form of location attribute used for data members of
16810    struct and union types.  In the special case of a FIELD_DECL node which
16811    represents a bit-field, the "offset" part of this special location
16812    descriptor must indicate the distance in bytes from the lowest-addressed
16813    byte of the containing struct or union type to the lowest-addressed byte of
16814    the "containing object" for the bit-field.  (See the `field_byte_offset'
16815    function above).
16816
16817    For any given bit-field, the "containing object" is a hypothetical object
16818    (of some integral or enum type) within which the given bit-field lives.  The
16819    type of this hypothetical "containing object" is always the same as the
16820    declared type of the individual bit-field itself (for GCC anyway... the
16821    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16822    bytes) of the hypothetical "containing object" which will be given in the
16823    DW_AT_byte_size attribute for this bit-field.  (See the
16824    `byte_size_attribute' function below.)  It is also used when calculating the
16825    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16826    function below.)
16827
16828    CTX is required: see the comment for VLR_CONTEXT.  */
16829
16830 static void
16831 add_data_member_location_attribute (dw_die_ref die,
16832                                     tree decl,
16833                                     struct vlr_context *ctx)
16834 {
16835   HOST_WIDE_INT offset;
16836   dw_loc_descr_ref loc_descr = 0;
16837
16838   if (TREE_CODE (decl) == TREE_BINFO)
16839     {
16840       /* We're working on the TAG_inheritance for a base class.  */
16841       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16842         {
16843           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16844              aren't at a fixed offset from all (sub)objects of the same
16845              type.  We need to extract the appropriate offset from our
16846              vtable.  The following dwarf expression means
16847
16848                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16849
16850              This is specific to the V3 ABI, of course.  */
16851
16852           dw_loc_descr_ref tmp;
16853
16854           /* Make a copy of the object address.  */
16855           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16856           add_loc_descr (&loc_descr, tmp);
16857
16858           /* Extract the vtable address.  */
16859           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16860           add_loc_descr (&loc_descr, tmp);
16861
16862           /* Calculate the address of the offset.  */
16863           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16864           gcc_assert (offset < 0);
16865
16866           tmp = int_loc_descriptor (-offset);
16867           add_loc_descr (&loc_descr, tmp);
16868           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16869           add_loc_descr (&loc_descr, tmp);
16870
16871           /* Extract the offset.  */
16872           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16873           add_loc_descr (&loc_descr, tmp);
16874
16875           /* Add it to the object address.  */
16876           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16877           add_loc_descr (&loc_descr, tmp);
16878         }
16879       else
16880         offset = tree_to_shwi (BINFO_OFFSET (decl));
16881     }
16882   else
16883     {
16884       loc_descr = field_byte_offset (decl, ctx, &offset);
16885
16886       /* If loc_descr is available then we know the field offset is dynamic.
16887          However, GDB does not handle dynamic field offsets very well at the
16888          moment.  */
16889       if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16890         {
16891           loc_descr = NULL;
16892           offset = 0;
16893         }
16894
16895       /* Data member location evalutation starts with the base address on the
16896          stack.  Compute the field offset and add it to this base address.  */
16897       else if (loc_descr != NULL)
16898         add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16899     }
16900
16901   if (! loc_descr)
16902     {
16903       if (dwarf_version > 2)
16904         {
16905           /* Don't need to output a location expression, just the constant. */
16906           if (offset < 0)
16907             add_AT_int (die, DW_AT_data_member_location, offset);
16908           else
16909             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16910           return;
16911         }
16912       else
16913         {
16914           enum dwarf_location_atom op;
16915
16916           /* The DWARF2 standard says that we should assume that the structure
16917              address is already on the stack, so we can specify a structure
16918              field address by using DW_OP_plus_uconst.  */
16919           op = DW_OP_plus_uconst;
16920           loc_descr = new_loc_descr (op, offset, 0);
16921         }
16922     }
16923
16924   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16925 }
16926
16927 /* Writes integer values to dw_vec_const array.  */
16928
16929 static void
16930 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16931 {
16932   while (size != 0)
16933     {
16934       *dest++ = val & 0xff;
16935       val >>= 8;
16936       --size;
16937     }
16938 }
16939
16940 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16941
16942 static HOST_WIDE_INT
16943 extract_int (const unsigned char *src, unsigned int size)
16944 {
16945   HOST_WIDE_INT val = 0;
16946
16947   src += size;
16948   while (size != 0)
16949     {
16950       val <<= 8;
16951       val |= *--src & 0xff;
16952       --size;
16953     }
16954   return val;
16955 }
16956
16957 /* Writes wide_int values to dw_vec_const array.  */
16958
16959 static void
16960 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
16961 {
16962   int i;
16963
16964   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
16965     {
16966       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
16967       return;
16968     }
16969
16970   /* We'd have to extend this code to support odd sizes.  */
16971   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
16972
16973   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
16974
16975   if (WORDS_BIG_ENDIAN)
16976     for (i = n - 1; i >= 0; i--)
16977       {
16978         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16979         dest += sizeof (HOST_WIDE_INT);
16980       }
16981   else
16982     for (i = 0; i < n; i++)
16983       {
16984         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16985         dest += sizeof (HOST_WIDE_INT);
16986       }
16987 }
16988
16989 /* Writes floating point values to dw_vec_const array.  */
16990
16991 static void
16992 insert_float (const_rtx rtl, unsigned char *array)
16993 {
16994   long val[4];
16995   int i;
16996
16997   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
16998
16999   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
17000   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17001     {
17002       insert_int (val[i], 4, array);
17003       array += 4;
17004     }
17005 }
17006
17007 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17008    does not have a "location" either in memory or in a register.  These
17009    things can arise in GNU C when a constant is passed as an actual parameter
17010    to an inlined function.  They can also arise in C++ where declared
17011    constants do not necessarily get memory "homes".  */
17012
17013 static bool
17014 add_const_value_attribute (dw_die_ref die, rtx rtl)
17015 {
17016   switch (GET_CODE (rtl))
17017     {
17018     case CONST_INT:
17019       {
17020         HOST_WIDE_INT val = INTVAL (rtl);
17021
17022         if (val < 0)
17023           add_AT_int (die, DW_AT_const_value, val);
17024         else
17025           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17026       }
17027       return true;
17028
17029     case CONST_WIDE_INT:
17030       {
17031         wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17032         unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17033                                  (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17034         wide_int w = wi::zext (w1, prec);
17035         add_AT_wide (die, DW_AT_const_value, w);
17036       }
17037       return true;
17038
17039     case CONST_DOUBLE:
17040       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17041          floating-point constant.  A CONST_DOUBLE is used whenever the
17042          constant requires more than one word in order to be adequately
17043          represented.  */
17044       {
17045         machine_mode mode = GET_MODE (rtl);
17046
17047         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17048           add_AT_double (die, DW_AT_const_value,
17049                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17050         else
17051           {
17052             unsigned int length = GET_MODE_SIZE (mode);
17053             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17054
17055             insert_float (rtl, array);
17056             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17057           }
17058       }
17059       return true;
17060
17061     case CONST_VECTOR:
17062       {
17063         machine_mode mode = GET_MODE (rtl);
17064         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17065         unsigned int length = CONST_VECTOR_NUNITS (rtl);
17066         unsigned char *array
17067           = ggc_vec_alloc<unsigned char> (length * elt_size);
17068         unsigned int i;
17069         unsigned char *p;
17070         machine_mode imode = GET_MODE_INNER (mode);
17071
17072         switch (GET_MODE_CLASS (mode))
17073           {
17074           case MODE_VECTOR_INT:
17075             for (i = 0, p = array; i < length; i++, p += elt_size)
17076               {
17077                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17078                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17079               }
17080             break;
17081
17082           case MODE_VECTOR_FLOAT:
17083             for (i = 0, p = array; i < length; i++, p += elt_size)
17084               {
17085                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17086                 insert_float (elt, p);
17087               }
17088             break;
17089
17090           default:
17091             gcc_unreachable ();
17092           }
17093
17094         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17095       }
17096       return true;
17097
17098     case CONST_STRING:
17099       if (dwarf_version >= 4 || !dwarf_strict)
17100         {
17101           dw_loc_descr_ref loc_result;
17102           resolve_one_addr (&rtl);
17103         rtl_addr:
17104           loc_result = new_addr_loc_descr (rtl, dtprel_false);
17105           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17106           add_AT_loc (die, DW_AT_location, loc_result);
17107           vec_safe_push (used_rtx_array, rtl);
17108           return true;
17109         }
17110       return false;
17111
17112     case CONST:
17113       if (CONSTANT_P (XEXP (rtl, 0)))
17114         return add_const_value_attribute (die, XEXP (rtl, 0));
17115       /* FALLTHROUGH */
17116     case SYMBOL_REF:
17117       if (!const_ok_for_output (rtl))
17118         return false;
17119     case LABEL_REF:
17120       if (dwarf_version >= 4 || !dwarf_strict)
17121         goto rtl_addr;
17122       return false;
17123
17124     case PLUS:
17125       /* In cases where an inlined instance of an inline function is passed
17126          the address of an `auto' variable (which is local to the caller) we
17127          can get a situation where the DECL_RTL of the artificial local
17128          variable (for the inlining) which acts as a stand-in for the
17129          corresponding formal parameter (of the inline function) will look
17130          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
17131          exactly a compile-time constant expression, but it isn't the address
17132          of the (artificial) local variable either.  Rather, it represents the
17133          *value* which the artificial local variable always has during its
17134          lifetime.  We currently have no way to represent such quasi-constant
17135          values in Dwarf, so for now we just punt and generate nothing.  */
17136       return false;
17137
17138     case HIGH:
17139     case CONST_FIXED:
17140       return false;
17141
17142     case MEM:
17143       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17144           && MEM_READONLY_P (rtl)
17145           && GET_MODE (rtl) == BLKmode)
17146         {
17147           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17148           return true;
17149         }
17150       return false;
17151
17152     default:
17153       /* No other kinds of rtx should be possible here.  */
17154       gcc_unreachable ();
17155     }
17156   return false;
17157 }
17158
17159 /* Determine whether the evaluation of EXPR references any variables
17160    or functions which aren't otherwise used (and therefore may not be
17161    output).  */
17162 static tree
17163 reference_to_unused (tree * tp, int * walk_subtrees,
17164                      void * data ATTRIBUTE_UNUSED)
17165 {
17166   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17167     *walk_subtrees = 0;
17168
17169   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17170       && ! TREE_ASM_WRITTEN (*tp))
17171     return *tp;
17172   /* ???  The C++ FE emits debug information for using decls, so
17173      putting gcc_unreachable here falls over.  See PR31899.  For now
17174      be conservative.  */
17175   else if (!symtab->global_info_ready
17176            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17177     return *tp;
17178   else if (TREE_CODE (*tp) == VAR_DECL)
17179     {
17180       varpool_node *node = varpool_node::get (*tp);
17181       if (!node || !node->definition)
17182         return *tp;
17183     }
17184   else if (TREE_CODE (*tp) == FUNCTION_DECL
17185            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17186     {
17187       /* The call graph machinery must have finished analyzing,
17188          optimizing and gimplifying the CU by now.
17189          So if *TP has no call graph node associated
17190          to it, it means *TP will not be emitted.  */
17191       if (!cgraph_node::get (*tp))
17192         return *tp;
17193     }
17194   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17195     return *tp;
17196
17197   return NULL_TREE;
17198 }
17199
17200 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17201    for use in a later add_const_value_attribute call.  */
17202
17203 static rtx
17204 rtl_for_decl_init (tree init, tree type)
17205 {
17206   rtx rtl = NULL_RTX;
17207
17208   STRIP_NOPS (init);
17209
17210   /* If a variable is initialized with a string constant without embedded
17211      zeros, build CONST_STRING.  */
17212   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17213     {
17214       tree enttype = TREE_TYPE (type);
17215       tree domain = TYPE_DOMAIN (type);
17216       machine_mode mode = TYPE_MODE (enttype);
17217
17218       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17219           && domain
17220           && integer_zerop (TYPE_MIN_VALUE (domain))
17221           && compare_tree_int (TYPE_MAX_VALUE (domain),
17222                                TREE_STRING_LENGTH (init) - 1) == 0
17223           && ((size_t) TREE_STRING_LENGTH (init)
17224               == strlen (TREE_STRING_POINTER (init)) + 1))
17225         {
17226           rtl = gen_rtx_CONST_STRING (VOIDmode,
17227                                       ggc_strdup (TREE_STRING_POINTER (init)));
17228           rtl = gen_rtx_MEM (BLKmode, rtl);
17229           MEM_READONLY_P (rtl) = 1;
17230         }
17231     }
17232   /* Other aggregates, and complex values, could be represented using
17233      CONCAT: FIXME!  */
17234   else if (AGGREGATE_TYPE_P (type)
17235            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17236                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17237            || TREE_CODE (type) == COMPLEX_TYPE)
17238     ;
17239   /* Vectors only work if their mode is supported by the target.
17240      FIXME: generic vectors ought to work too.  */
17241   else if (TREE_CODE (type) == VECTOR_TYPE
17242            && !VECTOR_MODE_P (TYPE_MODE (type)))
17243     ;
17244   /* If the initializer is something that we know will expand into an
17245      immediate RTL constant, expand it now.  We must be careful not to
17246      reference variables which won't be output.  */
17247   else if (initializer_constant_valid_p (init, type)
17248            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17249     {
17250       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17251          possible.  */
17252       if (TREE_CODE (type) == VECTOR_TYPE)
17253         switch (TREE_CODE (init))
17254           {
17255           case VECTOR_CST:
17256             break;
17257           case CONSTRUCTOR:
17258             if (TREE_CONSTANT (init))
17259               {
17260                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17261                 bool constant_p = true;
17262                 tree value;
17263                 unsigned HOST_WIDE_INT ix;
17264
17265                 /* Even when ctor is constant, it might contain non-*_CST
17266                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17267                    belong into VECTOR_CST nodes.  */
17268                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17269                   if (!CONSTANT_CLASS_P (value))
17270                     {
17271                       constant_p = false;
17272                       break;
17273                     }
17274
17275                 if (constant_p)
17276                   {
17277                     init = build_vector_from_ctor (type, elts);
17278                     break;
17279                   }
17280               }
17281             /* FALLTHRU */
17282
17283           default:
17284             return NULL;
17285           }
17286
17287       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17288
17289       /* If expand_expr returns a MEM, it wasn't immediate.  */
17290       gcc_assert (!rtl || !MEM_P (rtl));
17291     }
17292
17293   return rtl;
17294 }
17295
17296 /* Generate RTL for the variable DECL to represent its location.  */
17297
17298 static rtx
17299 rtl_for_decl_location (tree decl)
17300 {
17301   rtx rtl;
17302
17303   /* Here we have to decide where we are going to say the parameter "lives"
17304      (as far as the debugger is concerned).  We only have a couple of
17305      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17306
17307      DECL_RTL normally indicates where the parameter lives during most of the
17308      activation of the function.  If optimization is enabled however, this
17309      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17310      that the parameter doesn't really live anywhere (as far as the code
17311      generation parts of GCC are concerned) during most of the function's
17312      activation.  That will happen (for example) if the parameter is never
17313      referenced within the function.
17314
17315      We could just generate a location descriptor here for all non-NULL
17316      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17317      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17318      where DECL_RTL is NULL or is a pseudo-reg.
17319
17320      Note however that we can only get away with using DECL_INCOMING_RTL as
17321      a backup substitute for DECL_RTL in certain limited cases.  In cases
17322      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17323      we can be sure that the parameter was passed using the same type as it is
17324      declared to have within the function, and that its DECL_INCOMING_RTL
17325      points us to a place where a value of that type is passed.
17326
17327      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17328      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17329      because in these cases DECL_INCOMING_RTL points us to a value of some
17330      type which is *different* from the type of the parameter itself.  Thus,
17331      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17332      such cases, the debugger would end up (for example) trying to fetch a
17333      `float' from a place which actually contains the first part of a
17334      `double'.  That would lead to really incorrect and confusing
17335      output at debug-time.
17336
17337      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17338      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17339      are a couple of exceptions however.  On little-endian machines we can
17340      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17341      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17342      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17343      when (on a little-endian machine) a non-prototyped function has a
17344      parameter declared to be of type `short' or `char'.  In such cases,
17345      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17346      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17347      passed `int' value.  If the debugger then uses that address to fetch
17348      a `short' or a `char' (on a little-endian machine) the result will be
17349      the correct data, so we allow for such exceptional cases below.
17350
17351      Note that our goal here is to describe the place where the given formal
17352      parameter lives during most of the function's activation (i.e. between the
17353      end of the prologue and the start of the epilogue).  We'll do that as best
17354      as we can. Note however that if the given formal parameter is modified
17355      sometime during the execution of the function, then a stack backtrace (at
17356      debug-time) will show the function as having been called with the *new*
17357      value rather than the value which was originally passed in.  This happens
17358      rarely enough that it is not a major problem, but it *is* a problem, and
17359      I'd like to fix it.
17360
17361      A future version of dwarf2out.c may generate two additional attributes for
17362      any given DW_TAG_formal_parameter DIE which will describe the "passed
17363      type" and the "passed location" for the given formal parameter in addition
17364      to the attributes we now generate to indicate the "declared type" and the
17365      "active location" for each parameter.  This additional set of attributes
17366      could be used by debuggers for stack backtraces. Separately, note that
17367      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17368      This happens (for example) for inlined-instances of inline function formal
17369      parameters which are never referenced.  This really shouldn't be
17370      happening.  All PARM_DECL nodes should get valid non-NULL
17371      DECL_INCOMING_RTL values.  FIXME.  */
17372
17373   /* Use DECL_RTL as the "location" unless we find something better.  */
17374   rtl = DECL_RTL_IF_SET (decl);
17375
17376   /* When generating abstract instances, ignore everything except
17377      constants, symbols living in memory, and symbols living in
17378      fixed registers.  */
17379   if (! reload_completed)
17380     {
17381       if (rtl
17382           && (CONSTANT_P (rtl)
17383               || (MEM_P (rtl)
17384                   && CONSTANT_P (XEXP (rtl, 0)))
17385               || (REG_P (rtl)
17386                   && TREE_CODE (decl) == VAR_DECL
17387                   && TREE_STATIC (decl))))
17388         {
17389           rtl = targetm.delegitimize_address (rtl);
17390           return rtl;
17391         }
17392       rtl = NULL_RTX;
17393     }
17394   else if (TREE_CODE (decl) == PARM_DECL)
17395     {
17396       if (rtl == NULL_RTX
17397           || is_pseudo_reg (rtl)
17398           || (MEM_P (rtl)
17399               && is_pseudo_reg (XEXP (rtl, 0))
17400               && DECL_INCOMING_RTL (decl)
17401               && MEM_P (DECL_INCOMING_RTL (decl))
17402               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17403         {
17404           tree declared_type = TREE_TYPE (decl);
17405           tree passed_type = DECL_ARG_TYPE (decl);
17406           machine_mode dmode = TYPE_MODE (declared_type);
17407           machine_mode pmode = TYPE_MODE (passed_type);
17408
17409           /* This decl represents a formal parameter which was optimized out.
17410              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17411              all cases where (rtl == NULL_RTX) just below.  */
17412           if (dmode == pmode)
17413             rtl = DECL_INCOMING_RTL (decl);
17414           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17415                    && SCALAR_INT_MODE_P (dmode)
17416                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17417                    && DECL_INCOMING_RTL (decl))
17418             {
17419               rtx inc = DECL_INCOMING_RTL (decl);
17420               if (REG_P (inc))
17421                 rtl = inc;
17422               else if (MEM_P (inc))
17423                 {
17424                   if (BYTES_BIG_ENDIAN)
17425                     rtl = adjust_address_nv (inc, dmode,
17426                                              GET_MODE_SIZE (pmode)
17427                                              - GET_MODE_SIZE (dmode));
17428                   else
17429                     rtl = inc;
17430                 }
17431             }
17432         }
17433
17434       /* If the parm was passed in registers, but lives on the stack, then
17435          make a big endian correction if the mode of the type of the
17436          parameter is not the same as the mode of the rtl.  */
17437       /* ??? This is the same series of checks that are made in dbxout.c before
17438          we reach the big endian correction code there.  It isn't clear if all
17439          of these checks are necessary here, but keeping them all is the safe
17440          thing to do.  */
17441       else if (MEM_P (rtl)
17442                && XEXP (rtl, 0) != const0_rtx
17443                && ! CONSTANT_P (XEXP (rtl, 0))
17444                /* Not passed in memory.  */
17445                && !MEM_P (DECL_INCOMING_RTL (decl))
17446                /* Not passed by invisible reference.  */
17447                && (!REG_P (XEXP (rtl, 0))
17448                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17449                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17450 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17451                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17452 #endif
17453                      )
17454                /* Big endian correction check.  */
17455                && BYTES_BIG_ENDIAN
17456                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17457                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17458                    < UNITS_PER_WORD))
17459         {
17460           machine_mode addr_mode = get_address_mode (rtl);
17461           int offset = (UNITS_PER_WORD
17462                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17463
17464           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17465                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
17466         }
17467     }
17468   else if (TREE_CODE (decl) == VAR_DECL
17469            && rtl
17470            && MEM_P (rtl)
17471            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17472            && BYTES_BIG_ENDIAN)
17473     {
17474       machine_mode addr_mode = get_address_mode (rtl);
17475       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17476       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17477
17478       /* If a variable is declared "register" yet is smaller than
17479          a register, then if we store the variable to memory, it
17480          looks like we're storing a register-sized value, when in
17481          fact we are not.  We need to adjust the offset of the
17482          storage location to reflect the actual value's bytes,
17483          else gdb will not be able to display it.  */
17484       if (rsize > dsize)
17485         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17486                            plus_constant (addr_mode, XEXP (rtl, 0),
17487                                           rsize - dsize));
17488     }
17489
17490   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17491      and will have been substituted directly into all expressions that use it.
17492      C does not have such a concept, but C++ and other languages do.  */
17493   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17494     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17495
17496   if (rtl)
17497     rtl = targetm.delegitimize_address (rtl);
17498
17499   /* If we don't look past the constant pool, we risk emitting a
17500      reference to a constant pool entry that isn't referenced from
17501      code, and thus is not emitted.  */
17502   if (rtl)
17503     rtl = avoid_constant_pool_reference (rtl);
17504
17505   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17506      in the current CU, resolve_addr will remove the expression referencing
17507      it.  */
17508   if (rtl == NULL_RTX
17509       && TREE_CODE (decl) == VAR_DECL
17510       && !DECL_EXTERNAL (decl)
17511       && TREE_STATIC (decl)
17512       && DECL_NAME (decl)
17513       && !DECL_HARD_REGISTER (decl)
17514       && DECL_MODE (decl) != VOIDmode)
17515     {
17516       rtl = make_decl_rtl_for_debug (decl);
17517       if (!MEM_P (rtl)
17518           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17519           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17520         rtl = NULL_RTX;
17521     }
17522
17523   return rtl;
17524 }
17525
17526 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17527    returned.  If so, the decl for the COMMON block is returned, and the
17528    value is the offset into the common block for the symbol.  */
17529
17530 static tree
17531 fortran_common (tree decl, HOST_WIDE_INT *value)
17532 {
17533   tree val_expr, cvar;
17534   machine_mode mode;
17535   HOST_WIDE_INT bitsize, bitpos;
17536   tree offset;
17537   int unsignedp, reversep, volatilep = 0;
17538
17539   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17540      it does not have a value (the offset into the common area), or if it
17541      is thread local (as opposed to global) then it isn't common, and shouldn't
17542      be handled as such.  */
17543   if (TREE_CODE (decl) != VAR_DECL
17544       || !TREE_STATIC (decl)
17545       || !DECL_HAS_VALUE_EXPR_P (decl)
17546       || !is_fortran ())
17547     return NULL_TREE;
17548
17549   val_expr = DECL_VALUE_EXPR (decl);
17550   if (TREE_CODE (val_expr) != COMPONENT_REF)
17551     return NULL_TREE;
17552
17553   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17554                               &unsignedp, &reversep, &volatilep, true);
17555
17556   if (cvar == NULL_TREE
17557       || TREE_CODE (cvar) != VAR_DECL
17558       || DECL_ARTIFICIAL (cvar)
17559       || !TREE_PUBLIC (cvar))
17560     return NULL_TREE;
17561
17562   *value = 0;
17563   if (offset != NULL)
17564     {
17565       if (!tree_fits_shwi_p (offset))
17566         return NULL_TREE;
17567       *value = tree_to_shwi (offset);
17568     }
17569   if (bitpos != 0)
17570     *value += bitpos / BITS_PER_UNIT;
17571
17572   return cvar;
17573 }
17574
17575 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17576    data attribute for a variable or a parameter.  We generate the
17577    DW_AT_const_value attribute only in those cases where the given variable
17578    or parameter does not have a true "location" either in memory or in a
17579    register.  This can happen (for example) when a constant is passed as an
17580    actual argument in a call to an inline function.  (It's possible that
17581    these things can crop up in other ways also.)  Note that one type of
17582    constant value which can be passed into an inlined function is a constant
17583    pointer.  This can happen for example if an actual argument in an inlined
17584    function call evaluates to a compile-time constant address.
17585
17586    CACHE_P is true if it is worth caching the location list for DECL,
17587    so that future calls can reuse it rather than regenerate it from scratch.
17588    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17589    since we will need to refer to them each time the function is inlined.  */
17590
17591 static bool
17592 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17593 {
17594   rtx rtl;
17595   dw_loc_list_ref list;
17596   var_loc_list *loc_list;
17597   cached_dw_loc_list *cache;
17598
17599   if (early_dwarf)
17600     return false;
17601
17602   if (TREE_CODE (decl) == ERROR_MARK)
17603     return false;
17604
17605   if (get_AT (die, DW_AT_location)
17606       || get_AT (die, DW_AT_const_value))
17607     return true;
17608
17609   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17610               || TREE_CODE (decl) == RESULT_DECL);
17611
17612   /* Try to get some constant RTL for this decl, and use that as the value of
17613      the location.  */
17614
17615   rtl = rtl_for_decl_location (decl);
17616   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17617       && add_const_value_attribute (die, rtl))
17618     return true;
17619
17620   /* See if we have single element location list that is equivalent to
17621      a constant value.  That way we are better to use add_const_value_attribute
17622      rather than expanding constant value equivalent.  */
17623   loc_list = lookup_decl_loc (decl);
17624   if (loc_list
17625       && loc_list->first
17626       && loc_list->first->next == NULL
17627       && NOTE_P (loc_list->first->loc)
17628       && NOTE_VAR_LOCATION (loc_list->first->loc)
17629       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17630     {
17631       struct var_loc_node *node;
17632
17633       node = loc_list->first;
17634       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17635       if (GET_CODE (rtl) == EXPR_LIST)
17636         rtl = XEXP (rtl, 0);
17637       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17638           && add_const_value_attribute (die, rtl))
17639          return true;
17640     }
17641   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17642      list several times.  See if we've already cached the contents.  */
17643   list = NULL;
17644   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17645     cache_p = false;
17646   if (cache_p)
17647     {
17648       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17649       if (cache)
17650         list = cache->loc_list;
17651     }
17652   if (list == NULL)
17653     {
17654       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17655                                  NULL);
17656       /* It is usually worth caching this result if the decl is from
17657          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17658       if (cache_p && list && list->dw_loc_next)
17659         {
17660           cached_dw_loc_list **slot
17661             = cached_dw_loc_list_table->find_slot_with_hash (decl,
17662                                                              DECL_UID (decl),
17663                                                              INSERT);
17664           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17665           cache->decl_id = DECL_UID (decl);
17666           cache->loc_list = list;
17667           *slot = cache;
17668         }
17669     }
17670   if (list)
17671     {
17672       add_AT_location_description (die, DW_AT_location, list);
17673       return true;
17674     }
17675   /* None of that worked, so it must not really have a location;
17676      try adding a constant value attribute from the DECL_INITIAL.  */
17677   return tree_add_const_value_attribute_for_decl (die, decl);
17678 }
17679
17680 /* Helper function for tree_add_const_value_attribute.  Natively encode
17681    initializer INIT into an array.  Return true if successful.  */
17682
17683 static bool
17684 native_encode_initializer (tree init, unsigned char *array, int size)
17685 {
17686   tree type;
17687
17688   if (init == NULL_TREE)
17689     return false;
17690
17691   STRIP_NOPS (init);
17692   switch (TREE_CODE (init))
17693     {
17694     case STRING_CST:
17695       type = TREE_TYPE (init);
17696       if (TREE_CODE (type) == ARRAY_TYPE)
17697         {
17698           tree enttype = TREE_TYPE (type);
17699           machine_mode mode = TYPE_MODE (enttype);
17700
17701           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17702             return false;
17703           if (int_size_in_bytes (type) != size)
17704             return false;
17705           if (size > TREE_STRING_LENGTH (init))
17706             {
17707               memcpy (array, TREE_STRING_POINTER (init),
17708                       TREE_STRING_LENGTH (init));
17709               memset (array + TREE_STRING_LENGTH (init),
17710                       '\0', size - TREE_STRING_LENGTH (init));
17711             }
17712           else
17713             memcpy (array, TREE_STRING_POINTER (init), size);
17714           return true;
17715         }
17716       return false;
17717     case CONSTRUCTOR:
17718       type = TREE_TYPE (init);
17719       if (int_size_in_bytes (type) != size)
17720         return false;
17721       if (TREE_CODE (type) == ARRAY_TYPE)
17722         {
17723           HOST_WIDE_INT min_index;
17724           unsigned HOST_WIDE_INT cnt;
17725           int curpos = 0, fieldsize;
17726           constructor_elt *ce;
17727
17728           if (TYPE_DOMAIN (type) == NULL_TREE
17729               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17730             return false;
17731
17732           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17733           if (fieldsize <= 0)
17734             return false;
17735
17736           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17737           memset (array, '\0', size);
17738           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17739             {
17740               tree val = ce->value;
17741               tree index = ce->index;
17742               int pos = curpos;
17743               if (index && TREE_CODE (index) == RANGE_EXPR)
17744                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17745                       * fieldsize;
17746               else if (index)
17747                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17748
17749               if (val)
17750                 {
17751                   STRIP_NOPS (val);
17752                   if (!native_encode_initializer (val, array + pos, fieldsize))
17753                     return false;
17754                 }
17755               curpos = pos + fieldsize;
17756               if (index && TREE_CODE (index) == RANGE_EXPR)
17757                 {
17758                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
17759                               - tree_to_shwi (TREE_OPERAND (index, 0));
17760                   while (count-- > 0)
17761                     {
17762                       if (val)
17763                         memcpy (array + curpos, array + pos, fieldsize);
17764                       curpos += fieldsize;
17765                     }
17766                 }
17767               gcc_assert (curpos <= size);
17768             }
17769           return true;
17770         }
17771       else if (TREE_CODE (type) == RECORD_TYPE
17772                || TREE_CODE (type) == UNION_TYPE)
17773         {
17774           tree field = NULL_TREE;
17775           unsigned HOST_WIDE_INT cnt;
17776           constructor_elt *ce;
17777
17778           if (int_size_in_bytes (type) != size)
17779             return false;
17780
17781           if (TREE_CODE (type) == RECORD_TYPE)
17782             field = TYPE_FIELDS (type);
17783
17784           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17785             {
17786               tree val = ce->value;
17787               int pos, fieldsize;
17788
17789               if (ce->index != 0)
17790                 field = ce->index;
17791
17792               if (val)
17793                 STRIP_NOPS (val);
17794
17795               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17796                 return false;
17797
17798               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17799                   && TYPE_DOMAIN (TREE_TYPE (field))
17800                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17801                 return false;
17802               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17803                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17804                 return false;
17805               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17806               pos = int_byte_position (field);
17807               gcc_assert (pos + fieldsize <= size);
17808               if (val
17809                   && !native_encode_initializer (val, array + pos, fieldsize))
17810                 return false;
17811             }
17812           return true;
17813         }
17814       return false;
17815     case VIEW_CONVERT_EXPR:
17816     case NON_LVALUE_EXPR:
17817       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17818     default:
17819       return native_encode_expr (init, array, size) == size;
17820     }
17821 }
17822
17823 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17824    attribute is the const value T.  */
17825
17826 static bool
17827 tree_add_const_value_attribute (dw_die_ref die, tree t)
17828 {
17829   tree init;
17830   tree type = TREE_TYPE (t);
17831   rtx rtl;
17832
17833   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17834     return false;
17835
17836   init = t;
17837   gcc_assert (!DECL_P (init));
17838
17839   rtl = rtl_for_decl_init (init, type);
17840   if (rtl)
17841     return add_const_value_attribute (die, rtl);
17842   /* If the host and target are sane, try harder.  */
17843   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17844            && initializer_constant_valid_p (init, type))
17845     {
17846       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17847       if (size > 0 && (int) size == size)
17848         {
17849           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17850
17851           if (native_encode_initializer (init, array, size))
17852             {
17853               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17854               return true;
17855             }
17856           ggc_free (array);
17857         }
17858     }
17859   return false;
17860 }
17861
17862 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17863    attribute is the const value of T, where T is an integral constant
17864    variable with static storage duration
17865    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17866
17867 static bool
17868 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17869 {
17870
17871   if (!decl
17872       || (TREE_CODE (decl) != VAR_DECL
17873           && TREE_CODE (decl) != CONST_DECL)
17874       || (TREE_CODE (decl) == VAR_DECL
17875           && !TREE_STATIC (decl)))
17876     return false;
17877
17878   if (TREE_READONLY (decl)
17879       && ! TREE_THIS_VOLATILE (decl)
17880       && DECL_INITIAL (decl))
17881     /* OK */;
17882   else
17883     return false;
17884
17885   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17886   if (get_AT (var_die, DW_AT_const_value))
17887     return false;
17888
17889   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17890 }
17891
17892 /* Convert the CFI instructions for the current function into a
17893    location list.  This is used for DW_AT_frame_base when we targeting
17894    a dwarf2 consumer that does not support the dwarf3
17895    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17896    expressions.  */
17897
17898 static dw_loc_list_ref
17899 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17900 {
17901   int ix;
17902   dw_fde_ref fde;
17903   dw_loc_list_ref list, *list_tail;
17904   dw_cfi_ref cfi;
17905   dw_cfa_location last_cfa, next_cfa;
17906   const char *start_label, *last_label, *section;
17907   dw_cfa_location remember;
17908
17909   fde = cfun->fde;
17910   gcc_assert (fde != NULL);
17911
17912   section = secname_for_decl (current_function_decl);
17913   list_tail = &list;
17914   list = NULL;
17915
17916   memset (&next_cfa, 0, sizeof (next_cfa));
17917   next_cfa.reg = INVALID_REGNUM;
17918   remember = next_cfa;
17919
17920   start_label = fde->dw_fde_begin;
17921
17922   /* ??? Bald assumption that the CIE opcode list does not contain
17923      advance opcodes.  */
17924   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
17925     lookup_cfa_1 (cfi, &next_cfa, &remember);
17926
17927   last_cfa = next_cfa;
17928   last_label = start_label;
17929
17930   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17931     {
17932       /* If the first partition contained no CFI adjustments, the
17933          CIE opcodes apply to the whole first partition.  */
17934       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17935                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17936       list_tail =&(*list_tail)->dw_loc_next;
17937       start_label = last_label = fde->dw_fde_second_begin;
17938     }
17939
17940   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
17941     {
17942       switch (cfi->dw_cfi_opc)
17943         {
17944         case DW_CFA_set_loc:
17945         case DW_CFA_advance_loc1:
17946         case DW_CFA_advance_loc2:
17947         case DW_CFA_advance_loc4:
17948           if (!cfa_equal_p (&last_cfa, &next_cfa))
17949             {
17950               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17951                                          start_label, last_label, section);
17952
17953               list_tail = &(*list_tail)->dw_loc_next;
17954               last_cfa = next_cfa;
17955               start_label = last_label;
17956             }
17957           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17958           break;
17959
17960         case DW_CFA_advance_loc:
17961           /* The encoding is complex enough that we should never emit this.  */
17962           gcc_unreachable ();
17963
17964         default:
17965           lookup_cfa_1 (cfi, &next_cfa, &remember);
17966           break;
17967         }
17968       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17969         {
17970           if (!cfa_equal_p (&last_cfa, &next_cfa))
17971             {
17972               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17973                                          start_label, last_label, section);
17974
17975               list_tail = &(*list_tail)->dw_loc_next;
17976               last_cfa = next_cfa;
17977               start_label = last_label;
17978             }
17979           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17980                                      start_label, fde->dw_fde_end, section);
17981           list_tail = &(*list_tail)->dw_loc_next;
17982           start_label = last_label = fde->dw_fde_second_begin;
17983         }
17984     }
17985
17986   if (!cfa_equal_p (&last_cfa, &next_cfa))
17987     {
17988       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17989                                  start_label, last_label, section);
17990       list_tail = &(*list_tail)->dw_loc_next;
17991       start_label = last_label;
17992     }
17993
17994   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17995                              start_label,
17996                              fde->dw_fde_second_begin
17997                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17998                              section);
17999
18000   if (list && list->dw_loc_next)
18001     gen_llsym (list);
18002
18003   return list;
18004 }
18005
18006 /* Compute a displacement from the "steady-state frame pointer" to the
18007    frame base (often the same as the CFA), and store it in
18008    frame_pointer_fb_offset.  OFFSET is added to the displacement
18009    before the latter is negated.  */
18010
18011 static void
18012 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18013 {
18014   rtx reg, elim;
18015
18016 #ifdef FRAME_POINTER_CFA_OFFSET
18017   reg = frame_pointer_rtx;
18018   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18019 #else
18020   reg = arg_pointer_rtx;
18021   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18022 #endif
18023
18024   elim = (ira_use_lra_p
18025           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18026           : eliminate_regs (reg, VOIDmode, NULL_RTX));
18027   if (GET_CODE (elim) == PLUS)
18028     {
18029       offset += INTVAL (XEXP (elim, 1));
18030       elim = XEXP (elim, 0);
18031     }
18032
18033   frame_pointer_fb_offset = -offset;
18034
18035   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18036      in which to eliminate.  This is because it's stack pointer isn't 
18037      directly accessible as a register within the ISA.  To work around
18038      this, assume that while we cannot provide a proper value for
18039      frame_pointer_fb_offset, we won't need one either.  */
18040   frame_pointer_fb_offset_valid
18041     = ((SUPPORTS_STACK_ALIGNMENT
18042         && (elim == hard_frame_pointer_rtx
18043             || elim == stack_pointer_rtx))
18044        || elim == (frame_pointer_needed
18045                    ? hard_frame_pointer_rtx
18046                    : stack_pointer_rtx));
18047 }
18048
18049 /* Generate a DW_AT_name attribute given some string value to be included as
18050    the value of the attribute.  */
18051
18052 static void
18053 add_name_attribute (dw_die_ref die, const char *name_string)
18054 {
18055   if (name_string != NULL && *name_string != 0)
18056     {
18057       if (demangle_name_func)
18058         name_string = (*demangle_name_func) (name_string);
18059
18060       add_AT_string (die, DW_AT_name, name_string);
18061     }
18062 }
18063
18064 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18065    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18066    of TYPE accordingly.
18067
18068    ??? This is a temporary measure until after we're able to generate
18069    regular DWARF for the complex Ada type system.  */
18070
18071 static void 
18072 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18073                                      dw_die_ref context_die)
18074 {
18075   tree dtype;
18076   dw_die_ref dtype_die;
18077
18078   if (!lang_hooks.types.descriptive_type)
18079     return;
18080
18081   dtype = lang_hooks.types.descriptive_type (type);
18082   if (!dtype)
18083     return;
18084
18085   dtype_die = lookup_type_die (dtype);
18086   if (!dtype_die)
18087     {
18088       gen_type_die (dtype, context_die);
18089       dtype_die = lookup_type_die (dtype);
18090       gcc_assert (dtype_die);
18091     }
18092
18093   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18094 }
18095
18096 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
18097
18098 static const char *
18099 comp_dir_string (void)
18100 {
18101   const char *wd;
18102   char *wd1;
18103   static const char *cached_wd = NULL;
18104
18105   if (cached_wd != NULL)
18106     return cached_wd;
18107
18108   wd = get_src_pwd ();
18109   if (wd == NULL)
18110     return NULL;
18111
18112   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18113     {
18114       int wdlen;
18115
18116       wdlen = strlen (wd);
18117       wd1 = ggc_vec_alloc<char> (wdlen + 2);
18118       strcpy (wd1, wd);
18119       wd1 [wdlen] = DIR_SEPARATOR;
18120       wd1 [wdlen + 1] = 0;
18121       wd = wd1;
18122     }
18123
18124   cached_wd = remap_debug_filename (wd);
18125   return cached_wd;
18126 }
18127
18128 /* Generate a DW_AT_comp_dir attribute for DIE.  */
18129
18130 static void
18131 add_comp_dir_attribute (dw_die_ref die)
18132 {
18133   const char * wd = comp_dir_string ();
18134   if (wd != NULL)
18135     add_AT_string (die, DW_AT_comp_dir, wd);
18136 }
18137
18138 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18139    pointer computation, ...), output a representation for that bound according
18140    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
18141    loc_list_from_tree for the meaning of CONTEXT.  */
18142
18143 static void
18144 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18145                  int forms, const struct loc_descr_context *context)
18146 {
18147   dw_die_ref context_die, decl_die;
18148   dw_loc_list_ref list;
18149
18150   bool strip_conversions = true;
18151
18152   while (strip_conversions)
18153     switch (TREE_CODE (value))
18154       {
18155       case ERROR_MARK:
18156       case SAVE_EXPR:
18157         return;
18158
18159       CASE_CONVERT:
18160       case VIEW_CONVERT_EXPR:
18161         value = TREE_OPERAND (value, 0);
18162         break;
18163
18164       default:
18165         strip_conversions = false;
18166         break;
18167       }
18168
18169   /* If possible and permitted, output the attribute as a constant.  */
18170   if ((forms & dw_scalar_form_constant) != 0
18171       && TREE_CODE (value) == INTEGER_CST)
18172     {
18173       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18174
18175       /* If HOST_WIDE_INT is big enough then represent the bound as
18176          a constant value.  We need to choose a form based on
18177          whether the type is signed or unsigned.  We cannot just
18178          call add_AT_unsigned if the value itself is positive
18179          (add_AT_unsigned might add the unsigned value encoded as
18180          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
18181          bounds type and then sign extend any unsigned values found
18182          for signed types.  This is needed only for
18183          DW_AT_{lower,upper}_bound, since for most other attributes,
18184          consumers will treat DW_FORM_data[1248] as unsigned values,
18185          regardless of the underlying type.  */
18186       if (prec <= HOST_BITS_PER_WIDE_INT
18187           || tree_fits_uhwi_p (value))
18188         {
18189           if (TYPE_UNSIGNED (TREE_TYPE (value)))
18190             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18191           else
18192             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18193         }
18194       else
18195         /* Otherwise represent the bound as an unsigned value with
18196            the precision of its type.  The precision and signedness
18197            of the type will be necessary to re-interpret it
18198            unambiguously.  */
18199         add_AT_wide (die, attr, value);
18200       return;
18201     }
18202
18203   /* Otherwise, if it's possible and permitted too, output a reference to
18204      another DIE.  */
18205   if ((forms & dw_scalar_form_reference) != 0)
18206     {
18207       tree decl = NULL_TREE;
18208
18209       /* Some type attributes reference an outer type.  For instance, the upper
18210          bound of an array may reference an embedding record (this happens in
18211          Ada).  */
18212       if (TREE_CODE (value) == COMPONENT_REF
18213           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18214           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18215         decl = TREE_OPERAND (value, 1);
18216
18217       else if (TREE_CODE (value) == VAR_DECL
18218                || TREE_CODE (value) == PARM_DECL
18219                || TREE_CODE (value) == RESULT_DECL)
18220         decl = value;
18221
18222       if (decl != NULL_TREE)
18223         {
18224           dw_die_ref decl_die = lookup_decl_die (decl);
18225
18226           /* ??? Can this happen, or should the variable have been bound
18227              first?  Probably it can, since I imagine that we try to create
18228              the types of parameters in the order in which they exist in
18229              the list, and won't have created a forward reference to a
18230              later parameter.  */
18231           if (decl_die != NULL)
18232             {
18233               add_AT_die_ref (die, attr, decl_die);
18234               return;
18235             }
18236         }
18237     }
18238
18239   /* Last chance: try to create a stack operation procedure to evaluate the
18240      value.  Do nothing if even that is not possible or permitted.  */
18241   if ((forms & dw_scalar_form_exprloc) == 0)
18242     return;
18243
18244   list = loc_list_from_tree (value, 2, context);
18245   if (list == NULL || single_element_loc_list_p (list))
18246     {
18247       /* If this attribute is not a reference nor constant, it is
18248          a DWARF expression rather than location description.  For that
18249          loc_list_from_tree (value, 0, &context) is needed.  */
18250       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18251       if (list2 && single_element_loc_list_p (list2))
18252         {
18253           add_AT_loc (die, attr, list2->expr);
18254           return;
18255         }
18256     }
18257
18258   /* If that failed to give a single element location list, fall back to
18259      outputting this as a reference... still if permitted.  */
18260   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18261     return;
18262
18263   if (current_function_decl == 0)
18264     context_die = comp_unit_die ();
18265   else
18266     context_die = lookup_decl_die (current_function_decl);
18267
18268   decl_die = new_die (DW_TAG_variable, context_die, value);
18269   add_AT_flag (decl_die, DW_AT_artificial, 1);
18270   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18271                       context_die);
18272   add_AT_location_description (decl_die, DW_AT_location, list);
18273   add_AT_die_ref (die, attr, decl_die);
18274 }
18275
18276 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18277    default.  */
18278
18279 static int
18280 lower_bound_default (void)
18281 {
18282   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18283     {
18284     case DW_LANG_C:
18285     case DW_LANG_C89:
18286     case DW_LANG_C99:
18287     case DW_LANG_C11:
18288     case DW_LANG_C_plus_plus:
18289     case DW_LANG_C_plus_plus_11:
18290     case DW_LANG_C_plus_plus_14:
18291     case DW_LANG_ObjC:
18292     case DW_LANG_ObjC_plus_plus:
18293     case DW_LANG_Java:
18294       return 0;
18295     case DW_LANG_Fortran77:
18296     case DW_LANG_Fortran90:
18297     case DW_LANG_Fortran95:
18298     case DW_LANG_Fortran03:
18299     case DW_LANG_Fortran08:
18300       return 1;
18301     case DW_LANG_UPC:
18302     case DW_LANG_D:
18303     case DW_LANG_Python:
18304       return dwarf_version >= 4 ? 0 : -1;
18305     case DW_LANG_Ada95:
18306     case DW_LANG_Ada83:
18307     case DW_LANG_Cobol74:
18308     case DW_LANG_Cobol85:
18309     case DW_LANG_Pascal83:
18310     case DW_LANG_Modula2:
18311     case DW_LANG_PLI:
18312       return dwarf_version >= 4 ? 1 : -1;
18313     default:
18314       return -1;
18315     }
18316 }
18317
18318 /* Given a tree node describing an array bound (either lower or upper) output
18319    a representation for that bound.  */
18320
18321 static void
18322 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18323                 tree bound, const struct loc_descr_context *context)
18324 {
18325   int dflt;
18326
18327   while (1)
18328     switch (TREE_CODE (bound))
18329       {
18330       /* Strip all conversions.  */
18331       CASE_CONVERT:
18332       case VIEW_CONVERT_EXPR:
18333         bound = TREE_OPERAND (bound, 0);
18334         break;
18335
18336       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
18337          are even omitted when they are the default.  */
18338       case INTEGER_CST:
18339         /* If the value for this bound is the default one, we can even omit the
18340            attribute.  */
18341         if (bound_attr == DW_AT_lower_bound
18342             && tree_fits_shwi_p (bound)
18343             && (dflt = lower_bound_default ()) != -1
18344             && tree_to_shwi (bound) == dflt)
18345           return;
18346
18347         /* FALLTHRU */
18348
18349       default:
18350         /* Because of the complex interaction there can be with other GNAT
18351            encodings, GDB isn't ready yet to handle proper DWARF description
18352            for self-referencial subrange bounds: let GNAT encodings do the
18353            magic in such a case.  */
18354         if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18355             && contains_placeholder_p (bound))
18356           return;
18357
18358         add_scalar_info (subrange_die, bound_attr, bound,
18359                          dw_scalar_form_constant
18360                          | dw_scalar_form_exprloc
18361                          | dw_scalar_form_reference,
18362                          context);
18363         return;
18364       }
18365 }
18366
18367 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18368    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18369    Note that the block of subscript information for an array type also
18370    includes information about the element type of the given array type.
18371
18372    This function reuses previously set type and bound information if
18373    available.  */
18374
18375 static void
18376 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18377 {
18378   unsigned dimension_number;
18379   tree lower, upper;
18380   dw_die_ref child = type_die->die_child;
18381
18382   for (dimension_number = 0;
18383        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18384        type = TREE_TYPE (type), dimension_number++)
18385     {
18386       tree domain = TYPE_DOMAIN (type);
18387
18388       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18389         break;
18390
18391       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18392          and (in GNU C only) variable bounds.  Handle all three forms
18393          here.  */
18394
18395       /* Find and reuse a previously generated DW_TAG_subrange_type if
18396          available.
18397
18398          For multi-dimensional arrays, as we iterate through the
18399          various dimensions in the enclosing for loop above, we also
18400          iterate through the DIE children and pick at each
18401          DW_TAG_subrange_type previously generated (if available).
18402          Each child DW_TAG_subrange_type DIE describes the range of
18403          the current dimension.  At this point we should have as many
18404          DW_TAG_subrange_type's as we have dimensions in the
18405          array.  */
18406       dw_die_ref subrange_die = NULL;
18407       if (child)
18408         while (1)
18409           {
18410             child = child->die_sib;
18411             if (child->die_tag == DW_TAG_subrange_type)
18412               subrange_die = child;
18413             if (child == type_die->die_child)
18414               {
18415                 /* If we wrapped around, stop looking next time.  */
18416                 child = NULL;
18417                 break;
18418               }
18419             if (child->die_tag == DW_TAG_subrange_type)
18420               break;
18421           }
18422       if (!subrange_die)
18423         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18424
18425       if (domain)
18426         {
18427           /* We have an array type with specified bounds.  */
18428           lower = TYPE_MIN_VALUE (domain);
18429           upper = TYPE_MAX_VALUE (domain);
18430
18431           /* Define the index type.  */
18432           if (TREE_TYPE (domain)
18433               && !get_AT (subrange_die, DW_AT_type))
18434             {
18435               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18436                  TREE_TYPE field.  We can't emit debug info for this
18437                  because it is an unnamed integral type.  */
18438               if (TREE_CODE (domain) == INTEGER_TYPE
18439                   && TYPE_NAME (domain) == NULL_TREE
18440                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18441                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18442                 ;
18443               else
18444                 add_type_attribute (subrange_die, TREE_TYPE (domain),
18445                                     TYPE_UNQUALIFIED, false, type_die);
18446             }
18447
18448           /* ??? If upper is NULL, the array has unspecified length,
18449              but it does have a lower bound.  This happens with Fortran
18450                dimension arr(N:*)
18451              Since the debugger is definitely going to need to know N
18452              to produce useful results, go ahead and output the lower
18453              bound solo, and hope the debugger can cope.  */
18454
18455           if (!get_AT (subrange_die, DW_AT_lower_bound))
18456             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18457           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18458             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18459         }
18460
18461       /* Otherwise we have an array type with an unspecified length.  The
18462          DWARF-2 spec does not say how to handle this; let's just leave out the
18463          bounds.  */
18464     }
18465 }
18466
18467 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
18468
18469 static void
18470 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18471 {
18472   dw_die_ref decl_die;
18473   HOST_WIDE_INT size;
18474   dw_loc_descr_ref size_expr = NULL;
18475
18476   switch (TREE_CODE (tree_node))
18477     {
18478     case ERROR_MARK:
18479       size = 0;
18480       break;
18481     case ENUMERAL_TYPE:
18482     case RECORD_TYPE:
18483     case UNION_TYPE:
18484     case QUAL_UNION_TYPE:
18485       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18486           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18487         {
18488           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18489           return;
18490         }
18491       size_expr = type_byte_size (tree_node, &size);
18492       break;
18493     case FIELD_DECL:
18494       /* For a data member of a struct or union, the DW_AT_byte_size is
18495          generally given as the number of bytes normally allocated for an
18496          object of the *declared* type of the member itself.  This is true
18497          even for bit-fields.  */
18498       size = int_size_in_bytes (field_type (tree_node));
18499       break;
18500     default:
18501       gcc_unreachable ();
18502     }
18503
18504   /* Support for dynamically-sized objects was introduced by DWARFv3.
18505      At the moment, GDB does not handle variable byte sizes very well,
18506      though.  */
18507   if ((dwarf_version >= 3 || !dwarf_strict)
18508       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18509       && size_expr != NULL)
18510     add_AT_loc (die, DW_AT_byte_size, size_expr);
18511
18512   /* Note that `size' might be -1 when we get to this point.  If it is, that
18513      indicates that the byte size of the entity in question is variable and
18514      that we could not generate a DWARF expression that computes it.  */
18515   if (size >= 0)
18516     add_AT_unsigned (die, DW_AT_byte_size, size);
18517 }
18518
18519 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18520    which specifies the distance in bits from the highest order bit of the
18521    "containing object" for the bit-field to the highest order bit of the
18522    bit-field itself.
18523
18524    For any given bit-field, the "containing object" is a hypothetical object
18525    (of some integral or enum type) within which the given bit-field lives.  The
18526    type of this hypothetical "containing object" is always the same as the
18527    declared type of the individual bit-field itself.  The determination of the
18528    exact location of the "containing object" for a bit-field is rather
18529    complicated.  It's handled by the `field_byte_offset' function (above).
18530
18531    CTX is required: see the comment for VLR_CONTEXT.
18532
18533    Note that it is the size (in bytes) of the hypothetical "containing object"
18534    which will be given in the DW_AT_byte_size attribute for this bit-field.
18535    (See `byte_size_attribute' above).  */
18536
18537 static inline void
18538 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18539 {
18540   HOST_WIDE_INT object_offset_in_bytes;
18541   tree original_type = DECL_BIT_FIELD_TYPE (decl);
18542   HOST_WIDE_INT bitpos_int;
18543   HOST_WIDE_INT highest_order_object_bit_offset;
18544   HOST_WIDE_INT highest_order_field_bit_offset;
18545   HOST_WIDE_INT bit_offset;
18546
18547   field_byte_offset (decl, ctx, &object_offset_in_bytes);
18548
18549   /* Must be a field and a bit field.  */
18550   gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18551
18552   /* We can't yet handle bit-fields whose offsets are variable, so if we
18553      encounter such things, just return without generating any attribute
18554      whatsoever.  Likewise for variable or too large size.  */
18555   if (! tree_fits_shwi_p (bit_position (decl))
18556       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18557     return;
18558
18559   bitpos_int = int_bit_position (decl);
18560
18561   /* Note that the bit offset is always the distance (in bits) from the
18562      highest-order bit of the "containing object" to the highest-order bit of
18563      the bit-field itself.  Since the "high-order end" of any object or field
18564      is different on big-endian and little-endian machines, the computation
18565      below must take account of these differences.  */
18566   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18567   highest_order_field_bit_offset = bitpos_int;
18568
18569   if (! BYTES_BIG_ENDIAN)
18570     {
18571       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18572       highest_order_object_bit_offset +=
18573         simple_type_size_in_bits (original_type);
18574     }
18575
18576   bit_offset
18577     = (! BYTES_BIG_ENDIAN
18578        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18579        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18580
18581   if (bit_offset < 0)
18582     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18583   else
18584     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18585 }
18586
18587 /* For a FIELD_DECL node which represents a bit field, output an attribute
18588    which specifies the length in bits of the given field.  */
18589
18590 static inline void
18591 add_bit_size_attribute (dw_die_ref die, tree decl)
18592 {
18593   /* Must be a field and a bit field.  */
18594   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18595               && DECL_BIT_FIELD_TYPE (decl));
18596
18597   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18598     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18599 }
18600
18601 /* If the compiled language is ANSI C, then add a 'prototyped'
18602    attribute, if arg types are given for the parameters of a function.  */
18603
18604 static inline void
18605 add_prototyped_attribute (dw_die_ref die, tree func_type)
18606 {
18607   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18608     {
18609     case DW_LANG_C:
18610     case DW_LANG_C89:
18611     case DW_LANG_C99:
18612     case DW_LANG_C11:
18613     case DW_LANG_ObjC:
18614       if (prototype_p (func_type))
18615         add_AT_flag (die, DW_AT_prototyped, 1);
18616       break;
18617     default:
18618       break;
18619     }
18620 }
18621
18622 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18623    by looking in either the type declaration or object declaration
18624    equate table.  */
18625
18626 static inline dw_die_ref
18627 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18628 {
18629   dw_die_ref origin_die = NULL;
18630
18631   if (TREE_CODE (origin) != FUNCTION_DECL)
18632     {
18633       /* We may have gotten separated from the block for the inlined
18634          function, if we're in an exception handler or some such; make
18635          sure that the abstract function has been written out.
18636
18637          Doing this for nested functions is wrong, however; functions are
18638          distinct units, and our context might not even be inline.  */
18639       tree fn = origin;
18640
18641       if (TYPE_P (fn))
18642         fn = TYPE_STUB_DECL (fn);
18643
18644       fn = decl_function_context (fn);
18645       if (fn)
18646         dwarf2out_abstract_function (fn);
18647     }
18648
18649   if (DECL_P (origin))
18650     origin_die = lookup_decl_die (origin);
18651   else if (TYPE_P (origin))
18652     origin_die = lookup_type_die (origin);
18653
18654   /* XXX: Functions that are never lowered don't always have correct block
18655      trees (in the case of java, they simply have no block tree, in some other
18656      languages).  For these functions, there is nothing we can really do to
18657      output correct debug info for inlined functions in all cases.  Rather
18658      than die, we'll just produce deficient debug info now, in that we will
18659      have variables without a proper abstract origin.  In the future, when all
18660      functions are lowered, we should re-add a gcc_assert (origin_die)
18661      here.  */
18662
18663   if (origin_die)
18664     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18665   return origin_die;
18666 }
18667
18668 /* We do not currently support the pure_virtual attribute.  */
18669
18670 static inline void
18671 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18672 {
18673   if (DECL_VINDEX (func_decl))
18674     {
18675       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18676
18677       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18678         add_AT_loc (die, DW_AT_vtable_elem_location,
18679                     new_loc_descr (DW_OP_constu,
18680                                    tree_to_shwi (DECL_VINDEX (func_decl)),
18681                                    0));
18682
18683       /* GNU extension: Record what type this method came from originally.  */
18684       if (debug_info_level > DINFO_LEVEL_TERSE
18685           && DECL_CONTEXT (func_decl))
18686         add_AT_die_ref (die, DW_AT_containing_type,
18687                         lookup_type_die (DECL_CONTEXT (func_decl)));
18688     }
18689 }
18690 \f
18691 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18692    given decl.  This used to be a vendor extension until after DWARF 4
18693    standardized it.  */
18694
18695 static void
18696 add_linkage_attr (dw_die_ref die, tree decl)
18697 {
18698   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18699
18700   /* Mimic what assemble_name_raw does with a leading '*'.  */
18701   if (name[0] == '*')
18702     name = &name[1];
18703
18704   if (dwarf_version >= 4)
18705     add_AT_string (die, DW_AT_linkage_name, name);
18706   else
18707     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18708 }
18709
18710 /* Add source coordinate attributes for the given decl.  */
18711
18712 static void
18713 add_src_coords_attributes (dw_die_ref die, tree decl)
18714 {
18715   expanded_location s;
18716
18717   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18718     return;
18719   s = expand_location (DECL_SOURCE_LOCATION (decl));
18720   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18721   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18722 }
18723
18724 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18725
18726 static void
18727 add_linkage_name_raw (dw_die_ref die, tree decl)
18728 {
18729   /* Defer until we have an assembler name set.  */
18730   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18731     {
18732       limbo_die_node *asm_name;
18733
18734       asm_name = ggc_cleared_alloc<limbo_die_node> ();
18735       asm_name->die = die;
18736       asm_name->created_for = decl;
18737       asm_name->next = deferred_asm_name;
18738       deferred_asm_name = asm_name;
18739     }
18740   else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18741     add_linkage_attr (die, decl);
18742 }
18743
18744 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired.  */
18745
18746 static void
18747 add_linkage_name (dw_die_ref die, tree decl)
18748 {
18749   if (debug_info_level > DINFO_LEVEL_NONE
18750       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18751       && TREE_PUBLIC (decl)
18752       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18753       && die->die_tag != DW_TAG_member)
18754     add_linkage_name_raw (die, decl);
18755 }
18756
18757 /* Add a DW_AT_name attribute and source coordinate attribute for the
18758    given decl, but only if it actually has a name.  */
18759
18760 static void
18761 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18762 {
18763   tree decl_name;
18764
18765   decl_name = DECL_NAME (decl);
18766   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18767     {
18768       const char *name = dwarf2_name (decl, 0);
18769       if (name)
18770         add_name_attribute (die, name);
18771       if (! DECL_ARTIFICIAL (decl))
18772         add_src_coords_attributes (die, decl);
18773
18774       add_linkage_name (die, decl);
18775     }
18776
18777 #ifdef VMS_DEBUGGING_INFO
18778   /* Get the function's name, as described by its RTL.  This may be different
18779      from the DECL_NAME name used in the source file.  */
18780   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18781     {
18782       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18783                   XEXP (DECL_RTL (decl), 0), false);
18784       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18785     }
18786 #endif /* VMS_DEBUGGING_INFO */
18787 }
18788
18789 /* Add VALUE as a DW_AT_discr_value attribute to DIE.  */
18790
18791 static void
18792 add_discr_value (dw_die_ref die, dw_discr_value *value)
18793 {
18794   dw_attr_node attr;
18795
18796   attr.dw_attr = DW_AT_discr_value;
18797   attr.dw_attr_val.val_class = dw_val_class_discr_value;
18798   attr.dw_attr_val.val_entry = NULL;
18799   attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18800   if (value->pos)
18801     attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18802   else
18803     attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18804   add_dwarf_attr (die, &attr);
18805 }
18806
18807 /* Add DISCR_LIST as a DW_AT_discr_list to DIE.  */
18808
18809 static void
18810 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18811 {
18812   dw_attr_node attr;
18813
18814   attr.dw_attr = DW_AT_discr_list;
18815   attr.dw_attr_val.val_class = dw_val_class_discr_list;
18816   attr.dw_attr_val.val_entry = NULL;
18817   attr.dw_attr_val.v.val_discr_list = discr_list;
18818   add_dwarf_attr (die, &attr);
18819 }
18820
18821 static inline dw_discr_list_ref
18822 AT_discr_list (dw_attr_node *attr)
18823 {
18824   return attr->dw_attr_val.v.val_discr_list;
18825 }
18826
18827 #ifdef VMS_DEBUGGING_INFO
18828 /* Output the debug main pointer die for VMS */
18829
18830 void
18831 dwarf2out_vms_debug_main_pointer (void)
18832 {
18833   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18834   dw_die_ref die;
18835
18836   /* Allocate the VMS debug main subprogram die.  */
18837   die = ggc_cleared_alloc<die_node> ();
18838   die->die_tag = DW_TAG_subprogram;
18839   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18840   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18841                                current_function_funcdef_no);
18842   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18843
18844   /* Make it the first child of comp_unit_die ().  */
18845   die->die_parent = comp_unit_die ();
18846   if (comp_unit_die ()->die_child)
18847     {
18848       die->die_sib = comp_unit_die ()->die_child->die_sib;
18849       comp_unit_die ()->die_child->die_sib = die;
18850     }
18851   else
18852     {
18853       die->die_sib = die;
18854       comp_unit_die ()->die_child = die;
18855     }
18856 }
18857 #endif /* VMS_DEBUGGING_INFO */
18858
18859 /* Push a new declaration scope.  */
18860
18861 static void
18862 push_decl_scope (tree scope)
18863 {
18864   vec_safe_push (decl_scope_table, scope);
18865 }
18866
18867 /* Pop a declaration scope.  */
18868
18869 static inline void
18870 pop_decl_scope (void)
18871 {
18872   decl_scope_table->pop ();
18873 }
18874
18875 /* walk_tree helper function for uses_local_type, below.  */
18876
18877 static tree
18878 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18879 {
18880   if (!TYPE_P (*tp))
18881     *walk_subtrees = 0;
18882   else
18883     {
18884       tree name = TYPE_NAME (*tp);
18885       if (name && DECL_P (name) && decl_function_context (name))
18886         return *tp;
18887     }
18888   return NULL_TREE;
18889 }
18890
18891 /* If TYPE involves a function-local type (including a local typedef to a
18892    non-local type), returns that type; otherwise returns NULL_TREE.  */
18893
18894 static tree
18895 uses_local_type (tree type)
18896 {
18897   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18898   return used;
18899 }
18900
18901 /* Return the DIE for the scope that immediately contains this type.
18902    Non-named types that do not involve a function-local type get global
18903    scope.  Named types nested in namespaces or other types get their
18904    containing scope.  All other types (i.e. function-local named types) get
18905    the current active scope.  */
18906
18907 static dw_die_ref
18908 scope_die_for (tree t, dw_die_ref context_die)
18909 {
18910   dw_die_ref scope_die = NULL;
18911   tree containing_scope;
18912
18913   /* Non-types always go in the current scope.  */
18914   gcc_assert (TYPE_P (t));
18915
18916   /* Use the scope of the typedef, rather than the scope of the type
18917      it refers to.  */
18918   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
18919     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
18920   else
18921     containing_scope = TYPE_CONTEXT (t);
18922
18923   /* Use the containing namespace if there is one.  */
18924   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18925     {
18926       if (context_die == lookup_decl_die (containing_scope))
18927         /* OK */;
18928       else if (debug_info_level > DINFO_LEVEL_TERSE)
18929         context_die = get_context_die (containing_scope);
18930       else
18931         containing_scope = NULL_TREE;
18932     }
18933
18934   /* Ignore function type "scopes" from the C frontend.  They mean that
18935      a tagged type is local to a parmlist of a function declarator, but
18936      that isn't useful to DWARF.  */
18937   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18938     containing_scope = NULL_TREE;
18939
18940   if (SCOPE_FILE_SCOPE_P (containing_scope))
18941     {
18942       /* If T uses a local type keep it local as well, to avoid references
18943          to function-local DIEs from outside the function.  */
18944       if (current_function_decl && uses_local_type (t))
18945         scope_die = context_die;
18946       else
18947         scope_die = comp_unit_die ();
18948     }
18949   else if (TYPE_P (containing_scope))
18950     {
18951       /* For types, we can just look up the appropriate DIE.  */
18952       if (debug_info_level > DINFO_LEVEL_TERSE)
18953         scope_die = get_context_die (containing_scope);
18954       else
18955         {
18956           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18957           if (scope_die == NULL)
18958             scope_die = comp_unit_die ();
18959         }
18960     }
18961   else
18962     scope_die = context_die;
18963
18964   return scope_die;
18965 }
18966
18967 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18968
18969 static inline int
18970 local_scope_p (dw_die_ref context_die)
18971 {
18972   for (; context_die; context_die = context_die->die_parent)
18973     if (context_die->die_tag == DW_TAG_inlined_subroutine
18974         || context_die->die_tag == DW_TAG_subprogram)
18975       return 1;
18976
18977   return 0;
18978 }
18979
18980 /* Returns nonzero if CONTEXT_DIE is a class.  */
18981
18982 static inline int
18983 class_scope_p (dw_die_ref context_die)
18984 {
18985   return (context_die
18986           && (context_die->die_tag == DW_TAG_structure_type
18987               || context_die->die_tag == DW_TAG_class_type
18988               || context_die->die_tag == DW_TAG_interface_type
18989               || context_die->die_tag == DW_TAG_union_type));
18990 }
18991
18992 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18993    whether or not to treat a DIE in this context as a declaration.  */
18994
18995 static inline int
18996 class_or_namespace_scope_p (dw_die_ref context_die)
18997 {
18998   return (class_scope_p (context_die)
18999           || (context_die && context_die->die_tag == DW_TAG_namespace));
19000 }
19001
19002 /* Many forms of DIEs require a "type description" attribute.  This
19003    routine locates the proper "type descriptor" die for the type given
19004    by 'type' plus any additional qualifiers given by 'cv_quals', and
19005    adds a DW_AT_type attribute below the given die.  */
19006
19007 static void
19008 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19009                     bool reverse, dw_die_ref context_die)
19010 {
19011   enum tree_code code  = TREE_CODE (type);
19012   dw_die_ref type_die  = NULL;
19013
19014   /* ??? If this type is an unnamed subrange type of an integral, floating-point
19015      or fixed-point type, use the inner type.  This is because we have no
19016      support for unnamed types in base_type_die.  This can happen if this is
19017      an Ada subrange type.  Correct solution is emit a subrange type die.  */
19018   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19019       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19020     type = TREE_TYPE (type), code = TREE_CODE (type);
19021
19022   if (code == ERROR_MARK
19023       /* Handle a special case.  For functions whose return type is void, we
19024          generate *no* type attribute.  (Note that no object may have type
19025          `void', so this only applies to function return types).  */
19026       || code == VOID_TYPE)
19027     return;
19028
19029   type_die = modified_type_die (type,
19030                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19031                                 reverse,
19032                                 context_die);
19033
19034   if (type_die != NULL)
19035     add_AT_die_ref (object_die, DW_AT_type, type_die);
19036 }
19037
19038 /* Given an object die, add the calling convention attribute for the
19039    function call type.  */
19040 static void
19041 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19042 {
19043   enum dwarf_calling_convention value = DW_CC_normal;
19044
19045   value = ((enum dwarf_calling_convention)
19046            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19047
19048   if (is_fortran ()
19049       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19050     {
19051       /* DWARF 2 doesn't provide a way to identify a program's source-level
19052         entry point.  DW_AT_calling_convention attributes are only meant
19053         to describe functions' calling conventions.  However, lacking a
19054         better way to signal the Fortran main program, we used this for 
19055         a long time, following existing custom.  Now, DWARF 4 has 
19056         DW_AT_main_subprogram, which we add below, but some tools still
19057         rely on the old way, which we thus keep.  */
19058       value = DW_CC_program;
19059
19060       if (dwarf_version >= 4 || !dwarf_strict)
19061         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19062     }
19063
19064   /* Only add the attribute if the backend requests it, and
19065      is not DW_CC_normal.  */
19066   if (value && (value != DW_CC_normal))
19067     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19068 }
19069
19070 /* Given a tree pointer to a struct, class, union, or enum type node, return
19071    a pointer to the (string) tag name for the given type, or zero if the type
19072    was declared without a tag.  */
19073
19074 static const char *
19075 type_tag (const_tree type)
19076 {
19077   const char *name = 0;
19078
19079   if (TYPE_NAME (type) != 0)
19080     {
19081       tree t = 0;
19082
19083       /* Find the IDENTIFIER_NODE for the type name.  */
19084       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19085           && !TYPE_NAMELESS (type))
19086         t = TYPE_NAME (type);
19087
19088       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19089          a TYPE_DECL node, regardless of whether or not a `typedef' was
19090          involved.  */
19091       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19092                && ! DECL_IGNORED_P (TYPE_NAME (type)))
19093         {
19094           /* We want to be extra verbose.  Don't call dwarf_name if
19095              DECL_NAME isn't set.  The default hook for decl_printable_name
19096              doesn't like that, and in this context it's correct to return
19097              0, instead of "<anonymous>" or the like.  */
19098           if (DECL_NAME (TYPE_NAME (type))
19099               && !DECL_NAMELESS (TYPE_NAME (type)))
19100             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19101         }
19102
19103       /* Now get the name as a string, or invent one.  */
19104       if (!name && t != 0)
19105         name = IDENTIFIER_POINTER (t);
19106     }
19107
19108   return (name == 0 || *name == '\0') ? 0 : name;
19109 }
19110
19111 /* Return the type associated with a data member, make a special check
19112    for bit field types.  */
19113
19114 static inline tree
19115 member_declared_type (const_tree member)
19116 {
19117   return (DECL_BIT_FIELD_TYPE (member)
19118           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19119 }
19120
19121 /* Get the decl's label, as described by its RTL. This may be different
19122    from the DECL_NAME name used in the source file.  */
19123
19124 #if 0
19125 static const char *
19126 decl_start_label (tree decl)
19127 {
19128   rtx x;
19129   const char *fnname;
19130
19131   x = DECL_RTL (decl);
19132   gcc_assert (MEM_P (x));
19133
19134   x = XEXP (x, 0);
19135   gcc_assert (GET_CODE (x) == SYMBOL_REF);
19136
19137   fnname = XSTR (x, 0);
19138   return fnname;
19139 }
19140 #endif
19141 \f
19142 /* For variable-length arrays that have been previously generated, but
19143    may be incomplete due to missing subscript info, fill the subscript
19144    info.  Return TRUE if this is one of those cases.  */
19145 static bool
19146 fill_variable_array_bounds (tree type)
19147 {
19148   if (TREE_ASM_WRITTEN (type)
19149       && TREE_CODE (type) == ARRAY_TYPE
19150       && variably_modified_type_p (type, NULL))
19151     {
19152       dw_die_ref array_die = lookup_type_die (type);
19153       if (!array_die)
19154         return false;
19155       add_subscript_info (array_die, type, !is_ada ());
19156       return true;
19157     }
19158   return false;
19159 }
19160
19161 /* These routines generate the internal representation of the DIE's for
19162    the compilation unit.  Debugging information is collected by walking
19163    the declaration trees passed in from dwarf2out_decl().  */
19164
19165 static void
19166 gen_array_type_die (tree type, dw_die_ref context_die)
19167 {
19168   dw_die_ref array_die;
19169
19170   /* GNU compilers represent multidimensional array types as sequences of one
19171      dimensional array types whose element types are themselves array types.
19172      We sometimes squish that down to a single array_type DIE with multiple
19173      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19174      say that we are allowed to do this kind of compression in C, because
19175      there is no difference between an array of arrays and a multidimensional
19176      array.  We don't do this for Ada to remain as close as possible to the
19177      actual representation, which is especially important against the language
19178      flexibilty wrt arrays of variable size.  */
19179
19180   bool collapse_nested_arrays = !is_ada ();
19181
19182   if (fill_variable_array_bounds (type))
19183     return;
19184
19185   dw_die_ref scope_die = scope_die_for (type, context_die);
19186   tree element_type;
19187
19188   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19189      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19190   if (TYPE_STRING_FLAG (type)
19191       && TREE_CODE (type) == ARRAY_TYPE
19192       && is_fortran ()
19193       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19194     {
19195       HOST_WIDE_INT size;
19196
19197       array_die = new_die (DW_TAG_string_type, scope_die, type);
19198       add_name_attribute (array_die, type_tag (type));
19199       equate_type_number_to_die (type, array_die);
19200       size = int_size_in_bytes (type);
19201       if (size >= 0)
19202         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19203       else if (TYPE_DOMAIN (type) != NULL_TREE
19204                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19205                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19206         {
19207           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19208           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19209
19210           size = int_size_in_bytes (TREE_TYPE (szdecl));
19211           if (loc && size > 0)
19212             {
19213               add_AT_location_description (array_die, DW_AT_string_length, loc);
19214               if (size != DWARF2_ADDR_SIZE)
19215                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19216             }
19217         }
19218       return;
19219     }
19220
19221   array_die = new_die (DW_TAG_array_type, scope_die, type);
19222   add_name_attribute (array_die, type_tag (type));
19223   equate_type_number_to_die (type, array_die);
19224
19225   if (TREE_CODE (type) == VECTOR_TYPE)
19226     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19227
19228   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19229   if (is_fortran ()
19230       && TREE_CODE (type) == ARRAY_TYPE
19231       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19232       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19233     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19234
19235 #if 0
19236   /* We default the array ordering.  SDB will probably do
19237      the right things even if DW_AT_ordering is not present.  It's not even
19238      an issue until we start to get into multidimensional arrays anyway.  If
19239      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19240      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19241      and when we find out that we need to put these in, we will only do so
19242      for multidimensional arrays.  */
19243   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19244 #endif
19245
19246   if (TREE_CODE (type) == VECTOR_TYPE)
19247     {
19248       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19249       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19250       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19251       add_bound_info (subrange_die, DW_AT_upper_bound,
19252                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19253     }
19254   else
19255     add_subscript_info (array_die, type, collapse_nested_arrays);
19256
19257   /* Add representation of the type of the elements of this array type and
19258      emit the corresponding DIE if we haven't done it already.  */
19259   element_type = TREE_TYPE (type);
19260   if (collapse_nested_arrays)
19261     while (TREE_CODE (element_type) == ARRAY_TYPE)
19262       {
19263         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19264           break;
19265         element_type = TREE_TYPE (element_type);
19266       }
19267
19268   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19269                       TREE_CODE (type) == ARRAY_TYPE
19270                       && TYPE_REVERSE_STORAGE_ORDER (type),
19271                       context_die);
19272
19273   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19274   if (TYPE_ARTIFICIAL (type))
19275     add_AT_flag (array_die, DW_AT_artificial, 1);
19276
19277   if (get_AT (array_die, DW_AT_name))
19278     add_pubtype (type, array_die);
19279 }
19280
19281 /* This routine generates DIE for array with hidden descriptor, details
19282    are filled into *info by a langhook.  */
19283
19284 static void
19285 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19286                           dw_die_ref context_die)
19287 {
19288   const dw_die_ref scope_die = scope_die_for (type, context_die);
19289   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19290   const struct loc_descr_context context = { type, info->base_decl, NULL };
19291   int dim;
19292
19293   add_name_attribute (array_die, type_tag (type));
19294   equate_type_number_to_die (type, array_die);
19295
19296   if (info->ndimensions > 1)
19297     switch (info->ordering)
19298       {
19299       case array_descr_ordering_row_major:
19300         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19301         break;
19302       case array_descr_ordering_column_major:
19303         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19304         break;
19305       default:
19306         break;
19307       }
19308
19309   if (dwarf_version >= 3 || !dwarf_strict)
19310     {
19311       if (info->data_location)
19312         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19313                          dw_scalar_form_exprloc, &context);
19314       if (info->associated)
19315         add_scalar_info (array_die, DW_AT_associated, info->associated,
19316                          dw_scalar_form_constant
19317                          | dw_scalar_form_exprloc
19318                          | dw_scalar_form_reference, &context);
19319       if (info->allocated)
19320         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19321                          dw_scalar_form_constant
19322                          | dw_scalar_form_exprloc
19323                          | dw_scalar_form_reference, &context);
19324       if (info->stride)
19325         {
19326           const enum dwarf_attribute attr
19327             = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19328           const int forms
19329             = (info->stride_in_bits)
19330               ? dw_scalar_form_constant
19331               : (dw_scalar_form_constant
19332                  | dw_scalar_form_exprloc
19333                  | dw_scalar_form_reference);
19334
19335           add_scalar_info (array_die, attr, info->stride, forms, &context);
19336         }
19337     }
19338
19339   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19340
19341   for (dim = 0; dim < info->ndimensions; dim++)
19342     {
19343       dw_die_ref subrange_die
19344         = new_die (DW_TAG_subrange_type, array_die, NULL);
19345
19346       if (info->dimen[dim].bounds_type)
19347         add_type_attribute (subrange_die,
19348                             info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19349                             false, context_die);
19350       if (info->dimen[dim].lower_bound)
19351         add_bound_info (subrange_die, DW_AT_lower_bound,
19352                         info->dimen[dim].lower_bound, &context);
19353       if (info->dimen[dim].upper_bound)
19354         add_bound_info (subrange_die, DW_AT_upper_bound,
19355                         info->dimen[dim].upper_bound, &context);
19356       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19357         add_scalar_info (subrange_die, DW_AT_byte_stride,
19358                          info->dimen[dim].stride,
19359                          dw_scalar_form_constant
19360                          | dw_scalar_form_exprloc
19361                          | dw_scalar_form_reference,
19362                          &context);
19363     }
19364
19365   gen_type_die (info->element_type, context_die);
19366   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19367                       TREE_CODE (type) == ARRAY_TYPE
19368                       && TYPE_REVERSE_STORAGE_ORDER (type),
19369                       context_die);
19370
19371   if (get_AT (array_die, DW_AT_name))
19372     add_pubtype (type, array_die);
19373 }
19374
19375 #if 0
19376 static void
19377 gen_entry_point_die (tree decl, dw_die_ref context_die)
19378 {
19379   tree origin = decl_ultimate_origin (decl);
19380   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19381
19382   if (origin != NULL)
19383     add_abstract_origin_attribute (decl_die, origin);
19384   else
19385     {
19386       add_name_and_src_coords_attributes (decl_die, decl);
19387       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19388                           TYPE_UNQUALIFIED, false, context_die);
19389     }
19390
19391   if (DECL_ABSTRACT_P (decl))
19392     equate_decl_number_to_die (decl, decl_die);
19393   else
19394     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19395 }
19396 #endif
19397
19398 /* Walk through the list of incomplete types again, trying once more to
19399    emit full debugging info for them.  */
19400
19401 static void
19402 retry_incomplete_types (void)
19403 {
19404   int i;
19405
19406   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19407     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19408       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19409 }
19410
19411 /* Determine what tag to use for a record type.  */
19412
19413 static enum dwarf_tag
19414 record_type_tag (tree type)
19415 {
19416   if (! lang_hooks.types.classify_record)
19417     return DW_TAG_structure_type;
19418
19419   switch (lang_hooks.types.classify_record (type))
19420     {
19421     case RECORD_IS_STRUCT:
19422       return DW_TAG_structure_type;
19423
19424     case RECORD_IS_CLASS:
19425       return DW_TAG_class_type;
19426
19427     case RECORD_IS_INTERFACE:
19428       if (dwarf_version >= 3 || !dwarf_strict)
19429         return DW_TAG_interface_type;
19430       return DW_TAG_structure_type;
19431
19432     default:
19433       gcc_unreachable ();
19434     }
19435 }
19436
19437 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19438    include all of the information about the enumeration values also. Each
19439    enumerated type name/value is listed as a child of the enumerated type
19440    DIE.  */
19441
19442 static dw_die_ref
19443 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19444 {
19445   dw_die_ref type_die = lookup_type_die (type);
19446
19447   if (type_die == NULL)
19448     {
19449       type_die = new_die (DW_TAG_enumeration_type,
19450                           scope_die_for (type, context_die), type);
19451       equate_type_number_to_die (type, type_die);
19452       add_name_attribute (type_die, type_tag (type));
19453       if (dwarf_version >= 4 || !dwarf_strict)
19454         {
19455           if (ENUM_IS_SCOPED (type))
19456             add_AT_flag (type_die, DW_AT_enum_class, 1);
19457           if (ENUM_IS_OPAQUE (type))
19458             add_AT_flag (type_die, DW_AT_declaration, 1);
19459         }
19460     }
19461   else if (! TYPE_SIZE (type))
19462     return type_die;
19463   else
19464     remove_AT (type_die, DW_AT_declaration);
19465
19466   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19467      given enum type is incomplete, do not generate the DW_AT_byte_size
19468      attribute or the DW_AT_element_list attribute.  */
19469   if (TYPE_SIZE (type))
19470     {
19471       tree link;
19472
19473       TREE_ASM_WRITTEN (type) = 1;
19474       add_byte_size_attribute (type_die, type);
19475       if (dwarf_version >= 3 || !dwarf_strict)
19476         {
19477           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19478           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19479                               context_die);
19480         }
19481       if (TYPE_STUB_DECL (type) != NULL_TREE)
19482         {
19483           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19484           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19485         }
19486
19487       /* If the first reference to this type was as the return type of an
19488          inline function, then it may not have a parent.  Fix this now.  */
19489       if (type_die->die_parent == NULL)
19490         add_child_die (scope_die_for (type, context_die), type_die);
19491
19492       for (link = TYPE_VALUES (type);
19493            link != NULL; link = TREE_CHAIN (link))
19494         {
19495           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19496           tree value = TREE_VALUE (link);
19497
19498           add_name_attribute (enum_die,
19499                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19500
19501           if (TREE_CODE (value) == CONST_DECL)
19502             value = DECL_INITIAL (value);
19503
19504           if (simple_type_size_in_bits (TREE_TYPE (value))
19505               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19506             {
19507               /* For constant forms created by add_AT_unsigned DWARF
19508                  consumers (GDB, elfutils, etc.) always zero extend
19509                  the value.  Only when the actual value is negative
19510                  do we need to use add_AT_int to generate a constant
19511                  form that can represent negative values.  */
19512               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19513               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19514                 add_AT_unsigned (enum_die, DW_AT_const_value,
19515                                  (unsigned HOST_WIDE_INT) val);
19516               else
19517                 add_AT_int (enum_die, DW_AT_const_value, val);
19518             }
19519           else
19520             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
19521                that here.  TODO: This should be re-worked to use correct
19522                signed/unsigned double tags for all cases.  */
19523             add_AT_wide (enum_die, DW_AT_const_value, value);
19524         }
19525
19526       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19527       if (TYPE_ARTIFICIAL (type))
19528         add_AT_flag (type_die, DW_AT_artificial, 1);
19529     }
19530   else
19531     add_AT_flag (type_die, DW_AT_declaration, 1);
19532
19533   add_pubtype (type, type_die);
19534
19535   return type_die;
19536 }
19537
19538 /* Generate a DIE to represent either a real live formal parameter decl or to
19539    represent just the type of some formal parameter position in some function
19540    type.
19541
19542    Note that this routine is a bit unusual because its argument may be a
19543    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19544    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19545    node.  If it's the former then this function is being called to output a
19546    DIE to represent a formal parameter object (or some inlining thereof).  If
19547    it's the latter, then this function is only being called to output a
19548    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19549    argument type of some subprogram type.
19550    If EMIT_NAME_P is true, name and source coordinate attributes
19551    are emitted.  */
19552
19553 static dw_die_ref
19554 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19555                           dw_die_ref context_die)
19556 {
19557   tree node_or_origin = node ? node : origin;
19558   tree ultimate_origin;
19559   dw_die_ref parm_die = NULL;
19560   
19561   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19562     {
19563       parm_die = lookup_decl_die (node);
19564
19565       /* If the contexts differ, we may not be talking about the same
19566          thing.  */
19567       if (parm_die && parm_die->die_parent != context_die)
19568         {
19569           if (!DECL_ABSTRACT_P (node))
19570             {
19571               /* This can happen when creating an inlined instance, in
19572                  which case we need to create a new DIE that will get
19573                  annotated with DW_AT_abstract_origin.  */
19574               parm_die = NULL;
19575             }
19576           else
19577             {
19578               /* FIXME: Reuse DIE even with a differing context.
19579
19580                  This can happen when calling
19581                  dwarf2out_abstract_function to build debug info for
19582                  the abstract instance of a function for which we have
19583                  already generated a DIE in
19584                  dwarf2out_early_global_decl.
19585
19586                  Once we remove dwarf2out_abstract_function, we should
19587                  have a call to gcc_unreachable here.  */
19588             }
19589         }
19590
19591       if (parm_die && parm_die->die_parent == NULL)
19592         {
19593           /* Check that parm_die already has the right attributes that
19594              we would have added below.  If any attributes are
19595              missing, fall through to add them.  */
19596           if (! DECL_ABSTRACT_P (node_or_origin)
19597               && !get_AT (parm_die, DW_AT_location)
19598               && !get_AT (parm_die, DW_AT_const_value))
19599             /* We are missing  location info, and are about to add it.  */
19600             ;
19601           else
19602             {
19603               add_child_die (context_die, parm_die);
19604               return parm_die;
19605             }
19606         }
19607     }
19608
19609   /* If we have a previously generated DIE, use it, unless this is an
19610      concrete instance (origin != NULL), in which case we need a new
19611      DIE with a corresponding DW_AT_abstract_origin.  */
19612   bool reusing_die;
19613   if (parm_die && origin == NULL)
19614     reusing_die = true;
19615   else
19616     {
19617       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19618       reusing_die = false;
19619     }
19620
19621   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19622     {
19623     case tcc_declaration:
19624       ultimate_origin = decl_ultimate_origin (node_or_origin);
19625       if (node || ultimate_origin)
19626         origin = ultimate_origin;
19627
19628       if (reusing_die)
19629         goto add_location;
19630
19631       if (origin != NULL)
19632         add_abstract_origin_attribute (parm_die, origin);
19633       else if (emit_name_p)
19634         add_name_and_src_coords_attributes (parm_die, node);
19635       if (origin == NULL
19636           || (! DECL_ABSTRACT_P (node_or_origin)
19637               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19638                                            decl_function_context
19639                                                             (node_or_origin))))
19640         {
19641           tree type = TREE_TYPE (node_or_origin);
19642           if (decl_by_reference_p (node_or_origin))
19643             add_type_attribute (parm_die, TREE_TYPE (type),
19644                                 TYPE_UNQUALIFIED,
19645                                 false, context_die);
19646           else
19647             add_type_attribute (parm_die, type,
19648                                 decl_quals (node_or_origin),
19649                                 false, context_die);
19650         }
19651       if (origin == NULL && DECL_ARTIFICIAL (node))
19652         add_AT_flag (parm_die, DW_AT_artificial, 1);
19653     add_location:
19654       if (node && node != origin)
19655         equate_decl_number_to_die (node, parm_die);
19656       if (! DECL_ABSTRACT_P (node_or_origin))
19657         add_location_or_const_value_attribute (parm_die, node_or_origin,
19658                                                node == NULL);
19659
19660       break;
19661
19662     case tcc_type:
19663       /* We were called with some kind of a ..._TYPE node.  */
19664       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19665                           context_die);
19666       break;
19667
19668     default:
19669       gcc_unreachable ();
19670     }
19671
19672   return parm_die;
19673 }
19674
19675 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19676    children DW_TAG_formal_parameter DIEs representing the arguments of the
19677    parameter pack.
19678
19679    PARM_PACK must be a function parameter pack.
19680    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19681    must point to the subsequent arguments of the function PACK_ARG belongs to.
19682    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19683    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19684    following the last one for which a DIE was generated.  */
19685
19686 static dw_die_ref
19687 gen_formal_parameter_pack_die  (tree parm_pack,
19688                                 tree pack_arg,
19689                                 dw_die_ref subr_die,
19690                                 tree *next_arg)
19691 {
19692   tree arg;
19693   dw_die_ref parm_pack_die;
19694
19695   gcc_assert (parm_pack
19696               && lang_hooks.function_parameter_pack_p (parm_pack)
19697               && subr_die);
19698
19699   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19700   add_src_coords_attributes (parm_pack_die, parm_pack);
19701
19702   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19703     {
19704       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19705                                                                  parm_pack))
19706         break;
19707       gen_formal_parameter_die (arg, NULL,
19708                                 false /* Don't emit name attribute.  */,
19709                                 parm_pack_die);
19710     }
19711   if (next_arg)
19712     *next_arg = arg;
19713   return parm_pack_die;
19714 }
19715
19716 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19717    at the end of an (ANSI prototyped) formal parameters list.  */
19718
19719 static void
19720 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19721 {
19722   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19723 }
19724
19725 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19726    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19727    parameters as specified in some function type specification (except for
19728    those which appear as part of a function *definition*).  */
19729
19730 static void
19731 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19732 {
19733   tree link;
19734   tree formal_type = NULL;
19735   tree first_parm_type;
19736   tree arg;
19737
19738   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19739     {
19740       arg = DECL_ARGUMENTS (function_or_method_type);
19741       function_or_method_type = TREE_TYPE (function_or_method_type);
19742     }
19743   else
19744     arg = NULL_TREE;
19745
19746   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19747
19748   /* Make our first pass over the list of formal parameter types and output a
19749      DW_TAG_formal_parameter DIE for each one.  */
19750   for (link = first_parm_type; link; )
19751     {
19752       dw_die_ref parm_die;
19753
19754       formal_type = TREE_VALUE (link);
19755       if (formal_type == void_type_node)
19756         break;
19757
19758       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19759       if (!POINTER_BOUNDS_TYPE_P (formal_type))
19760         {
19761           parm_die = gen_formal_parameter_die (formal_type, NULL,
19762                                                true /* Emit name attribute.  */,
19763                                                context_die);
19764           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19765               && link == first_parm_type)
19766             {
19767               add_AT_flag (parm_die, DW_AT_artificial, 1);
19768               if (dwarf_version >= 3 || !dwarf_strict)
19769                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19770             }
19771           else if (arg && DECL_ARTIFICIAL (arg))
19772             add_AT_flag (parm_die, DW_AT_artificial, 1);
19773         }
19774
19775       link = TREE_CHAIN (link);
19776       if (arg)
19777         arg = DECL_CHAIN (arg);
19778     }
19779
19780   /* If this function type has an ellipsis, add a
19781      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19782   if (formal_type != void_type_node)
19783     gen_unspecified_parameters_die (function_or_method_type, context_die);
19784
19785   /* Make our second (and final) pass over the list of formal parameter types
19786      and output DIEs to represent those types (as necessary).  */
19787   for (link = TYPE_ARG_TYPES (function_or_method_type);
19788        link && TREE_VALUE (link);
19789        link = TREE_CHAIN (link))
19790     gen_type_die (TREE_VALUE (link), context_die);
19791 }
19792
19793 /* We want to generate the DIE for TYPE so that we can generate the
19794    die for MEMBER, which has been defined; we will need to refer back
19795    to the member declaration nested within TYPE.  If we're trying to
19796    generate minimal debug info for TYPE, processing TYPE won't do the
19797    trick; we need to attach the member declaration by hand.  */
19798
19799 static void
19800 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19801 {
19802   gen_type_die (type, context_die);
19803
19804   /* If we're trying to avoid duplicate debug info, we may not have
19805      emitted the member decl for this function.  Emit it now.  */
19806   if (TYPE_STUB_DECL (type)
19807       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19808       && ! lookup_decl_die (member))
19809     {
19810       dw_die_ref type_die;
19811       gcc_assert (!decl_ultimate_origin (member));
19812
19813       push_decl_scope (type);
19814       type_die = lookup_type_die_strip_naming_typedef (type);
19815       if (TREE_CODE (member) == FUNCTION_DECL)
19816         gen_subprogram_die (member, type_die);
19817       else if (TREE_CODE (member) == FIELD_DECL)
19818         {
19819           /* Ignore the nameless fields that are used to skip bits but handle
19820              C++ anonymous unions and structs.  */
19821           if (DECL_NAME (member) != NULL_TREE
19822               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19823               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19824             {
19825               struct vlr_context vlr_ctx = {
19826                 DECL_CONTEXT (member), /* struct_type */
19827                 NULL_TREE /* variant_part_offset */
19828               };
19829               gen_type_die (member_declared_type (member), type_die);
19830               gen_field_die (member, &vlr_ctx, type_die);
19831             }
19832         }
19833       else
19834         gen_variable_die (member, NULL_TREE, type_die);
19835
19836       pop_decl_scope ();
19837     }
19838 }
19839 \f
19840 /* Forward declare these functions, because they are mutually recursive
19841   with their set_block_* pairing functions.  */
19842 static void set_decl_origin_self (tree);
19843 static void set_decl_abstract_flags (tree, vec<tree> &);
19844
19845 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
19846    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
19847    that it points to the node itself, thus indicating that the node is its
19848    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
19849    the given node is NULL, recursively descend the decl/block tree which
19850    it is the root of, and for each other ..._DECL or BLOCK node contained
19851    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
19852    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
19853    values to point to themselves.  */
19854
19855 static void
19856 set_block_origin_self (tree stmt)
19857 {
19858   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
19859     {
19860       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
19861
19862       {
19863         tree local_decl;
19864
19865         for (local_decl = BLOCK_VARS (stmt);
19866              local_decl != NULL_TREE;
19867              local_decl = DECL_CHAIN (local_decl))
19868           /* Do not recurse on nested functions since the inlining status
19869              of parent and child can be different as per the DWARF spec.  */
19870           if (TREE_CODE (local_decl) != FUNCTION_DECL
19871               && !DECL_EXTERNAL (local_decl))
19872             set_decl_origin_self (local_decl);
19873       }
19874
19875       {
19876         tree subblock;
19877
19878         for (subblock = BLOCK_SUBBLOCKS (stmt);
19879              subblock != NULL_TREE;
19880              subblock = BLOCK_CHAIN (subblock))
19881           set_block_origin_self (subblock);     /* Recurse.  */
19882       }
19883     }
19884 }
19885
19886 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
19887    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
19888    node to so that it points to the node itself, thus indicating that the
19889    node represents its own (abstract) origin.  Additionally, if the
19890    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
19891    the decl/block tree of which the given node is the root of, and for
19892    each other ..._DECL or BLOCK node contained therein whose
19893    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
19894    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
19895    point to themselves.  */
19896
19897 static void
19898 set_decl_origin_self (tree decl)
19899 {
19900   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
19901     {
19902       DECL_ABSTRACT_ORIGIN (decl) = decl;
19903       if (TREE_CODE (decl) == FUNCTION_DECL)
19904         {
19905           tree arg;
19906
19907           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19908             DECL_ABSTRACT_ORIGIN (arg) = arg;
19909           if (DECL_INITIAL (decl) != NULL_TREE
19910               && DECL_INITIAL (decl) != error_mark_node)
19911             set_block_origin_self (DECL_INITIAL (decl));
19912         }
19913     }
19914 }
19915 \f
19916 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
19917    and if it wasn't 1 before, push it to abstract_vec vector.
19918    For all local decls and all local sub-blocks (recursively) do it
19919    too.  */
19920
19921 static void
19922 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
19923 {
19924   tree local_decl;
19925   tree subblock;
19926   unsigned int i;
19927
19928   if (!BLOCK_ABSTRACT (stmt))
19929     {
19930       abstract_vec.safe_push (stmt);
19931       BLOCK_ABSTRACT (stmt) = 1;
19932     }
19933
19934   for (local_decl = BLOCK_VARS (stmt);
19935        local_decl != NULL_TREE;
19936        local_decl = DECL_CHAIN (local_decl))
19937     if (! DECL_EXTERNAL (local_decl))
19938       set_decl_abstract_flags (local_decl, abstract_vec);
19939
19940   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19941     {
19942       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
19943       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
19944           || TREE_CODE (local_decl) == PARM_DECL)
19945         set_decl_abstract_flags (local_decl, abstract_vec);
19946     }
19947
19948   for (subblock = BLOCK_SUBBLOCKS (stmt);
19949        subblock != NULL_TREE;
19950        subblock = BLOCK_CHAIN (subblock))
19951     set_block_abstract_flags (subblock, abstract_vec);
19952 }
19953
19954 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
19955    to 1 and if it wasn't 1 before, push to abstract_vec vector.
19956    In the case where the decl is a FUNCTION_DECL also set the abstract
19957    flags for all of the parameters, local vars, local
19958    blocks and sub-blocks (recursively).  */
19959
19960 static void
19961 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
19962 {
19963   if (!DECL_ABSTRACT_P (decl))
19964     {
19965       abstract_vec.safe_push (decl);
19966       DECL_ABSTRACT_P (decl) = 1;
19967     }
19968
19969   if (TREE_CODE (decl) == FUNCTION_DECL)
19970     {
19971       tree arg;
19972
19973       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19974         if (!DECL_ABSTRACT_P (arg))
19975           {
19976             abstract_vec.safe_push (arg);
19977             DECL_ABSTRACT_P (arg) = 1;
19978           }
19979       if (DECL_INITIAL (decl) != NULL_TREE
19980           && DECL_INITIAL (decl) != error_mark_node)
19981         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
19982     }
19983 }
19984
19985 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19986    may later generate inlined and/or out-of-line instances of.
19987
19988    FIXME: In the early-dwarf world, this function, and most of the
19989           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
19990           the abstract instance.  All we would need to do is annotate
19991           the early DIE with the appropriate DW_AT_inline in late
19992           dwarf (perhaps in gen_inlined_subroutine_die).
19993
19994           However, we can't do this yet, because LTO streaming of DIEs
19995           has not been implemented yet.  */
19996
19997 static void
19998 dwarf2out_abstract_function (tree decl)
19999 {
20000   dw_die_ref old_die;
20001   tree save_fn;
20002   tree context;
20003   hash_table<decl_loc_hasher> *old_decl_loc_table;
20004   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20005   int old_call_site_count, old_tail_call_site_count;
20006   struct call_arg_loc_node *old_call_arg_locations;
20007
20008   /* Make sure we have the actual abstract inline, not a clone.  */
20009   decl = DECL_ORIGIN (decl);
20010
20011   old_die = lookup_decl_die (decl);
20012   if (old_die && get_AT (old_die, DW_AT_inline))
20013     /* We've already generated the abstract instance.  */
20014     return;
20015
20016   /* We can be called while recursively when seeing block defining inlined subroutine
20017      DIE.  Be sure to not clobber the outer location table nor use it or we would
20018      get locations in abstract instantces.  */
20019   old_decl_loc_table = decl_loc_table;
20020   decl_loc_table = NULL;
20021   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20022   cached_dw_loc_list_table = NULL;
20023   old_call_arg_locations = call_arg_locations;
20024   call_arg_locations = NULL;
20025   old_call_site_count = call_site_count;
20026   call_site_count = -1;
20027   old_tail_call_site_count = tail_call_site_count;
20028   tail_call_site_count = -1;
20029
20030   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20031      we don't get confused by DECL_ABSTRACT_P.  */
20032   if (debug_info_level > DINFO_LEVEL_TERSE)
20033     {
20034       context = decl_class_context (decl);
20035       if (context)
20036         gen_type_die_for_member
20037           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20038     }
20039
20040   /* Pretend we've just finished compiling this function.  */
20041   save_fn = current_function_decl;
20042   current_function_decl = decl;
20043
20044   auto_vec<tree, 64> abstract_vec;
20045   set_decl_abstract_flags (decl, abstract_vec);
20046   dwarf2out_decl (decl);
20047   unsigned int i;
20048   tree t;
20049   FOR_EACH_VEC_ELT (abstract_vec, i, t)
20050     if (TREE_CODE (t) == BLOCK)
20051       BLOCK_ABSTRACT (t) = 0;
20052     else
20053       DECL_ABSTRACT_P (t) = 0;
20054
20055   current_function_decl = save_fn;
20056   decl_loc_table = old_decl_loc_table;
20057   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20058   call_arg_locations = old_call_arg_locations;
20059   call_site_count = old_call_site_count;
20060   tail_call_site_count = old_tail_call_site_count;
20061 }
20062
20063 /* Helper function of premark_used_types() which gets called through
20064    htab_traverse.
20065
20066    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20067    marked as unused by prune_unused_types.  */
20068
20069 bool
20070 premark_used_types_helper (tree const &type, void *)
20071 {
20072   dw_die_ref die;
20073
20074   die = lookup_type_die (type);
20075   if (die != NULL)
20076     die->die_perennial_p = 1;
20077   return true;
20078 }
20079
20080 /* Helper function of premark_types_used_by_global_vars which gets called
20081    through htab_traverse.
20082
20083    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20084    marked as unused by prune_unused_types. The DIE of the type is marked
20085    only if the global variable using the type will actually be emitted.  */
20086
20087 int
20088 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20089                                           void *)
20090 {
20091   struct types_used_by_vars_entry *entry;
20092   dw_die_ref die;
20093
20094   entry = (struct types_used_by_vars_entry *) *slot;
20095   gcc_assert (entry->type != NULL
20096               && entry->var_decl != NULL);
20097   die = lookup_type_die (entry->type);
20098   if (die)
20099     {
20100       /* Ask cgraph if the global variable really is to be emitted.
20101          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
20102       varpool_node *node = varpool_node::get (entry->var_decl);
20103       if (node && node->definition)
20104         {
20105           die->die_perennial_p = 1;
20106           /* Keep the parent DIEs as well.  */
20107           while ((die = die->die_parent) && die->die_perennial_p == 0)
20108             die->die_perennial_p = 1;
20109         }
20110     }
20111   return 1;
20112 }
20113
20114 /* Mark all members of used_types_hash as perennial.  */
20115
20116 static void
20117 premark_used_types (struct function *fun)
20118 {
20119   if (fun && fun->used_types_hash)
20120     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20121 }
20122
20123 /* Mark all members of types_used_by_vars_entry as perennial.  */
20124
20125 static void
20126 premark_types_used_by_global_vars (void)
20127 {
20128   if (types_used_by_vars_hash)
20129     types_used_by_vars_hash
20130       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20131 }
20132
20133 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20134    for CA_LOC call arg loc node.  */
20135
20136 static dw_die_ref
20137 gen_call_site_die (tree decl, dw_die_ref subr_die,
20138                    struct call_arg_loc_node *ca_loc)
20139 {
20140   dw_die_ref stmt_die = NULL, die;
20141   tree block = ca_loc->block;
20142
20143   while (block
20144          && block != DECL_INITIAL (decl)
20145          && TREE_CODE (block) == BLOCK)
20146     {
20147       stmt_die = BLOCK_DIE (block);
20148       if (stmt_die)
20149         break;
20150       block = BLOCK_SUPERCONTEXT (block);
20151     }
20152   if (stmt_die == NULL)
20153     stmt_die = subr_die;
20154   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20155   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20156   if (ca_loc->tail_call_p)
20157     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20158   if (ca_loc->symbol_ref)
20159     {
20160       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20161       if (tdie)
20162         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20163       else
20164         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20165     }
20166   return die;
20167 }
20168
20169 /* Generate a DIE to represent a declared function (either file-scope or
20170    block-local).  */
20171
20172 static void
20173 gen_subprogram_die (tree decl, dw_die_ref context_die)
20174 {
20175   tree origin = decl_ultimate_origin (decl);
20176   dw_die_ref subr_die;
20177   dw_die_ref old_die = lookup_decl_die (decl);
20178
20179   /* This function gets called multiple times for different stages of
20180      the debug process.  For example, for func() in this code:
20181
20182         namespace S
20183         {
20184           void func() { ... }
20185         }
20186
20187      ...we get called 4 times.  Twice in early debug and twice in
20188      late debug:
20189
20190      Early debug
20191      -----------
20192
20193        1. Once while generating func() within the namespace.  This is
20194           the declaration.  The declaration bit below is set, as the
20195           context is the namespace.
20196
20197           A new DIE will be generated with DW_AT_declaration set.
20198
20199        2. Once for func() itself.  This is the specification.  The
20200           declaration bit below is clear as the context is the CU.
20201
20202           We will use the cached DIE from (1) to create a new DIE with
20203           DW_AT_specification pointing to the declaration in (1).
20204
20205      Late debug via rest_of_handle_final()
20206      -------------------------------------
20207
20208        3. Once generating func() within the namespace.  This is also the
20209           declaration, as in (1), but this time we will early exit below
20210           as we have a cached DIE and a declaration needs no additional
20211           annotations (no locations), as the source declaration line
20212           info is enough.
20213
20214        4. Once for func() itself.  As in (2), this is the specification,
20215           but this time we will re-use the cached DIE, and just annotate
20216           it with the location information that should now be available.
20217
20218      For something without namespaces, but with abstract instances, we
20219      are also called a multiple times:
20220
20221         class Base
20222         {
20223         public:
20224           Base ();        // constructor declaration (1)
20225         };
20226
20227         Base::Base () { } // constructor specification (2)
20228
20229     Early debug
20230     -----------
20231
20232        1. Once for the Base() constructor by virtue of it being a
20233           member of the Base class.  This is done via
20234           rest_of_type_compilation.
20235
20236           This is a declaration, so a new DIE will be created with
20237           DW_AT_declaration.
20238
20239        2. Once for the Base() constructor definition, but this time
20240           while generating the abstract instance of the base
20241           constructor (__base_ctor) which is being generated via early
20242           debug of reachable functions.
20243
20244           Even though we have a cached version of the declaration (1),
20245           we will create a DW_AT_specification of the declaration DIE
20246           in (1).
20247
20248        3. Once for the __base_ctor itself, but this time, we generate
20249           an DW_AT_abstract_origin version of the DW_AT_specification in
20250           (2).
20251
20252     Late debug via rest_of_handle_final
20253     -----------------------------------
20254
20255        4. One final time for the __base_ctor (which will have a cached
20256           DIE with DW_AT_abstract_origin created in (3).  This time,
20257           we will just annotate the location information now
20258           available.
20259   */
20260   int declaration = (current_function_decl != decl
20261                      || class_or_namespace_scope_p (context_die));
20262
20263   premark_used_types (DECL_STRUCT_FUNCTION (decl));
20264
20265   /* Now that the C++ front end lazily declares artificial member fns, we
20266      might need to retrofit the declaration into its class.  */
20267   if (!declaration && !origin && !old_die
20268       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20269       && !class_or_namespace_scope_p (context_die)
20270       && debug_info_level > DINFO_LEVEL_TERSE)
20271     old_die = force_decl_die (decl);
20272
20273   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
20274   if (origin != NULL)
20275     {
20276       gcc_assert (!declaration || local_scope_p (context_die));
20277
20278       /* Fixup die_parent for the abstract instance of a nested
20279          inline function.  */
20280       if (old_die && old_die->die_parent == NULL)
20281         add_child_die (context_die, old_die);
20282
20283       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20284         {
20285           /* If we have a DW_AT_abstract_origin we have a working
20286              cached version.  */
20287           subr_die = old_die;
20288         }
20289       else
20290         {
20291           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20292           add_abstract_origin_attribute (subr_die, origin);
20293           /*  This is where the actual code for a cloned function is.
20294               Let's emit linkage name attribute for it.  This helps
20295               debuggers to e.g, set breakpoints into
20296               constructors/destructors when the user asks "break
20297               K::K".  */
20298           add_linkage_name (subr_die, decl);
20299         }
20300     }
20301   /* A cached copy, possibly from early dwarf generation.  Reuse as
20302      much as possible.  */
20303   else if (old_die)
20304     {
20305       /* A declaration that has been previously dumped needs no
20306          additional information.  */
20307       if (declaration)
20308         return;
20309
20310       if (!get_AT_flag (old_die, DW_AT_declaration)
20311           /* We can have a normal definition following an inline one in the
20312              case of redefinition of GNU C extern inlines.
20313              It seems reasonable to use AT_specification in this case.  */
20314           && !get_AT (old_die, DW_AT_inline))
20315         {
20316           /* Detect and ignore this case, where we are trying to output
20317              something we have already output.  */
20318           if (get_AT (old_die, DW_AT_low_pc)
20319               || get_AT (old_die, DW_AT_ranges))
20320             return;
20321
20322           /* If we have no location information, this must be a
20323              partially generated DIE from early dwarf generation.
20324              Fall through and generate it.  */
20325         }
20326
20327       /* If the definition comes from the same place as the declaration,
20328          maybe use the old DIE.  We always want the DIE for this function
20329          that has the *_pc attributes to be under comp_unit_die so the
20330          debugger can find it.  We also need to do this for abstract
20331          instances of inlines, since the spec requires the out-of-line copy
20332          to have the same parent.  For local class methods, this doesn't
20333          apply; we just use the old DIE.  */
20334       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20335       struct dwarf_file_data * file_index = lookup_filename (s.file);
20336       if ((is_cu_die (old_die->die_parent)
20337            /* This condition fixes the inconsistency/ICE with the
20338               following Fortran test (or some derivative thereof) while
20339               building libgfortran:
20340
20341                  module some_m
20342                  contains
20343                     logical function funky (FLAG)
20344                       funky = .true.
20345                    end function
20346                  end module
20347            */
20348            || (old_die->die_parent
20349                && old_die->die_parent->die_tag == DW_TAG_module)
20350            || context_die == NULL)
20351            && (DECL_ARTIFICIAL (decl)
20352                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20353                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
20354                        == (unsigned) s.line))))
20355         {
20356           subr_die = old_die;
20357
20358           /* Clear out the declaration attribute, but leave the
20359              parameters so they can be augmented with location
20360              information later.  Unless this was a declaration, in
20361              which case, wipe out the nameless parameters and recreate
20362              them further down.  */
20363           if (remove_AT (subr_die, DW_AT_declaration))
20364             {
20365
20366               remove_AT (subr_die, DW_AT_object_pointer);
20367               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20368             }
20369         }
20370       /* Make a specification pointing to the previously built
20371          declaration.  */
20372       else
20373         {
20374           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20375           add_AT_specification (subr_die, old_die);
20376           add_pubname (decl, subr_die);
20377           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20378             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20379           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20380             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20381
20382           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20383              emit the real type on the definition die.  */
20384           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20385             {
20386               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20387               if (die == auto_die || die == decltype_auto_die)
20388                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20389                                     TYPE_UNQUALIFIED, false, context_die);
20390             }
20391         }
20392     }
20393   /* Create a fresh DIE for anything else.  */
20394   else
20395     {
20396       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20397
20398       if (TREE_PUBLIC (decl))
20399         add_AT_flag (subr_die, DW_AT_external, 1);
20400
20401       add_name_and_src_coords_attributes (subr_die, decl);
20402       add_pubname (decl, subr_die);
20403       if (debug_info_level > DINFO_LEVEL_TERSE)
20404         {
20405           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20406           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20407                               TYPE_UNQUALIFIED, false, context_die);
20408         }
20409
20410       add_pure_or_virtual_attribute (subr_die, decl);
20411       if (DECL_ARTIFICIAL (decl))
20412         add_AT_flag (subr_die, DW_AT_artificial, 1);
20413
20414       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20415         add_AT_flag (subr_die, DW_AT_noreturn, 1);
20416
20417       add_accessibility_attribute (subr_die, decl);
20418     }
20419
20420   /* Unless we have an existing non-declaration DIE, equate the new
20421      DIE.  */
20422   if (!old_die || is_declaration_die (old_die))
20423     equate_decl_number_to_die (decl, subr_die);
20424
20425   if (declaration)
20426     {
20427       if (!old_die || !get_AT (old_die, DW_AT_inline))
20428         {
20429           add_AT_flag (subr_die, DW_AT_declaration, 1);
20430
20431           /* If this is an explicit function declaration then generate
20432              a DW_AT_explicit attribute.  */
20433           if (lang_hooks.decls.function_decl_explicit_p (decl)
20434               && (dwarf_version >= 3 || !dwarf_strict))
20435             add_AT_flag (subr_die, DW_AT_explicit, 1);
20436
20437           /* If this is a C++11 deleted special function member then generate
20438              a DW_AT_GNU_deleted attribute.  */
20439           if (lang_hooks.decls.function_decl_deleted_p (decl)
20440               && (! dwarf_strict))
20441             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
20442         }
20443     }
20444   /* Tag abstract instances with DW_AT_inline.  */
20445   else if (DECL_ABSTRACT_P (decl))
20446     {
20447       if (DECL_DECLARED_INLINE_P (decl))
20448         {
20449           if (cgraph_function_possibly_inlined_p (decl))
20450             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20451           else
20452             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20453         }
20454       else
20455         {
20456           if (cgraph_function_possibly_inlined_p (decl))
20457             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20458           else
20459             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20460         }
20461
20462       if (DECL_DECLARED_INLINE_P (decl)
20463           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20464         add_AT_flag (subr_die, DW_AT_artificial, 1);
20465     }
20466   /* For non DECL_EXTERNALs, if range information is available, fill
20467      the DIE with it.  */
20468   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20469     {
20470       HOST_WIDE_INT cfa_fb_offset;
20471
20472       struct function *fun = DECL_STRUCT_FUNCTION (decl);
20473
20474       if (!flag_reorder_blocks_and_partition)
20475         {
20476           dw_fde_ref fde = fun->fde;
20477           if (fde->dw_fde_begin)
20478             {
20479               /* We have already generated the labels.  */
20480              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20481                                  fde->dw_fde_end, false);
20482             }
20483           else
20484             {
20485               /* Create start/end labels and add the range.  */
20486               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20487               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20488               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20489                                            current_function_funcdef_no);
20490               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20491                                            current_function_funcdef_no);
20492              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20493                                  false);
20494             }
20495
20496 #if VMS_DEBUGGING_INFO
20497       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20498          Section 2.3 Prologue and Epilogue Attributes:
20499          When a breakpoint is set on entry to a function, it is generally
20500          desirable for execution to be suspended, not on the very first
20501          instruction of the function, but rather at a point after the
20502          function's frame has been set up, after any language defined local
20503          declaration processing has been completed, and before execution of
20504          the first statement of the function begins. Debuggers generally
20505          cannot properly determine where this point is.  Similarly for a
20506          breakpoint set on exit from a function. The prologue and epilogue
20507          attributes allow a compiler to communicate the location(s) to use.  */
20508
20509       {
20510         if (fde->dw_fde_vms_end_prologue)
20511           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20512             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20513
20514         if (fde->dw_fde_vms_begin_epilogue)
20515           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20516             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20517       }
20518 #endif
20519
20520         }
20521       else
20522         {
20523           /* Generate pubnames entries for the split function code ranges.  */
20524           dw_fde_ref fde = fun->fde;
20525
20526           if (fde->dw_fde_second_begin)
20527             {
20528               if (dwarf_version >= 3 || !dwarf_strict)
20529                 {
20530                   /* We should use ranges for non-contiguous code section 
20531                      addresses.  Use the actual code range for the initial
20532                      section, since the HOT/COLD labels might precede an 
20533                      alignment offset.  */
20534                   bool range_list_added = false;
20535                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20536                                         fde->dw_fde_end, &range_list_added,
20537                                         false);
20538                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20539                                         fde->dw_fde_second_end,
20540                                        &range_list_added, false);
20541                   if (range_list_added)
20542                     add_ranges (NULL);
20543                 }
20544               else
20545                 {
20546                   /* There is no real support in DW2 for this .. so we make
20547                      a work-around.  First, emit the pub name for the segment
20548                      containing the function label.  Then make and emit a
20549                      simplified subprogram DIE for the second segment with the
20550                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20551                      linkage name for the second die so that gdb will find both
20552                      sections when given "b foo".  */
20553                   const char *name = NULL;
20554                   tree decl_name = DECL_NAME (decl);
20555                   dw_die_ref seg_die;
20556
20557                   /* Do the 'primary' section.   */
20558                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20559                                       fde->dw_fde_end, false);
20560
20561                   /* Build a minimal DIE for the secondary section.  */
20562                   seg_die = new_die (DW_TAG_subprogram,
20563                                      subr_die->die_parent, decl);
20564
20565                   if (TREE_PUBLIC (decl))
20566                     add_AT_flag (seg_die, DW_AT_external, 1);
20567
20568                   if (decl_name != NULL 
20569                       && IDENTIFIER_POINTER (decl_name) != NULL)
20570                     {
20571                       name = dwarf2_name (decl, 1);
20572                       if (! DECL_ARTIFICIAL (decl))
20573                         add_src_coords_attributes (seg_die, decl);
20574
20575                       add_linkage_name (seg_die, decl);
20576                     }
20577                   gcc_assert (name != NULL);
20578                   add_pure_or_virtual_attribute (seg_die, decl);
20579                   if (DECL_ARTIFICIAL (decl))
20580                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20581
20582                   name = concat ("__second_sect_of_", name, NULL); 
20583                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20584                                       fde->dw_fde_second_end, false);
20585                   add_name_attribute (seg_die, name);
20586                   if (want_pubnames ())
20587                     add_pubname_string (name, seg_die);
20588                 }
20589             }
20590           else
20591            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20592                                false);
20593         }
20594
20595       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20596
20597       /* We define the "frame base" as the function's CFA.  This is more
20598          convenient for several reasons: (1) It's stable across the prologue
20599          and epilogue, which makes it better than just a frame pointer,
20600          (2) With dwarf3, there exists a one-byte encoding that allows us
20601          to reference the .debug_frame data by proxy, but failing that,
20602          (3) We can at least reuse the code inspection and interpretation
20603          code that determines the CFA position at various points in the
20604          function.  */
20605       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20606         {
20607           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20608           add_AT_loc (subr_die, DW_AT_frame_base, op);
20609         }
20610       else
20611         {
20612           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20613           if (list->dw_loc_next)
20614             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20615           else
20616             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20617         }
20618
20619       /* Compute a displacement from the "steady-state frame pointer" to
20620          the CFA.  The former is what all stack slots and argument slots
20621          will reference in the rtl; the latter is what we've told the
20622          debugger about.  We'll need to adjust all frame_base references
20623          by this displacement.  */
20624       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20625
20626       if (fun->static_chain_decl)
20627         {
20628           /* DWARF requires here a location expression that computes the
20629              address of the enclosing subprogram's frame base.  The machinery
20630              in tree-nested.c is supposed to store this specific address in the
20631              last field of the FRAME record.  */
20632           const tree frame_type
20633             = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20634           const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20635
20636           tree fb_expr
20637             = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20638           fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20639                             fb_expr, fb_decl, NULL_TREE);
20640
20641           add_AT_location_description (subr_die, DW_AT_static_link,
20642                                        loc_list_from_tree (fb_expr, 0, NULL));
20643         }
20644     }
20645
20646   /* Generate child dies for template paramaters.  */
20647   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20648     gen_generic_params_dies (decl);
20649
20650   /* Now output descriptions of the arguments for this function. This gets
20651      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20652      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20653      `...' at the end of the formal parameter list.  In order to find out if
20654      there was a trailing ellipsis or not, we must instead look at the type
20655      associated with the FUNCTION_DECL.  This will be a node of type
20656      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20657      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20658      an ellipsis at the end.  */
20659
20660   /* In the case where we are describing a mere function declaration, all we
20661      need to do here (and all we *can* do here) is to describe the *types* of
20662      its formal parameters.  */
20663   if (debug_info_level <= DINFO_LEVEL_TERSE)
20664     ;
20665   else if (declaration)
20666     gen_formal_types_die (decl, subr_die);
20667   else
20668     {
20669       /* Generate DIEs to represent all known formal parameters.  */
20670       tree parm = DECL_ARGUMENTS (decl);
20671       tree generic_decl = early_dwarf
20672         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20673       tree generic_decl_parm = generic_decl
20674                                 ? DECL_ARGUMENTS (generic_decl)
20675                                 : NULL;
20676
20677       /* Now we want to walk the list of parameters of the function and
20678          emit their relevant DIEs.
20679
20680          We consider the case of DECL being an instance of a generic function
20681          as well as it being a normal function.
20682
20683          If DECL is an instance of a generic function we walk the
20684          parameters of the generic function declaration _and_ the parameters of
20685          DECL itself. This is useful because we want to emit specific DIEs for
20686          function parameter packs and those are declared as part of the
20687          generic function declaration. In that particular case,
20688          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20689          That DIE has children DIEs representing the set of arguments
20690          of the pack. Note that the set of pack arguments can be empty.
20691          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20692          children DIE.
20693
20694          Otherwise, we just consider the parameters of DECL.  */
20695       while (generic_decl_parm || parm)
20696         {
20697           if (generic_decl_parm
20698               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20699             gen_formal_parameter_pack_die (generic_decl_parm,
20700                                            parm, subr_die,
20701                                            &parm);
20702           else if (parm && !POINTER_BOUNDS_P (parm))
20703             {
20704               dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20705
20706               if (parm == DECL_ARGUMENTS (decl)
20707                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20708                   && parm_die
20709                   && (dwarf_version >= 3 || !dwarf_strict))
20710                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20711
20712               parm = DECL_CHAIN (parm);
20713             }
20714           else if (parm)
20715             parm = DECL_CHAIN (parm);
20716
20717           if (generic_decl_parm)
20718             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20719         }
20720
20721       /* Decide whether we need an unspecified_parameters DIE at the end.
20722          There are 2 more cases to do this for: 1) the ansi ... declaration -
20723          this is detectable when the end of the arg list is not a
20724          void_type_node 2) an unprototyped function declaration (not a
20725          definition).  This just means that we have no info about the
20726          parameters at all.  */
20727       if (prototype_p (TREE_TYPE (decl)))
20728         {
20729           /* This is the prototyped case, check for....  */
20730           if (stdarg_p (TREE_TYPE (decl)))
20731             gen_unspecified_parameters_die (decl, subr_die);
20732         }
20733       else if (DECL_INITIAL (decl) == NULL_TREE)
20734         gen_unspecified_parameters_die (decl, subr_die);
20735     }
20736
20737   if (subr_die != old_die)
20738     /* Add the calling convention attribute if requested.  */
20739     add_calling_convention_attribute (subr_die, decl);
20740
20741   /* Output Dwarf info for all of the stuff within the body of the function
20742      (if it has one - it may be just a declaration).
20743
20744      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20745      a function.  This BLOCK actually represents the outermost binding contour
20746      for the function, i.e. the contour in which the function's formal
20747      parameters and labels get declared. Curiously, it appears that the front
20748      end doesn't actually put the PARM_DECL nodes for the current function onto
20749      the BLOCK_VARS list for this outer scope, but are strung off of the
20750      DECL_ARGUMENTS list for the function instead.
20751
20752      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20753      the LABEL_DECL nodes for the function however, and we output DWARF info
20754      for those in decls_for_scope.  Just within the `outer_scope' there will be
20755      a BLOCK node representing the function's outermost pair of curly braces,
20756      and any blocks used for the base and member initializers of a C++
20757      constructor function.  */
20758   tree outer_scope = DECL_INITIAL (decl);
20759   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20760     {
20761       int call_site_note_count = 0;
20762       int tail_call_site_note_count = 0;
20763
20764       /* Emit a DW_TAG_variable DIE for a named return value.  */
20765       if (DECL_NAME (DECL_RESULT (decl)))
20766         gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
20767
20768       /* The first time through decls_for_scope we will generate the
20769          DIEs for the locals.  The second time, we fill in the
20770          location info.  */
20771       decls_for_scope (outer_scope, subr_die);
20772
20773       if (call_arg_locations && !dwarf_strict)
20774         {
20775           struct call_arg_loc_node *ca_loc;
20776           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20777             {
20778               dw_die_ref die = NULL;
20779               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20780               rtx arg, next_arg;
20781
20782               for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
20783                           ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
20784                           : NULL_RTX);
20785                    arg; arg = next_arg)
20786                 {
20787                   dw_loc_descr_ref reg, val;
20788                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20789                   dw_die_ref cdie, tdie = NULL;
20790
20791                   next_arg = XEXP (arg, 1);
20792                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20793                       && next_arg
20794                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20795                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20796                       && REGNO (XEXP (XEXP (arg, 0), 0))
20797                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20798                     next_arg = XEXP (next_arg, 1);
20799                   if (mode == VOIDmode)
20800                     {
20801                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20802                       if (mode == VOIDmode)
20803                         mode = GET_MODE (XEXP (arg, 0));
20804                     }
20805                   if (mode == VOIDmode || mode == BLKmode)
20806                     continue;
20807                   /* Get dynamic information about call target only if we
20808                      have no static information: we cannot generate both
20809                      DW_AT_abstract_origin and DW_AT_GNU_call_site_target
20810                      attributes.  */
20811                   if (ca_loc->symbol_ref == NULL_RTX)
20812                     {
20813                       if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20814                         {
20815                           tloc = XEXP (XEXP (arg, 0), 1);
20816                           continue;
20817                         }
20818                       else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20819                                && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20820                         {
20821                           tlocc = XEXP (XEXP (arg, 0), 1);
20822                           continue;
20823                         }
20824                     }
20825                   reg = NULL;
20826                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20827                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20828                                               VAR_INIT_STATUS_INITIALIZED);
20829                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20830                     {
20831                       rtx mem = XEXP (XEXP (arg, 0), 0);
20832                       reg = mem_loc_descriptor (XEXP (mem, 0),
20833                                                 get_address_mode (mem),
20834                                                 GET_MODE (mem),
20835                                                 VAR_INIT_STATUS_INITIALIZED);
20836                     }
20837                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20838                            == DEBUG_PARAMETER_REF)
20839                     {
20840                       tree tdecl
20841                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20842                       tdie = lookup_decl_die (tdecl);
20843                       if (tdie == NULL)
20844                         continue;
20845                     }
20846                   else
20847                     continue;
20848                   if (reg == NULL
20849                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
20850                          != DEBUG_PARAMETER_REF)
20851                     continue;
20852                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20853                                             VOIDmode,
20854                                             VAR_INIT_STATUS_INITIALIZED);
20855                   if (val == NULL)
20856                     continue;
20857                   if (die == NULL)
20858                     die = gen_call_site_die (decl, subr_die, ca_loc);
20859                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20860                                   NULL_TREE);
20861                   if (reg != NULL)
20862                     add_AT_loc (cdie, DW_AT_location, reg);
20863                   else if (tdie != NULL)
20864                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20865                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20866                   if (next_arg != XEXP (arg, 1))
20867                     {
20868                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20869                       if (mode == VOIDmode)
20870                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20871                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20872                                                             0), 1),
20873                                                 mode, VOIDmode,
20874                                                 VAR_INIT_STATUS_INITIALIZED);
20875                       if (val != NULL)
20876                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20877                     }
20878                 }
20879               if (die == NULL
20880                   && (ca_loc->symbol_ref || tloc))
20881                 die = gen_call_site_die (decl, subr_die, ca_loc);
20882               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20883                 {
20884                   dw_loc_descr_ref tval = NULL;
20885
20886                   if (tloc != NULL_RTX)
20887                     tval = mem_loc_descriptor (tloc,
20888                                                GET_MODE (tloc) == VOIDmode
20889                                                ? Pmode : GET_MODE (tloc),
20890                                                VOIDmode,
20891                                                VAR_INIT_STATUS_INITIALIZED);
20892                   if (tval)
20893                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20894                   else if (tlocc != NULL_RTX)
20895                     {
20896                       tval = mem_loc_descriptor (tlocc,
20897                                                  GET_MODE (tlocc) == VOIDmode
20898                                                  ? Pmode : GET_MODE (tlocc),
20899                                                  VOIDmode,
20900                                                  VAR_INIT_STATUS_INITIALIZED);
20901                       if (tval)
20902                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20903                                     tval);
20904                     }
20905                 }
20906               if (die != NULL)
20907                 {
20908                   call_site_note_count++;
20909                   if (ca_loc->tail_call_p)
20910                     tail_call_site_note_count++;
20911                 }
20912             }
20913         }
20914       call_arg_locations = NULL;
20915       call_arg_loc_last = NULL;
20916       if (tail_call_site_count >= 0
20917           && tail_call_site_count == tail_call_site_note_count
20918           && !dwarf_strict)
20919         {
20920           if (call_site_count >= 0
20921               && call_site_count == call_site_note_count)
20922             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20923           else
20924             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20925         }
20926       call_site_count = -1;
20927       tail_call_site_count = -1;
20928     }
20929 }
20930
20931 /* Returns a hash value for X (which really is a die_struct).  */
20932
20933 hashval_t
20934 block_die_hasher::hash (die_struct *d)
20935 {
20936   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20937 }
20938
20939 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20940    as decl_id and die_parent of die_struct Y.  */
20941
20942 bool
20943 block_die_hasher::equal (die_struct *x, die_struct *y)
20944 {
20945   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
20946 }
20947
20948 /* Return TRUE if DECL, which may have been previously generated as
20949    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
20950    true if decl (or its origin) is either an extern declaration or a
20951    class/namespace scoped declaration.
20952
20953    The declare_in_namespace support causes us to get two DIEs for one
20954    variable, both of which are declarations.  We want to avoid
20955    considering one to be a specification, so we must test for
20956    DECLARATION and DW_AT_declaration.  */
20957 static inline bool
20958 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
20959 {
20960   return (old_die && TREE_STATIC (decl) && !declaration
20961           && get_AT_flag (old_die, DW_AT_declaration) == 1);
20962 }
20963
20964 /* Return true if DECL is a local static.  */
20965
20966 static inline bool
20967 local_function_static (tree decl)
20968 {
20969   gcc_assert (TREE_CODE (decl) == VAR_DECL);
20970   return TREE_STATIC (decl)
20971     && DECL_CONTEXT (decl)
20972     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
20973 }
20974
20975 /* Generate a DIE to represent a declared data object.
20976    Either DECL or ORIGIN must be non-null.  */
20977
20978 static void
20979 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20980 {
20981   HOST_WIDE_INT off = 0;
20982   tree com_decl;
20983   tree decl_or_origin = decl ? decl : origin;
20984   tree ultimate_origin;
20985   dw_die_ref var_die;
20986   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20987   dw_die_ref origin_die = NULL;
20988   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20989                       || class_or_namespace_scope_p (context_die));
20990   bool specialization_p = false;
20991
20992   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20993   if (decl || ultimate_origin)
20994     origin = ultimate_origin;
20995   com_decl = fortran_common (decl_or_origin, &off);
20996
20997   /* Symbol in common gets emitted as a child of the common block, in the form
20998      of a data member.  */
20999   if (com_decl)
21000     {
21001       dw_die_ref com_die;
21002       dw_loc_list_ref loc;
21003       die_node com_die_arg;
21004
21005       var_die = lookup_decl_die (decl_or_origin);
21006       if (var_die)
21007         {
21008           if (get_AT (var_die, DW_AT_location) == NULL)
21009             {
21010               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21011               if (loc)
21012                 {
21013                   if (off)
21014                     {
21015                       /* Optimize the common case.  */
21016                       if (single_element_loc_list_p (loc)
21017                           && loc->expr->dw_loc_opc == DW_OP_addr
21018                           && loc->expr->dw_loc_next == NULL
21019                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21020                              == SYMBOL_REF)
21021                         {
21022                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21023                           loc->expr->dw_loc_oprnd1.v.val_addr
21024                             = plus_constant (GET_MODE (x), x , off);
21025                         }
21026                       else
21027                         loc_list_plus_const (loc, off);
21028                     }
21029                   add_AT_location_description (var_die, DW_AT_location, loc);
21030                   remove_AT (var_die, DW_AT_declaration);
21031                 }
21032             }
21033           return;
21034         }
21035
21036       if (common_block_die_table == NULL)
21037         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21038
21039       com_die_arg.decl_id = DECL_UID (com_decl);
21040       com_die_arg.die_parent = context_die;
21041       com_die = common_block_die_table->find (&com_die_arg);
21042       loc = loc_list_from_tree (com_decl, 2, NULL);
21043       if (com_die == NULL)
21044         {
21045           const char *cnam
21046             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21047           die_node **slot;
21048
21049           com_die = new_die (DW_TAG_common_block, context_die, decl);
21050           add_name_and_src_coords_attributes (com_die, com_decl);
21051           if (loc)
21052             {
21053               add_AT_location_description (com_die, DW_AT_location, loc);
21054               /* Avoid sharing the same loc descriptor between
21055                  DW_TAG_common_block and DW_TAG_variable.  */
21056               loc = loc_list_from_tree (com_decl, 2, NULL);
21057             }
21058           else if (DECL_EXTERNAL (decl))
21059             add_AT_flag (com_die, DW_AT_declaration, 1);
21060           if (want_pubnames ())
21061             add_pubname_string (cnam, com_die); /* ??? needed? */
21062           com_die->decl_id = DECL_UID (com_decl);
21063           slot = common_block_die_table->find_slot (com_die, INSERT);
21064           *slot = com_die;
21065         }
21066       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21067         {
21068           add_AT_location_description (com_die, DW_AT_location, loc);
21069           loc = loc_list_from_tree (com_decl, 2, NULL);
21070           remove_AT (com_die, DW_AT_declaration);
21071         }
21072       var_die = new_die (DW_TAG_variable, com_die, decl);
21073       add_name_and_src_coords_attributes (var_die, decl);
21074       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl), false,
21075                           context_die);
21076       add_AT_flag (var_die, DW_AT_external, 1);
21077       if (loc)
21078         {
21079           if (off)
21080             {
21081               /* Optimize the common case.  */
21082               if (single_element_loc_list_p (loc)
21083                   && loc->expr->dw_loc_opc == DW_OP_addr
21084                   && loc->expr->dw_loc_next == NULL
21085                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21086                 {
21087                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21088                   loc->expr->dw_loc_oprnd1.v.val_addr
21089                     = plus_constant (GET_MODE (x), x, off);
21090                 }
21091               else
21092                 loc_list_plus_const (loc, off);
21093             }
21094           add_AT_location_description (var_die, DW_AT_location, loc);
21095         }
21096       else if (DECL_EXTERNAL (decl))
21097         add_AT_flag (var_die, DW_AT_declaration, 1);
21098       equate_decl_number_to_die (decl, var_die);
21099       return;
21100     }
21101
21102   if (old_die)
21103     {
21104       if (declaration)
21105         {
21106           /* A declaration that has been previously dumped, needs no
21107              further annotations, since it doesn't need location on
21108              the second pass.  */
21109           return;
21110         }
21111       else if (decl_will_get_specification_p (old_die, decl, declaration)
21112                && !get_AT (old_die, DW_AT_specification))
21113         {
21114           /* Fall-thru so we can make a new variable die along with a
21115              DW_AT_specification.  */
21116         }
21117       else if (origin && old_die->die_parent != context_die)
21118         {
21119           /* If we will be creating an inlined instance, we need a
21120              new DIE that will get annotated with
21121              DW_AT_abstract_origin.  Clear things so we can get a
21122              new DIE.  */
21123           gcc_assert (!DECL_ABSTRACT_P (decl));
21124           old_die = NULL;
21125         }
21126       else
21127         {
21128           /* If a DIE was dumped early, it still needs location info.
21129              Skip to where we fill the location bits.  */
21130           var_die = old_die;
21131           goto gen_variable_die_location;
21132         }
21133     }
21134
21135   /* For static data members, the declaration in the class is supposed
21136      to have DW_TAG_member tag; the specification should still be
21137      DW_TAG_variable referencing the DW_TAG_member DIE.  */
21138   if (declaration && class_scope_p (context_die))
21139     var_die = new_die (DW_TAG_member, context_die, decl);
21140   else
21141     var_die = new_die (DW_TAG_variable, context_die, decl);
21142
21143   if (origin != NULL)
21144     origin_die = add_abstract_origin_attribute (var_die, origin);
21145
21146   /* Loop unrolling can create multiple blocks that refer to the same
21147      static variable, so we must test for the DW_AT_declaration flag.
21148
21149      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21150      copy decls and set the DECL_ABSTRACT_P flag on them instead of
21151      sharing them.
21152
21153      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
21154   else if (decl_will_get_specification_p (old_die, decl, declaration))
21155     {
21156       /* This is a definition of a C++ class level static.  */
21157       add_AT_specification (var_die, old_die);
21158       specialization_p = true;
21159       if (DECL_NAME (decl))
21160         {
21161           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21162           struct dwarf_file_data * file_index = lookup_filename (s.file);
21163
21164           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21165             add_AT_file (var_die, DW_AT_decl_file, file_index);
21166
21167           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21168             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21169
21170           if (old_die->die_tag == DW_TAG_member)
21171             add_linkage_name (var_die, decl);
21172         }
21173     }
21174   else
21175     add_name_and_src_coords_attributes (var_die, decl);
21176
21177   if ((origin == NULL && !specialization_p)
21178       || (origin != NULL
21179           && !DECL_ABSTRACT_P (decl_or_origin)
21180           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21181                                        decl_function_context
21182                                                         (decl_or_origin))))
21183     {
21184       tree type = TREE_TYPE (decl_or_origin);
21185
21186       if (decl_by_reference_p (decl_or_origin))
21187         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21188                             context_die);
21189       else
21190         add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21191                             context_die);
21192     }
21193
21194   if (origin == NULL && !specialization_p)
21195     {
21196       if (TREE_PUBLIC (decl))
21197         add_AT_flag (var_die, DW_AT_external, 1);
21198
21199       if (DECL_ARTIFICIAL (decl))
21200         add_AT_flag (var_die, DW_AT_artificial, 1);
21201
21202       add_accessibility_attribute (var_die, decl);
21203     }
21204
21205   if (declaration)
21206     add_AT_flag (var_die, DW_AT_declaration, 1);
21207
21208   if (decl && (DECL_ABSTRACT_P (decl)
21209                || !old_die || is_declaration_die (old_die)))
21210     equate_decl_number_to_die (decl, var_die);
21211
21212  gen_variable_die_location:
21213   if (! declaration
21214       && (! DECL_ABSTRACT_P (decl_or_origin)
21215           /* Local static vars are shared between all clones/inlines,
21216              so emit DW_AT_location on the abstract DIE if DECL_RTL is
21217              already set.  */
21218           || (TREE_CODE (decl_or_origin) == VAR_DECL
21219               && TREE_STATIC (decl_or_origin)
21220               && DECL_RTL_SET_P (decl_or_origin)))
21221       /* When abstract origin already has DW_AT_location attribute, no need
21222          to add it again.  */
21223       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21224     {
21225       if (early_dwarf)
21226         add_pubname (decl_or_origin, var_die);
21227       else
21228         add_location_or_const_value_attribute (var_die, decl_or_origin,
21229                                                decl == NULL);
21230     }
21231   else
21232     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21233 }
21234
21235 /* Generate a DIE to represent a named constant.  */
21236
21237 static void
21238 gen_const_die (tree decl, dw_die_ref context_die)
21239 {
21240   dw_die_ref const_die;
21241   tree type = TREE_TYPE (decl);
21242
21243   const_die = lookup_decl_die (decl);
21244   if (const_die)
21245     return;
21246
21247   const_die = new_die (DW_TAG_constant, context_die, decl);
21248   equate_decl_number_to_die (decl, const_die);
21249   add_name_and_src_coords_attributes (const_die, decl);
21250   add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21251   if (TREE_PUBLIC (decl))
21252     add_AT_flag (const_die, DW_AT_external, 1);
21253   if (DECL_ARTIFICIAL (decl))
21254     add_AT_flag (const_die, DW_AT_artificial, 1);
21255   tree_add_const_value_attribute_for_decl (const_die, decl);
21256 }
21257
21258 /* Generate a DIE to represent a label identifier.  */
21259
21260 static void
21261 gen_label_die (tree decl, dw_die_ref context_die)
21262 {
21263   tree origin = decl_ultimate_origin (decl);
21264   dw_die_ref lbl_die = lookup_decl_die (decl);
21265   rtx insn;
21266   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21267
21268   if (!lbl_die)
21269     {
21270       lbl_die = new_die (DW_TAG_label, context_die, decl);
21271       equate_decl_number_to_die (decl, lbl_die);
21272
21273       if (origin != NULL)
21274         add_abstract_origin_attribute (lbl_die, origin);
21275       else
21276         add_name_and_src_coords_attributes (lbl_die, decl);
21277     }
21278
21279   if (DECL_ABSTRACT_P (decl))
21280     equate_decl_number_to_die (decl, lbl_die);
21281   else
21282     {
21283       insn = DECL_RTL_IF_SET (decl);
21284
21285       /* Deleted labels are programmer specified labels which have been
21286          eliminated because of various optimizations.  We still emit them
21287          here so that it is possible to put breakpoints on them.  */
21288       if (insn
21289           && (LABEL_P (insn)
21290               || ((NOTE_P (insn)
21291                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21292         {
21293           /* When optimization is enabled (via -O) some parts of the compiler
21294              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21295              represent source-level labels which were explicitly declared by
21296              the user.  This really shouldn't be happening though, so catch
21297              it if it ever does happen.  */
21298           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21299
21300           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21301           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21302         }
21303       else if (insn
21304                && NOTE_P (insn)
21305                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21306                && CODE_LABEL_NUMBER (insn) != -1)
21307         {
21308           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21309           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21310         }
21311     }
21312 }
21313
21314 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
21315    attributes to the DIE for a block STMT, to describe where the inlined
21316    function was called from.  This is similar to add_src_coords_attributes.  */
21317
21318 static inline void
21319 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21320 {
21321   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21322
21323   if (dwarf_version >= 3 || !dwarf_strict)
21324     {
21325       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21326       add_AT_unsigned (die, DW_AT_call_line, s.line);
21327     }
21328 }
21329
21330
21331 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21332    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
21333
21334 static inline void
21335 add_high_low_attributes (tree stmt, dw_die_ref die)
21336 {
21337   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21338
21339   if (BLOCK_FRAGMENT_CHAIN (stmt)
21340       && (dwarf_version >= 3 || !dwarf_strict))
21341     {
21342       tree chain, superblock = NULL_TREE;
21343       dw_die_ref pdie;
21344       dw_attr_node *attr = NULL;
21345
21346       if (inlined_function_outer_scope_p (stmt))
21347         {
21348           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21349                                        BLOCK_NUMBER (stmt));
21350           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21351         }
21352
21353       /* Optimize duplicate .debug_ranges lists or even tails of
21354          lists.  If this BLOCK has same ranges as its supercontext,
21355          lookup DW_AT_ranges attribute in the supercontext (and
21356          recursively so), verify that the ranges_table contains the
21357          right values and use it instead of adding a new .debug_range.  */
21358       for (chain = stmt, pdie = die;
21359            BLOCK_SAME_RANGE (chain);
21360            chain = BLOCK_SUPERCONTEXT (chain))
21361         {
21362           dw_attr_node *new_attr;
21363
21364           pdie = pdie->die_parent;
21365           if (pdie == NULL)
21366             break;
21367           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21368             break;
21369           new_attr = get_AT (pdie, DW_AT_ranges);
21370           if (new_attr == NULL
21371               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21372             break;
21373           attr = new_attr;
21374           superblock = BLOCK_SUPERCONTEXT (chain);
21375         }
21376       if (attr != NULL
21377           && (ranges_table[attr->dw_attr_val.v.val_offset
21378                            / 2 / DWARF2_ADDR_SIZE].num
21379               == BLOCK_NUMBER (superblock))
21380           && BLOCK_FRAGMENT_CHAIN (superblock))
21381         {
21382           unsigned long off = attr->dw_attr_val.v.val_offset
21383                               / 2 / DWARF2_ADDR_SIZE;
21384           unsigned long supercnt = 0, thiscnt = 0;
21385           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21386                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21387             {
21388               ++supercnt;
21389               gcc_checking_assert (ranges_table[off + supercnt].num
21390                                    == BLOCK_NUMBER (chain));
21391             }
21392           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21393           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21394                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21395             ++thiscnt;
21396           gcc_assert (supercnt >= thiscnt);
21397           add_AT_range_list (die, DW_AT_ranges,
21398                              ((off + supercnt - thiscnt)
21399                               * 2 * DWARF2_ADDR_SIZE),
21400                              false);
21401           return;
21402         }
21403
21404       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21405
21406       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21407       do
21408         {
21409           add_ranges (chain);
21410           chain = BLOCK_FRAGMENT_CHAIN (chain);
21411         }
21412       while (chain);
21413       add_ranges (NULL);
21414     }
21415   else
21416     {
21417       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21418       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21419                                    BLOCK_NUMBER (stmt));
21420       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21421                                    BLOCK_NUMBER (stmt));
21422       add_AT_low_high_pc (die, label, label_high, false);
21423     }
21424 }
21425
21426 /* Generate a DIE for a lexical block.  */
21427
21428 static void
21429 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21430 {
21431   dw_die_ref old_die = BLOCK_DIE (stmt);
21432   dw_die_ref stmt_die = NULL;
21433   if (!old_die)
21434     {
21435       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21436       BLOCK_DIE (stmt) = stmt_die;
21437     }
21438
21439   if (BLOCK_ABSTRACT (stmt))
21440     {
21441       if (old_die)
21442         {
21443           /* This must have been generated early and it won't even
21444              need location information since it's a DW_AT_inline
21445              function.  */
21446           if (flag_checking)
21447             for (dw_die_ref c = context_die; c; c = c->die_parent)
21448               if (c->die_tag == DW_TAG_inlined_subroutine
21449                   || c->die_tag == DW_TAG_subprogram)
21450                 {
21451                   gcc_assert (get_AT (c, DW_AT_inline));
21452                   break;
21453                 }
21454           return;
21455         }
21456     }
21457   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21458     {
21459       /* If this is an inlined instance, create a new lexical die for
21460          anything below to attach DW_AT_abstract_origin to.  */
21461       if (old_die)
21462         {
21463           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21464           BLOCK_DIE (stmt) = stmt_die;
21465           old_die = NULL;
21466         }
21467     }
21468
21469   if (old_die)
21470     stmt_die = old_die;
21471
21472   /* A non abstract block whose blocks have already been reordered
21473      should have the instruction range for this block.  If so, set the
21474      high/low attributes.  */
21475   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21476     {
21477       gcc_assert (stmt_die);
21478       add_high_low_attributes (stmt, stmt_die);
21479     }
21480
21481   decls_for_scope (stmt, stmt_die);
21482 }
21483
21484 /* Generate a DIE for an inlined subprogram.  */
21485
21486 static void
21487 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21488 {
21489   tree decl;
21490
21491   /* The instance of function that is effectively being inlined shall not
21492      be abstract.  */
21493   gcc_assert (! BLOCK_ABSTRACT (stmt));
21494
21495   decl = block_ultimate_origin (stmt);
21496
21497   /* Make sure any inlined functions are known to be inlineable.  */
21498   gcc_checking_assert (DECL_ABSTRACT_P (decl)
21499                        || cgraph_function_possibly_inlined_p (decl));
21500
21501   /* Emit info for the abstract instance first, if we haven't yet.  We
21502      must emit this even if the block is abstract, otherwise when we
21503      emit the block below (or elsewhere), we may end up trying to emit
21504      a die whose origin die hasn't been emitted, and crashing.  */
21505   dwarf2out_abstract_function (decl);
21506
21507   if (! BLOCK_ABSTRACT (stmt))
21508     {
21509       dw_die_ref subr_die
21510         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21511
21512       if (call_arg_locations)
21513         BLOCK_DIE (stmt) = subr_die;
21514       add_abstract_origin_attribute (subr_die, decl);
21515       if (TREE_ASM_WRITTEN (stmt))
21516         add_high_low_attributes (stmt, subr_die);
21517       add_call_src_coords_attributes (stmt, subr_die);
21518
21519       decls_for_scope (stmt, subr_die);
21520     }
21521 }
21522
21523 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
21524    the comment for VLR_CONTEXT.  */
21525
21526 static void
21527 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21528 {
21529   dw_die_ref decl_die;
21530
21531   if (TREE_TYPE (decl) == error_mark_node)
21532     return;
21533
21534   decl_die = new_die (DW_TAG_member, context_die, decl);
21535   add_name_and_src_coords_attributes (decl_die, decl);
21536   add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21537                       TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21538                       context_die);
21539
21540   if (DECL_BIT_FIELD_TYPE (decl))
21541     {
21542       add_byte_size_attribute (decl_die, decl);
21543       add_bit_size_attribute (decl_die, decl);
21544       add_bit_offset_attribute (decl_die, decl, ctx);
21545     }
21546
21547   /* If we have a variant part offset, then we are supposed to process a member
21548      of a QUAL_UNION_TYPE, which is how we represent variant parts in
21549      trees.  */
21550   gcc_assert (ctx->variant_part_offset == NULL_TREE
21551               || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21552   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21553     add_data_member_location_attribute (decl_die, decl, ctx);
21554
21555   if (DECL_ARTIFICIAL (decl))
21556     add_AT_flag (decl_die, DW_AT_artificial, 1);
21557
21558   add_accessibility_attribute (decl_die, decl);
21559
21560   /* Equate decl number to die, so that we can look up this decl later on.  */
21561   equate_decl_number_to_die (decl, decl_die);
21562 }
21563
21564 #if 0
21565 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21566    Use modified_type_die instead.
21567    We keep this code here just in case these types of DIEs may be needed to
21568    represent certain things in other languages (e.g. Pascal) someday.  */
21569
21570 static void
21571 gen_pointer_type_die (tree type, dw_die_ref context_die)
21572 {
21573   dw_die_ref ptr_die
21574     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21575
21576   equate_type_number_to_die (type, ptr_die);
21577   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21578                       context_die);
21579   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21580 }
21581
21582 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21583    Use modified_type_die instead.
21584    We keep this code here just in case these types of DIEs may be needed to
21585    represent certain things in other languages (e.g. Pascal) someday.  */
21586
21587 static void
21588 gen_reference_type_die (tree type, dw_die_ref context_die)
21589 {
21590   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21591
21592   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21593     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21594   else
21595     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21596
21597   equate_type_number_to_die (type, ref_die);
21598   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21599                       context_die);
21600   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21601 }
21602 #endif
21603
21604 /* Generate a DIE for a pointer to a member type.  */
21605
21606 static void
21607 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21608 {
21609   dw_die_ref ptr_die
21610     = new_die (DW_TAG_ptr_to_member_type,
21611                scope_die_for (type, context_die), type);
21612
21613   equate_type_number_to_die (type, ptr_die);
21614   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21615                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21616   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21617                       context_die);
21618 }
21619
21620 static char *producer_string;
21621
21622 /* Return a heap allocated producer string including command line options
21623    if -grecord-gcc-switches.  */
21624
21625 static char *
21626 gen_producer_string (void)
21627 {
21628   size_t j;
21629   auto_vec<const char *> switches;
21630   const char *language_string = lang_hooks.name;
21631   char *producer, *tail;
21632   const char *p;
21633   size_t len = dwarf_record_gcc_switches ? 0 : 3;
21634   size_t plen = strlen (language_string) + 1 + strlen (version_string);
21635
21636   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21637     switch (save_decoded_options[j].opt_index)
21638       {
21639       case OPT_o:
21640       case OPT_d:
21641       case OPT_dumpbase:
21642       case OPT_dumpdir:
21643       case OPT_auxbase:
21644       case OPT_auxbase_strip:
21645       case OPT_quiet:
21646       case OPT_version:
21647       case OPT_v:
21648       case OPT_w:
21649       case OPT_L:
21650       case OPT_D:
21651       case OPT_I:
21652       case OPT_U:
21653       case OPT_SPECIAL_unknown:
21654       case OPT_SPECIAL_ignore:
21655       case OPT_SPECIAL_program_name:
21656       case OPT_SPECIAL_input_file:
21657       case OPT_grecord_gcc_switches:
21658       case OPT_gno_record_gcc_switches:
21659       case OPT__output_pch_:
21660       case OPT_fdiagnostics_show_location_:
21661       case OPT_fdiagnostics_show_option:
21662       case OPT_fdiagnostics_show_caret:
21663       case OPT_fdiagnostics_color_:
21664       case OPT_fverbose_asm:
21665       case OPT____:
21666       case OPT__sysroot_:
21667       case OPT_nostdinc:
21668       case OPT_nostdinc__:
21669       case OPT_fpreprocessed:
21670       case OPT_fltrans_output_list_:
21671       case OPT_fresolution_:
21672       case OPT_fdebug_prefix_map_:
21673         /* Ignore these.  */
21674         continue;
21675       default:
21676         if (cl_options[save_decoded_options[j].opt_index].flags
21677             & CL_NO_DWARF_RECORD)
21678           continue;
21679         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21680                              == '-');
21681         switch (save_decoded_options[j].canonical_option[0][1])
21682           {
21683           case 'M':
21684           case 'i':
21685           case 'W':
21686             continue;
21687           case 'f':
21688             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21689                          "dump", 4) == 0)
21690               continue;
21691             break;
21692           default:
21693             break;
21694           }
21695         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21696         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21697         break;
21698       }
21699
21700   producer = XNEWVEC (char, plen + 1 + len + 1);
21701   tail = producer;
21702   sprintf (tail, "%s %s", language_string, version_string);
21703   tail += plen;
21704
21705   FOR_EACH_VEC_ELT (switches, j, p)
21706     {
21707       len = strlen (p);
21708       *tail = ' ';
21709       memcpy (tail + 1, p, len);
21710       tail += len + 1;
21711     }
21712
21713   *tail = '\0';
21714   return producer;
21715 }
21716
21717 /* Given a C and/or C++ language/version string return the "highest".
21718    C++ is assumed to be "higher" than C in this case.  Used for merging
21719    LTO translation unit languages.  */
21720 static const char *
21721 highest_c_language (const char *lang1, const char *lang2)
21722 {
21723   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21724     return "GNU C++14";
21725   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21726     return "GNU C++11";
21727   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21728     return "GNU C++98";
21729
21730   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21731     return "GNU C11";
21732   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21733     return "GNU C99";
21734   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21735     return "GNU C89";
21736
21737   gcc_unreachable ();
21738 }
21739
21740
21741 /* Generate the DIE for the compilation unit.  */
21742
21743 static dw_die_ref
21744 gen_compile_unit_die (const char *filename)
21745 {
21746   dw_die_ref die;
21747   const char *language_string = lang_hooks.name;
21748   int language;
21749
21750   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21751
21752   if (filename)
21753     {
21754       add_name_attribute (die, filename);
21755       /* Don't add cwd for <built-in>.  */
21756       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21757         add_comp_dir_attribute (die);
21758     }
21759
21760   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
21761
21762   /* If our producer is LTO try to figure out a common language to use
21763      from the global list of translation units.  */
21764   if (strcmp (language_string, "GNU GIMPLE") == 0)
21765     {
21766       unsigned i;
21767       tree t;
21768       const char *common_lang = NULL;
21769
21770       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
21771         {
21772           if (!TRANSLATION_UNIT_LANGUAGE (t))
21773             continue;
21774           if (!common_lang)
21775             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21776           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21777             ;
21778           else if (strncmp (common_lang, "GNU C", 5) == 0
21779                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21780             /* Mixing C and C++ is ok, use C++ in that case.  */
21781             common_lang = highest_c_language (common_lang,
21782                                               TRANSLATION_UNIT_LANGUAGE (t));
21783           else
21784             {
21785               /* Fall back to C.  */
21786               common_lang = NULL;
21787               break;
21788             }
21789         }
21790
21791       if (common_lang)
21792         language_string = common_lang;
21793     }
21794
21795   language = DW_LANG_C;
21796   if (strncmp (language_string, "GNU C", 5) == 0
21797       && ISDIGIT (language_string[5]))
21798     {
21799       language = DW_LANG_C89;
21800       if (dwarf_version >= 3 || !dwarf_strict)
21801         {
21802           if (strcmp (language_string, "GNU C89") != 0)
21803             language = DW_LANG_C99;
21804
21805           if (dwarf_version >= 5 /* || !dwarf_strict */)
21806             if (strcmp (language_string, "GNU C11") == 0)
21807               language = DW_LANG_C11;
21808         }
21809     }
21810   else if (strncmp (language_string, "GNU C++", 7) == 0)
21811     {
21812       language = DW_LANG_C_plus_plus;
21813       if (dwarf_version >= 5 /* || !dwarf_strict */)
21814         {
21815           if (strcmp (language_string, "GNU C++11") == 0)
21816             language = DW_LANG_C_plus_plus_11;
21817           else if (strcmp (language_string, "GNU C++14") == 0)
21818             language = DW_LANG_C_plus_plus_14;
21819         }
21820     }
21821   else if (strcmp (language_string, "GNU F77") == 0)
21822     language = DW_LANG_Fortran77;
21823   else if (strcmp (language_string, "GNU Pascal") == 0)
21824     language = DW_LANG_Pascal83;
21825   else if (dwarf_version >= 3 || !dwarf_strict)
21826     {
21827       if (strcmp (language_string, "GNU Ada") == 0)
21828         language = DW_LANG_Ada95;
21829       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21830         {
21831           language = DW_LANG_Fortran95;
21832           if (dwarf_version >= 5 /* || !dwarf_strict */)
21833             {
21834               if (strcmp (language_string, "GNU Fortran2003") == 0)
21835                 language = DW_LANG_Fortran03;
21836               else if (strcmp (language_string, "GNU Fortran2008") == 0)
21837                 language = DW_LANG_Fortran08;
21838             }
21839         }
21840       else if (strcmp (language_string, "GNU Java") == 0)
21841         language = DW_LANG_Java;
21842       else if (strcmp (language_string, "GNU Objective-C") == 0)
21843         language = DW_LANG_ObjC;
21844       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21845         language = DW_LANG_ObjC_plus_plus;
21846       else if (dwarf_version >= 5 || !dwarf_strict)
21847         {
21848           if (strcmp (language_string, "GNU Go") == 0)
21849             language = DW_LANG_Go;
21850         }
21851     }
21852   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
21853   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21854     language = DW_LANG_Fortran90;
21855
21856   add_AT_unsigned (die, DW_AT_language, language);
21857
21858   switch (language)
21859     {
21860     case DW_LANG_Fortran77:
21861     case DW_LANG_Fortran90:
21862     case DW_LANG_Fortran95:
21863     case DW_LANG_Fortran03:
21864     case DW_LANG_Fortran08:
21865       /* Fortran has case insensitive identifiers and the front-end
21866          lowercases everything.  */
21867       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21868       break;
21869     default:
21870       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21871       break;
21872     }
21873   return die;
21874 }
21875
21876 /* Generate the DIE for a base class.  */
21877
21878 static void
21879 gen_inheritance_die (tree binfo, tree access, tree type,
21880                      dw_die_ref context_die)
21881 {
21882   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21883   struct vlr_context ctx = { type, NULL };
21884
21885   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
21886                       context_die);
21887   add_data_member_location_attribute (die, binfo, &ctx);
21888
21889   if (BINFO_VIRTUAL_P (binfo))
21890     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21891
21892   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21893      children, otherwise the default is DW_ACCESS_public.  In DWARF2
21894      the default has always been DW_ACCESS_private.  */
21895   if (access == access_public_node)
21896     {
21897       if (dwarf_version == 2
21898           || context_die->die_tag == DW_TAG_class_type)
21899       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21900     }
21901   else if (access == access_protected_node)
21902     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21903   else if (dwarf_version > 2
21904            && context_die->die_tag != DW_TAG_class_type)
21905     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21906 }
21907
21908 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
21909    structure.  */
21910 static bool
21911 is_variant_part (tree decl)
21912 {
21913   return (TREE_CODE (decl) == FIELD_DECL
21914           && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
21915 }
21916
21917 /* Check that OPERAND is a reference to a field in STRUCT_TYPE.  If it is,
21918    return the FIELD_DECL.  Return NULL_TREE otherwise.  */
21919
21920 static tree
21921 analyze_discr_in_predicate (tree operand, tree struct_type)
21922 {
21923   bool continue_stripping = true;
21924   while (continue_stripping)
21925     switch (TREE_CODE (operand))
21926       {
21927       CASE_CONVERT:
21928         operand = TREE_OPERAND (operand, 0);
21929         break;
21930       default:
21931         continue_stripping = false;
21932         break;
21933       }
21934
21935   /* Match field access to members of struct_type only.  */
21936   if (TREE_CODE (operand) == COMPONENT_REF
21937       && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
21938       && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
21939       && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
21940     return TREE_OPERAND (operand, 1);
21941   else
21942     return NULL_TREE;
21943 }
21944
21945 /* Check that SRC is a constant integer that can be represented as a native
21946    integer constant (either signed or unsigned).  If so, store it into DEST and
21947    return true.  Return false otherwise. */
21948
21949 static bool
21950 get_discr_value (tree src, dw_discr_value *dest)
21951 {
21952   bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
21953
21954   if (TREE_CODE (src) != INTEGER_CST
21955       || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
21956     return false;
21957
21958   dest->pos = is_unsigned;
21959   if (is_unsigned)
21960     dest->v.uval = tree_to_uhwi (src);
21961   else
21962     dest->v.sval = tree_to_shwi (src);
21963
21964   return true;
21965 }
21966
21967 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
21968    FIELD_DECL in STRUCT_TYPE that represents a variant part.  If unsuccessful,
21969    store NULL_TREE in DISCR_DECL.  Otherwise:
21970
21971      - store the discriminant field in STRUCT_TYPE that controls the variant
21972        part to *DISCR_DECL
21973
21974      - put in *DISCR_LISTS_P an array where for each variant, the item
21975        represents the corresponding matching list of discriminant values.
21976
21977      - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
21978        the above array.
21979
21980    Note that when the array is allocated (i.e. when the analysis is
21981    successful), it is up to the caller to free the array.  */
21982
21983 static void
21984 analyze_variants_discr (tree variant_part_decl,
21985                         tree struct_type,
21986                         tree *discr_decl,
21987                         dw_discr_list_ref **discr_lists_p,
21988                         unsigned *discr_lists_length)
21989 {
21990   tree variant_part_type = TREE_TYPE (variant_part_decl);
21991   tree variant;
21992   dw_discr_list_ref *discr_lists;
21993   unsigned i;
21994
21995   /* Compute how many variants there are in this variant part.  */
21996   *discr_lists_length = 0;
21997   for (variant = TYPE_FIELDS (variant_part_type);
21998        variant != NULL_TREE;
21999        variant = DECL_CHAIN (variant))
22000     ++*discr_lists_length;
22001
22002   *discr_decl = NULL_TREE;
22003   *discr_lists_p
22004     = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22005                                      sizeof (**discr_lists_p));
22006   discr_lists = *discr_lists_p;
22007
22008   /* And then analyze all variants to extract discriminant information for all
22009      of them.  This analysis is conservative: as soon as we detect something we
22010      do not support, abort everything and pretend we found nothing.  */
22011   for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22012        variant != NULL_TREE;
22013        variant = DECL_CHAIN (variant), ++i)
22014     {
22015       tree match_expr = DECL_QUALIFIER (variant);
22016
22017       /* Now, try to analyze the predicate and deduce a discriminant for
22018          it.  */
22019       if (match_expr == boolean_true_node)
22020         /* Typically happens for the default variant: it matches all cases that
22021            previous variants rejected.  Don't output any matching value for
22022            this one.  */
22023         continue;
22024
22025       /* The following loop tries to iterate over each discriminant
22026          possibility: single values or ranges.  */
22027       while (match_expr != NULL_TREE)
22028         {
22029           tree next_round_match_expr;
22030           tree candidate_discr = NULL_TREE;
22031           dw_discr_list_ref new_node = NULL;
22032
22033           /* Possibilities are matched one after the other by nested
22034              TRUTH_ORIF_EXPR expressions.  Process the current possibility and
22035              continue with the rest at next iteration.  */
22036           if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22037             {
22038               next_round_match_expr = TREE_OPERAND (match_expr, 0);
22039               match_expr = TREE_OPERAND (match_expr, 1);
22040             }
22041           else
22042             next_round_match_expr = NULL_TREE;
22043
22044           if (match_expr == boolean_false_node)
22045             /* This sub-expression matches nothing: just wait for the next
22046                one.  */
22047             ;
22048
22049           else if (TREE_CODE (match_expr) == EQ_EXPR)
22050             {
22051               /* We are matching:  <discr_field> == <integer_cst>
22052                  This sub-expression matches a single value.  */
22053               tree integer_cst = TREE_OPERAND (match_expr, 1);
22054
22055               candidate_discr
22056                = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22057                                              struct_type);
22058
22059               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22060               if (!get_discr_value (integer_cst,
22061                                     &new_node->dw_discr_lower_bound))
22062                 goto abort;
22063               new_node->dw_discr_range = false;
22064             }
22065
22066           else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22067             {
22068               /* We are matching:
22069                    <discr_field> > <integer_cst>
22070                    && <discr_field> < <integer_cst>.
22071                  This sub-expression matches the range of values between the
22072                  two matched integer constants.  Note that comparisons can be
22073                  inclusive or exclusive.  */
22074               tree candidate_discr_1, candidate_discr_2;
22075               tree lower_cst, upper_cst;
22076               bool lower_cst_included, upper_cst_included;
22077               tree lower_op = TREE_OPERAND (match_expr, 0);
22078               tree upper_op = TREE_OPERAND (match_expr, 1);
22079
22080               /* When the comparison is exclusive, the integer constant is not
22081                  the discriminant range bound we are looking for: we will have
22082                  to increment or decrement it.  */
22083               if (TREE_CODE (lower_op) == GE_EXPR)
22084                 lower_cst_included = true;
22085               else if (TREE_CODE (lower_op) == GT_EXPR)
22086                 lower_cst_included = false;
22087               else
22088                 goto abort;
22089
22090               if (TREE_CODE (upper_op) == LE_EXPR)
22091                 upper_cst_included = true;
22092               else if (TREE_CODE (upper_op) == LT_EXPR)
22093                 upper_cst_included = false;
22094               else
22095                 goto abort;
22096
22097               /* Extract the discriminant from the first operand and check it
22098                  is consistant with the same analysis in the second
22099                  operand.  */
22100               candidate_discr_1
22101                 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22102                                               struct_type);
22103               candidate_discr_2
22104                 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22105                                               struct_type);
22106               if (candidate_discr_1 == candidate_discr_2)
22107                 candidate_discr = candidate_discr_1;
22108               else
22109                 goto abort;
22110
22111               /* Extract bounds from both.  */
22112               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22113               lower_cst = TREE_OPERAND (lower_op, 1);
22114               upper_cst = TREE_OPERAND (upper_op, 1);
22115
22116               if (!lower_cst_included)
22117                 lower_cst
22118                   = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22119                                   lower_cst,
22120                                   build_int_cst (TREE_TYPE (lower_cst), 1)));
22121               if (!upper_cst_included)
22122                 upper_cst
22123                   = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22124                                   upper_cst,
22125                                   build_int_cst (TREE_TYPE (upper_cst), 1)));
22126
22127               if (!get_discr_value (lower_cst,
22128                                     &new_node->dw_discr_lower_bound)
22129                   || !get_discr_value (upper_cst,
22130                                        &new_node->dw_discr_upper_bound))
22131                 goto abort;
22132
22133               new_node->dw_discr_range = true;
22134             }
22135
22136           else
22137             /* Unsupported sub-expression: we cannot determine the set of
22138                matching discriminant values.  Abort everything.  */
22139             goto abort;
22140
22141           /* If the discriminant info is not consistant with what we saw so
22142              far, consider the analysis failed and abort everything.  */
22143           if (candidate_discr == NULL_TREE
22144               || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22145             goto abort;
22146           else
22147             *discr_decl = candidate_discr;
22148
22149           if (new_node != NULL)
22150             {
22151               new_node->dw_discr_next = discr_lists[i];
22152               discr_lists[i] = new_node;
22153             }
22154           match_expr = next_round_match_expr;
22155         }
22156     }
22157
22158   /* If we reach this point, we could match everything we were interested
22159      in.  */
22160   return;
22161
22162 abort:
22163   /* Clean all data structure and return no result.  */
22164   free (*discr_lists_p);
22165   *discr_lists_p = NULL;
22166   *discr_decl = NULL_TREE;
22167 }
22168
22169 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22170    of STRUCT_TYPE, a record type.  This new DIE is emitted as the next child
22171    under CONTEXT_DIE.
22172
22173    Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22174    QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter.  The members for
22175    this type, which are record types, represent the available variants and each
22176    has a DECL_QUALIFIER attribute.  The discriminant and the discriminant
22177    values are inferred from these attributes.
22178
22179    In trees, the offsets for the fields inside these sub-records are relative
22180    to the variant part itself, whereas the corresponding DIEs should have
22181    offset attributes that are relative to the embedding record base address.
22182    This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22183    must be an expression that computes the offset of the variant part to
22184    describe in DWARF.  */
22185
22186 static void
22187 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22188                   dw_die_ref context_die)
22189 {
22190   const tree variant_part_type = TREE_TYPE (variant_part_decl);
22191   tree variant_part_offset = vlr_ctx->variant_part_offset;
22192   struct loc_descr_context ctx = {
22193     vlr_ctx->struct_type, /* context_type */
22194     NULL_TREE,            /* base_decl */
22195     NULL                  /* dpi */
22196   };
22197
22198   /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22199      NULL_TREE if there is no such field.  */
22200   tree discr_decl = NULL_TREE;
22201   dw_discr_list_ref *discr_lists;
22202   unsigned discr_lists_length = 0;
22203   unsigned i;
22204
22205   dw_die_ref dwarf_proc_die = NULL;
22206   dw_die_ref variant_part_die
22207     = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22208
22209   equate_decl_number_to_die (variant_part_decl, variant_part_die);
22210
22211   analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22212                           &discr_decl, &discr_lists, &discr_lists_length);
22213
22214   if (discr_decl != NULL_TREE)
22215     {
22216       dw_die_ref discr_die = lookup_decl_die (discr_decl);
22217
22218       if (discr_die)
22219         add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22220       else
22221         /* We have no DIE for the discriminant, so just discard all
22222            discrimimant information in the output.  */
22223         discr_decl = NULL_TREE;
22224     }
22225
22226   /* If the offset for this variant part is more complex than a constant,
22227      create a DWARF procedure for it so that we will not have to generate DWARF
22228      expressions for it for each member.  */
22229   if (TREE_CODE (variant_part_offset) != INTEGER_CST
22230       && (dwarf_version >= 3 || !dwarf_strict))
22231     {
22232       const tree dwarf_proc_fndecl
22233         = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22234                       build_function_type (TREE_TYPE (variant_part_offset),
22235                                            NULL_TREE));
22236       const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22237       const dw_loc_descr_ref dwarf_proc_body
22238         = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22239
22240       dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22241                                            dwarf_proc_fndecl, context_die);
22242       if (dwarf_proc_die != NULL)
22243         variant_part_offset = dwarf_proc_call;
22244     }
22245
22246   /* Output DIEs for all variants.  */
22247   i = 0;
22248   for (tree variant = TYPE_FIELDS (variant_part_type);
22249        variant != NULL_TREE;
22250        variant = DECL_CHAIN (variant), ++i)
22251     {
22252       tree variant_type = TREE_TYPE (variant);
22253       dw_die_ref variant_die;
22254
22255       /* All variants (i.e. members of a variant part) are supposed to be
22256          encoded as structures.  Sub-variant parts are QUAL_UNION_TYPE fields
22257          under these records.  */
22258       gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22259
22260       variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22261       equate_decl_number_to_die (variant, variant_die);
22262
22263       /* Output discriminant values this variant matches, if any.  */
22264       if (discr_decl == NULL || discr_lists[i] == NULL)
22265         /* In the case we have discriminant information at all, this is
22266            probably the default variant: as the standard says, don't
22267            output any discriminant value/list attribute.  */
22268         ;
22269       else if (discr_lists[i]->dw_discr_next == NULL
22270                && !discr_lists[i]->dw_discr_range)
22271         /* If there is only one accepted value, don't bother outputting a
22272            list.  */
22273         add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22274       else
22275         add_discr_list (variant_die, discr_lists[i]);
22276
22277       for (tree member = TYPE_FIELDS (variant_type);
22278            member != NULL_TREE;
22279            member = DECL_CHAIN (member))
22280         {
22281           struct vlr_context vlr_sub_ctx = {
22282             vlr_ctx->struct_type, /* struct_type */
22283             NULL                  /* variant_part_offset */
22284           };
22285           if (is_variant_part (member))
22286             {
22287               /* All offsets for fields inside variant parts are relative to
22288                  the top-level embedding RECORD_TYPE's base address.  On the
22289                  other hand, offsets in GCC's types are relative to the
22290                  nested-most variant part.  So we have to sum offsets each time
22291                  we recurse.  */
22292
22293               vlr_sub_ctx.variant_part_offset
22294                 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22295                                 variant_part_offset, byte_position (member)));
22296               gen_variant_part (member, &vlr_sub_ctx, variant_die);
22297             }
22298           else
22299             {
22300               vlr_sub_ctx.variant_part_offset = variant_part_offset;
22301               gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22302             }
22303         }
22304     }
22305
22306   free (discr_lists);
22307 }
22308
22309 /* Generate a DIE for a class member.  */
22310
22311 static void
22312 gen_member_die (tree type, dw_die_ref context_die)
22313 {
22314   tree member;
22315   tree binfo = TYPE_BINFO (type);
22316   dw_die_ref child;
22317
22318   /* If this is not an incomplete type, output descriptions of each of its
22319      members. Note that as we output the DIEs necessary to represent the
22320      members of this record or union type, we will also be trying to output
22321      DIEs to represent the *types* of those members. However the `type'
22322      function (above) will specifically avoid generating type DIEs for member
22323      types *within* the list of member DIEs for this (containing) type except
22324      for those types (of members) which are explicitly marked as also being
22325      members of this (containing) type themselves.  The g++ front- end can
22326      force any given type to be treated as a member of some other (containing)
22327      type by setting the TYPE_CONTEXT of the given (member) type to point to
22328      the TREE node representing the appropriate (containing) type.  */
22329
22330   /* First output info about the base classes.  */
22331   if (binfo)
22332     {
22333       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22334       int i;
22335       tree base;
22336
22337       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22338         gen_inheritance_die (base,
22339                              (accesses ? (*accesses)[i] : access_public_node),
22340                              type,
22341                              context_die);
22342     }
22343
22344   /* Now output info about the data members and type members.  */
22345   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22346     {
22347       struct vlr_context vlr_ctx = { type, NULL_TREE };
22348
22349       /* If we thought we were generating minimal debug info for TYPE
22350          and then changed our minds, some of the member declarations
22351          may have already been defined.  Don't define them again, but
22352          do put them in the right order.  */
22353
22354       child = lookup_decl_die (member);
22355       if (child)
22356         splice_child_die (context_die, child);
22357
22358       /* Do not generate standard DWARF for variant parts if we are generating
22359          the corresponding GNAT encodings: DIEs generated for both would
22360          conflict in our mappings.  */
22361       else if (is_variant_part (member)
22362                && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22363         {
22364           vlr_ctx.variant_part_offset = byte_position (member);
22365           gen_variant_part (member, &vlr_ctx, context_die);
22366         }
22367       else
22368         {
22369           vlr_ctx.variant_part_offset = NULL_TREE;
22370           gen_decl_die (member, NULL, &vlr_ctx, context_die);
22371         }
22372     }
22373
22374   /* We do not keep type methods in type variants.  */
22375   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22376   /* Now output info about the function members (if any).  */
22377   if (TYPE_METHODS (type) != error_mark_node)
22378     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22379       {
22380         /* Don't include clones in the member list.  */
22381         if (DECL_ABSTRACT_ORIGIN (member))
22382           continue;
22383         /* Nor constructors for anonymous classes.  */
22384         if (DECL_ARTIFICIAL (member)
22385             && dwarf2_name (member, 0) == NULL)
22386           continue;
22387
22388         child = lookup_decl_die (member);
22389         if (child)
22390           splice_child_die (context_die, child);
22391         else
22392           gen_decl_die (member, NULL, NULL, context_die);
22393       }
22394 }
22395
22396 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
22397    is set, we pretend that the type was never defined, so we only get the
22398    member DIEs needed by later specification DIEs.  */
22399
22400 static void
22401 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22402                                 enum debug_info_usage usage)
22403 {
22404   if (TREE_ASM_WRITTEN (type))
22405     {
22406       /* Fill in the bound of variable-length fields in late dwarf if
22407          still incomplete.  */
22408       if (!early_dwarf && variably_modified_type_p (type, NULL))
22409         for (tree member = TYPE_FIELDS (type);
22410              member;
22411              member = DECL_CHAIN (member))
22412           fill_variable_array_bounds (TREE_TYPE (member));
22413       return;
22414     }
22415
22416   dw_die_ref type_die = lookup_type_die (type);
22417   dw_die_ref scope_die = 0;
22418   int nested = 0;
22419   int complete = (TYPE_SIZE (type)
22420                   && (! TYPE_STUB_DECL (type)
22421                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22422   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22423   complete = complete && should_emit_struct_debug (type, usage);
22424
22425   if (type_die && ! complete)
22426     return;
22427
22428   if (TYPE_CONTEXT (type) != NULL_TREE
22429       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22430           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22431     nested = 1;
22432
22433   scope_die = scope_die_for (type, context_die);
22434
22435   /* Generate child dies for template paramaters.  */
22436   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22437     schedule_generic_params_dies_gen (type);
22438
22439   if (! type_die || (nested && is_cu_die (scope_die)))
22440     /* First occurrence of type or toplevel definition of nested class.  */
22441     {
22442       dw_die_ref old_die = type_die;
22443
22444       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22445                           ? record_type_tag (type) : DW_TAG_union_type,
22446                           scope_die, type);
22447       equate_type_number_to_die (type, type_die);
22448       if (old_die)
22449         add_AT_specification (type_die, old_die);
22450       else
22451         add_name_attribute (type_die, type_tag (type));
22452     }
22453   else
22454     remove_AT (type_die, DW_AT_declaration);
22455
22456   /* If this type has been completed, then give it a byte_size attribute and
22457      then give a list of members.  */
22458   if (complete && !ns_decl)
22459     {
22460       /* Prevent infinite recursion in cases where the type of some member of
22461          this type is expressed in terms of this type itself.  */
22462       TREE_ASM_WRITTEN (type) = 1;
22463       add_byte_size_attribute (type_die, type);
22464       if (TYPE_STUB_DECL (type) != NULL_TREE)
22465         {
22466           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22467           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22468         }
22469
22470       /* If the first reference to this type was as the return type of an
22471          inline function, then it may not have a parent.  Fix this now.  */
22472       if (type_die->die_parent == NULL)
22473         add_child_die (scope_die, type_die);
22474
22475       push_decl_scope (type);
22476       gen_member_die (type, type_die);
22477       pop_decl_scope ();
22478
22479       add_gnat_descriptive_type_attribute (type_die, type, context_die);
22480       if (TYPE_ARTIFICIAL (type))
22481         add_AT_flag (type_die, DW_AT_artificial, 1);
22482
22483       /* GNU extension: Record what type our vtable lives in.  */
22484       if (TYPE_VFIELD (type))
22485         {
22486           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22487
22488           gen_type_die (vtype, context_die);
22489           add_AT_die_ref (type_die, DW_AT_containing_type,
22490                           lookup_type_die (vtype));
22491         }
22492     }
22493   else
22494     {
22495       add_AT_flag (type_die, DW_AT_declaration, 1);
22496
22497       /* We don't need to do this for function-local types.  */
22498       if (TYPE_STUB_DECL (type)
22499           && ! decl_function_context (TYPE_STUB_DECL (type)))
22500         vec_safe_push (incomplete_types, type);
22501     }
22502
22503   if (get_AT (type_die, DW_AT_name))
22504     add_pubtype (type, type_die);
22505 }
22506
22507 /* Generate a DIE for a subroutine _type_.  */
22508
22509 static void
22510 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22511 {
22512   tree return_type = TREE_TYPE (type);
22513   dw_die_ref subr_die
22514     = new_die (DW_TAG_subroutine_type,
22515                scope_die_for (type, context_die), type);
22516
22517   equate_type_number_to_die (type, subr_die);
22518   add_prototyped_attribute (subr_die, type);
22519   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22520                       context_die);
22521   gen_formal_types_die (type, subr_die);
22522
22523   if (get_AT (subr_die, DW_AT_name))
22524     add_pubtype (type, subr_die);
22525 }
22526
22527 /* Generate a DIE for a type definition.  */
22528
22529 static void
22530 gen_typedef_die (tree decl, dw_die_ref context_die)
22531 {
22532   dw_die_ref type_die;
22533   tree origin;
22534
22535   if (TREE_ASM_WRITTEN (decl))
22536     {
22537       if (DECL_ORIGINAL_TYPE (decl))
22538         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22539       return;
22540     }
22541
22542   TREE_ASM_WRITTEN (decl) = 1;
22543   type_die = new_die (DW_TAG_typedef, context_die, decl);
22544   origin = decl_ultimate_origin (decl);
22545   if (origin != NULL)
22546     add_abstract_origin_attribute (type_die, origin);
22547   else
22548     {
22549       tree type;
22550
22551       add_name_and_src_coords_attributes (type_die, decl);
22552       if (DECL_ORIGINAL_TYPE (decl))
22553         {
22554           type = DECL_ORIGINAL_TYPE (decl);
22555
22556           if (type == error_mark_node)
22557             return;
22558
22559           gcc_assert (type != TREE_TYPE (decl));
22560           equate_type_number_to_die (TREE_TYPE (decl), type_die);
22561         }
22562       else
22563         {
22564           type = TREE_TYPE (decl);
22565
22566           if (type == error_mark_node)
22567             return;
22568
22569           if (is_naming_typedef_decl (TYPE_NAME (type)))
22570             {
22571               /* Here, we are in the case of decl being a typedef naming
22572                  an anonymous type, e.g:
22573                      typedef struct {...} foo;
22574                  In that case TREE_TYPE (decl) is not a typedef variant
22575                  type and TYPE_NAME of the anonymous type is set to the
22576                  TYPE_DECL of the typedef. This construct is emitted by
22577                  the C++ FE.
22578
22579                  TYPE is the anonymous struct named by the typedef
22580                  DECL. As we need the DW_AT_type attribute of the
22581                  DW_TAG_typedef to point to the DIE of TYPE, let's
22582                  generate that DIE right away. add_type_attribute
22583                  called below will then pick (via lookup_type_die) that
22584                  anonymous struct DIE.  */
22585               if (!TREE_ASM_WRITTEN (type))
22586                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22587
22588               /* This is a GNU Extension.  We are adding a
22589                  DW_AT_linkage_name attribute to the DIE of the
22590                  anonymous struct TYPE.  The value of that attribute
22591                  is the name of the typedef decl naming the anonymous
22592                  struct.  This greatly eases the work of consumers of
22593                  this debug info.  */
22594               add_linkage_name_raw (lookup_type_die (type), decl);
22595             }
22596         }
22597
22598       add_type_attribute (type_die, type, decl_quals (decl), false,
22599                           context_die);
22600
22601       if (is_naming_typedef_decl (decl))
22602         /* We want that all subsequent calls to lookup_type_die with
22603            TYPE in argument yield the DW_TAG_typedef we have just
22604            created.  */
22605         equate_type_number_to_die (type, type_die);
22606
22607       add_accessibility_attribute (type_die, decl);
22608     }
22609
22610   if (DECL_ABSTRACT_P (decl))
22611     equate_decl_number_to_die (decl, type_die);
22612
22613   if (get_AT (type_die, DW_AT_name))
22614     add_pubtype (decl, type_die);
22615 }
22616
22617 /* Generate a DIE for a struct, class, enum or union type.  */
22618
22619 static void
22620 gen_tagged_type_die (tree type,
22621                      dw_die_ref context_die,
22622                      enum debug_info_usage usage)
22623 {
22624   int need_pop;
22625
22626   if (type == NULL_TREE
22627       || !is_tagged_type (type))
22628     return;
22629
22630   if (TREE_ASM_WRITTEN (type))
22631     need_pop = 0;
22632   /* If this is a nested type whose containing class hasn't been written
22633      out yet, writing it out will cover this one, too.  This does not apply
22634      to instantiations of member class templates; they need to be added to
22635      the containing class as they are generated.  FIXME: This hurts the
22636      idea of combining type decls from multiple TUs, since we can't predict
22637      what set of template instantiations we'll get.  */
22638   else if (TYPE_CONTEXT (type)
22639       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22640       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22641     {
22642       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22643
22644       if (TREE_ASM_WRITTEN (type))
22645         return;
22646
22647       /* If that failed, attach ourselves to the stub.  */
22648       push_decl_scope (TYPE_CONTEXT (type));
22649       context_die = lookup_type_die (TYPE_CONTEXT (type));
22650       need_pop = 1;
22651     }
22652   else if (TYPE_CONTEXT (type) != NULL_TREE
22653            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22654     {
22655       /* If this type is local to a function that hasn't been written
22656          out yet, use a NULL context for now; it will be fixed up in
22657          decls_for_scope.  */
22658       context_die = lookup_decl_die (TYPE_CONTEXT (type));
22659       /* A declaration DIE doesn't count; nested types need to go in the
22660          specification.  */
22661       if (context_die && is_declaration_die (context_die))
22662         context_die = NULL;
22663       need_pop = 0;
22664     }
22665   else
22666     {
22667       context_die = declare_in_namespace (type, context_die);
22668       need_pop = 0;
22669     }
22670
22671   if (TREE_CODE (type) == ENUMERAL_TYPE)
22672     {
22673       /* This might have been written out by the call to
22674          declare_in_namespace.  */
22675       if (!TREE_ASM_WRITTEN (type))
22676         gen_enumeration_type_die (type, context_die);
22677     }
22678   else
22679     gen_struct_or_union_type_die (type, context_die, usage);
22680
22681   if (need_pop)
22682     pop_decl_scope ();
22683
22684   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22685      it up if it is ever completed.  gen_*_type_die will set it for us
22686      when appropriate.  */
22687 }
22688
22689 /* Generate a type description DIE.  */
22690
22691 static void
22692 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22693                          enum debug_info_usage usage)
22694 {
22695   struct array_descr_info info;
22696
22697   if (type == NULL_TREE || type == error_mark_node)
22698     return;
22699
22700   if (flag_checking && type)
22701      verify_type (type);
22702
22703   if (TYPE_NAME (type) != NULL_TREE
22704       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22705       && is_redundant_typedef (TYPE_NAME (type))
22706       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22707     /* The DECL of this type is a typedef we don't want to emit debug
22708        info for but we want debug info for its underlying typedef.
22709        This can happen for e.g, the injected-class-name of a C++
22710        type.  */
22711     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22712
22713   /* If TYPE is a typedef type variant, let's generate debug info
22714      for the parent typedef which TYPE is a type of.  */
22715   if (typedef_variant_p (type))
22716     {
22717       if (TREE_ASM_WRITTEN (type))
22718         return;
22719
22720       /* Prevent broken recursion; we can't hand off to the same type.  */
22721       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22722
22723       /* Give typedefs the right scope.  */
22724       context_die = scope_die_for (type, context_die);
22725
22726       TREE_ASM_WRITTEN (type) = 1;
22727
22728       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22729       return;
22730     }
22731
22732   /* If type is an anonymous tagged type named by a typedef, let's
22733      generate debug info for the typedef.  */
22734   if (is_naming_typedef_decl (TYPE_NAME (type)))
22735     {
22736       /* Use the DIE of the containing namespace as the parent DIE of
22737          the type description DIE we want to generate.  */
22738       if (DECL_CONTEXT (TYPE_NAME (type))
22739           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22740         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22741
22742       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22743       return;
22744     }
22745
22746   /* We are going to output a DIE to represent the unqualified version
22747      of this type (i.e. without any const or volatile qualifiers) so
22748      get the main variant (i.e. the unqualified version) of this type
22749      now.  (Vectors and arrays are special because the debugging info is in the
22750      cloned type itself).  */
22751   if (TREE_CODE (type) != VECTOR_TYPE
22752       && TREE_CODE (type) != ARRAY_TYPE)
22753     type = type_main_variant (type);
22754
22755   /* If this is an array type with hidden descriptor, handle it first.  */
22756   if (!TREE_ASM_WRITTEN (type)
22757       && lang_hooks.types.get_array_descr_info)
22758     {
22759       memset (&info, 0, sizeof (info));
22760       if (lang_hooks.types.get_array_descr_info (type, &info))
22761         {
22762           /* Fortran sometimes emits array types with no dimension.  */
22763           gcc_assert (info.ndimensions >= 0
22764                       && (info.ndimensions
22765                           <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
22766           gen_descr_array_type_die (type, &info, context_die);
22767           TREE_ASM_WRITTEN (type) = 1;
22768           return;
22769         }
22770     }
22771
22772   if (TREE_ASM_WRITTEN (type))
22773     {
22774       /* Variable-length types may be incomplete even if
22775          TREE_ASM_WRITTEN.  For such types, fall through to
22776          gen_array_type_die() and possibly fill in
22777          DW_AT_{upper,lower}_bound attributes.  */
22778       if ((TREE_CODE (type) != ARRAY_TYPE
22779            && TREE_CODE (type) != RECORD_TYPE
22780            && TREE_CODE (type) != UNION_TYPE
22781            && TREE_CODE (type) != QUAL_UNION_TYPE)
22782           || !variably_modified_type_p (type, NULL))
22783         return;
22784     }
22785
22786   switch (TREE_CODE (type))
22787     {
22788     case ERROR_MARK:
22789       break;
22790
22791     case POINTER_TYPE:
22792     case REFERENCE_TYPE:
22793       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
22794          ensures that the gen_type_die recursion will terminate even if the
22795          type is recursive.  Recursive types are possible in Ada.  */
22796       /* ??? We could perhaps do this for all types before the switch
22797          statement.  */
22798       TREE_ASM_WRITTEN (type) = 1;
22799
22800       /* For these types, all that is required is that we output a DIE (or a
22801          set of DIEs) to represent the "basis" type.  */
22802       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22803                                 DINFO_USAGE_IND_USE);
22804       break;
22805
22806     case OFFSET_TYPE:
22807       /* This code is used for C++ pointer-to-data-member types.
22808          Output a description of the relevant class type.  */
22809       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
22810                                         DINFO_USAGE_IND_USE);
22811
22812       /* Output a description of the type of the object pointed to.  */
22813       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22814                                         DINFO_USAGE_IND_USE);
22815
22816       /* Now output a DIE to represent this pointer-to-data-member type
22817          itself.  */
22818       gen_ptr_to_mbr_type_die (type, context_die);
22819       break;
22820
22821     case FUNCTION_TYPE:
22822       /* Force out return type (in case it wasn't forced out already).  */
22823       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22824                                         DINFO_USAGE_DIR_USE);
22825       gen_subroutine_type_die (type, context_die);
22826       break;
22827
22828     case METHOD_TYPE:
22829       /* Force out return type (in case it wasn't forced out already).  */
22830       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22831                                         DINFO_USAGE_DIR_USE);
22832       gen_subroutine_type_die (type, context_die);
22833       break;
22834
22835     case ARRAY_TYPE:
22836     case VECTOR_TYPE:
22837       gen_array_type_die (type, context_die);
22838       break;
22839
22840     case ENUMERAL_TYPE:
22841     case RECORD_TYPE:
22842     case UNION_TYPE:
22843     case QUAL_UNION_TYPE:
22844       gen_tagged_type_die (type, context_die, usage);
22845       return;
22846
22847     case VOID_TYPE:
22848     case INTEGER_TYPE:
22849     case REAL_TYPE:
22850     case FIXED_POINT_TYPE:
22851     case COMPLEX_TYPE:
22852     case BOOLEAN_TYPE:
22853     case POINTER_BOUNDS_TYPE:
22854       /* No DIEs needed for fundamental types.  */
22855       break;
22856
22857     case NULLPTR_TYPE:
22858     case LANG_TYPE:
22859       /* Just use DW_TAG_unspecified_type.  */
22860       {
22861         dw_die_ref type_die = lookup_type_die (type);
22862         if (type_die == NULL)
22863           {
22864             tree name = TYPE_IDENTIFIER (type);
22865             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
22866                                 type);
22867             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
22868             equate_type_number_to_die (type, type_die);
22869           }
22870       }
22871       break;
22872
22873     default:
22874       if (is_cxx_auto (type))
22875         {
22876           tree name = TYPE_IDENTIFIER (type);
22877           dw_die_ref *die = (name == get_identifier ("auto")
22878                              ? &auto_die : &decltype_auto_die);
22879           if (!*die)
22880             {
22881               *die = new_die (DW_TAG_unspecified_type,
22882                               comp_unit_die (), NULL_TREE);
22883               add_name_attribute (*die, IDENTIFIER_POINTER (name));
22884             }
22885           equate_type_number_to_die (type, *die);
22886           break;
22887         }
22888       gcc_unreachable ();
22889     }
22890
22891   TREE_ASM_WRITTEN (type) = 1;
22892 }
22893
22894 static void
22895 gen_type_die (tree type, dw_die_ref context_die)
22896 {
22897   if (type != error_mark_node)
22898     {
22899       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
22900       if (flag_checking)
22901         {
22902           dw_die_ref die = lookup_type_die (type);
22903           if (die)
22904             check_die (die);
22905         }
22906     }
22907 }
22908
22909 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
22910    things which are local to the given block.  */
22911
22912 static void
22913 gen_block_die (tree stmt, dw_die_ref context_die)
22914 {
22915   int must_output_die = 0;
22916   bool inlined_func;
22917
22918   /* Ignore blocks that are NULL.  */
22919   if (stmt == NULL_TREE)
22920     return;
22921
22922   inlined_func = inlined_function_outer_scope_p (stmt);
22923
22924   /* If the block is one fragment of a non-contiguous block, do not
22925      process the variables, since they will have been done by the
22926      origin block.  Do process subblocks.  */
22927   if (BLOCK_FRAGMENT_ORIGIN (stmt))
22928     {
22929       tree sub;
22930
22931       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
22932         gen_block_die (sub, context_die);
22933
22934       return;
22935     }
22936
22937   /* Determine if we need to output any Dwarf DIEs at all to represent this
22938      block.  */
22939   if (inlined_func)
22940     /* The outer scopes for inlinings *must* always be represented.  We
22941        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
22942     must_output_die = 1;
22943   else
22944     {
22945       /* Determine if this block directly contains any "significant"
22946          local declarations which we will need to output DIEs for.  */
22947       if (debug_info_level > DINFO_LEVEL_TERSE)
22948         /* We are not in terse mode so *any* local declaration counts
22949            as being a "significant" one.  */
22950         must_output_die = ((BLOCK_VARS (stmt) != NULL
22951                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
22952                            && (TREE_USED (stmt)
22953                                || TREE_ASM_WRITTEN (stmt)
22954                                || BLOCK_ABSTRACT (stmt)));
22955       else if ((TREE_USED (stmt)
22956                 || TREE_ASM_WRITTEN (stmt)
22957                 || BLOCK_ABSTRACT (stmt))
22958                && !dwarf2out_ignore_block (stmt))
22959         must_output_die = 1;
22960     }
22961
22962   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
22963      DIE for any block which contains no significant local declarations at
22964      all.  Rather, in such cases we just call `decls_for_scope' so that any
22965      needed Dwarf info for any sub-blocks will get properly generated. Note
22966      that in terse mode, our definition of what constitutes a "significant"
22967      local declaration gets restricted to include only inlined function
22968      instances and local (nested) function definitions.  */
22969   if (must_output_die)
22970     {
22971       if (inlined_func)
22972         {
22973           /* If STMT block is abstract, that means we have been called
22974              indirectly from dwarf2out_abstract_function.
22975              That function rightfully marks the descendent blocks (of
22976              the abstract function it is dealing with) as being abstract,
22977              precisely to prevent us from emitting any
22978              DW_TAG_inlined_subroutine DIE as a descendent
22979              of an abstract function instance. So in that case, we should
22980              not call gen_inlined_subroutine_die.
22981
22982              Later though, when cgraph asks dwarf2out to emit info
22983              for the concrete instance of the function decl into which
22984              the concrete instance of STMT got inlined, the later will lead
22985              to the generation of a DW_TAG_inlined_subroutine DIE.  */
22986           if (! BLOCK_ABSTRACT (stmt))
22987             gen_inlined_subroutine_die (stmt, context_die);
22988         }
22989       else
22990         gen_lexical_block_die (stmt, context_die);
22991     }
22992   else
22993     decls_for_scope (stmt, context_die);
22994 }
22995
22996 /* Process variable DECL (or variable with origin ORIGIN) within
22997    block STMT and add it to CONTEXT_DIE.  */
22998 static void
22999 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23000 {
23001   dw_die_ref die;
23002   tree decl_or_origin = decl ? decl : origin;
23003
23004   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23005     die = lookup_decl_die (decl_or_origin);
23006   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23007            && TYPE_DECL_IS_STUB (decl_or_origin))
23008     die = lookup_type_die (TREE_TYPE (decl_or_origin));
23009   else
23010     die = NULL;
23011
23012   if (die != NULL && die->die_parent == NULL)
23013     add_child_die (context_die, die);
23014   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23015     {
23016       if (early_dwarf)
23017         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23018                                              stmt, context_die);
23019     }
23020   else
23021     gen_decl_die (decl, origin, NULL, context_die);
23022 }
23023
23024 /* Generate all of the decls declared within a given scope and (recursively)
23025    all of its sub-blocks.  */
23026
23027 static void
23028 decls_for_scope (tree stmt, dw_die_ref context_die)
23029 {
23030   tree decl;
23031   unsigned int i;
23032   tree subblocks;
23033
23034   /* Ignore NULL blocks.  */
23035   if (stmt == NULL_TREE)
23036     return;
23037
23038   /* Output the DIEs to represent all of the data objects and typedefs
23039      declared directly within this block but not within any nested
23040      sub-blocks.  Also, nested function and tag DIEs have been
23041      generated with a parent of NULL; fix that up now.  We don't
23042      have to do this if we're at -g1.  */
23043   if (debug_info_level > DINFO_LEVEL_TERSE)
23044     {
23045       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23046         process_scope_var (stmt, decl, NULL_TREE, context_die);
23047       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23048         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23049                            context_die);
23050     }
23051
23052   /* Even if we're at -g1, we need to process the subblocks in order to get
23053      inlined call information.  */
23054
23055   /* Output the DIEs to represent all sub-blocks (and the items declared
23056      therein) of this block.  */
23057   for (subblocks = BLOCK_SUBBLOCKS (stmt);
23058        subblocks != NULL;
23059        subblocks = BLOCK_CHAIN (subblocks))
23060     gen_block_die (subblocks, context_die);
23061 }
23062
23063 /* Is this a typedef we can avoid emitting?  */
23064
23065 bool
23066 is_redundant_typedef (const_tree decl)
23067 {
23068   if (TYPE_DECL_IS_STUB (decl))
23069     return true;
23070
23071   if (DECL_ARTIFICIAL (decl)
23072       && DECL_CONTEXT (decl)
23073       && is_tagged_type (DECL_CONTEXT (decl))
23074       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23075       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23076     /* Also ignore the artificial member typedef for the class name.  */
23077     return true;
23078
23079   return false;
23080 }
23081
23082 /* Return TRUE if TYPE is a typedef that names a type for linkage
23083    purposes. This kind of typedefs is produced by the C++ FE for
23084    constructs like:
23085
23086    typedef struct {...} foo;
23087
23088    In that case, there is no typedef variant type produced for foo.
23089    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23090    struct type.  */
23091
23092 static bool
23093 is_naming_typedef_decl (const_tree decl)
23094 {
23095   if (decl == NULL_TREE
23096       || TREE_CODE (decl) != TYPE_DECL
23097       || !is_tagged_type (TREE_TYPE (decl))
23098       || DECL_IS_BUILTIN (decl)
23099       || is_redundant_typedef (decl)
23100       /* It looks like Ada produces TYPE_DECLs that are very similar
23101          to C++ naming typedefs but that have different
23102          semantics. Let's be specific to c++ for now.  */
23103       || !is_cxx ())
23104     return FALSE;
23105
23106   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23107           && TYPE_NAME (TREE_TYPE (decl)) == decl
23108           && (TYPE_STUB_DECL (TREE_TYPE (decl))
23109               != TYPE_NAME (TREE_TYPE (decl))));
23110 }
23111
23112 /* Looks up the DIE for a context.  */
23113
23114 static inline dw_die_ref
23115 lookup_context_die (tree context)
23116 {
23117   if (context)
23118     {
23119       /* Find die that represents this context.  */
23120       if (TYPE_P (context))
23121         {
23122           context = TYPE_MAIN_VARIANT (context);
23123           dw_die_ref ctx = lookup_type_die (context);
23124           if (!ctx)
23125             return NULL;
23126           return strip_naming_typedef (context, ctx);
23127         }
23128       else
23129         return lookup_decl_die (context);
23130     }
23131   return comp_unit_die ();
23132 }
23133
23134 /* Returns the DIE for a context.  */
23135
23136 static inline dw_die_ref
23137 get_context_die (tree context)
23138 {
23139   if (context)
23140     {
23141       /* Find die that represents this context.  */
23142       if (TYPE_P (context))
23143         {
23144           context = TYPE_MAIN_VARIANT (context);
23145           return strip_naming_typedef (context, force_type_die (context));
23146         }
23147       else
23148         return force_decl_die (context);
23149     }
23150   return comp_unit_die ();
23151 }
23152
23153 /* Returns the DIE for decl.  A DIE will always be returned.  */
23154
23155 static dw_die_ref
23156 force_decl_die (tree decl)
23157 {
23158   dw_die_ref decl_die;
23159   unsigned saved_external_flag;
23160   tree save_fn = NULL_TREE;
23161   decl_die = lookup_decl_die (decl);
23162   if (!decl_die)
23163     {
23164       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23165
23166       decl_die = lookup_decl_die (decl);
23167       if (decl_die)
23168         return decl_die;
23169
23170       switch (TREE_CODE (decl))
23171         {
23172         case FUNCTION_DECL:
23173           /* Clear current_function_decl, so that gen_subprogram_die thinks
23174              that this is a declaration. At this point, we just want to force
23175              declaration die.  */
23176           save_fn = current_function_decl;
23177           current_function_decl = NULL_TREE;
23178           gen_subprogram_die (decl, context_die);
23179           current_function_decl = save_fn;
23180           break;
23181
23182         case VAR_DECL:
23183           /* Set external flag to force declaration die. Restore it after
23184            gen_decl_die() call.  */
23185           saved_external_flag = DECL_EXTERNAL (decl);
23186           DECL_EXTERNAL (decl) = 1;
23187           gen_decl_die (decl, NULL, NULL, context_die);
23188           DECL_EXTERNAL (decl) = saved_external_flag;
23189           break;
23190
23191         case NAMESPACE_DECL:
23192           if (dwarf_version >= 3 || !dwarf_strict)
23193             dwarf2out_decl (decl);
23194           else
23195             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
23196             decl_die = comp_unit_die ();
23197           break;
23198
23199         case TRANSLATION_UNIT_DECL:
23200           decl_die = comp_unit_die ();
23201           break;
23202
23203         default:
23204           gcc_unreachable ();
23205         }
23206
23207       /* We should be able to find the DIE now.  */
23208       if (!decl_die)
23209         decl_die = lookup_decl_die (decl);
23210       gcc_assert (decl_die);
23211     }
23212
23213   return decl_die;
23214 }
23215
23216 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
23217    always returned.  */
23218
23219 static dw_die_ref
23220 force_type_die (tree type)
23221 {
23222   dw_die_ref type_die;
23223
23224   type_die = lookup_type_die (type);
23225   if (!type_die)
23226     {
23227       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23228
23229       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23230                                     false, context_die);
23231       gcc_assert (type_die);
23232     }
23233   return type_die;
23234 }
23235
23236 /* Force out any required namespaces to be able to output DECL,
23237    and return the new context_die for it, if it's changed.  */
23238
23239 static dw_die_ref
23240 setup_namespace_context (tree thing, dw_die_ref context_die)
23241 {
23242   tree context = (DECL_P (thing)
23243                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23244   if (context && TREE_CODE (context) == NAMESPACE_DECL)
23245     /* Force out the namespace.  */
23246     context_die = force_decl_die (context);
23247
23248   return context_die;
23249 }
23250
23251 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23252    type) within its namespace, if appropriate.
23253
23254    For compatibility with older debuggers, namespace DIEs only contain
23255    declarations; all definitions are emitted at CU scope, with
23256    DW_AT_specification pointing to the declaration (like with class
23257    members).  */
23258
23259 static dw_die_ref
23260 declare_in_namespace (tree thing, dw_die_ref context_die)
23261 {
23262   dw_die_ref ns_context;
23263
23264   if (debug_info_level <= DINFO_LEVEL_TERSE)
23265     return context_die;
23266
23267   /* External declarations in the local scope only need to be emitted
23268      once, not once in the namespace and once in the scope.
23269
23270      This avoids declaring the `extern' below in the
23271      namespace DIE as well as in the innermost scope:
23272
23273           namespace S
23274           {
23275             int i=5;
23276             int foo()
23277             {
23278               int i=8;
23279               extern int i;
23280               return i;
23281             }
23282           }
23283   */
23284   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23285     return context_die;
23286
23287   /* If this decl is from an inlined function, then don't try to emit it in its
23288      namespace, as we will get confused.  It would have already been emitted
23289      when the abstract instance of the inline function was emitted anyways.  */
23290   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23291     return context_die;
23292
23293   ns_context = setup_namespace_context (thing, context_die);
23294
23295   if (ns_context != context_die)
23296     {
23297       if (is_fortran ())
23298         return ns_context;
23299       if (DECL_P (thing))
23300         gen_decl_die (thing, NULL, NULL, ns_context);
23301       else
23302         gen_type_die (thing, ns_context);
23303     }
23304   return context_die;
23305 }
23306
23307 /* Generate a DIE for a namespace or namespace alias.  */
23308
23309 static void
23310 gen_namespace_die (tree decl, dw_die_ref context_die)
23311 {
23312   dw_die_ref namespace_die;
23313
23314   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23315      they are an alias of.  */
23316   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23317     {
23318       /* Output a real namespace or module.  */
23319       context_die = setup_namespace_context (decl, comp_unit_die ());
23320       namespace_die = new_die (is_fortran ()
23321                                ? DW_TAG_module : DW_TAG_namespace,
23322                                context_die, decl);
23323       /* For Fortran modules defined in different CU don't add src coords.  */
23324       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23325         {
23326           const char *name = dwarf2_name (decl, 0);
23327           if (name)
23328             add_name_attribute (namespace_die, name);
23329         }
23330       else
23331         add_name_and_src_coords_attributes (namespace_die, decl);
23332       if (DECL_EXTERNAL (decl))
23333         add_AT_flag (namespace_die, DW_AT_declaration, 1);
23334       equate_decl_number_to_die (decl, namespace_die);
23335     }
23336   else
23337     {
23338       /* Output a namespace alias.  */
23339
23340       /* Force out the namespace we are an alias of, if necessary.  */
23341       dw_die_ref origin_die
23342         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23343
23344       if (DECL_FILE_SCOPE_P (decl)
23345           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23346         context_die = setup_namespace_context (decl, comp_unit_die ());
23347       /* Now create the namespace alias DIE.  */
23348       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23349       add_name_and_src_coords_attributes (namespace_die, decl);
23350       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23351       equate_decl_number_to_die (decl, namespace_die);
23352     }
23353   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
23354   if (want_pubnames ())
23355     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23356 }
23357
23358 /* Generate Dwarf debug information for a decl described by DECL.
23359    The return value is currently only meaningful for PARM_DECLs,
23360    for all other decls it returns NULL.
23361
23362    If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23363    It can be NULL otherwise.  */
23364
23365 static dw_die_ref
23366 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23367               dw_die_ref context_die)
23368 {
23369   tree decl_or_origin = decl ? decl : origin;
23370   tree class_origin = NULL, ultimate_origin;
23371
23372   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23373     return NULL;
23374
23375   /* Ignore pointer bounds decls.  */
23376   if (DECL_P (decl_or_origin)
23377       && TREE_TYPE (decl_or_origin)
23378       && POINTER_BOUNDS_P (decl_or_origin))
23379     return NULL;
23380
23381   switch (TREE_CODE (decl_or_origin))
23382     {
23383     case ERROR_MARK:
23384       break;
23385
23386     case CONST_DECL:
23387       if (!is_fortran () && !is_ada ())
23388         {
23389           /* The individual enumerators of an enum type get output when we output
23390              the Dwarf representation of the relevant enum type itself.  */
23391           break;
23392         }
23393
23394       /* Emit its type.  */
23395       gen_type_die (TREE_TYPE (decl), context_die);
23396
23397       /* And its containing namespace.  */
23398       context_die = declare_in_namespace (decl, context_die);
23399
23400       gen_const_die (decl, context_die);
23401       break;
23402
23403     case FUNCTION_DECL:
23404       /* Don't output any DIEs to represent mere function declarations,
23405          unless they are class members or explicit block externs.  */
23406       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23407           && DECL_FILE_SCOPE_P (decl_or_origin)
23408           && (current_function_decl == NULL_TREE
23409               || DECL_ARTIFICIAL (decl_or_origin)))
23410         break;
23411
23412 #if 0
23413       /* FIXME */
23414       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23415          on local redeclarations of global functions.  That seems broken.  */
23416       if (current_function_decl != decl)
23417         /* This is only a declaration.  */;
23418 #endif
23419
23420       /* If we're emitting a clone, emit info for the abstract instance.  */
23421       if (origin || DECL_ORIGIN (decl) != decl)
23422         dwarf2out_abstract_function (origin
23423                                      ? DECL_ORIGIN (origin)
23424                                      : DECL_ABSTRACT_ORIGIN (decl));
23425
23426       /* If we're emitting an out-of-line copy of an inline function,
23427          emit info for the abstract instance and set up to refer to it.  */
23428       else if (cgraph_function_possibly_inlined_p (decl)
23429                && ! DECL_ABSTRACT_P (decl)
23430                && ! class_or_namespace_scope_p (context_die)
23431                /* dwarf2out_abstract_function won't emit a die if this is just
23432                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
23433                   that case, because that works only if we have a die.  */
23434                && DECL_INITIAL (decl) != NULL_TREE)
23435         {
23436           dwarf2out_abstract_function (decl);
23437           set_decl_origin_self (decl);
23438         }
23439
23440       /* Otherwise we're emitting the primary DIE for this decl.  */
23441       else if (debug_info_level > DINFO_LEVEL_TERSE)
23442         {
23443           /* Before we describe the FUNCTION_DECL itself, make sure that we
23444              have its containing type.  */
23445           if (!origin)
23446             origin = decl_class_context (decl);
23447           if (origin != NULL_TREE)
23448             gen_type_die (origin, context_die);
23449
23450           /* And its return type.  */
23451           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23452
23453           /* And its virtual context.  */
23454           if (DECL_VINDEX (decl) != NULL_TREE)
23455             gen_type_die (DECL_CONTEXT (decl), context_die);
23456
23457           /* Make sure we have a member DIE for decl.  */
23458           if (origin != NULL_TREE)
23459             gen_type_die_for_member (origin, decl, context_die);
23460
23461           /* And its containing namespace.  */
23462           context_die = declare_in_namespace (decl, context_die);
23463         }
23464
23465       /* Now output a DIE to represent the function itself.  */
23466       if (decl)
23467         gen_subprogram_die (decl, context_die);
23468       break;
23469
23470     case TYPE_DECL:
23471       /* If we are in terse mode, don't generate any DIEs to represent any
23472          actual typedefs.  */
23473       if (debug_info_level <= DINFO_LEVEL_TERSE)
23474         break;
23475
23476       /* In the special case of a TYPE_DECL node representing the declaration
23477          of some type tag, if the given TYPE_DECL is marked as having been
23478          instantiated from some other (original) TYPE_DECL node (e.g. one which
23479          was generated within the original definition of an inline function) we
23480          used to generate a special (abbreviated) DW_TAG_structure_type,
23481          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
23482          should be actually referencing those DIEs, as variable DIEs with that
23483          type would be emitted already in the abstract origin, so it was always
23484          removed during unused type prunning.  Don't add anything in this
23485          case.  */
23486       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23487         break;
23488
23489       if (is_redundant_typedef (decl))
23490         gen_type_die (TREE_TYPE (decl), context_die);
23491       else
23492         /* Output a DIE to represent the typedef itself.  */
23493         gen_typedef_die (decl, context_die);
23494       break;
23495
23496     case LABEL_DECL:
23497       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23498         gen_label_die (decl, context_die);
23499       break;
23500
23501     case VAR_DECL:
23502     case RESULT_DECL:
23503       /* If we are in terse mode, don't generate any DIEs to represent any
23504          variable declarations or definitions.  */
23505       if (debug_info_level <= DINFO_LEVEL_TERSE)
23506         break;
23507
23508       /* Output any DIEs that are needed to specify the type of this data
23509          object.  */
23510       if (decl_by_reference_p (decl_or_origin))
23511         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23512       else
23513         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23514
23515       /* And its containing type.  */
23516       class_origin = decl_class_context (decl_or_origin);
23517       if (class_origin != NULL_TREE)
23518         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23519
23520       /* And its containing namespace.  */
23521       context_die = declare_in_namespace (decl_or_origin, context_die);
23522
23523       /* Now output the DIE to represent the data object itself.  This gets
23524          complicated because of the possibility that the VAR_DECL really
23525          represents an inlined instance of a formal parameter for an inline
23526          function.  */
23527       ultimate_origin = decl_ultimate_origin (decl_or_origin);
23528       if (ultimate_origin != NULL_TREE
23529           && TREE_CODE (ultimate_origin) == PARM_DECL)
23530         gen_formal_parameter_die (decl, origin,
23531                                   true /* Emit name attribute.  */,
23532                                   context_die);
23533       else
23534         gen_variable_die (decl, origin, context_die);
23535       break;
23536
23537     case FIELD_DECL:
23538       gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23539       /* Ignore the nameless fields that are used to skip bits but handle C++
23540          anonymous unions and structs.  */
23541       if (DECL_NAME (decl) != NULL_TREE
23542           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23543           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23544         {
23545           gen_type_die (member_declared_type (decl), context_die);
23546           gen_field_die (decl, ctx, context_die);
23547         }
23548       break;
23549
23550     case PARM_DECL:
23551       if (DECL_BY_REFERENCE (decl_or_origin))
23552         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23553       else
23554         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23555       return gen_formal_parameter_die (decl, origin,
23556                                        true /* Emit name attribute.  */,
23557                                        context_die);
23558
23559     case NAMESPACE_DECL:
23560       if (dwarf_version >= 3 || !dwarf_strict)
23561         gen_namespace_die (decl, context_die);
23562       break;
23563
23564     case IMPORTED_DECL:
23565       dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23566                                            DECL_CONTEXT (decl), context_die);
23567       break;
23568
23569     case NAMELIST_DECL:
23570       gen_namelist_decl (DECL_NAME (decl), context_die,
23571                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
23572       break;
23573
23574     default:
23575       /* Probably some frontend-internal decl.  Assume we don't care.  */
23576       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23577       break;
23578     }
23579
23580   return NULL;
23581 }
23582 \f
23583 /* Output initial debug information for global DECL.  Called at the
23584    end of the parsing process.
23585
23586    This is the initial debug generation process.  As such, the DIEs
23587    generated may be incomplete.  A later debug generation pass
23588    (dwarf2out_late_global_decl) will augment the information generated
23589    in this pass (e.g., with complete location info).  */
23590
23591 static void
23592 dwarf2out_early_global_decl (tree decl)
23593 {
23594   set_early_dwarf s;
23595
23596   /* gen_decl_die() will set DECL_ABSTRACT because
23597      cgraph_function_possibly_inlined_p() returns true.  This is in
23598      turn will cause DW_AT_inline attributes to be set.
23599
23600      This happens because at early dwarf generation, there is no
23601      cgraph information, causing cgraph_function_possibly_inlined_p()
23602      to return true.  Trick cgraph_function_possibly_inlined_p()
23603      while we generate dwarf early.  */
23604   bool save = symtab->global_info_ready;
23605   symtab->global_info_ready = true;
23606
23607   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
23608      other DECLs and they can point to template types or other things
23609      that dwarf2out can't handle when done via dwarf2out_decl.  */
23610   if (TREE_CODE (decl) != TYPE_DECL
23611       && TREE_CODE (decl) != PARM_DECL)
23612     {
23613       tree save_fndecl = current_function_decl;
23614       if (TREE_CODE (decl) == FUNCTION_DECL)
23615         {
23616           /* No cfun means the symbol has no body, so there's nothing
23617              to emit.  */
23618           if (!DECL_STRUCT_FUNCTION (decl))
23619             goto early_decl_exit;
23620
23621           current_function_decl = decl;
23622         }
23623       dwarf2out_decl (decl);
23624       if (TREE_CODE (decl) == FUNCTION_DECL)
23625         current_function_decl = save_fndecl;
23626     }
23627  early_decl_exit:
23628   symtab->global_info_ready = save;
23629 }
23630
23631 /* Output debug information for global decl DECL.  Called from
23632    toplev.c after compilation proper has finished.  */
23633
23634 static void
23635 dwarf2out_late_global_decl (tree decl)
23636 {
23637   /* We have to generate early debug late for LTO.  */
23638   if (in_lto_p)
23639     dwarf2out_early_global_decl (decl);
23640
23641     /* Fill-in any location information we were unable to determine
23642        on the first pass.  */
23643   if (TREE_CODE (decl) == VAR_DECL
23644       && !POINTER_BOUNDS_P (decl))
23645     {
23646       dw_die_ref die = lookup_decl_die (decl);
23647       if (die)
23648         add_location_or_const_value_attribute (die, decl, false);
23649     }
23650 }
23651
23652 /* Output debug information for type decl DECL.  Called from toplev.c
23653    and from language front ends (to record built-in types).  */
23654 static void
23655 dwarf2out_type_decl (tree decl, int local)
23656 {
23657   if (!local)
23658     {
23659       set_early_dwarf s;
23660       dwarf2out_decl (decl);
23661     }
23662 }
23663
23664 /* Output debug information for imported module or decl DECL.
23665    NAME is non-NULL name in the lexical block if the decl has been renamed.
23666    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23667    that DECL belongs to.
23668    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
23669 static void
23670 dwarf2out_imported_module_or_decl_1 (tree decl,
23671                                      tree name,
23672                                      tree lexical_block,
23673                                      dw_die_ref lexical_block_die)
23674 {
23675   expanded_location xloc;
23676   dw_die_ref imported_die = NULL;
23677   dw_die_ref at_import_die;
23678
23679   if (TREE_CODE (decl) == IMPORTED_DECL)
23680     {
23681       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23682       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23683       gcc_assert (decl);
23684     }
23685   else
23686     xloc = expand_location (input_location);
23687
23688   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23689     {
23690       at_import_die = force_type_die (TREE_TYPE (decl));
23691       /* For namespace N { typedef void T; } using N::T; base_type_die
23692          returns NULL, but DW_TAG_imported_declaration requires
23693          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
23694       if (!at_import_die)
23695         {
23696           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23697           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23698           at_import_die = lookup_type_die (TREE_TYPE (decl));
23699           gcc_assert (at_import_die);
23700         }
23701     }
23702   else
23703     {
23704       at_import_die = lookup_decl_die (decl);
23705       if (!at_import_die)
23706         {
23707           /* If we're trying to avoid duplicate debug info, we may not have
23708              emitted the member decl for this field.  Emit it now.  */
23709           if (TREE_CODE (decl) == FIELD_DECL)
23710             {
23711               tree type = DECL_CONTEXT (decl);
23712
23713               if (TYPE_CONTEXT (type)
23714                   && TYPE_P (TYPE_CONTEXT (type))
23715                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
23716                                                 DINFO_USAGE_DIR_USE))
23717                 return;
23718               gen_type_die_for_member (type, decl,
23719                                        get_context_die (TYPE_CONTEXT (type)));
23720             }
23721           if (TREE_CODE (decl) == NAMELIST_DECL)
23722             at_import_die = gen_namelist_decl (DECL_NAME (decl),
23723                                          get_context_die (DECL_CONTEXT (decl)),
23724                                          NULL_TREE);
23725           else
23726             at_import_die = force_decl_die (decl);
23727         }
23728     }
23729
23730   if (TREE_CODE (decl) == NAMESPACE_DECL)
23731     {
23732       if (dwarf_version >= 3 || !dwarf_strict)
23733         imported_die = new_die (DW_TAG_imported_module,
23734                                 lexical_block_die,
23735                                 lexical_block);
23736       else
23737         return;
23738     }
23739   else
23740     imported_die = new_die (DW_TAG_imported_declaration,
23741                             lexical_block_die,
23742                             lexical_block);
23743
23744   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23745   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23746   if (name)
23747     add_AT_string (imported_die, DW_AT_name,
23748                    IDENTIFIER_POINTER (name));
23749   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23750 }
23751
23752 /* Output debug information for imported module or decl DECL.
23753    NAME is non-NULL name in context if the decl has been renamed.
23754    CHILD is true if decl is one of the renamed decls as part of
23755    importing whole module.  */
23756
23757 static void
23758 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
23759                                    bool child)
23760 {
23761   /* dw_die_ref at_import_die;  */
23762   dw_die_ref scope_die;
23763
23764   if (debug_info_level <= DINFO_LEVEL_TERSE)
23765     return;
23766
23767   gcc_assert (decl);
23768
23769   set_early_dwarf s;
23770
23771   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
23772      We need decl DIE for reference and scope die. First, get DIE for the decl
23773      itself.  */
23774
23775   /* Get the scope die for decl context. Use comp_unit_die for global module
23776      or decl. If die is not found for non globals, force new die.  */
23777   if (context
23778       && TYPE_P (context)
23779       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
23780     return;
23781
23782   if (!(dwarf_version >= 3 || !dwarf_strict))
23783     return;
23784
23785   scope_die = get_context_die (context);
23786
23787   if (child)
23788     {
23789       gcc_assert (scope_die->die_child);
23790       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
23791       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
23792       scope_die = scope_die->die_child;
23793     }
23794
23795   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
23796   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
23797 }
23798
23799 /* Output debug information for namelists.   */
23800
23801 static dw_die_ref
23802 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
23803 {
23804   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
23805   tree value;
23806   unsigned i;
23807
23808   if (debug_info_level <= DINFO_LEVEL_TERSE)
23809     return NULL;
23810
23811   gcc_assert (scope_die != NULL);
23812   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
23813   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
23814
23815   /* If there are no item_decls, we have a nondefining namelist, e.g.
23816      with USE association; hence, set DW_AT_declaration.  */
23817   if (item_decls == NULL_TREE)
23818     {
23819       add_AT_flag (nml_die, DW_AT_declaration, 1);
23820       return nml_die;
23821     }
23822
23823   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
23824     {
23825       nml_item_ref_die = lookup_decl_die (value);
23826       if (!nml_item_ref_die)
23827         nml_item_ref_die = force_decl_die (value);
23828
23829       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
23830       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
23831     }
23832   return nml_die;
23833 }
23834
23835
23836 /* Write the debugging output for DECL and return the DIE.  */
23837
23838 static void
23839 dwarf2out_decl (tree decl)
23840 {
23841   dw_die_ref context_die = comp_unit_die ();
23842
23843   switch (TREE_CODE (decl))
23844     {
23845     case ERROR_MARK:
23846       return;
23847
23848     case FUNCTION_DECL:
23849       /* What we would really like to do here is to filter out all mere
23850          file-scope declarations of file-scope functions which are never
23851          referenced later within this translation unit (and keep all of ones
23852          that *are* referenced later on) but we aren't clairvoyant, so we have
23853          no idea which functions will be referenced in the future (i.e. later
23854          on within the current translation unit). So here we just ignore all
23855          file-scope function declarations which are not also definitions.  If
23856          and when the debugger needs to know something about these functions,
23857          it will have to hunt around and find the DWARF information associated
23858          with the definition of the function.
23859
23860          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
23861          nodes represent definitions and which ones represent mere
23862          declarations.  We have to check DECL_INITIAL instead. That's because
23863          the C front-end supports some weird semantics for "extern inline"
23864          function definitions.  These can get inlined within the current
23865          translation unit (and thus, we need to generate Dwarf info for their
23866          abstract instances so that the Dwarf info for the concrete inlined
23867          instances can have something to refer to) but the compiler never
23868          generates any out-of-lines instances of such things (despite the fact
23869          that they *are* definitions).
23870
23871          The important point is that the C front-end marks these "extern
23872          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
23873          them anyway. Note that the C++ front-end also plays some similar games
23874          for inline function definitions appearing within include files which
23875          also contain `#pragma interface' pragmas.
23876
23877          If we are called from dwarf2out_abstract_function output a DIE
23878          anyway.  We can end up here this way with early inlining and LTO
23879          where the inlined function is output in a different LTRANS unit
23880          or not at all.  */
23881       if (DECL_INITIAL (decl) == NULL_TREE
23882           && ! DECL_ABSTRACT_P (decl))
23883         return;
23884
23885       /* If we're a nested function, initially use a parent of NULL; if we're
23886          a plain function, this will be fixed up in decls_for_scope.  If
23887          we're a method, it will be ignored, since we already have a DIE.  */
23888       if (decl_function_context (decl)
23889           /* But if we're in terse mode, we don't care about scope.  */
23890           && debug_info_level > DINFO_LEVEL_TERSE)
23891         context_die = NULL;
23892       break;
23893
23894     case VAR_DECL:
23895       /* For local statics lookup proper context die.  */
23896       if (local_function_static (decl))
23897         context_die = lookup_decl_die (DECL_CONTEXT (decl));
23898
23899       /* If we are in terse mode, don't generate any DIEs to represent any
23900          variable declarations or definitions.  */
23901       if (debug_info_level <= DINFO_LEVEL_TERSE)
23902         return;
23903       break;
23904
23905     case CONST_DECL:
23906       if (debug_info_level <= DINFO_LEVEL_TERSE)
23907         return;
23908       if (!is_fortran () && !is_ada ())
23909         return;
23910       if (TREE_STATIC (decl) && decl_function_context (decl))
23911         context_die = lookup_decl_die (DECL_CONTEXT (decl));
23912       break;
23913
23914     case NAMESPACE_DECL:
23915     case IMPORTED_DECL:
23916       if (debug_info_level <= DINFO_LEVEL_TERSE)
23917         return;
23918       if (lookup_decl_die (decl) != NULL)
23919         return;
23920       break;
23921
23922     case TYPE_DECL:
23923       /* Don't emit stubs for types unless they are needed by other DIEs.  */
23924       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
23925         return;
23926
23927       /* Don't bother trying to generate any DIEs to represent any of the
23928          normal built-in types for the language we are compiling.  */
23929       if (DECL_IS_BUILTIN (decl))
23930         return;
23931
23932       /* If we are in terse mode, don't generate any DIEs for types.  */
23933       if (debug_info_level <= DINFO_LEVEL_TERSE)
23934         return;
23935
23936       /* If we're a function-scope tag, initially use a parent of NULL;
23937          this will be fixed up in decls_for_scope.  */
23938       if (decl_function_context (decl))
23939         context_die = NULL;
23940
23941       break;
23942
23943     case NAMELIST_DECL:
23944       break;
23945
23946     default:
23947       return;
23948     }
23949
23950   gen_decl_die (decl, NULL, NULL, context_die);
23951
23952   if (flag_checking)
23953     {
23954       dw_die_ref die = lookup_decl_die (decl);
23955       if (die)
23956         check_die (die);
23957     }
23958 }
23959
23960 /* Write the debugging output for DECL.  */
23961
23962 static void
23963 dwarf2out_function_decl (tree decl)
23964 {
23965   dwarf2out_decl (decl);
23966   call_arg_locations = NULL;
23967   call_arg_loc_last = NULL;
23968   call_site_count = -1;
23969   tail_call_site_count = -1;
23970   decl_loc_table->empty ();
23971   cached_dw_loc_list_table->empty ();
23972 }
23973
23974 /* Output a marker (i.e. a label) for the beginning of the generated code for
23975    a lexical block.  */
23976
23977 static void
23978 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
23979                        unsigned int blocknum)
23980 {
23981   switch_to_section (current_function_section ());
23982   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
23983 }
23984
23985 /* Output a marker (i.e. a label) for the end of the generated code for a
23986    lexical block.  */
23987
23988 static void
23989 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
23990 {
23991   switch_to_section (current_function_section ());
23992   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
23993 }
23994
23995 /* Returns nonzero if it is appropriate not to emit any debugging
23996    information for BLOCK, because it doesn't contain any instructions.
23997
23998    Don't allow this for blocks with nested functions or local classes
23999    as we would end up with orphans, and in the presence of scheduling
24000    we may end up calling them anyway.  */
24001
24002 static bool
24003 dwarf2out_ignore_block (const_tree block)
24004 {
24005   tree decl;
24006   unsigned int i;
24007
24008   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24009     if (TREE_CODE (decl) == FUNCTION_DECL
24010         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24011       return 0;
24012   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24013     {
24014       decl = BLOCK_NONLOCALIZED_VAR (block, i);
24015       if (TREE_CODE (decl) == FUNCTION_DECL
24016           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24017       return 0;
24018     }
24019
24020   return 1;
24021 }
24022
24023 /* Hash table routines for file_hash.  */
24024
24025 bool
24026 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24027 {
24028   return filename_cmp (p1->filename, p2) == 0;
24029 }
24030
24031 hashval_t
24032 dwarf_file_hasher::hash (dwarf_file_data *p)
24033 {
24034   return htab_hash_string (p->filename);
24035 }
24036
24037 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24038    dwarf2out.c) and return its "index".  The index of each (known) filename is
24039    just a unique number which is associated with only that one filename.  We
24040    need such numbers for the sake of generating labels (in the .debug_sfnames
24041    section) and references to those files numbers (in the .debug_srcinfo
24042    and .debug_macinfo sections).  If the filename given as an argument is not
24043    found in our current list, add it to the list and assign it the next
24044    available unique index number.  */
24045
24046 static struct dwarf_file_data *
24047 lookup_filename (const char *file_name)
24048 {
24049   struct dwarf_file_data * created;
24050
24051   if (!file_name)
24052     return NULL;
24053
24054   dwarf_file_data **slot
24055     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24056                                        INSERT);
24057   if (*slot)
24058     return *slot;
24059
24060   created = ggc_alloc<dwarf_file_data> ();
24061   created->filename = file_name;
24062   created->emitted_number = 0;
24063   *slot = created;
24064   return created;
24065 }
24066
24067 /* If the assembler will construct the file table, then translate the compiler
24068    internal file table number into the assembler file table number, and emit
24069    a .file directive if we haven't already emitted one yet.  The file table
24070    numbers are different because we prune debug info for unused variables and
24071    types, which may include filenames.  */
24072
24073 static int
24074 maybe_emit_file (struct dwarf_file_data * fd)
24075 {
24076   if (! fd->emitted_number)
24077     {
24078       if (last_emitted_file)
24079         fd->emitted_number = last_emitted_file->emitted_number + 1;
24080       else
24081         fd->emitted_number = 1;
24082       last_emitted_file = fd;
24083
24084       if (DWARF2_ASM_LINE_DEBUG_INFO)
24085         {
24086           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24087           output_quoted_string (asm_out_file,
24088                                 remap_debug_filename (fd->filename));
24089           fputc ('\n', asm_out_file);
24090         }
24091     }
24092
24093   return fd->emitted_number;
24094 }
24095
24096 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24097    That generation should happen after function debug info has been
24098    generated. The value of the attribute is the constant value of ARG.  */
24099
24100 static void
24101 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24102 {
24103   die_arg_entry entry;
24104
24105   if (!die || !arg)
24106     return;
24107
24108   gcc_assert (early_dwarf);
24109
24110   if (!tmpl_value_parm_die_table)
24111     vec_alloc (tmpl_value_parm_die_table, 32);
24112
24113   entry.die = die;
24114   entry.arg = arg;
24115   vec_safe_push (tmpl_value_parm_die_table, entry);
24116 }
24117
24118 /* Return TRUE if T is an instance of generic type, FALSE
24119    otherwise.  */
24120
24121 static bool
24122 generic_type_p (tree t)
24123 {
24124   if (t == NULL_TREE || !TYPE_P (t))
24125     return false;
24126   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24127 }
24128
24129 /* Schedule the generation of the generic parameter dies for the
24130   instance of generic type T. The proper generation itself is later
24131   done by gen_scheduled_generic_parms_dies. */
24132
24133 static void
24134 schedule_generic_params_dies_gen (tree t)
24135 {
24136   if (!generic_type_p (t))
24137     return;
24138
24139   gcc_assert (early_dwarf);
24140
24141   if (!generic_type_instances)
24142     vec_alloc (generic_type_instances, 256);
24143
24144   vec_safe_push (generic_type_instances, t);
24145 }
24146
24147 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24148    by append_entry_to_tmpl_value_parm_die_table. This function must
24149    be called after function DIEs have been generated.  */
24150
24151 static void
24152 gen_remaining_tmpl_value_param_die_attribute (void)
24153 {
24154   if (tmpl_value_parm_die_table)
24155     {
24156       unsigned i, j;
24157       die_arg_entry *e;
24158
24159       /* We do this in two phases - first get the cases we can
24160          handle during early-finish, preserving those we cannot
24161          (containing symbolic constants where we don't yet know
24162          whether we are going to output the referenced symbols).
24163          For those we try again at late-finish.  */
24164       j = 0;
24165       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24166         {
24167           if (!tree_add_const_value_attribute (e->die, e->arg))
24168             (*tmpl_value_parm_die_table)[j++] = *e;
24169         }
24170       tmpl_value_parm_die_table->truncate (j);
24171     }
24172 }
24173
24174 /* Generate generic parameters DIEs for instances of generic types
24175    that have been previously scheduled by
24176    schedule_generic_params_dies_gen. This function must be called
24177    after all the types of the CU have been laid out.  */
24178
24179 static void
24180 gen_scheduled_generic_parms_dies (void)
24181 {
24182   unsigned i;
24183   tree t;
24184
24185   if (!generic_type_instances)
24186     return;
24187   
24188   /* We end up "recursing" into schedule_generic_params_dies_gen, so
24189      pretend this generation is part of "early dwarf" as well.  */
24190   set_early_dwarf s;
24191
24192   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24193     if (COMPLETE_TYPE_P (t))
24194       gen_generic_params_dies (t);
24195
24196   generic_type_instances = NULL;
24197 }
24198
24199
24200 /* Replace DW_AT_name for the decl with name.  */
24201
24202 static void
24203 dwarf2out_set_name (tree decl, tree name)
24204 {
24205   dw_die_ref die;
24206   dw_attr_node *attr;
24207   const char *dname;
24208
24209   die = TYPE_SYMTAB_DIE (decl);
24210   if (!die)
24211     return;
24212
24213   dname = dwarf2_name (name, 0);
24214   if (!dname)
24215     return;
24216
24217   attr = get_AT (die, DW_AT_name);
24218   if (attr)
24219     {
24220       struct indirect_string_node *node;
24221
24222       node = find_AT_string (dname);
24223       /* replace the string.  */
24224       attr->dw_attr_val.v.val_str = node;
24225     }
24226
24227   else
24228     add_name_attribute (die, dname);
24229 }
24230
24231 /* True if before or during processing of the first function being emitted.  */
24232 static bool in_first_function_p = true;
24233 /* True if loc_note during dwarf2out_var_location call might still be
24234    before first real instruction at address equal to .Ltext0.  */
24235 static bool maybe_at_text_label_p = true;
24236 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
24237 static unsigned int first_loclabel_num_not_at_text_label;
24238
24239 /* Called by the final INSN scan whenever we see a var location.  We
24240    use it to drop labels in the right places, and throw the location in
24241    our lookup table.  */
24242
24243 static void
24244 dwarf2out_var_location (rtx_insn *loc_note)
24245 {
24246   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24247   struct var_loc_node *newloc;
24248   rtx_insn *next_real, *next_note;
24249   rtx_insn *call_insn = NULL;
24250   static const char *last_label;
24251   static const char *last_postcall_label;
24252   static bool last_in_cold_section_p;
24253   static rtx_insn *expected_next_loc_note;
24254   tree decl;
24255   bool var_loc_p;
24256
24257   if (!NOTE_P (loc_note))
24258     {
24259       if (CALL_P (loc_note))
24260         {
24261           call_site_count++;
24262           if (SIBLING_CALL_P (loc_note))
24263             tail_call_site_count++;
24264           if (optimize == 0 && !flag_var_tracking)
24265             {
24266               /* When the var-tracking pass is not running, there is no note
24267                  for indirect calls whose target is compile-time known. In this
24268                  case, process such calls specifically so that we generate call
24269                  sites for them anyway.  */
24270               rtx x = PATTERN (loc_note);
24271               if (GET_CODE (x) == PARALLEL)
24272                 x = XVECEXP (x, 0, 0);
24273               if (GET_CODE (x) == SET)
24274                 x = SET_SRC (x);
24275               if (GET_CODE (x) == CALL)
24276                 x = XEXP (x, 0);
24277               if (!MEM_P (x)
24278                   || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24279                   || !SYMBOL_REF_DECL (XEXP (x, 0))
24280                   || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24281                       != FUNCTION_DECL))
24282                 {
24283                   call_insn = loc_note;
24284                   loc_note = NULL;
24285                   var_loc_p = false;
24286
24287                   next_real = next_real_insn (call_insn);
24288                   next_note = NULL;
24289                   cached_next_real_insn = NULL;
24290                   goto create_label;
24291                 }
24292             }
24293         }
24294       return;
24295     }
24296
24297   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24298   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24299     return;
24300
24301   /* Optimize processing a large consecutive sequence of location
24302      notes so we don't spend too much time in next_real_insn.  If the
24303      next insn is another location note, remember the next_real_insn
24304      calculation for next time.  */
24305   next_real = cached_next_real_insn;
24306   if (next_real)
24307     {
24308       if (expected_next_loc_note != loc_note)
24309         next_real = NULL;
24310     }
24311
24312   next_note = NEXT_INSN (loc_note);
24313   if (! next_note
24314       || next_note->deleted ()
24315       || ! NOTE_P (next_note)
24316       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24317           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24318     next_note = NULL;
24319
24320   if (! next_real)
24321     next_real = next_real_insn (loc_note);
24322
24323   if (next_note)
24324     {
24325       expected_next_loc_note = next_note;
24326       cached_next_real_insn = next_real;
24327     }
24328   else
24329     cached_next_real_insn = NULL;
24330
24331   /* If there are no instructions which would be affected by this note,
24332      don't do anything.  */
24333   if (var_loc_p
24334       && next_real == NULL_RTX
24335       && !NOTE_DURING_CALL_P (loc_note))
24336     return;
24337
24338 create_label:
24339
24340   if (next_real == NULL_RTX)
24341     next_real = get_last_insn ();
24342
24343   /* If there were any real insns between note we processed last time
24344      and this note (or if it is the first note), clear
24345      last_{,postcall_}label so that they are not reused this time.  */
24346   if (last_var_location_insn == NULL_RTX
24347       || last_var_location_insn != next_real
24348       || last_in_cold_section_p != in_cold_section_p)
24349     {
24350       last_label = NULL;
24351       last_postcall_label = NULL;
24352     }
24353
24354   if (var_loc_p)
24355     {
24356       decl = NOTE_VAR_LOCATION_DECL (loc_note);
24357       newloc = add_var_loc_to_decl (decl, loc_note,
24358                                     NOTE_DURING_CALL_P (loc_note)
24359                                     ? last_postcall_label : last_label);
24360       if (newloc == NULL)
24361         return;
24362     }
24363   else
24364     {
24365       decl = NULL_TREE;
24366       newloc = NULL;
24367     }
24368
24369   /* If there were no real insns between note we processed last time
24370      and this note, use the label we emitted last time.  Otherwise
24371      create a new label and emit it.  */
24372   if (last_label == NULL)
24373     {
24374       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24375       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24376       loclabel_num++;
24377       last_label = ggc_strdup (loclabel);
24378       /* See if loclabel might be equal to .Ltext0.  If yes,
24379          bump first_loclabel_num_not_at_text_label.  */
24380       if (!have_multiple_function_sections
24381           && in_first_function_p
24382           && maybe_at_text_label_p)
24383         {
24384           static rtx_insn *last_start;
24385           rtx_insn *insn;
24386           for (insn = loc_note; insn; insn = previous_insn (insn))
24387             if (insn == last_start)
24388               break;
24389             else if (!NONDEBUG_INSN_P (insn))
24390               continue;
24391             else
24392               {
24393                 rtx body = PATTERN (insn);
24394                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24395                   continue;
24396                 /* Inline asm could occupy zero bytes.  */
24397                 else if (GET_CODE (body) == ASM_INPUT
24398                          || asm_noperands (body) >= 0)
24399                   continue;
24400 #ifdef HAVE_attr_length
24401                 else if (get_attr_min_length (insn) == 0)
24402                   continue;
24403 #endif
24404                 else
24405                   {
24406                     /* Assume insn has non-zero length.  */
24407                     maybe_at_text_label_p = false;
24408                     break;
24409                   }
24410               }
24411           if (maybe_at_text_label_p)
24412             {
24413               last_start = loc_note;
24414               first_loclabel_num_not_at_text_label = loclabel_num;
24415             }
24416         }
24417     }
24418
24419   gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24420               || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24421
24422   if (!var_loc_p)
24423     {
24424       struct call_arg_loc_node *ca_loc
24425         = ggc_cleared_alloc<call_arg_loc_node> ();
24426       rtx_insn *prev
24427         = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24428
24429       ca_loc->call_arg_loc_note = loc_note;
24430       ca_loc->next = NULL;
24431       ca_loc->label = last_label;
24432       gcc_assert (prev
24433                   && (CALL_P (prev)
24434                       || (NONJUMP_INSN_P (prev)
24435                           && GET_CODE (PATTERN (prev)) == SEQUENCE
24436                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24437       if (!CALL_P (prev))
24438         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24439       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24440
24441       /* Look for a SYMBOL_REF in the "prev" instruction.  */
24442       rtx x = get_call_rtx_from (PATTERN (prev));
24443       if (x)
24444         {
24445           /* Try to get the call symbol, if any.  */
24446           if (MEM_P (XEXP (x, 0)))
24447             x = XEXP (x, 0);
24448           /* First, look for a memory access to a symbol_ref.  */
24449           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24450               && SYMBOL_REF_DECL (XEXP (x, 0))
24451               && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24452             ca_loc->symbol_ref = XEXP (x, 0);
24453           /* Otherwise, look at a compile-time known user-level function
24454              declaration.  */
24455           else if (MEM_P (x)
24456                    && MEM_EXPR (x)
24457                    && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24458             ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24459         }
24460
24461       ca_loc->block = insn_scope (prev);
24462       if (call_arg_locations)
24463         call_arg_loc_last->next = ca_loc;
24464       else
24465         call_arg_locations = ca_loc;
24466       call_arg_loc_last = ca_loc;
24467     }
24468   else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24469     newloc->label = last_label;
24470   else
24471     {
24472       if (!last_postcall_label)
24473         {
24474           sprintf (loclabel, "%s-1", last_label);
24475           last_postcall_label = ggc_strdup (loclabel);
24476         }
24477       newloc->label = last_postcall_label;
24478     }
24479
24480   last_var_location_insn = next_real;
24481   last_in_cold_section_p = in_cold_section_p;
24482 }
24483
24484 /* Called from finalize_size_functions for size functions so that their body
24485    can be encoded in the debug info to describe the layout of variable-length
24486    structures.  */
24487
24488 static void
24489 dwarf2out_size_function (tree decl)
24490 {
24491   function_to_dwarf_procedure (decl);
24492 }
24493
24494 /* Note in one location list that text section has changed.  */
24495
24496 int
24497 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24498 {
24499   var_loc_list *list = *slot;
24500   if (list->first)
24501     list->last_before_switch
24502       = list->last->next ? list->last->next : list->last;
24503   return 1;
24504 }
24505
24506 /* Note in all location lists that text section has changed.  */
24507
24508 static void
24509 var_location_switch_text_section (void)
24510 {
24511   if (decl_loc_table == NULL)
24512     return;
24513
24514   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24515 }
24516
24517 /* Create a new line number table.  */
24518
24519 static dw_line_info_table *
24520 new_line_info_table (void)
24521 {
24522   dw_line_info_table *table;
24523
24524   table = ggc_cleared_alloc<dw_line_info_table> ();
24525   table->file_num = 1;
24526   table->line_num = 1;
24527   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24528
24529   return table;
24530 }
24531
24532 /* Lookup the "current" table into which we emit line info, so
24533    that we don't have to do it for every source line.  */
24534
24535 static void
24536 set_cur_line_info_table (section *sec)
24537 {
24538   dw_line_info_table *table;
24539
24540   if (sec == text_section)
24541     table = text_section_line_info;
24542   else if (sec == cold_text_section)
24543     {
24544       table = cold_text_section_line_info;
24545       if (!table)
24546         {
24547           cold_text_section_line_info = table = new_line_info_table ();
24548           table->end_label = cold_end_label;
24549         }
24550     }
24551   else
24552     {
24553       const char *end_label;
24554
24555       if (flag_reorder_blocks_and_partition)
24556         {
24557           if (in_cold_section_p)
24558             end_label = crtl->subsections.cold_section_end_label;
24559           else
24560             end_label = crtl->subsections.hot_section_end_label;
24561         }
24562       else
24563         {
24564           char label[MAX_ARTIFICIAL_LABEL_BYTES];
24565           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24566                                        current_function_funcdef_no);
24567           end_label = ggc_strdup (label);
24568         }
24569
24570       table = new_line_info_table ();
24571       table->end_label = end_label;
24572
24573       vec_safe_push (separate_line_info, table);
24574     }
24575
24576   if (DWARF2_ASM_LINE_DEBUG_INFO)
24577     table->is_stmt = (cur_line_info_table
24578                       ? cur_line_info_table->is_stmt
24579                       : DWARF_LINE_DEFAULT_IS_STMT_START);
24580   cur_line_info_table = table;
24581 }
24582
24583
24584 /* We need to reset the locations at the beginning of each
24585    function. We can't do this in the end_function hook, because the
24586    declarations that use the locations won't have been output when
24587    that hook is called.  Also compute have_multiple_function_sections here.  */
24588
24589 static void
24590 dwarf2out_begin_function (tree fun)
24591 {
24592   section *sec = function_section (fun);
24593
24594   if (sec != text_section)
24595     have_multiple_function_sections = true;
24596
24597   if (flag_reorder_blocks_and_partition && !cold_text_section)
24598     {
24599       gcc_assert (current_function_decl == fun);
24600       cold_text_section = unlikely_text_section ();
24601       switch_to_section (cold_text_section);
24602       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24603       switch_to_section (sec);
24604     }
24605
24606   dwarf2out_note_section_used ();
24607   call_site_count = 0;
24608   tail_call_site_count = 0;
24609
24610   set_cur_line_info_table (sec);
24611 }
24612
24613 /* Helper function of dwarf2out_end_function, called only after emitting
24614    the very first function into assembly.  Check if some .debug_loc range
24615    might end with a .LVL* label that could be equal to .Ltext0.
24616    In that case we must force using absolute addresses in .debug_loc ranges,
24617    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24618    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24619    list terminator.
24620    Set have_multiple_function_sections to true in that case and
24621    terminate htab traversal.  */
24622
24623 int
24624 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24625 {
24626   var_loc_list *entry = *slot;
24627   struct var_loc_node *node;
24628
24629   node = entry->first;
24630   if (node && node->next && node->next->label)
24631     {
24632       unsigned int i;
24633       const char *label = node->next->label;
24634       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24635
24636       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24637         {
24638           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24639           if (strcmp (label, loclabel) == 0)
24640             {
24641               have_multiple_function_sections = true;
24642               return 0;
24643             }
24644         }
24645     }
24646   return 1;
24647 }
24648
24649 /* Hook called after emitting a function into assembly.
24650    This does something only for the very first function emitted.  */
24651
24652 static void
24653 dwarf2out_end_function (unsigned int)
24654 {
24655   if (in_first_function_p
24656       && !have_multiple_function_sections
24657       && first_loclabel_num_not_at_text_label
24658       && decl_loc_table)
24659     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24660   in_first_function_p = false;
24661   maybe_at_text_label_p = false;
24662 }
24663
24664 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
24665    front-ends register a translation unit even before dwarf2out_init is
24666    called.  */
24667 static tree main_translation_unit = NULL_TREE;
24668
24669 /* Hook called by front-ends after they built their main translation unit.
24670    Associate comp_unit_die to UNIT.  */
24671
24672 static void
24673 dwarf2out_register_main_translation_unit (tree unit)
24674 {
24675   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24676               && main_translation_unit == NULL_TREE);
24677   main_translation_unit = unit;
24678   /* If dwarf2out_init has not been called yet, it will perform the association
24679      itself looking at main_translation_unit.  */
24680   if (decl_die_table != NULL)
24681     equate_decl_number_to_die (unit, comp_unit_die ());
24682 }
24683
24684 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
24685
24686 static void
24687 push_dw_line_info_entry (dw_line_info_table *table,
24688                          enum dw_line_info_opcode opcode, unsigned int val)
24689 {
24690   dw_line_info_entry e;
24691   e.opcode = opcode;
24692   e.val = val;
24693   vec_safe_push (table->entries, e);
24694 }
24695
24696 /* Output a label to mark the beginning of a source code line entry
24697    and record information relating to this source line, in
24698    'line_info_table' for later output of the .debug_line section.  */
24699 /* ??? The discriminator parameter ought to be unsigned.  */
24700
24701 static void
24702 dwarf2out_source_line (unsigned int line, const char *filename,
24703                        int discriminator, bool is_stmt)
24704 {
24705   unsigned int file_num;
24706   dw_line_info_table *table;
24707
24708   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24709     return;
24710
24711   /* The discriminator column was added in dwarf4.  Simplify the below
24712      by simply removing it if we're not supposed to output it.  */
24713   if (dwarf_version < 4 && dwarf_strict)
24714     discriminator = 0;
24715
24716   table = cur_line_info_table;
24717   file_num = maybe_emit_file (lookup_filename (filename));
24718
24719   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
24720      the debugger has used the second (possibly duplicate) line number
24721      at the beginning of the function to mark the end of the prologue.
24722      We could eliminate any other duplicates within the function.  For
24723      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24724      that second line number entry.  */
24725   /* Recall that this end-of-prologue indication is *not* the same thing
24726      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
24727      to which the hook corresponds, follows the last insn that was 
24728      emitted by gen_prologue.  What we need is to precede the first insn
24729      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24730      insn that corresponds to something the user wrote.  These may be
24731      very different locations once scheduling is enabled.  */
24732
24733   if (0 && file_num == table->file_num
24734       && line == table->line_num
24735       && discriminator == table->discrim_num
24736       && is_stmt == table->is_stmt)
24737     return;
24738
24739   switch_to_section (current_function_section ());
24740
24741   /* If requested, emit something human-readable.  */
24742   if (flag_debug_asm)
24743     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24744
24745   if (DWARF2_ASM_LINE_DEBUG_INFO)
24746     {
24747       /* Emit the .loc directive understood by GNU as.  */
24748       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24749          file_num, line, is_stmt, discriminator */
24750       fputs ("\t.loc ", asm_out_file);
24751       fprint_ul (asm_out_file, file_num);
24752       putc (' ', asm_out_file);
24753       fprint_ul (asm_out_file, line);
24754       putc (' ', asm_out_file);
24755       putc ('0', asm_out_file);
24756
24757       if (is_stmt != table->is_stmt)
24758         {
24759           fputs (" is_stmt ", asm_out_file);
24760           putc (is_stmt ? '1' : '0', asm_out_file);
24761         }
24762       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
24763         {
24764           gcc_assert (discriminator > 0);
24765           fputs (" discriminator ", asm_out_file);
24766           fprint_ul (asm_out_file, (unsigned long) discriminator);
24767         }
24768       putc ('\n', asm_out_file);
24769     }
24770   else
24771     {
24772       unsigned int label_num = ++line_info_label_num;
24773
24774       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
24775
24776       push_dw_line_info_entry (table, LI_set_address, label_num);
24777       if (file_num != table->file_num)
24778         push_dw_line_info_entry (table, LI_set_file, file_num);
24779       if (discriminator != table->discrim_num)
24780         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
24781       if (is_stmt != table->is_stmt)
24782         push_dw_line_info_entry (table, LI_negate_stmt, 0);
24783       push_dw_line_info_entry (table, LI_set_line, line);
24784     }
24785
24786   table->file_num = file_num;
24787   table->line_num = line;
24788   table->discrim_num = discriminator;
24789   table->is_stmt = is_stmt;
24790   table->in_use = true;
24791 }
24792
24793 /* Record the beginning of a new source file.  */
24794
24795 static void
24796 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
24797 {
24798   if (flag_eliminate_dwarf2_dups)
24799     {
24800       /* Record the beginning of the file for break_out_includes.  */
24801       dw_die_ref bincl_die;
24802
24803       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
24804       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
24805     }
24806
24807   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24808     {
24809       macinfo_entry e;
24810       e.code = DW_MACINFO_start_file;
24811       e.lineno = lineno;
24812       e.info = ggc_strdup (filename);
24813       vec_safe_push (macinfo_table, e);
24814     }
24815 }
24816
24817 /* Record the end of a source file.  */
24818
24819 static void
24820 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
24821 {
24822   if (flag_eliminate_dwarf2_dups)
24823     /* Record the end of the file for break_out_includes.  */
24824     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
24825
24826   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24827     {
24828       macinfo_entry e;
24829       e.code = DW_MACINFO_end_file;
24830       e.lineno = lineno;
24831       e.info = NULL;
24832       vec_safe_push (macinfo_table, e);
24833     }
24834 }
24835
24836 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
24837    the tail part of the directive line, i.e. the part which is past the
24838    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24839
24840 static void
24841 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
24842                   const char *buffer ATTRIBUTE_UNUSED)
24843 {
24844   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24845     {
24846       macinfo_entry e;
24847       /* Insert a dummy first entry to be able to optimize the whole
24848          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24849       if (macinfo_table->is_empty () && lineno <= 1)
24850         {
24851           e.code = 0;
24852           e.lineno = 0;
24853           e.info = NULL;
24854           vec_safe_push (macinfo_table, e);
24855         }
24856       e.code = DW_MACINFO_define;
24857       e.lineno = lineno;
24858       e.info = ggc_strdup (buffer);
24859       vec_safe_push (macinfo_table, e);
24860     }
24861 }
24862
24863 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
24864    the tail part of the directive line, i.e. the part which is past the
24865    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24866
24867 static void
24868 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
24869                  const char *buffer ATTRIBUTE_UNUSED)
24870 {
24871   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24872     {
24873       macinfo_entry e;
24874       /* Insert a dummy first entry to be able to optimize the whole
24875          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24876       if (macinfo_table->is_empty () && lineno <= 1)
24877         {
24878           e.code = 0;
24879           e.lineno = 0;
24880           e.info = NULL;
24881           vec_safe_push (macinfo_table, e);
24882         }
24883       e.code = DW_MACINFO_undef;
24884       e.lineno = lineno;
24885       e.info = ggc_strdup (buffer);
24886       vec_safe_push (macinfo_table, e);
24887     }
24888 }
24889
24890 /* Helpers to manipulate hash table of CUs.  */
24891
24892 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
24893 {
24894   static inline hashval_t hash (const macinfo_entry *);
24895   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
24896 };
24897
24898 inline hashval_t
24899 macinfo_entry_hasher::hash (const macinfo_entry *entry)
24900 {
24901   return htab_hash_string (entry->info);
24902 }
24903
24904 inline bool
24905 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
24906                              const macinfo_entry *entry2)
24907 {
24908   return !strcmp (entry1->info, entry2->info);
24909 }
24910
24911 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
24912
24913 /* Output a single .debug_macinfo entry.  */
24914
24915 static void
24916 output_macinfo_op (macinfo_entry *ref)
24917 {
24918   int file_num;
24919   size_t len;
24920   struct indirect_string_node *node;
24921   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24922   struct dwarf_file_data *fd;
24923
24924   switch (ref->code)
24925     {
24926     case DW_MACINFO_start_file:
24927       fd = lookup_filename (ref->info);
24928       file_num = maybe_emit_file (fd);
24929       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
24930       dw2_asm_output_data_uleb128 (ref->lineno,
24931                                    "Included from line number %lu", 
24932                                    (unsigned long) ref->lineno);
24933       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
24934       break;
24935     case DW_MACINFO_end_file:
24936       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
24937       break;
24938     case DW_MACINFO_define:
24939     case DW_MACINFO_undef:
24940       len = strlen (ref->info) + 1;
24941       if (!dwarf_strict
24942           && len > DWARF_OFFSET_SIZE
24943           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
24944           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
24945         {
24946           ref->code = ref->code == DW_MACINFO_define
24947                       ? DW_MACRO_GNU_define_indirect
24948                       : DW_MACRO_GNU_undef_indirect;
24949           output_macinfo_op (ref);
24950           return;
24951         }
24952       dw2_asm_output_data (1, ref->code,
24953                            ref->code == DW_MACINFO_define
24954                            ? "Define macro" : "Undefine macro");
24955       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
24956                                    (unsigned long) ref->lineno);
24957       dw2_asm_output_nstring (ref->info, -1, "The macro");
24958       break;
24959     case DW_MACRO_GNU_define_indirect:
24960     case DW_MACRO_GNU_undef_indirect:
24961       node = find_AT_string (ref->info);
24962       gcc_assert (node
24963                   && ((node->form == DW_FORM_strp)
24964                       || (node->form == DW_FORM_GNU_str_index)));
24965       dw2_asm_output_data (1, ref->code,
24966                            ref->code == DW_MACRO_GNU_define_indirect
24967                            ? "Define macro indirect"
24968                            : "Undefine macro indirect");
24969       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
24970                                    (unsigned long) ref->lineno);
24971       if (node->form == DW_FORM_strp)
24972         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
24973                                debug_str_section, "The macro: \"%s\"",
24974                                ref->info);
24975       else
24976         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
24977                                      ref->info);
24978       break;
24979     case DW_MACRO_GNU_transparent_include:
24980       dw2_asm_output_data (1, ref->code, "Transparent include");
24981       ASM_GENERATE_INTERNAL_LABEL (label,
24982                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
24983       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
24984       break;
24985     default:
24986       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
24987                ASM_COMMENT_START, (unsigned long) ref->code);
24988       break;
24989     }
24990 }
24991
24992 /* Attempt to make a sequence of define/undef macinfo ops shareable with
24993    other compilation unit .debug_macinfo sections.  IDX is the first
24994    index of a define/undef, return the number of ops that should be
24995    emitted in a comdat .debug_macinfo section and emit
24996    a DW_MACRO_GNU_transparent_include entry referencing it.
24997    If the define/undef entry should be emitted normally, return 0.  */
24998
24999 static unsigned
25000 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25001                         macinfo_hash_type **macinfo_htab)
25002 {
25003   macinfo_entry *first, *second, *cur, *inc;
25004   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25005   unsigned char checksum[16];
25006   struct md5_ctx ctx;
25007   char *grp_name, *tail;
25008   const char *base;
25009   unsigned int i, count, encoded_filename_len, linebuf_len;
25010   macinfo_entry **slot;
25011
25012   first = &(*macinfo_table)[idx];
25013   second = &(*macinfo_table)[idx + 1];
25014
25015   /* Optimize only if there are at least two consecutive define/undef ops,
25016      and either all of them are before first DW_MACINFO_start_file
25017      with lineno {0,1} (i.e. predefined macro block), or all of them are
25018      in some included header file.  */
25019   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25020     return 0;
25021   if (vec_safe_is_empty (files))
25022     {
25023       if (first->lineno > 1 || second->lineno > 1)
25024         return 0;
25025     }
25026   else if (first->lineno == 0)
25027     return 0;
25028
25029   /* Find the last define/undef entry that can be grouped together
25030      with first and at the same time compute md5 checksum of their
25031      codes, linenumbers and strings.  */
25032   md5_init_ctx (&ctx);
25033   for (i = idx; macinfo_table->iterate (i, &cur); i++)
25034     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25035       break;
25036     else if (vec_safe_is_empty (files) && cur->lineno > 1)
25037       break;
25038     else
25039       {
25040         unsigned char code = cur->code;
25041         md5_process_bytes (&code, 1, &ctx);
25042         checksum_uleb128 (cur->lineno, &ctx);
25043         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25044       }
25045   md5_finish_ctx (&ctx, checksum);
25046   count = i - idx;
25047
25048   /* From the containing include filename (if any) pick up just
25049      usable characters from its basename.  */
25050   if (vec_safe_is_empty (files))
25051     base = "";
25052   else
25053     base = lbasename (files->last ().info);
25054   for (encoded_filename_len = 0, i = 0; base[i]; i++)
25055     if (ISIDNUM (base[i]) || base[i] == '.')
25056       encoded_filename_len++;
25057   /* Count . at the end.  */
25058   if (encoded_filename_len)
25059     encoded_filename_len++;
25060
25061   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25062   linebuf_len = strlen (linebuf);
25063
25064   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
25065   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25066                          + 16 * 2 + 1);
25067   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25068   tail = grp_name + 4;
25069   if (encoded_filename_len)
25070     {
25071       for (i = 0; base[i]; i++)
25072         if (ISIDNUM (base[i]) || base[i] == '.')
25073           *tail++ = base[i];
25074       *tail++ = '.';
25075     }
25076   memcpy (tail, linebuf, linebuf_len);
25077   tail += linebuf_len;
25078   *tail++ = '.';
25079   for (i = 0; i < 16; i++)
25080     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25081
25082   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25083      in the empty vector entry before the first define/undef.  */
25084   inc = &(*macinfo_table)[idx - 1];
25085   inc->code = DW_MACRO_GNU_transparent_include;
25086   inc->lineno = 0;
25087   inc->info = ggc_strdup (grp_name);
25088   if (!*macinfo_htab)
25089     *macinfo_htab = new macinfo_hash_type (10);
25090   /* Avoid emitting duplicates.  */
25091   slot = (*macinfo_htab)->find_slot (inc, INSERT);
25092   if (*slot != NULL)
25093     {
25094       inc->code = 0;
25095       inc->info = NULL;
25096       /* If such an entry has been used before, just emit
25097          a DW_MACRO_GNU_transparent_include op.  */
25098       inc = *slot;
25099       output_macinfo_op (inc);
25100       /* And clear all macinfo_entry in the range to avoid emitting them
25101          in the second pass.  */
25102       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25103         {
25104           cur->code = 0;
25105           cur->info = NULL;
25106         }
25107     }
25108   else
25109     {
25110       *slot = inc;
25111       inc->lineno = (*macinfo_htab)->elements ();
25112       output_macinfo_op (inc);
25113     }
25114   return count;
25115 }
25116
25117 /* Save any strings needed by the macinfo table in the debug str
25118    table.  All strings must be collected into the table by the time
25119    index_string is called.  */
25120
25121 static void
25122 save_macinfo_strings (void)
25123 {
25124   unsigned len;
25125   unsigned i;
25126   macinfo_entry *ref;
25127
25128   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25129     {
25130       switch (ref->code)
25131         {
25132           /* Match the logic in output_macinfo_op to decide on
25133              indirect strings.  */
25134           case DW_MACINFO_define:
25135           case DW_MACINFO_undef:
25136             len = strlen (ref->info) + 1;
25137             if (!dwarf_strict
25138                 && len > DWARF_OFFSET_SIZE
25139                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25140                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25141               set_indirect_string (find_AT_string (ref->info));
25142             break;
25143           case DW_MACRO_GNU_define_indirect:
25144           case DW_MACRO_GNU_undef_indirect:
25145             set_indirect_string (find_AT_string (ref->info));
25146             break;
25147           default:
25148             break;
25149         }
25150     }
25151 }
25152
25153 /* Output macinfo section(s).  */
25154
25155 static void
25156 output_macinfo (void)
25157 {
25158   unsigned i;
25159   unsigned long length = vec_safe_length (macinfo_table);
25160   macinfo_entry *ref;
25161   vec<macinfo_entry, va_gc> *files = NULL;
25162   macinfo_hash_type *macinfo_htab = NULL;
25163
25164   if (! length)
25165     return;
25166
25167   /* output_macinfo* uses these interchangeably.  */
25168   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25169               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25170               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25171               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25172
25173   /* For .debug_macro emit the section header.  */
25174   if (!dwarf_strict)
25175     {
25176       dw2_asm_output_data (2, 4, "DWARF macro version number");
25177       if (DWARF_OFFSET_SIZE == 8)
25178         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25179       else
25180         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25181       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25182                              (!dwarf_split_debug_info ? debug_line_section_label
25183                               : debug_skeleton_line_section_label),
25184                              debug_line_section, NULL);
25185     }
25186
25187   /* In the first loop, it emits the primary .debug_macinfo section
25188      and after each emitted op the macinfo_entry is cleared.
25189      If a longer range of define/undef ops can be optimized using
25190      DW_MACRO_GNU_transparent_include, the
25191      DW_MACRO_GNU_transparent_include op is emitted and kept in
25192      the vector before the first define/undef in the range and the
25193      whole range of define/undef ops is not emitted and kept.  */
25194   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25195     {
25196       switch (ref->code)
25197         {
25198         case DW_MACINFO_start_file:
25199           vec_safe_push (files, *ref);
25200           break;
25201         case DW_MACINFO_end_file:
25202           if (!vec_safe_is_empty (files))
25203             files->pop ();
25204           break;
25205         case DW_MACINFO_define:
25206         case DW_MACINFO_undef:
25207           if (!dwarf_strict
25208               && HAVE_COMDAT_GROUP
25209               && vec_safe_length (files) != 1
25210               && i > 0
25211               && i + 1 < length
25212               && (*macinfo_table)[i - 1].code == 0)
25213             {
25214               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25215               if (count)
25216                 {
25217                   i += count - 1;
25218                   continue;
25219                 }
25220             }
25221           break;
25222         case 0:
25223           /* A dummy entry may be inserted at the beginning to be able
25224              to optimize the whole block of predefined macros.  */
25225           if (i == 0)
25226             continue;
25227         default:
25228           break;
25229         }
25230       output_macinfo_op (ref);
25231       ref->info = NULL;
25232       ref->code = 0;
25233     }
25234
25235   if (!macinfo_htab)
25236     return;
25237
25238   delete macinfo_htab;
25239   macinfo_htab = NULL;
25240
25241   /* If any DW_MACRO_GNU_transparent_include were used, on those
25242      DW_MACRO_GNU_transparent_include entries terminate the
25243      current chain and switch to a new comdat .debug_macinfo
25244      section and emit the define/undef entries within it.  */
25245   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25246     switch (ref->code)
25247       {
25248       case 0:
25249         continue;
25250       case DW_MACRO_GNU_transparent_include:
25251         {
25252           char label[MAX_ARTIFICIAL_LABEL_BYTES];
25253           tree comdat_key = get_identifier (ref->info);
25254           /* Terminate the previous .debug_macinfo section.  */
25255           dw2_asm_output_data (1, 0, "End compilation unit");
25256           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25257                                          SECTION_DEBUG
25258                                          | SECTION_LINKONCE,
25259                                          comdat_key);
25260           ASM_GENERATE_INTERNAL_LABEL (label,
25261                                        DEBUG_MACRO_SECTION_LABEL,
25262                                        ref->lineno);
25263           ASM_OUTPUT_LABEL (asm_out_file, label);
25264           ref->code = 0;
25265           ref->info = NULL;
25266           dw2_asm_output_data (2, 4, "DWARF macro version number");
25267           if (DWARF_OFFSET_SIZE == 8)
25268             dw2_asm_output_data (1, 1, "Flags: 64-bit");
25269           else
25270             dw2_asm_output_data (1, 0, "Flags: 32-bit");
25271         }
25272         break;
25273       case DW_MACINFO_define:
25274       case DW_MACINFO_undef:
25275         output_macinfo_op (ref);
25276         ref->code = 0;
25277         ref->info = NULL;
25278         break;
25279       default:
25280         gcc_unreachable ();
25281       }
25282 }
25283
25284 /* Set up for Dwarf output at the start of compilation.  */
25285
25286 static void
25287 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25288 {
25289   /* This option is currently broken, see (PR53118 and PR46102).  */
25290   if (flag_eliminate_dwarf2_dups
25291       && strstr (lang_hooks.name, "C++"))
25292     {
25293       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25294       flag_eliminate_dwarf2_dups = 0;
25295     }
25296
25297   /* Allocate the file_table.  */
25298   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25299
25300 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25301   /* Allocate the decl_die_table.  */
25302   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25303
25304   /* Allocate the decl_loc_table.  */
25305   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25306
25307   /* Allocate the cached_dw_loc_list_table.  */
25308   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25309
25310   /* Allocate the initial hunk of the decl_scope_table.  */
25311   vec_alloc (decl_scope_table, 256);
25312
25313   /* Allocate the initial hunk of the abbrev_die_table.  */
25314   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25315     (ABBREV_DIE_TABLE_INCREMENT);
25316   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25317   /* Zero-th entry is allocated, but unused.  */
25318   abbrev_die_table_in_use = 1;
25319
25320   /* Allocate the dwarf_proc_stack_usage_map.  */
25321   dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25322
25323   /* Allocate the pubtypes and pubnames vectors.  */
25324   vec_alloc (pubname_table, 32);
25325   vec_alloc (pubtype_table, 32);
25326
25327   vec_alloc (incomplete_types, 64);
25328
25329   vec_alloc (used_rtx_array, 32);
25330
25331   if (!dwarf_split_debug_info)
25332     {
25333       debug_info_section = get_section (DEBUG_INFO_SECTION,
25334                                         SECTION_DEBUG, NULL);
25335       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25336                                           SECTION_DEBUG, NULL);
25337       debug_loc_section = get_section (DEBUG_LOC_SECTION,
25338                                        SECTION_DEBUG, NULL);
25339     }
25340   else
25341     {
25342       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25343                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25344       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25345                                           SECTION_DEBUG | SECTION_EXCLUDE,
25346                                           NULL);
25347       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25348                                         SECTION_DEBUG, NULL);
25349       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25350                                                  SECTION_DEBUG, NULL);
25351       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25352                                                    SECTION_DEBUG, NULL);
25353       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25354                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25355
25356       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25357          the main .o, but the skeleton_line goes into the split off dwo.  */
25358       debug_skeleton_line_section
25359           = get_section (DEBUG_DWO_LINE_SECTION,
25360                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25361       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25362                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25363       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25364                                                SECTION_DEBUG | SECTION_EXCLUDE,
25365                                                NULL);
25366       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25367                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25368       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25369                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25370       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25371                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25372     }
25373   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25374                                        SECTION_DEBUG, NULL);
25375   debug_macinfo_section = get_section (dwarf_strict
25376                                        ? DEBUG_MACINFO_SECTION
25377                                        : DEBUG_MACRO_SECTION,
25378                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
25379   debug_line_section = get_section (DEBUG_LINE_SECTION,
25380                                     SECTION_DEBUG, NULL);
25381   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25382                                         SECTION_DEBUG, NULL);
25383   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25384                                         SECTION_DEBUG, NULL);
25385   debug_str_section = get_section (DEBUG_STR_SECTION,
25386                                    DEBUG_STR_SECTION_FLAGS, NULL);
25387   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25388                                       SECTION_DEBUG, NULL);
25389   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25390                                      SECTION_DEBUG, NULL);
25391
25392   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25393   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25394                                DEBUG_ABBREV_SECTION_LABEL, 0);
25395   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25396   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25397                                COLD_TEXT_SECTION_LABEL, 0);
25398   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25399
25400   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25401                                DEBUG_INFO_SECTION_LABEL, 0);
25402   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25403                                DEBUG_LINE_SECTION_LABEL, 0);
25404   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25405                                DEBUG_RANGES_SECTION_LABEL, 0);
25406   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25407                                DEBUG_ADDR_SECTION_LABEL, 0);
25408   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25409                                dwarf_strict
25410                                ? DEBUG_MACINFO_SECTION_LABEL
25411                                : DEBUG_MACRO_SECTION_LABEL, 0);
25412   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25413
25414   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25415     vec_alloc (macinfo_table, 64);
25416
25417   switch_to_section (text_section);
25418   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25419 #endif
25420
25421   /* Make sure the line number table for .text always exists.  */
25422   text_section_line_info = new_line_info_table ();
25423   text_section_line_info->end_label = text_end_label;
25424
25425 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25426   cur_line_info_table = text_section_line_info;
25427 #endif
25428
25429   /* If front-ends already registered a main translation unit but we were not
25430      ready to perform the association, do this now.  */
25431   if (main_translation_unit != NULL_TREE)
25432     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25433 }
25434
25435 /* Called before compile () starts outputtting functions, variables
25436    and toplevel asms into assembly.  */
25437
25438 static void
25439 dwarf2out_assembly_start (void)
25440 {
25441   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25442       && dwarf2out_do_cfi_asm ()
25443       && (!(flag_unwind_tables || flag_exceptions)
25444           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25445     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25446 }
25447
25448 /* A helper function for dwarf2out_finish called through
25449    htab_traverse.  Assign a string its index.  All strings must be
25450    collected into the table by the time index_string is called,
25451    because the indexing code relies on htab_traverse to traverse nodes
25452    in the same order for each run. */
25453
25454 int
25455 index_string (indirect_string_node **h, unsigned int *index)
25456 {
25457   indirect_string_node *node = *h;
25458
25459   find_string_form (node);
25460   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25461     {
25462       gcc_assert (node->index == NO_INDEX_ASSIGNED);
25463       node->index = *index;
25464       *index += 1;
25465     }
25466   return 1;
25467 }
25468
25469 /* A helper function for output_indirect_strings called through
25470    htab_traverse.  Output the offset to a string and update the
25471    current offset.  */
25472
25473 int
25474 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25475 {
25476   indirect_string_node *node = *h;
25477
25478   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25479     {
25480       /* Assert that this node has been assigned an index.  */
25481       gcc_assert (node->index != NO_INDEX_ASSIGNED
25482                   && node->index != NOT_INDEXED);
25483       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25484                            "indexed string 0x%x: %s", node->index, node->str);
25485       *offset += strlen (node->str) + 1;
25486     }
25487   return 1;
25488 }
25489
25490 /* A helper function for dwarf2out_finish called through
25491    htab_traverse.  Output the indexed string.  */
25492
25493 int
25494 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25495 {
25496   struct indirect_string_node *node = *h;
25497
25498   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25499     {
25500       /* Assert that the strings are output in the same order as their
25501          indexes were assigned.  */
25502       gcc_assert (*cur_idx == node->index);
25503       assemble_string (node->str, strlen (node->str) + 1);
25504       *cur_idx += 1;
25505     }
25506   return 1;
25507 }
25508
25509 /* A helper function for dwarf2out_finish called through
25510    htab_traverse.  Emit one queued .debug_str string.  */
25511
25512 int
25513 output_indirect_string (indirect_string_node **h, void *)
25514 {
25515   struct indirect_string_node *node = *h;
25516
25517   node->form = find_string_form (node);
25518   if (node->form == DW_FORM_strp && node->refcount > 0)
25519     {
25520       ASM_OUTPUT_LABEL (asm_out_file, node->label);
25521       assemble_string (node->str, strlen (node->str) + 1);
25522     }
25523
25524   return 1;
25525 }
25526
25527 /* Output the indexed string table.  */
25528
25529 static void
25530 output_indirect_strings (void)
25531 {
25532   switch_to_section (debug_str_section);
25533   if (!dwarf_split_debug_info)
25534     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25535   else
25536     {
25537       unsigned int offset = 0;
25538       unsigned int cur_idx = 0;
25539
25540       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25541
25542       switch_to_section (debug_str_offsets_section);
25543       debug_str_hash->traverse_noresize
25544         <unsigned int *, output_index_string_offset> (&offset);
25545       switch_to_section (debug_str_dwo_section);
25546       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25547         (&cur_idx);
25548     }
25549 }
25550
25551 /* Callback for htab_traverse to assign an index to an entry in the
25552    table, and to write that entry to the .debug_addr section.  */
25553
25554 int
25555 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25556 {
25557   addr_table_entry *entry = *slot;
25558
25559   if (entry->refcount == 0)
25560     {
25561       gcc_assert (entry->index == NO_INDEX_ASSIGNED
25562                   || entry->index == NOT_INDEXED);
25563       return 1;
25564     }
25565
25566   gcc_assert (entry->index == *cur_index);
25567   (*cur_index)++;
25568
25569   switch (entry->kind)
25570     {
25571       case ate_kind_rtx:
25572         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25573                                  "0x%x", entry->index);
25574         break;
25575       case ate_kind_rtx_dtprel:
25576         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25577         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25578                                              DWARF2_ADDR_SIZE,
25579                                              entry->addr.rtl);
25580         fputc ('\n', asm_out_file);
25581         break;
25582       case ate_kind_label:
25583         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25584                                  "0x%x", entry->index);
25585         break;
25586       default:
25587         gcc_unreachable ();
25588     }
25589   return 1;
25590 }
25591
25592 /* Produce the .debug_addr section.  */
25593
25594 static void
25595 output_addr_table (void)
25596 {
25597   unsigned int index = 0;
25598   if (addr_index_table == NULL || addr_index_table->size () == 0)
25599     return;
25600
25601   switch_to_section (debug_addr_section);
25602   addr_index_table
25603     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25604 }
25605
25606 #if ENABLE_ASSERT_CHECKING
25607 /* Verify that all marks are clear.  */
25608
25609 static void
25610 verify_marks_clear (dw_die_ref die)
25611 {
25612   dw_die_ref c;
25613
25614   gcc_assert (! die->die_mark);
25615   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25616 }
25617 #endif /* ENABLE_ASSERT_CHECKING */
25618
25619 /* Clear the marks for a die and its children.
25620    Be cool if the mark isn't set.  */
25621
25622 static void
25623 prune_unmark_dies (dw_die_ref die)
25624 {
25625   dw_die_ref c;
25626
25627   if (die->die_mark)
25628     die->die_mark = 0;
25629   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25630 }
25631
25632 /* Given LOC that is referenced by a DIE we're marking as used, find all
25633    referenced DWARF procedures it references and mark them as used.  */
25634
25635 static void
25636 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25637 {
25638   for (; loc != NULL; loc = loc->dw_loc_next)
25639     switch (loc->dw_loc_opc)
25640       {
25641       case DW_OP_call2:
25642       case DW_OP_call4:
25643       case DW_OP_call_ref:
25644         prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25645         break;
25646       default:
25647         break;
25648       }
25649 }
25650
25651 /* Given DIE that we're marking as used, find any other dies
25652    it references as attributes and mark them as used.  */
25653
25654 static void
25655 prune_unused_types_walk_attribs (dw_die_ref die)
25656 {
25657   dw_attr_node *a;
25658   unsigned ix;
25659
25660   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25661     {
25662       switch (AT_class (a))
25663         {
25664         /* Make sure DWARF procedures referenced by location descriptions will
25665            get emitted.  */
25666         case dw_val_class_loc:
25667           prune_unused_types_walk_loc_descr (AT_loc (a));
25668           break;
25669         case dw_val_class_loc_list:
25670           for (dw_loc_list_ref list = AT_loc_list (a);
25671                list != NULL;
25672                list = list->dw_loc_next)
25673             prune_unused_types_walk_loc_descr (list->expr);
25674           break;
25675
25676         case dw_val_class_die_ref:
25677           /* A reference to another DIE.
25678              Make sure that it will get emitted.
25679              If it was broken out into a comdat group, don't follow it.  */
25680           if (! AT_ref (a)->comdat_type_p
25681               || a->dw_attr == DW_AT_specification)
25682             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25683           break;
25684
25685         case dw_val_class_str:
25686           /* Set the string's refcount to 0 so that prune_unused_types_mark
25687              accounts properly for it.  */
25688           a->dw_attr_val.v.val_str->refcount = 0;
25689           break;
25690
25691         default:
25692           break;
25693         }
25694     }
25695 }
25696
25697 /* Mark the generic parameters and arguments children DIEs of DIE.  */
25698
25699 static void
25700 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25701 {
25702   dw_die_ref c;
25703
25704   if (die == NULL || die->die_child == NULL)
25705     return;
25706   c = die->die_child;
25707   do
25708     {
25709       if (is_template_parameter (c))
25710         prune_unused_types_mark (c, 1);
25711       c = c->die_sib;
25712     } while (c && c != die->die_child);
25713 }
25714
25715 /* Mark DIE as being used.  If DOKIDS is true, then walk down
25716    to DIE's children.  */
25717
25718 static void
25719 prune_unused_types_mark (dw_die_ref die, int dokids)
25720 {
25721   dw_die_ref c;
25722
25723   if (die->die_mark == 0)
25724     {
25725       /* We haven't done this node yet.  Mark it as used.  */
25726       die->die_mark = 1;
25727       /* If this is the DIE of a generic type instantiation,
25728          mark the children DIEs that describe its generic parms and
25729          args.  */
25730       prune_unused_types_mark_generic_parms_dies (die);
25731
25732       /* We also have to mark its parents as used.
25733          (But we don't want to mark our parent's kids due to this,
25734          unless it is a class.)  */
25735       if (die->die_parent)
25736         prune_unused_types_mark (die->die_parent,
25737                                  class_scope_p (die->die_parent));
25738
25739       /* Mark any referenced nodes.  */
25740       prune_unused_types_walk_attribs (die);
25741
25742       /* If this node is a specification,
25743          also mark the definition, if it exists.  */
25744       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
25745         prune_unused_types_mark (die->die_definition, 1);
25746     }
25747
25748   if (dokids && die->die_mark != 2)
25749     {
25750       /* We need to walk the children, but haven't done so yet.
25751          Remember that we've walked the kids.  */
25752       die->die_mark = 2;
25753
25754       /* If this is an array type, we need to make sure our
25755          kids get marked, even if they're types.  If we're
25756          breaking out types into comdat sections, do this
25757          for all type definitions.  */
25758       if (die->die_tag == DW_TAG_array_type
25759           || (use_debug_types
25760               && is_type_die (die) && ! is_declaration_die (die)))
25761         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
25762       else
25763         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25764     }
25765 }
25766
25767 /* For local classes, look if any static member functions were emitted
25768    and if so, mark them.  */
25769
25770 static void
25771 prune_unused_types_walk_local_classes (dw_die_ref die)
25772 {
25773   dw_die_ref c;
25774
25775   if (die->die_mark == 2)
25776     return;
25777
25778   switch (die->die_tag)
25779     {
25780     case DW_TAG_structure_type:
25781     case DW_TAG_union_type:
25782     case DW_TAG_class_type:
25783       break;
25784
25785     case DW_TAG_subprogram:
25786       if (!get_AT_flag (die, DW_AT_declaration)
25787           || die->die_definition != NULL)
25788         prune_unused_types_mark (die, 1);
25789       return;
25790
25791     default:
25792       return;
25793     }
25794
25795   /* Mark children.  */
25796   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
25797 }
25798
25799 /* Walk the tree DIE and mark types that we actually use.  */
25800
25801 static void
25802 prune_unused_types_walk (dw_die_ref die)
25803 {
25804   dw_die_ref c;
25805
25806   /* Don't do anything if this node is already marked and
25807      children have been marked as well.  */
25808   if (die->die_mark == 2)
25809     return;
25810
25811   switch (die->die_tag)
25812     {
25813     case DW_TAG_structure_type:
25814     case DW_TAG_union_type:
25815     case DW_TAG_class_type:
25816       if (die->die_perennial_p)
25817         break;
25818
25819       for (c = die->die_parent; c; c = c->die_parent)
25820         if (c->die_tag == DW_TAG_subprogram)
25821           break;
25822
25823       /* Finding used static member functions inside of classes
25824          is needed just for local classes, because for other classes
25825          static member function DIEs with DW_AT_specification
25826          are emitted outside of the DW_TAG_*_type.  If we ever change
25827          it, we'd need to call this even for non-local classes.  */
25828       if (c)
25829         prune_unused_types_walk_local_classes (die);
25830
25831       /* It's a type node --- don't mark it.  */
25832       return;
25833
25834     case DW_TAG_const_type:
25835     case DW_TAG_packed_type:
25836     case DW_TAG_pointer_type:
25837     case DW_TAG_reference_type:
25838     case DW_TAG_rvalue_reference_type:
25839     case DW_TAG_volatile_type:
25840     case DW_TAG_typedef:
25841     case DW_TAG_array_type:
25842     case DW_TAG_interface_type:
25843     case DW_TAG_friend:
25844     case DW_TAG_enumeration_type:
25845     case DW_TAG_subroutine_type:
25846     case DW_TAG_string_type:
25847     case DW_TAG_set_type:
25848     case DW_TAG_subrange_type:
25849     case DW_TAG_ptr_to_member_type:
25850     case DW_TAG_file_type:
25851       /* Type nodes are useful only when other DIEs reference them --- don't
25852          mark them.  */
25853       /* FALLTHROUGH */
25854
25855     case DW_TAG_dwarf_procedure:
25856       /* Likewise for DWARF procedures.  */
25857
25858       if (die->die_perennial_p)
25859         break;
25860
25861       return;
25862
25863     default:
25864       /* Mark everything else.  */
25865       break;
25866   }
25867
25868   if (die->die_mark == 0)
25869     {
25870       die->die_mark = 1;
25871
25872       /* Now, mark any dies referenced from here.  */
25873       prune_unused_types_walk_attribs (die);
25874     }
25875
25876   die->die_mark = 2;
25877
25878   /* Mark children.  */
25879   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25880 }
25881
25882 /* Increment the string counts on strings referred to from DIE's
25883    attributes.  */
25884
25885 static void
25886 prune_unused_types_update_strings (dw_die_ref die)
25887 {
25888   dw_attr_node *a;
25889   unsigned ix;
25890
25891   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25892     if (AT_class (a) == dw_val_class_str)
25893       {
25894         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
25895         s->refcount++;
25896         /* Avoid unnecessarily putting strings that are used less than
25897            twice in the hash table.  */
25898         if (s->refcount
25899             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
25900           {
25901             indirect_string_node **slot
25902               = debug_str_hash->find_slot_with_hash (s->str,
25903                                                      htab_hash_string (s->str),
25904                                                      INSERT);
25905             gcc_assert (*slot == NULL);
25906             *slot = s;
25907           }
25908       }
25909 }
25910
25911 /* Remove from the tree DIE any dies that aren't marked.  */
25912
25913 static void
25914 prune_unused_types_prune (dw_die_ref die)
25915 {
25916   dw_die_ref c;
25917
25918   gcc_assert (die->die_mark);
25919   prune_unused_types_update_strings (die);
25920
25921   if (! die->die_child)
25922     return;
25923
25924   c = die->die_child;
25925   do {
25926     dw_die_ref prev = c;
25927     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
25928       if (c == die->die_child)
25929         {
25930           /* No marked children between 'prev' and the end of the list.  */
25931           if (prev == c)
25932             /* No marked children at all.  */
25933             die->die_child = NULL;
25934           else
25935             {
25936               prev->die_sib = c->die_sib;
25937               die->die_child = prev;
25938             }
25939           return;
25940         }
25941
25942     if (c != prev->die_sib)
25943       prev->die_sib = c;
25944     prune_unused_types_prune (c);
25945   } while (c != die->die_child);
25946 }
25947
25948 /* Remove dies representing declarations that we never use.  */
25949
25950 static void
25951 prune_unused_types (void)
25952 {
25953   unsigned int i;
25954   limbo_die_node *node;
25955   comdat_type_node *ctnode;
25956   pubname_entry *pub;
25957   dw_die_ref base_type;
25958
25959 #if ENABLE_ASSERT_CHECKING
25960   /* All the marks should already be clear.  */
25961   verify_marks_clear (comp_unit_die ());
25962   for (node = limbo_die_list; node; node = node->next)
25963     verify_marks_clear (node->die);
25964   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25965     verify_marks_clear (ctnode->root_die);
25966 #endif /* ENABLE_ASSERT_CHECKING */
25967
25968   /* Mark types that are used in global variables.  */
25969   premark_types_used_by_global_vars ();
25970
25971   /* Set the mark on nodes that are actually used.  */
25972   prune_unused_types_walk (comp_unit_die ());
25973   for (node = limbo_die_list; node; node = node->next)
25974     prune_unused_types_walk (node->die);
25975   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25976     {
25977       prune_unused_types_walk (ctnode->root_die);
25978       prune_unused_types_mark (ctnode->type_die, 1);
25979     }
25980
25981   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
25982      are unusual in that they are pubnames that are the children of pubtypes.
25983      They should only be marked via their parent DW_TAG_enumeration_type die,
25984      not as roots in themselves.  */
25985   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
25986     if (pub->die->die_tag != DW_TAG_enumerator)
25987       prune_unused_types_mark (pub->die, 1);
25988   for (i = 0; base_types.iterate (i, &base_type); i++)
25989     prune_unused_types_mark (base_type, 1);
25990
25991   if (debug_str_hash)
25992     debug_str_hash->empty ();
25993   if (skeleton_debug_str_hash)
25994     skeleton_debug_str_hash->empty ();
25995   prune_unused_types_prune (comp_unit_die ());
25996   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
25997     {
25998       node = *pnode;
25999       if (!node->die->die_mark)
26000         *pnode = node->next;
26001       else
26002         {
26003           prune_unused_types_prune (node->die);
26004           pnode = &node->next;
26005         }
26006     }
26007   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26008     prune_unused_types_prune (ctnode->root_die);
26009
26010   /* Leave the marks clear.  */
26011   prune_unmark_dies (comp_unit_die ());
26012   for (node = limbo_die_list; node; node = node->next)
26013     prune_unmark_dies (node->die);
26014   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26015     prune_unmark_dies (ctnode->root_die);
26016 }
26017
26018 /* Set the parameter to true if there are any relative pathnames in
26019    the file table.  */
26020 int
26021 file_table_relative_p (dwarf_file_data **slot, bool *p)
26022 {
26023   struct dwarf_file_data *d = *slot;
26024   if (!IS_ABSOLUTE_PATH (d->filename))
26025     {
26026       *p = true;
26027       return 0;
26028     }
26029   return 1;
26030 }
26031
26032 /* Helpers to manipulate hash table of comdat type units.  */
26033
26034 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26035 {
26036   static inline hashval_t hash (const comdat_type_node *);
26037   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26038 };
26039
26040 inline hashval_t
26041 comdat_type_hasher::hash (const comdat_type_node *type_node)
26042 {
26043   hashval_t h;
26044   memcpy (&h, type_node->signature, sizeof (h));
26045   return h;
26046 }
26047
26048 inline bool
26049 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26050                            const comdat_type_node *type_node_2)
26051 {
26052   return (! memcmp (type_node_1->signature, type_node_2->signature,
26053                     DWARF_TYPE_SIGNATURE_SIZE));
26054 }
26055
26056 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26057    to the location it would have been added, should we know its
26058    DECL_ASSEMBLER_NAME when we added other attributes.  This will
26059    probably improve compactness of debug info, removing equivalent
26060    abbrevs, and hide any differences caused by deferring the
26061    computation of the assembler name, triggered by e.g. PCH.  */
26062
26063 static inline void
26064 move_linkage_attr (dw_die_ref die)
26065 {
26066   unsigned ix = vec_safe_length (die->die_attr);
26067   dw_attr_node linkage = (*die->die_attr)[ix - 1];
26068
26069   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26070               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26071
26072   while (--ix > 0)
26073     {
26074       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26075
26076       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26077         break;
26078     }
26079
26080   if (ix != vec_safe_length (die->die_attr) - 1)
26081     {
26082       die->die_attr->pop ();
26083       die->die_attr->quick_insert (ix, linkage);
26084     }
26085 }
26086
26087 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26088    referenced from typed stack ops and count how often they are used.  */
26089
26090 static void
26091 mark_base_types (dw_loc_descr_ref loc)
26092 {
26093   dw_die_ref base_type = NULL;
26094
26095   for (; loc; loc = loc->dw_loc_next)
26096     {
26097       switch (loc->dw_loc_opc)
26098         {
26099         case DW_OP_GNU_regval_type:
26100         case DW_OP_GNU_deref_type:
26101           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26102           break;
26103         case DW_OP_GNU_convert:
26104         case DW_OP_GNU_reinterpret:
26105           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26106             continue;
26107           /* FALLTHRU */
26108         case DW_OP_GNU_const_type:
26109           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26110           break;
26111         case DW_OP_GNU_entry_value:
26112           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26113           continue;
26114         default:
26115           continue;
26116         }
26117       gcc_assert (base_type->die_parent == comp_unit_die ());
26118       if (base_type->die_mark)
26119         base_type->die_mark++;
26120       else
26121         {
26122           base_types.safe_push (base_type);
26123           base_type->die_mark = 1;
26124         }
26125     }
26126 }
26127
26128 /* Comparison function for sorting marked base types.  */
26129
26130 static int
26131 base_type_cmp (const void *x, const void *y)
26132 {
26133   dw_die_ref dx = *(const dw_die_ref *) x;
26134   dw_die_ref dy = *(const dw_die_ref *) y;
26135   unsigned int byte_size1, byte_size2;
26136   unsigned int encoding1, encoding2;
26137   if (dx->die_mark > dy->die_mark)
26138     return -1;
26139   if (dx->die_mark < dy->die_mark)
26140     return 1;
26141   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26142   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26143   if (byte_size1 < byte_size2)
26144     return 1;
26145   if (byte_size1 > byte_size2)
26146     return -1;
26147   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26148   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26149   if (encoding1 < encoding2)
26150     return 1;
26151   if (encoding1 > encoding2)
26152     return -1;
26153   return 0;
26154 }
26155
26156 /* Move base types marked by mark_base_types as early as possible
26157    in the CU, sorted by decreasing usage count both to make the
26158    uleb128 references as small as possible and to make sure they
26159    will have die_offset already computed by calc_die_sizes when
26160    sizes of typed stack loc ops is computed.  */
26161
26162 static void
26163 move_marked_base_types (void)
26164 {
26165   unsigned int i;
26166   dw_die_ref base_type, die, c;
26167
26168   if (base_types.is_empty ())
26169     return;
26170
26171   /* Sort by decreasing usage count, they will be added again in that
26172      order later on.  */
26173   base_types.qsort (base_type_cmp);
26174   die = comp_unit_die ();
26175   c = die->die_child;
26176   do
26177     {
26178       dw_die_ref prev = c;
26179       c = c->die_sib;
26180       while (c->die_mark)
26181         {
26182           remove_child_with_prev (c, prev);
26183           /* As base types got marked, there must be at least
26184              one node other than DW_TAG_base_type.  */
26185           gcc_assert (c != c->die_sib);
26186           c = c->die_sib;
26187         }
26188     }
26189   while (c != die->die_child);
26190   gcc_assert (die->die_child);
26191   c = die->die_child;
26192   for (i = 0; base_types.iterate (i, &base_type); i++)
26193     {
26194       base_type->die_mark = 0;
26195       base_type->die_sib = c->die_sib;
26196       c->die_sib = base_type;
26197       c = base_type;
26198     }
26199 }
26200
26201 /* Helper function for resolve_addr, attempt to resolve
26202    one CONST_STRING, return true if successful.  Similarly verify that
26203    SYMBOL_REFs refer to variables emitted in the current CU.  */
26204
26205 static bool
26206 resolve_one_addr (rtx *addr)
26207 {
26208   rtx rtl = *addr;
26209
26210   if (GET_CODE (rtl) == CONST_STRING)
26211     {
26212       size_t len = strlen (XSTR (rtl, 0)) + 1;
26213       tree t = build_string (len, XSTR (rtl, 0));
26214       tree tlen = size_int (len - 1);
26215       TREE_TYPE (t)
26216         = build_array_type (char_type_node, build_index_type (tlen));
26217       rtl = lookup_constant_def (t);
26218       if (!rtl || !MEM_P (rtl))
26219         return false;
26220       rtl = XEXP (rtl, 0);
26221       if (GET_CODE (rtl) == SYMBOL_REF
26222           && SYMBOL_REF_DECL (rtl)
26223           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26224         return false;
26225       vec_safe_push (used_rtx_array, rtl);
26226       *addr = rtl;
26227       return true;
26228     }
26229
26230   if (GET_CODE (rtl) == SYMBOL_REF
26231       && SYMBOL_REF_DECL (rtl))
26232     {
26233       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26234         {
26235           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26236             return false;
26237         }
26238       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26239         return false;
26240     }
26241
26242   if (GET_CODE (rtl) == CONST)
26243     {
26244       subrtx_ptr_iterator::array_type array;
26245       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26246         if (!resolve_one_addr (*iter))
26247           return false;
26248     }
26249
26250   return true;
26251 }
26252
26253 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26254    if possible, and create DW_TAG_dwarf_procedure that can be referenced
26255    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
26256
26257 static rtx
26258 string_cst_pool_decl (tree t)
26259 {
26260   rtx rtl = output_constant_def (t, 1);
26261   unsigned char *array;
26262   dw_loc_descr_ref l;
26263   tree decl;
26264   size_t len;
26265   dw_die_ref ref;
26266
26267   if (!rtl || !MEM_P (rtl))
26268     return NULL_RTX;
26269   rtl = XEXP (rtl, 0);
26270   if (GET_CODE (rtl) != SYMBOL_REF
26271       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26272     return NULL_RTX;
26273
26274   decl = SYMBOL_REF_DECL (rtl);
26275   if (!lookup_decl_die (decl))
26276     {
26277       len = TREE_STRING_LENGTH (t);
26278       vec_safe_push (used_rtx_array, rtl);
26279       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26280       array = ggc_vec_alloc<unsigned char> (len);
26281       memcpy (array, TREE_STRING_POINTER (t), len);
26282       l = new_loc_descr (DW_OP_implicit_value, len, 0);
26283       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26284       l->dw_loc_oprnd2.v.val_vec.length = len;
26285       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26286       l->dw_loc_oprnd2.v.val_vec.array = array;
26287       add_AT_loc (ref, DW_AT_location, l);
26288       equate_decl_number_to_die (decl, ref);
26289     }
26290   return rtl;
26291 }
26292
26293 /* Helper function of resolve_addr_in_expr.  LOC is
26294    a DW_OP_addr followed by DW_OP_stack_value, either at the start
26295    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26296    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
26297    with DW_OP_GNU_implicit_pointer if possible
26298    and return true, if unsuccessful, return false.  */
26299
26300 static bool
26301 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26302 {
26303   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26304   HOST_WIDE_INT offset = 0;
26305   dw_die_ref ref = NULL;
26306   tree decl;
26307
26308   if (GET_CODE (rtl) == CONST
26309       && GET_CODE (XEXP (rtl, 0)) == PLUS
26310       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26311     {
26312       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26313       rtl = XEXP (XEXP (rtl, 0), 0);
26314     }
26315   if (GET_CODE (rtl) == CONST_STRING)
26316     {
26317       size_t len = strlen (XSTR (rtl, 0)) + 1;
26318       tree t = build_string (len, XSTR (rtl, 0));
26319       tree tlen = size_int (len - 1);
26320
26321       TREE_TYPE (t)
26322         = build_array_type (char_type_node, build_index_type (tlen));
26323       rtl = string_cst_pool_decl (t);
26324       if (!rtl)
26325         return false;
26326     }
26327   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26328     {
26329       decl = SYMBOL_REF_DECL (rtl);
26330       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26331         {
26332           ref = lookup_decl_die (decl);
26333           if (ref && (get_AT (ref, DW_AT_location)
26334                       || get_AT (ref, DW_AT_const_value)))
26335             {
26336               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26337               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26338               loc->dw_loc_oprnd1.val_entry = NULL;
26339               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26340               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26341               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26342               loc->dw_loc_oprnd2.v.val_int = offset;
26343               return true;
26344             }
26345         }
26346     }
26347   return false;
26348 }
26349
26350 /* Helper function for resolve_addr, handle one location
26351    expression, return false if at least one CONST_STRING or SYMBOL_REF in
26352    the location list couldn't be resolved.  */
26353
26354 static bool
26355 resolve_addr_in_expr (dw_loc_descr_ref loc)
26356 {
26357   dw_loc_descr_ref keep = NULL;
26358   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26359     switch (loc->dw_loc_opc)
26360       {
26361       case DW_OP_addr:
26362         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26363           {
26364             if ((prev == NULL
26365                  || prev->dw_loc_opc == DW_OP_piece
26366                  || prev->dw_loc_opc == DW_OP_bit_piece)
26367                 && loc->dw_loc_next
26368                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26369                 && !dwarf_strict
26370                 && optimize_one_addr_into_implicit_ptr (loc))
26371               break;
26372             return false;
26373           }
26374         break;
26375       case DW_OP_GNU_addr_index:
26376       case DW_OP_GNU_const_index:
26377         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26378             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26379           {
26380             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26381             if (!resolve_one_addr (&rtl))
26382               return false;
26383             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26384             loc->dw_loc_oprnd1.val_entry =
26385                 add_addr_table_entry (rtl, ate_kind_rtx);
26386           }
26387         break;
26388       case DW_OP_const4u:
26389       case DW_OP_const8u:
26390         if (loc->dtprel
26391             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26392           return false;
26393         break;
26394       case DW_OP_plus_uconst:
26395         if (size_of_loc_descr (loc)
26396             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26397               + 1
26398             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26399           {
26400             dw_loc_descr_ref repl
26401               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26402             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26403             add_loc_descr (&repl, loc->dw_loc_next);
26404             *loc = *repl;
26405           }
26406         break;
26407       case DW_OP_implicit_value:
26408         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26409             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26410           return false;
26411         break;
26412       case DW_OP_GNU_implicit_pointer:
26413       case DW_OP_GNU_parameter_ref:
26414         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26415           {
26416             dw_die_ref ref
26417               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26418             if (ref == NULL)
26419               return false;
26420             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26421             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26422             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26423           }
26424         break;
26425       case DW_OP_GNU_const_type:
26426       case DW_OP_GNU_regval_type:
26427       case DW_OP_GNU_deref_type:
26428       case DW_OP_GNU_convert:
26429       case DW_OP_GNU_reinterpret:
26430         while (loc->dw_loc_next
26431                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26432           {
26433             dw_die_ref base1, base2;
26434             unsigned enc1, enc2, size1, size2;
26435             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26436                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26437               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26438             else if (loc->dw_loc_oprnd1.val_class
26439                      == dw_val_class_unsigned_const)
26440               break;
26441             else
26442               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26443             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26444                 == dw_val_class_unsigned_const)
26445               break;
26446             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26447             gcc_assert (base1->die_tag == DW_TAG_base_type
26448                         && base2->die_tag == DW_TAG_base_type);
26449             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26450             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26451             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26452             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26453             if (size1 == size2
26454                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26455                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26456                      && loc != keep)
26457                     || enc1 == enc2))
26458               {
26459                 /* Optimize away next DW_OP_GNU_convert after
26460                    adjusting LOC's base type die reference.  */
26461                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26462                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26463                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26464                 else
26465                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26466                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26467                 continue;
26468               }
26469             /* Don't change integer DW_OP_GNU_convert after e.g. floating
26470                point typed stack entry.  */
26471             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26472               keep = loc->dw_loc_next;
26473             break;
26474           }
26475         break;
26476       default:
26477         break;
26478       }
26479   return true;
26480 }
26481
26482 /* Helper function of resolve_addr.  DIE had DW_AT_location of
26483    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26484    and DW_OP_addr couldn't be resolved.  resolve_addr has already
26485    removed the DW_AT_location attribute.  This function attempts to
26486    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26487    to it or DW_AT_const_value attribute, if possible.  */
26488
26489 static void
26490 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26491 {
26492   if (TREE_CODE (decl) != VAR_DECL
26493       || lookup_decl_die (decl) != die
26494       || DECL_EXTERNAL (decl)
26495       || !TREE_STATIC (decl)
26496       || DECL_INITIAL (decl) == NULL_TREE
26497       || DECL_P (DECL_INITIAL (decl))
26498       || get_AT (die, DW_AT_const_value))
26499     return;
26500
26501   tree init = DECL_INITIAL (decl);
26502   HOST_WIDE_INT offset = 0;
26503   /* For variables that have been optimized away and thus
26504      don't have a memory location, see if we can emit
26505      DW_AT_const_value instead.  */
26506   if (tree_add_const_value_attribute (die, init))
26507     return;
26508   if (dwarf_strict)
26509     return;
26510   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26511      and ADDR_EXPR refers to a decl that has DW_AT_location or
26512      DW_AT_const_value (but isn't addressable, otherwise
26513      resolving the original DW_OP_addr wouldn't fail), see if
26514      we can add DW_OP_GNU_implicit_pointer.  */
26515   STRIP_NOPS (init);
26516   if (TREE_CODE (init) == POINTER_PLUS_EXPR
26517       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26518     {
26519       offset = tree_to_shwi (TREE_OPERAND (init, 1));
26520       init = TREE_OPERAND (init, 0);
26521       STRIP_NOPS (init);
26522     }
26523   if (TREE_CODE (init) != ADDR_EXPR)
26524     return;
26525   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26526        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26527       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26528           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26529           && TREE_OPERAND (init, 0) != decl))
26530     {
26531       dw_die_ref ref;
26532       dw_loc_descr_ref l;
26533
26534       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26535         {
26536           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26537           if (!rtl)
26538             return;
26539           decl = SYMBOL_REF_DECL (rtl);
26540         }
26541       else
26542         decl = TREE_OPERAND (init, 0);
26543       ref = lookup_decl_die (decl);
26544       if (ref == NULL
26545           || (!get_AT (ref, DW_AT_location)
26546               && !get_AT (ref, DW_AT_const_value)))
26547         return;
26548       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26549       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26550       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26551       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26552       add_AT_loc (die, DW_AT_location, l);
26553     }
26554 }
26555
26556 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26557    an address in .rodata section if the string literal is emitted there,
26558    or remove the containing location list or replace DW_AT_const_value
26559    with DW_AT_location and empty location expression, if it isn't found
26560    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
26561    to something that has been emitted in the current CU.  */
26562
26563 static void
26564 resolve_addr (dw_die_ref die)
26565 {
26566   dw_die_ref c;
26567   dw_attr_node *a;
26568   dw_loc_list_ref *curr, *start, loc;
26569   unsigned ix;
26570
26571   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26572     switch (AT_class (a))
26573       {
26574       case dw_val_class_loc_list:
26575         start = curr = AT_loc_list_ptr (a);
26576         loc = *curr;
26577         gcc_assert (loc);
26578         /* The same list can be referenced more than once.  See if we have
26579            already recorded the result from a previous pass.  */
26580         if (loc->replaced)
26581           *curr = loc->dw_loc_next;
26582         else if (!loc->resolved_addr)
26583           {
26584             /* As things stand, we do not expect or allow one die to
26585                reference a suffix of another die's location list chain.
26586                References must be identical or completely separate.
26587                There is therefore no need to cache the result of this
26588                pass on any list other than the first; doing so
26589                would lead to unnecessary writes.  */
26590             while (*curr)
26591               {
26592                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
26593                 if (!resolve_addr_in_expr ((*curr)->expr))
26594                   {
26595                     dw_loc_list_ref next = (*curr)->dw_loc_next;
26596                     dw_loc_descr_ref l = (*curr)->expr;
26597
26598                     if (next && (*curr)->ll_symbol)
26599                       {
26600                         gcc_assert (!next->ll_symbol);
26601                         next->ll_symbol = (*curr)->ll_symbol;
26602                       }
26603                     if (dwarf_split_debug_info)
26604                       remove_loc_list_addr_table_entries (l);
26605                     *curr = next;
26606                   }
26607                 else
26608                   {
26609                     mark_base_types ((*curr)->expr);
26610                     curr = &(*curr)->dw_loc_next;
26611                   }
26612               }
26613             if (loc == *start)
26614               loc->resolved_addr = 1;
26615             else
26616               {
26617                 loc->replaced = 1;
26618                 loc->dw_loc_next = *start;
26619               }
26620           }
26621         if (!*start)
26622           {
26623             remove_AT (die, a->dw_attr);
26624             ix--;
26625           }
26626         break;
26627       case dw_val_class_loc:
26628         {
26629           dw_loc_descr_ref l = AT_loc (a);
26630           /* For -gdwarf-2 don't attempt to optimize
26631              DW_AT_data_member_location containing
26632              DW_OP_plus_uconst - older consumers might
26633              rely on it being that op instead of a more complex,
26634              but shorter, location description.  */
26635           if ((dwarf_version > 2
26636                || a->dw_attr != DW_AT_data_member_location
26637                || l == NULL
26638                || l->dw_loc_opc != DW_OP_plus_uconst
26639                || l->dw_loc_next != NULL)
26640               && !resolve_addr_in_expr (l))
26641             {
26642               if (dwarf_split_debug_info)
26643                 remove_loc_list_addr_table_entries (l);
26644               if (l != NULL
26645                   && l->dw_loc_next == NULL
26646                   && l->dw_loc_opc == DW_OP_addr
26647                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
26648                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
26649                   && a->dw_attr == DW_AT_location)
26650                 {
26651                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
26652                   remove_AT (die, a->dw_attr);
26653                   ix--;
26654                   optimize_location_into_implicit_ptr (die, decl);
26655                   break;
26656                 }
26657               remove_AT (die, a->dw_attr);
26658               ix--;
26659             }
26660           else
26661             mark_base_types (l);
26662         }
26663         break;
26664       case dw_val_class_addr:
26665         if (a->dw_attr == DW_AT_const_value
26666             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
26667           {
26668             if (AT_index (a) != NOT_INDEXED)
26669               remove_addr_table_entry (a->dw_attr_val.val_entry);
26670             remove_AT (die, a->dw_attr);
26671             ix--;
26672           }
26673         if (die->die_tag == DW_TAG_GNU_call_site
26674             && a->dw_attr == DW_AT_abstract_origin)
26675           {
26676             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
26677             dw_die_ref tdie = lookup_decl_die (tdecl);
26678             dw_die_ref cdie;
26679             if (tdie == NULL
26680                 && DECL_EXTERNAL (tdecl)
26681                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
26682                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
26683               {
26684                 /* Creating a full DIE for tdecl is overly expensive and
26685                    at this point even wrong when in the LTO phase
26686                    as it can end up generating new type DIEs we didn't
26687                    output and thus optimize_external_refs will crash.  */
26688                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
26689                 add_AT_flag (tdie, DW_AT_external, 1);
26690                 add_AT_flag (tdie, DW_AT_declaration, 1);
26691                 add_linkage_attr (tdie, tdecl);
26692                 add_name_and_src_coords_attributes (tdie, tdecl);
26693                 equate_decl_number_to_die (tdecl, tdie);
26694               }
26695             if (tdie)
26696               {
26697                 a->dw_attr_val.val_class = dw_val_class_die_ref;
26698                 a->dw_attr_val.v.val_die_ref.die = tdie;
26699                 a->dw_attr_val.v.val_die_ref.external = 0;
26700               }
26701             else
26702               {
26703                 if (AT_index (a) != NOT_INDEXED)
26704                   remove_addr_table_entry (a->dw_attr_val.val_entry);
26705                 remove_AT (die, a->dw_attr);
26706                 ix--;
26707               }
26708           }
26709         break;
26710       default:
26711         break;
26712       }
26713
26714   FOR_EACH_CHILD (die, c, resolve_addr (c));
26715 }
26716 \f
26717 /* Helper routines for optimize_location_lists.
26718    This pass tries to share identical local lists in .debug_loc
26719    section.  */
26720
26721 /* Iteratively hash operands of LOC opcode into HSTATE.  */
26722
26723 static void
26724 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
26725 {
26726   dw_val_ref val1 = &loc->dw_loc_oprnd1;
26727   dw_val_ref val2 = &loc->dw_loc_oprnd2;
26728
26729   switch (loc->dw_loc_opc)
26730     {
26731     case DW_OP_const4u:
26732     case DW_OP_const8u:
26733       if (loc->dtprel)
26734         goto hash_addr;
26735       /* FALLTHRU */
26736     case DW_OP_const1u:
26737     case DW_OP_const1s:
26738     case DW_OP_const2u:
26739     case DW_OP_const2s:
26740     case DW_OP_const4s:
26741     case DW_OP_const8s:
26742     case DW_OP_constu:
26743     case DW_OP_consts:
26744     case DW_OP_pick:
26745     case DW_OP_plus_uconst:
26746     case DW_OP_breg0:
26747     case DW_OP_breg1:
26748     case DW_OP_breg2:
26749     case DW_OP_breg3:
26750     case DW_OP_breg4:
26751     case DW_OP_breg5:
26752     case DW_OP_breg6:
26753     case DW_OP_breg7:
26754     case DW_OP_breg8:
26755     case DW_OP_breg9:
26756     case DW_OP_breg10:
26757     case DW_OP_breg11:
26758     case DW_OP_breg12:
26759     case DW_OP_breg13:
26760     case DW_OP_breg14:
26761     case DW_OP_breg15:
26762     case DW_OP_breg16:
26763     case DW_OP_breg17:
26764     case DW_OP_breg18:
26765     case DW_OP_breg19:
26766     case DW_OP_breg20:
26767     case DW_OP_breg21:
26768     case DW_OP_breg22:
26769     case DW_OP_breg23:
26770     case DW_OP_breg24:
26771     case DW_OP_breg25:
26772     case DW_OP_breg26:
26773     case DW_OP_breg27:
26774     case DW_OP_breg28:
26775     case DW_OP_breg29:
26776     case DW_OP_breg30:
26777     case DW_OP_breg31:
26778     case DW_OP_regx:
26779     case DW_OP_fbreg:
26780     case DW_OP_piece:
26781     case DW_OP_deref_size:
26782     case DW_OP_xderef_size:
26783       hstate.add_object (val1->v.val_int);
26784       break;
26785     case DW_OP_skip:
26786     case DW_OP_bra:
26787       {
26788         int offset;
26789
26790         gcc_assert (val1->val_class == dw_val_class_loc);
26791         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
26792         hstate.add_object (offset);
26793       }
26794       break;
26795     case DW_OP_implicit_value:
26796       hstate.add_object (val1->v.val_unsigned);
26797       switch (val2->val_class)
26798         {
26799         case dw_val_class_const:
26800           hstate.add_object (val2->v.val_int);
26801           break;
26802         case dw_val_class_vec:
26803           {
26804             unsigned int elt_size = val2->v.val_vec.elt_size;
26805             unsigned int len = val2->v.val_vec.length;
26806
26807             hstate.add_int (elt_size);
26808             hstate.add_int (len);
26809             hstate.add (val2->v.val_vec.array, len * elt_size);
26810           }
26811           break;
26812         case dw_val_class_const_double:
26813           hstate.add_object (val2->v.val_double.low);
26814           hstate.add_object (val2->v.val_double.high);
26815           break;
26816         case dw_val_class_wide_int:
26817           hstate.add (val2->v.val_wide->get_val (),
26818                       get_full_len (*val2->v.val_wide)
26819                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26820           break;
26821         case dw_val_class_addr: 
26822           inchash::add_rtx (val2->v.val_addr, hstate);
26823           break;
26824         default:
26825           gcc_unreachable ();
26826         }
26827       break;
26828     case DW_OP_bregx:
26829     case DW_OP_bit_piece:
26830       hstate.add_object (val1->v.val_int);
26831       hstate.add_object (val2->v.val_int);
26832       break;
26833     case DW_OP_addr:
26834     hash_addr:
26835       if (loc->dtprel)
26836         {
26837           unsigned char dtprel = 0xd1;
26838           hstate.add_object (dtprel);
26839         }
26840       inchash::add_rtx (val1->v.val_addr, hstate);
26841       break;
26842     case DW_OP_GNU_addr_index:
26843     case DW_OP_GNU_const_index:
26844       {
26845         if (loc->dtprel)
26846           {
26847             unsigned char dtprel = 0xd1;
26848             hstate.add_object (dtprel);
26849           }
26850         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
26851       }
26852       break;
26853     case DW_OP_GNU_implicit_pointer:
26854       hstate.add_int (val2->v.val_int);
26855       break;
26856     case DW_OP_GNU_entry_value:
26857       hstate.add_object (val1->v.val_loc);
26858       break;
26859     case DW_OP_GNU_regval_type:
26860     case DW_OP_GNU_deref_type:
26861       {
26862         unsigned int byte_size
26863           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
26864         unsigned int encoding
26865           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
26866         hstate.add_object (val1->v.val_int);
26867         hstate.add_object (byte_size);
26868         hstate.add_object (encoding);
26869       }
26870       break;
26871     case DW_OP_GNU_convert:
26872     case DW_OP_GNU_reinterpret:
26873       if (val1->val_class == dw_val_class_unsigned_const)
26874         {
26875           hstate.add_object (val1->v.val_unsigned);
26876           break;
26877         }
26878       /* FALLTHRU */
26879     case DW_OP_GNU_const_type:
26880       {
26881         unsigned int byte_size
26882           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
26883         unsigned int encoding
26884           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
26885         hstate.add_object (byte_size);
26886         hstate.add_object (encoding);
26887         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
26888           break;
26889         hstate.add_object (val2->val_class);
26890         switch (val2->val_class)
26891           {
26892           case dw_val_class_const:
26893             hstate.add_object (val2->v.val_int);
26894             break;
26895           case dw_val_class_vec:
26896             {
26897               unsigned int elt_size = val2->v.val_vec.elt_size;
26898               unsigned int len = val2->v.val_vec.length;
26899
26900               hstate.add_object (elt_size);
26901               hstate.add_object (len);
26902               hstate.add (val2->v.val_vec.array, len * elt_size);
26903             }
26904             break;
26905           case dw_val_class_const_double:
26906             hstate.add_object (val2->v.val_double.low);
26907             hstate.add_object (val2->v.val_double.high);
26908             break;
26909           case dw_val_class_wide_int:
26910             hstate.add (val2->v.val_wide->get_val (),
26911                         get_full_len (*val2->v.val_wide)
26912                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26913             break;
26914           default:
26915             gcc_unreachable ();
26916           }
26917       }
26918       break;
26919
26920     default:
26921       /* Other codes have no operands.  */
26922       break;
26923     }
26924 }
26925
26926 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
26927
26928 static inline void
26929 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
26930 {
26931   dw_loc_descr_ref l;
26932   bool sizes_computed = false;
26933   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
26934   size_of_locs (loc);
26935
26936   for (l = loc; l != NULL; l = l->dw_loc_next)
26937     {
26938       enum dwarf_location_atom opc = l->dw_loc_opc;
26939       hstate.add_object (opc);
26940       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
26941         {
26942           size_of_locs (loc);
26943           sizes_computed = true;
26944         }
26945       hash_loc_operands (l, hstate);
26946     }
26947 }
26948
26949 /* Compute hash of the whole location list LIST_HEAD.  */
26950
26951 static inline void
26952 hash_loc_list (dw_loc_list_ref list_head)
26953 {
26954   dw_loc_list_ref curr = list_head;
26955   inchash::hash hstate;
26956
26957   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
26958     {
26959       hstate.add (curr->begin, strlen (curr->begin) + 1);
26960       hstate.add (curr->end, strlen (curr->end) + 1);
26961       if (curr->section)
26962         hstate.add (curr->section, strlen (curr->section) + 1);
26963       hash_locs (curr->expr, hstate);
26964     }
26965   list_head->hash = hstate.end ();
26966 }
26967
26968 /* Return true if X and Y opcodes have the same operands.  */
26969
26970 static inline bool
26971 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
26972 {
26973   dw_val_ref valx1 = &x->dw_loc_oprnd1;
26974   dw_val_ref valx2 = &x->dw_loc_oprnd2;
26975   dw_val_ref valy1 = &y->dw_loc_oprnd1;
26976   dw_val_ref valy2 = &y->dw_loc_oprnd2;
26977
26978   switch (x->dw_loc_opc)
26979     {
26980     case DW_OP_const4u:
26981     case DW_OP_const8u:
26982       if (x->dtprel)
26983         goto hash_addr;
26984       /* FALLTHRU */
26985     case DW_OP_const1u:
26986     case DW_OP_const1s:
26987     case DW_OP_const2u:
26988     case DW_OP_const2s:
26989     case DW_OP_const4s:
26990     case DW_OP_const8s:
26991     case DW_OP_constu:
26992     case DW_OP_consts:
26993     case DW_OP_pick:
26994     case DW_OP_plus_uconst:
26995     case DW_OP_breg0:
26996     case DW_OP_breg1:
26997     case DW_OP_breg2:
26998     case DW_OP_breg3:
26999     case DW_OP_breg4:
27000     case DW_OP_breg5:
27001     case DW_OP_breg6:
27002     case DW_OP_breg7:
27003     case DW_OP_breg8:
27004     case DW_OP_breg9:
27005     case DW_OP_breg10:
27006     case DW_OP_breg11:
27007     case DW_OP_breg12:
27008     case DW_OP_breg13:
27009     case DW_OP_breg14:
27010     case DW_OP_breg15:
27011     case DW_OP_breg16:
27012     case DW_OP_breg17:
27013     case DW_OP_breg18:
27014     case DW_OP_breg19:
27015     case DW_OP_breg20:
27016     case DW_OP_breg21:
27017     case DW_OP_breg22:
27018     case DW_OP_breg23:
27019     case DW_OP_breg24:
27020     case DW_OP_breg25:
27021     case DW_OP_breg26:
27022     case DW_OP_breg27:
27023     case DW_OP_breg28:
27024     case DW_OP_breg29:
27025     case DW_OP_breg30:
27026     case DW_OP_breg31:
27027     case DW_OP_regx:
27028     case DW_OP_fbreg:
27029     case DW_OP_piece:
27030     case DW_OP_deref_size:
27031     case DW_OP_xderef_size:
27032       return valx1->v.val_int == valy1->v.val_int;
27033     case DW_OP_skip:
27034     case DW_OP_bra:
27035       /* If splitting debug info, the use of DW_OP_GNU_addr_index
27036         can cause irrelevant differences in dw_loc_addr.  */
27037       gcc_assert (valx1->val_class == dw_val_class_loc
27038                   && valy1->val_class == dw_val_class_loc
27039                   && (dwarf_split_debug_info
27040                       || x->dw_loc_addr == y->dw_loc_addr));
27041       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27042     case DW_OP_implicit_value:
27043       if (valx1->v.val_unsigned != valy1->v.val_unsigned
27044           || valx2->val_class != valy2->val_class)
27045         return false;
27046       switch (valx2->val_class)
27047         {
27048         case dw_val_class_const:
27049           return valx2->v.val_int == valy2->v.val_int;
27050         case dw_val_class_vec:
27051           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27052                  && valx2->v.val_vec.length == valy2->v.val_vec.length
27053                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27054                             valx2->v.val_vec.elt_size
27055                             * valx2->v.val_vec.length) == 0;
27056         case dw_val_class_const_double:
27057           return valx2->v.val_double.low == valy2->v.val_double.low
27058                  && valx2->v.val_double.high == valy2->v.val_double.high;
27059         case dw_val_class_wide_int:
27060           return *valx2->v.val_wide == *valy2->v.val_wide;
27061         case dw_val_class_addr:
27062           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27063         default:
27064           gcc_unreachable ();
27065         }
27066     case DW_OP_bregx:
27067     case DW_OP_bit_piece:
27068       return valx1->v.val_int == valy1->v.val_int
27069              && valx2->v.val_int == valy2->v.val_int;
27070     case DW_OP_addr:
27071     hash_addr:
27072       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27073     case DW_OP_GNU_addr_index:
27074     case DW_OP_GNU_const_index:
27075       {
27076         rtx ax1 = valx1->val_entry->addr.rtl;
27077         rtx ay1 = valy1->val_entry->addr.rtl;
27078         return rtx_equal_p (ax1, ay1);
27079       }
27080     case DW_OP_GNU_implicit_pointer:
27081       return valx1->val_class == dw_val_class_die_ref
27082              && valx1->val_class == valy1->val_class
27083              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27084              && valx2->v.val_int == valy2->v.val_int;
27085     case DW_OP_GNU_entry_value:
27086       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27087     case DW_OP_GNU_const_type:
27088       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27089           || valx2->val_class != valy2->val_class)
27090         return false;
27091       switch (valx2->val_class)
27092         {
27093         case dw_val_class_const:
27094           return valx2->v.val_int == valy2->v.val_int;
27095         case dw_val_class_vec:
27096           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27097                  && valx2->v.val_vec.length == valy2->v.val_vec.length
27098                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27099                             valx2->v.val_vec.elt_size
27100                             * valx2->v.val_vec.length) == 0;
27101         case dw_val_class_const_double:
27102           return valx2->v.val_double.low == valy2->v.val_double.low
27103                  && valx2->v.val_double.high == valy2->v.val_double.high;
27104         case dw_val_class_wide_int:
27105           return *valx2->v.val_wide == *valy2->v.val_wide;
27106         default:
27107           gcc_unreachable ();
27108         }
27109     case DW_OP_GNU_regval_type:
27110     case DW_OP_GNU_deref_type:
27111       return valx1->v.val_int == valy1->v.val_int
27112              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27113     case DW_OP_GNU_convert:
27114     case DW_OP_GNU_reinterpret:
27115       if (valx1->val_class != valy1->val_class)
27116         return false;
27117       if (valx1->val_class == dw_val_class_unsigned_const)
27118         return valx1->v.val_unsigned == valy1->v.val_unsigned;
27119       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27120     case DW_OP_GNU_parameter_ref:
27121       return valx1->val_class == dw_val_class_die_ref
27122              && valx1->val_class == valy1->val_class
27123              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27124     default:
27125       /* Other codes have no operands.  */
27126       return true;
27127     }
27128 }
27129
27130 /* Return true if DWARF location expressions X and Y are the same.  */
27131
27132 static inline bool
27133 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27134 {
27135   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27136     if (x->dw_loc_opc != y->dw_loc_opc
27137         || x->dtprel != y->dtprel
27138         || !compare_loc_operands (x, y))
27139       break;
27140   return x == NULL && y == NULL;
27141 }
27142
27143 /* Hashtable helpers.  */
27144
27145 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27146 {
27147   static inline hashval_t hash (const dw_loc_list_struct *);
27148   static inline bool equal (const dw_loc_list_struct *,
27149                             const dw_loc_list_struct *);
27150 };
27151
27152 /* Return precomputed hash of location list X.  */
27153
27154 inline hashval_t
27155 loc_list_hasher::hash (const dw_loc_list_struct *x)
27156 {
27157   return x->hash;
27158 }
27159
27160 /* Return true if location lists A and B are the same.  */
27161
27162 inline bool
27163 loc_list_hasher::equal (const dw_loc_list_struct *a,
27164                         const dw_loc_list_struct *b)
27165 {
27166   if (a == b)
27167     return 1;
27168   if (a->hash != b->hash)
27169     return 0;
27170   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27171     if (strcmp (a->begin, b->begin) != 0
27172         || strcmp (a->end, b->end) != 0
27173         || (a->section == NULL) != (b->section == NULL)
27174         || (a->section && strcmp (a->section, b->section) != 0)
27175         || !compare_locs (a->expr, b->expr))
27176       break;
27177   return a == NULL && b == NULL;
27178 }
27179
27180 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27181
27182
27183 /* Recursively optimize location lists referenced from DIE
27184    children and share them whenever possible.  */
27185
27186 static void
27187 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27188 {
27189   dw_die_ref c;
27190   dw_attr_node *a;
27191   unsigned ix;
27192   dw_loc_list_struct **slot;
27193
27194   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27195     if (AT_class (a) == dw_val_class_loc_list)
27196       {
27197         dw_loc_list_ref list = AT_loc_list (a);
27198         /* TODO: perform some optimizations here, before hashing
27199            it and storing into the hash table.  */
27200         hash_loc_list (list);
27201         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27202         if (*slot == NULL)
27203           *slot = list;
27204         else
27205           a->dw_attr_val.v.val_loc_list = *slot;
27206       }
27207
27208   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27209 }
27210
27211
27212 /* Recursively assign each location list a unique index into the debug_addr
27213    section.  */
27214
27215 static void
27216 index_location_lists (dw_die_ref die)
27217 {
27218   dw_die_ref c;
27219   dw_attr_node *a;
27220   unsigned ix;
27221
27222   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27223     if (AT_class (a) == dw_val_class_loc_list)
27224       {
27225         dw_loc_list_ref list = AT_loc_list (a);
27226         dw_loc_list_ref curr;
27227         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27228           {
27229             /* Don't index an entry that has already been indexed
27230                or won't be output.  */
27231             if (curr->begin_entry != NULL
27232                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27233               continue;
27234
27235             curr->begin_entry
27236                 = add_addr_table_entry (xstrdup (curr->begin),
27237                                         ate_kind_label);
27238           }
27239       }
27240
27241   FOR_EACH_CHILD (die, c, index_location_lists (c));
27242 }
27243
27244 /* Optimize location lists referenced from DIE
27245    children and share them whenever possible.  */
27246
27247 static void
27248 optimize_location_lists (dw_die_ref die)
27249 {
27250   loc_list_hash_type htab (500);
27251   optimize_location_lists_1 (die, &htab);
27252 }
27253 \f
27254 /* Traverse the limbo die list, and add parent/child links.  The only
27255    dies without parents that should be here are concrete instances of
27256    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
27257    For concrete instances, we can get the parent die from the abstract
27258    instance.  */
27259
27260 static void
27261 flush_limbo_die_list (void)
27262 {
27263   limbo_die_node *node, *next_node;
27264
27265   for (node = limbo_die_list; node; node = next_node)
27266     {
27267       dw_die_ref die = node->die;
27268       next_node = node->next;
27269
27270       if (die->die_parent == NULL)
27271         {
27272           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27273
27274           if (origin && origin->die_parent)
27275             add_child_die (origin->die_parent, die);
27276           else if (is_cu_die (die))
27277             ;
27278           else if (seen_error ())
27279             /* It's OK to be confused by errors in the input.  */
27280             add_child_die (comp_unit_die (), die);
27281           else
27282             {
27283               /* In certain situations, the lexical block containing a
27284                  nested function can be optimized away, which results
27285                  in the nested function die being orphaned.  Likewise
27286                  with the return type of that nested function.  Force
27287                  this to be a child of the containing function.
27288
27289                  It may happen that even the containing function got fully
27290                  inlined and optimized out.  In that case we are lost and
27291                  assign the empty child.  This should not be big issue as
27292                  the function is likely unreachable too.  */
27293               gcc_assert (node->created_for);
27294
27295               if (DECL_P (node->created_for))
27296                 origin = get_context_die (DECL_CONTEXT (node->created_for));
27297               else if (TYPE_P (node->created_for))
27298                 origin = scope_die_for (node->created_for, comp_unit_die ());
27299               else
27300                 origin = comp_unit_die ();
27301
27302               add_child_die (origin, die);
27303             }
27304         }
27305     }
27306
27307   limbo_die_list = NULL;
27308 }
27309
27310 /* Output stuff that dwarf requires at the end of every file,
27311    and generate the DWARF-2 debugging info.  */
27312
27313 static void
27314 dwarf2out_finish (const char *filename)
27315 {
27316   comdat_type_node *ctnode;
27317   dw_die_ref main_comp_unit_die;
27318
27319   /* Flush out any latecomers to the limbo party.  */
27320   flush_limbo_die_list ();
27321
27322   /* We shouldn't have any symbols with delayed asm names for
27323      DIEs generated after early finish.  */
27324   gcc_assert (deferred_asm_name == NULL);
27325
27326   /* PCH might result in DW_AT_producer string being restored from the
27327      header compilation, so always fill it with empty string initially
27328      and overwrite only here.  */
27329   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27330   producer_string = gen_producer_string ();
27331   producer->dw_attr_val.v.val_str->refcount--;
27332   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27333
27334   gen_remaining_tmpl_value_param_die_attribute ();
27335
27336   /* Add the name for the main input file now.  We delayed this from
27337      dwarf2out_init to avoid complications with PCH.
27338      For LTO produced units use a fixed artificial name to avoid
27339      leaking tempfile names into the dwarf.  */
27340   if (!in_lto_p)
27341     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27342   else
27343     add_name_attribute (comp_unit_die (), "<artificial>");
27344   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27345     add_comp_dir_attribute (comp_unit_die ());
27346   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27347     {
27348       bool p = false;
27349       file_table->traverse<bool *, file_table_relative_p> (&p);
27350       if (p)
27351         add_comp_dir_attribute (comp_unit_die ());
27352     }
27353
27354 #if ENABLE_ASSERT_CHECKING
27355   {
27356     dw_die_ref die = comp_unit_die (), c;
27357     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27358   }
27359 #endif
27360   resolve_addr (comp_unit_die ());
27361   move_marked_base_types ();
27362
27363   /* Walk through the list of incomplete types again, trying once more to
27364      emit full debugging info for them.  */
27365   retry_incomplete_types ();
27366
27367   if (flag_eliminate_unused_debug_types)
27368     prune_unused_types ();
27369
27370   /* Generate separate COMDAT sections for type DIEs. */
27371   if (use_debug_types)
27372     {
27373       break_out_comdat_types (comp_unit_die ());
27374
27375       /* Each new type_unit DIE was added to the limbo die list when created.
27376          Since these have all been added to comdat_type_list, clear the
27377          limbo die list.  */
27378       limbo_die_list = NULL;
27379
27380       /* For each new comdat type unit, copy declarations for incomplete
27381          types to make the new unit self-contained (i.e., no direct
27382          references to the main compile unit).  */
27383       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27384         copy_decls_for_unworthy_types (ctnode->root_die);
27385       copy_decls_for_unworthy_types (comp_unit_die ());
27386
27387       /* In the process of copying declarations from one unit to another,
27388          we may have left some declarations behind that are no longer
27389          referenced.  Prune them.  */
27390       prune_unused_types ();
27391     }
27392
27393   /* Generate separate CUs for each of the include files we've seen.
27394      They will go into limbo_die_list.  */
27395   if (flag_eliminate_dwarf2_dups)
27396     break_out_includes (comp_unit_die ());
27397
27398   /* Traverse the DIE's and add sibling attributes to those DIE's that
27399      have children.  */
27400   add_sibling_attributes (comp_unit_die ());
27401   limbo_die_node *node;
27402   for (node = limbo_die_list; node; node = node->next)
27403     add_sibling_attributes (node->die);
27404   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27405     add_sibling_attributes (ctnode->root_die);
27406
27407   /* When splitting DWARF info, we put some attributes in the
27408      skeleton compile_unit DIE that remains in the .o, while
27409      most attributes go in the DWO compile_unit_die.  */
27410   if (dwarf_split_debug_info)
27411     main_comp_unit_die = gen_compile_unit_die (NULL);
27412   else
27413     main_comp_unit_die = comp_unit_die ();
27414
27415   /* Output a terminator label for the .text section.  */
27416   switch_to_section (text_section);
27417   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27418   if (cold_text_section)
27419     {
27420       switch_to_section (cold_text_section);
27421       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27422     }
27423
27424   /* We can only use the low/high_pc attributes if all of the code was
27425      in .text.  */
27426   if (!have_multiple_function_sections 
27427       || (dwarf_version < 3 && dwarf_strict))
27428     {
27429       /* Don't add if the CU has no associated code.  */
27430       if (text_section_used)
27431         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27432                             text_end_label, true);
27433     }
27434   else
27435     {
27436       unsigned fde_idx;
27437       dw_fde_ref fde;
27438       bool range_list_added = false;
27439
27440       if (text_section_used)
27441         add_ranges_by_labels (main_comp_unit_die, text_section_label,
27442                               text_end_label, &range_list_added, true);
27443       if (cold_text_section_used)
27444         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27445                               cold_end_label, &range_list_added, true);
27446
27447       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27448         {
27449           if (DECL_IGNORED_P (fde->decl))
27450             continue;
27451           if (!fde->in_std_section)
27452             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27453                                   fde->dw_fde_end, &range_list_added,
27454                                   true);
27455           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27456             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27457                                   fde->dw_fde_second_end, &range_list_added,
27458                                   true);
27459         }
27460
27461       if (range_list_added)
27462         {
27463           /* We need to give .debug_loc and .debug_ranges an appropriate
27464              "base address".  Use zero so that these addresses become
27465              absolute.  Historically, we've emitted the unexpected
27466              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27467              Emit both to give time for other tools to adapt.  */
27468           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27469           if (! dwarf_strict && dwarf_version < 4)
27470             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27471
27472           add_ranges (NULL);
27473         }
27474     }
27475
27476   if (debug_info_level >= DINFO_LEVEL_TERSE)
27477     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27478                     debug_line_section_label);
27479
27480   if (have_macinfo)
27481     add_AT_macptr (comp_unit_die (),
27482                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27483                    macinfo_section_label);
27484
27485   if (dwarf_split_debug_info)
27486     {
27487       /* optimize_location_lists calculates the size of the lists,
27488          so index them first, and assign indices to the entries.
27489          Although optimize_location_lists will remove entries from
27490          the table, it only does so for duplicates, and therefore
27491          only reduces ref_counts to 1.  */
27492       index_location_lists (comp_unit_die ());
27493
27494       if (addr_index_table != NULL)
27495         {
27496           unsigned int index = 0;
27497           addr_index_table
27498             ->traverse_noresize<unsigned int *, index_addr_table_entry>
27499             (&index);
27500         }
27501     }
27502
27503   if (have_location_lists)
27504     optimize_location_lists (comp_unit_die ());
27505
27506   save_macinfo_strings ();
27507
27508   if (dwarf_split_debug_info)
27509     {
27510       unsigned int index = 0;
27511
27512       /* Add attributes common to skeleton compile_units and
27513          type_units.  Because these attributes include strings, it
27514          must be done before freezing the string table.  Top-level
27515          skeleton die attrs are added when the skeleton type unit is
27516          created, so ensure it is created by this point.  */
27517       add_top_level_skeleton_die_attrs (main_comp_unit_die);
27518       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27519     }
27520
27521   /* Output all of the compilation units.  We put the main one last so that
27522      the offsets are available to output_pubnames.  */
27523   for (node = limbo_die_list; node; node = node->next)
27524     output_comp_unit (node->die, 0);
27525
27526   hash_table<comdat_type_hasher> comdat_type_table (100);
27527   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27528     {
27529       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
27530
27531       /* Don't output duplicate types.  */
27532       if (*slot != HTAB_EMPTY_ENTRY)
27533         continue;
27534
27535       /* Add a pointer to the line table for the main compilation unit
27536          so that the debugger can make sense of DW_AT_decl_file
27537          attributes.  */
27538       if (debug_info_level >= DINFO_LEVEL_TERSE)
27539         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
27540                         (!dwarf_split_debug_info
27541                          ? debug_line_section_label
27542                          : debug_skeleton_line_section_label));
27543
27544       output_comdat_type_unit (ctnode);
27545       *slot = ctnode;
27546     }
27547
27548   /* The AT_pubnames attribute needs to go in all skeleton dies, including
27549      both the main_cu and all skeleton TUs.  Making this call unconditional
27550      would end up either adding a second copy of the AT_pubnames attribute, or
27551      requiring a special case in add_top_level_skeleton_die_attrs.  */
27552   if (!dwarf_split_debug_info)
27553     add_AT_pubnames (comp_unit_die ());
27554
27555   if (dwarf_split_debug_info)
27556     {
27557       int mark;
27558       unsigned char checksum[16];
27559       struct md5_ctx ctx;
27560
27561       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
27562       md5_init_ctx (&ctx);
27563       mark = 0;
27564       die_checksum (comp_unit_die (), &ctx, &mark);
27565       unmark_all_dies (comp_unit_die ());
27566       md5_finish_ctx (&ctx, checksum);
27567
27568       /* Use the first 8 bytes of the checksum as the dwo_id,
27569         and add it to both comp-unit DIEs.  */
27570       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
27571       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
27572
27573       /* Add the base offset of the ranges table to the skeleton
27574         comp-unit DIE.  */
27575       if (ranges_table_in_use)
27576         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
27577                         ranges_section_label);
27578
27579       switch_to_section (debug_addr_section);
27580       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
27581       output_addr_table ();
27582     }
27583
27584   /* Output the main compilation unit if non-empty or if .debug_macinfo
27585      or .debug_macro will be emitted.  */
27586   output_comp_unit (comp_unit_die (), have_macinfo);
27587
27588   if (dwarf_split_debug_info && info_section_emitted)
27589     output_skeleton_debug_sections (main_comp_unit_die);
27590
27591   /* Output the abbreviation table.  */
27592   if (abbrev_die_table_in_use != 1)
27593     {
27594       switch_to_section (debug_abbrev_section);
27595       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
27596       output_abbrev_section ();
27597     }
27598
27599   /* Output location list section if necessary.  */
27600   if (have_location_lists)
27601     {
27602       /* Output the location lists info.  */
27603       switch_to_section (debug_loc_section);
27604       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
27605       output_location_lists (comp_unit_die ());
27606     }
27607
27608   output_pubtables ();
27609
27610   /* Output the address range information if a CU (.debug_info section)
27611      was emitted.  We output an empty table even if we had no functions
27612      to put in it.  This because the consumer has no way to tell the
27613      difference between an empty table that we omitted and failure to
27614      generate a table that would have contained data.  */
27615   if (info_section_emitted)
27616     {
27617       switch_to_section (debug_aranges_section);
27618       output_aranges ();
27619     }
27620
27621   /* Output ranges section if necessary.  */
27622   if (ranges_table_in_use)
27623     {
27624       switch_to_section (debug_ranges_section);
27625       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
27626       output_ranges ();
27627     }
27628
27629   /* Have to end the macro section.  */
27630   if (have_macinfo)
27631     {
27632       switch_to_section (debug_macinfo_section);
27633       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
27634       output_macinfo ();
27635       dw2_asm_output_data (1, 0, "End compilation unit");
27636     }
27637
27638   /* Output the source line correspondence table.  We must do this
27639      even if there is no line information.  Otherwise, on an empty
27640      translation unit, we will generate a present, but empty,
27641      .debug_info section.  IRIX 6.5 `nm' will then complain when
27642      examining the file.  This is done late so that any filenames
27643      used by the debug_info section are marked as 'used'.  */
27644   switch_to_section (debug_line_section);
27645   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
27646   if (! DWARF2_ASM_LINE_DEBUG_INFO)
27647     output_line_info (false);
27648
27649   if (dwarf_split_debug_info && info_section_emitted)
27650     {
27651       switch_to_section (debug_skeleton_line_section);
27652       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
27653       output_line_info (true);
27654     }
27655
27656   /* If we emitted any indirect strings, output the string table too.  */
27657   if (debug_str_hash || skeleton_debug_str_hash)
27658     output_indirect_strings ();
27659 }
27660
27661 /* Perform any cleanups needed after the early debug generation pass
27662    has run.  */
27663
27664 static void
27665 dwarf2out_early_finish (void)
27666 {
27667   limbo_die_node *node;
27668
27669   /* Add DW_AT_linkage_name for all deferred DIEs.  */
27670   for (node = deferred_asm_name; node; node = node->next)
27671     {
27672       tree decl = node->created_for;
27673       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
27674           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
27675              ended up in deferred_asm_name before we knew it was
27676              constant and never written to disk.  */
27677           && DECL_ASSEMBLER_NAME (decl))
27678         {
27679           add_linkage_attr (node->die, decl);
27680           move_linkage_attr (node->die);
27681         }
27682     }
27683   deferred_asm_name = NULL;
27684
27685   /* The point here is to flush out the limbo list so that it is empty
27686      and we don't need to stream it for LTO.  */
27687   flush_limbo_die_list ();
27688
27689   gen_scheduled_generic_parms_dies ();
27690   gen_remaining_tmpl_value_param_die_attribute ();
27691 }
27692
27693 /* Reset all state within dwarf2out.c so that we can rerun the compiler
27694    within the same process.  For use by toplev::finalize.  */
27695
27696 void
27697 dwarf2out_c_finalize (void)
27698 {
27699   last_var_location_insn = NULL;
27700   cached_next_real_insn = NULL;
27701   used_rtx_array = NULL;
27702   incomplete_types = NULL;
27703   decl_scope_table = NULL;
27704   debug_info_section = NULL;
27705   debug_skeleton_info_section = NULL;
27706   debug_abbrev_section = NULL;
27707   debug_skeleton_abbrev_section = NULL;
27708   debug_aranges_section = NULL;
27709   debug_addr_section = NULL;
27710   debug_macinfo_section = NULL;
27711   debug_line_section = NULL;
27712   debug_skeleton_line_section = NULL;
27713   debug_loc_section = NULL;
27714   debug_pubnames_section = NULL;
27715   debug_pubtypes_section = NULL;
27716   debug_str_section = NULL;
27717   debug_str_dwo_section = NULL;
27718   debug_str_offsets_section = NULL;
27719   debug_ranges_section = NULL;
27720   debug_frame_section = NULL;
27721   fde_vec = NULL;
27722   debug_str_hash = NULL;
27723   skeleton_debug_str_hash = NULL;
27724   dw2_string_counter = 0;
27725   have_multiple_function_sections = false;
27726   text_section_used = false;
27727   cold_text_section_used = false;
27728   cold_text_section = NULL;
27729   current_unit_personality = NULL;
27730
27731   next_die_offset = 0;
27732   single_comp_unit_die = NULL;
27733   comdat_type_list = NULL;
27734   limbo_die_list = NULL;
27735   file_table = NULL;
27736   decl_die_table = NULL;
27737   common_block_die_table = NULL;
27738   decl_loc_table = NULL;
27739   call_arg_locations = NULL;
27740   call_arg_loc_last = NULL;
27741   call_site_count = -1;
27742   tail_call_site_count = -1;
27743   cached_dw_loc_list_table = NULL;
27744   abbrev_die_table = NULL;
27745   abbrev_die_table_allocated = 0;
27746   abbrev_die_table_in_use = 0;
27747   delete dwarf_proc_stack_usage_map;
27748   dwarf_proc_stack_usage_map = NULL;
27749   line_info_label_num = 0;
27750   cur_line_info_table = NULL;
27751   text_section_line_info = NULL;
27752   cold_text_section_line_info = NULL;
27753   separate_line_info = NULL;
27754   info_section_emitted = false;
27755   pubname_table = NULL;
27756   pubtype_table = NULL;
27757   macinfo_table = NULL;
27758   ranges_table = NULL;
27759   ranges_table_allocated = 0;
27760   ranges_table_in_use = 0;
27761   ranges_by_label = 0;
27762   ranges_by_label_allocated = 0;
27763   ranges_by_label_in_use = 0;
27764   have_location_lists = false;
27765   loclabel_num = 0;
27766   poc_label_num = 0;
27767   last_emitted_file = NULL;
27768   label_num = 0;
27769   tmpl_value_parm_die_table = NULL;
27770   generic_type_instances = NULL;
27771   frame_pointer_fb_offset = 0;
27772   frame_pointer_fb_offset_valid = false;
27773   base_types.release ();
27774   XDELETEVEC (producer_string);
27775   producer_string = NULL;
27776 }
27777
27778 #include "gt-dwarf2out.h"